4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/Apackage com.apple.laf;
4632N/A
4632N/Aimport java.awt.*;
4632N/Aimport java.awt.event.*;
4632N/Aimport java.awt.peer.MenuComponentPeer;
4632N/Aimport java.security.PrivilegedAction;
4632N/Aimport java.util.Hashtable;
4632N/A
4632N/Aimport javax.swing.*;
4632N/A
4632N/Aimport sun.lwawt.LWToolkit;
4632N/Aimport sun.lwawt.macosx.*;
4632N/A
4632N/Aclass ScreenMenu extends Menu implements ContainerListener, ComponentListener, ScreenMenuPropertyHandler {
4632N/A static {
4632N/A java.security.AccessController.doPrivileged((PrivilegedAction<?>)new sun.security.action.LoadLibraryAction("awt"));
4632N/A }
4632N/A
4632N/A // screen menu stuff
4632N/A public static native long addMenuListeners(ScreenMenu listener, long nativeMenu);
4632N/A public static native void removeMenuListeners(long modelPtr);
4632N/A
4632N/A long fModelPtr = 0;
4632N/A
4632N/A Hashtable<Component, MenuItem> fItems;
4632N/A JMenu fInvoker;
4632N/A
4632N/A Component fLastMouseEventTarget;
4632N/A Rectangle fLastTargetRect;
4632N/A private volatile Rectangle[] fItemBounds;
4632N/A
4632N/A // Array of child hashes used to see if we need to recreate the Menu.
4632N/A int childHashArray[];
4632N/A
4632N/A ScreenMenu(final JMenu invoker) {
4632N/A super(invoker.getText());
4632N/A fInvoker = invoker;
4632N/A
4632N/A int count = fInvoker.getMenuComponentCount();
4632N/A if (count < 5) count = 5;
4632N/A fItems = new Hashtable<Component, MenuItem>(count);
4632N/A setEnabled(fInvoker.isEnabled());
4632N/A updateItems();
4632N/A }
4632N/A
4632N/A // I'm always 'visible', but never on screen
4632N/A static class ScreenMenuComponent extends Container {
4632N/A public boolean isVisible() { return true; }
4632N/A public boolean isShowing() { return true; }
4632N/A public void setVisible(final boolean b) {}
4632N/A public void show() {}
4632N/A }
4632N/A
4632N/A ScreenMenuComponent makeScreenMenuComponent() {
4632N/A return new ScreenMenuComponent();
4632N/A }
4632N/A
4632N/A
4632N/A /**
4632N/A * Determine if we need to tear down the Menu and re-create it, since the contents may have changed in the Menu opened listener and
4632N/A * we do not get notified of it, because EDT is busy in our code. We only need to update if the menu contents have changed in some
4632N/A * way, such as the number of menu items, the text of the menuitems, icon, shortcut etc.
4632N/A */
4632N/A static boolean needsUpdate(final Component items[], final int childHashArray[]) {
4632N/A if (items == null || childHashArray == null) {
4632N/A return true;
4632N/A }
4632N/A if (childHashArray.length != items.length) {
4632N/A return true;
4632N/A }
4632N/A for (int i = 0; i < items.length; i++) {
4632N/A final int hashCode = getHashCode(items[i]);
4632N/A if (hashCode != childHashArray[i]) {
4632N/A return true;
4632N/A }
4632N/A }
4632N/A return false;
4632N/A }
4632N/A
4632N/A /**
4632N/A * Used to recreate the AWT based Menu structure that implements the Screen Menu.
4632N/A * Also computes hashcode and stores them so that we can compare them later in needsUpdate.
4632N/A */
4632N/A void updateItems() {
4632N/A final int count = fInvoker.getMenuComponentCount();
4632N/A final Component[] items = fInvoker.getMenuComponents();
4632N/A if (needsUpdate(items, childHashArray)) {
4632N/A removeAll();
4632N/A if (count <= 0) return;
4632N/A
4632N/A childHashArray = new int[count];
4632N/A for (int i = 0; i < count; i++) {
4632N/A addItem(items[i]);
4632N/A childHashArray[i] = getHashCode(items[i]);
4632N/A }
4632N/A }
4632N/A }
4632N/A
4632N/A /**
4632N/A * Callback from JavaMenuUpdater.m -- called when menu first opens
4632N/A */
4632N/A public void invokeOpenLater() {
4632N/A final JMenu invoker = fInvoker;
4632N/A if (invoker == null) {
4632N/A System.err.println("invoker is null!");
4632N/A return;
4632N/A }
4632N/A
4632N/A try {
4632N/A LWCToolkit.invokeAndWait(new Runnable() {
4632N/A public void run() {
4632N/A invoker.setSelected(true);
4632N/A invoker.validate();
4632N/A updateItems();
4632N/A fItemBounds = new Rectangle[invoker.getMenuComponentCount()];
4632N/A }
4632N/A }, null);
4632N/A } catch (final Exception e) {
4632N/A System.err.println(e);
4632N/A e.printStackTrace();
4632N/A }
4632N/A }
4632N/A
4632N/A /**
4632N/A * Callback from JavaMenuUpdater.m -- called when menu closes.
4632N/A */
4632N/A public void invokeMenuClosing() {
4632N/A final JMenu invoker = fInvoker;
4632N/A if (invoker == null) return;
4632N/A
4632N/A try {
4632N/A LWCToolkit.invokeAndWait(new Runnable() {
4632N/A public void run() {
4632N/A invoker.setSelected(false);
4632N/A
4632N/A // Null out the tracking rectangles and the array.
4632N/A if (fItemBounds != null) {
4632N/A for (int i = 0; i < fItemBounds.length; i++) {
4632N/A fItemBounds[i] = null;
4632N/A }
4632N/A }
4632N/A
4632N/A fItemBounds = null;
4632N/A }
4632N/A }, null);
4632N/A } catch (final Exception e) {
4632N/A e.printStackTrace();
4632N/A }
4632N/A }
4632N/A
4632N/A /**
4632N/A * Callback from JavaMenuUpdater.m -- called when menu item is hilighted.
4632N/A *
4632N/A * @param inWhichItem The menu item selected by the user. -1 if mouse moves off the menu.
4632N/A * @param itemRectTop
4632N/A * @param itemRectLeft
4632N/A * @param itemRectBottom
4632N/A * @param itemRectRight Tracking rectangle coordinates.
4632N/A */
4632N/A public void handleItemTargeted(final int inWhichItem, final int itemRectTop, final int itemRectLeft, final int itemRectBottom, final int itemRectRight) {
4632N/A if (fItemBounds == null || inWhichItem < 0 || inWhichItem > (fItemBounds.length - 1)) return;
4632N/A final Rectangle itemRect = new Rectangle(itemRectLeft, itemRectTop, itemRectRight - itemRectLeft, itemRectBottom - itemRectTop);
4632N/A fItemBounds[inWhichItem] = itemRect;
4632N/A }
4632N/A
4632N/A /**
4632N/A * Callback from JavaMenuUpdater.m -- called when mouse event happens on the menu.
4632N/A */
4632N/A public void handleMouseEvent(final int kind, final int x, final int y, final int modifiers, final long when) {
4632N/A if (kind == 0) return;
4632N/A if (fItemBounds == null) return;
4632N/A
4632N/A SwingUtilities.invokeLater(new Runnable() {
4632N/A @Override
4632N/A public void run() {
4632N/A Component target = null;
4632N/A Rectangle targetRect = null;
4632N/A for (int i = 0; i < fItemBounds.length; i++) {
4632N/A final Rectangle testRect = fItemBounds[i];
4632N/A if (testRect != null) {
4632N/A if (testRect.contains(x, y)) {
4632N/A target = fInvoker.getMenuComponent(i);
4632N/A targetRect = testRect;
4632N/A break;
4632N/A }
4632N/A }
4632N/A }
4632N/A if (target == null && fLastMouseEventTarget == null) return;
4632N/A
4632N/A // Send a mouseExited to the previously hilited item, if it wasn't 0.
4632N/A if (target != fLastMouseEventTarget) {
4632N/A if (fLastMouseEventTarget != null) {
4632N/A LWToolkit.postEvent(new MouseEvent(fLastMouseEventTarget, MouseEvent.MOUSE_EXITED, when, modifiers, x - fLastTargetRect.x, y - fLastTargetRect.y, 0, false));
4632N/A }
4632N/A // Send a mouseEntered to the current hilited item, if it wasn't 0.
4632N/A if (target != null) {
4632N/A LWToolkit.postEvent(new MouseEvent(target, MouseEvent.MOUSE_ENTERED, when, modifiers, x - targetRect.x, y - targetRect.y, 0, false));
4632N/A }
4632N/A fLastMouseEventTarget = target;
4632N/A fLastTargetRect = targetRect;
4632N/A }
4632N/A // Post a mouse event to the current item.
4632N/A if (target == null) return;
4632N/A LWToolkit.postEvent(new MouseEvent(target, kind, when, modifiers, x - targetRect.x, y - targetRect.y, 0, false));
4632N/A }
4632N/A });
4632N/A }
4632N/A
4632N/A ScreenMenuPropertyListener fPropertyListener;
4632N/A public void addNotify() {
4632N/A super.addNotify();
4632N/A if (fModelPtr == 0) {
4632N/A fInvoker.addContainerListener(this);
4632N/A fInvoker.addComponentListener(this);
4632N/A fPropertyListener = new ScreenMenuPropertyListener(this);
4632N/A fInvoker.addPropertyChangeListener(fPropertyListener);
4632N/A
4632N/A final Icon icon = fInvoker.getIcon();
4632N/A if (icon != null) {
4632N/A this.setIcon(icon);
4632N/A }
4632N/A
4632N/A final String tooltipText = fInvoker.getToolTipText();
4632N/A if (tooltipText != null) {
4632N/A this.setToolTipText(tooltipText);
4632N/A }
4632N/A final MenuComponentPeer peer = getPeer();
4632N/A if (peer instanceof CMenu) {
4632N/A final CMenu menu = (CMenu)peer;
4632N/A final long nativeMenu = menu.getNativeMenu();
4632N/A fModelPtr = addMenuListeners(this, nativeMenu);
4632N/A }
4632N/A }
4632N/A }
4632N/A
4632N/A public void removeNotify() {
4632N/A // Call super so that the NSMenu has been removed, before we release the delegate in removeMenuListeners
4632N/A super.removeNotify();
4632N/A fItems.clear();
4632N/A if (fModelPtr != 0) {
4632N/A removeMenuListeners(fModelPtr);
4632N/A fModelPtr = 0;
4632N/A fInvoker.removeContainerListener(this);
4632N/A fInvoker.removeComponentListener(this);
4632N/A fInvoker.removePropertyChangeListener(fPropertyListener);
4632N/A }
4632N/A }
4632N/A
4632N/A /**
4632N/A * Invoked when a component has been added to the container.
4632N/A */
4632N/A public void componentAdded(final ContainerEvent e) {
4632N/A addItem(e.getChild());
4632N/A }
4632N/A
4632N/A /**
4632N/A * Invoked when a component has been removed from the container.
4632N/A */
4632N/A public void componentRemoved(final ContainerEvent e) {
4632N/A final Component child = e.getChild();
4632N/A final MenuItem sm = fItems.get(child);
4632N/A if (sm == null) return;
4632N/A
4632N/A remove(sm);
4632N/A fItems.remove(sm);
4632N/A }
4632N/A
4632N/A /**
4632N/A * Invoked when the component's size changes.
4632N/A */
4632N/A public void componentResized(final ComponentEvent e) {}
4632N/A
4632N/A /**
4632N/A * Invoked when the component's position changes.
4632N/A */
4632N/A public void componentMoved(final ComponentEvent e) {}
4632N/A
4632N/A /**
4632N/A * Invoked when the component has been made visible.
4632N/A * See componentHidden - we should still have a MenuItem
4632N/A * it just isn't inserted
4632N/A */
4632N/A public void componentShown(final ComponentEvent e) {
4632N/A setVisible(true);
4632N/A }
4632N/A
4632N/A /**
4632N/A * Invoked when the component has been made invisible.
4632N/A * MenuComponent.setVisible does nothing,
4632N/A * so we remove the ScreenMenuItem from the ScreenMenu
4632N/A * but leave it in fItems
4632N/A */
4632N/A public void componentHidden(final ComponentEvent e) {
4632N/A setVisible(false);
4632N/A }
4632N/A
4632N/A public void setVisible(final boolean b) {
4632N/A // Tell our parent to add/remove us
4632N/A final MenuContainer parent = getParent();
4632N/A
4632N/A if (parent != null) {
4632N/A if (parent instanceof ScreenMenu) {
4632N/A final ScreenMenu sm = (ScreenMenu)parent;
4632N/A sm.setChildVisible(fInvoker, b);
4632N/A }
4632N/A }
4632N/A }
4632N/A
4632N/A public void setChildVisible(final JMenuItem child, final boolean b) {
4632N/A fItems.remove(child);
4632N/A updateItems();
4632N/A }
4632N/A
4632N/A public void setAccelerator(final KeyStroke ks) {}
4632N/A
4632N/A // only check and radio items can be indeterminate
4632N/A public void setIndeterminate(boolean indeterminate) { }
4632N/A
4632N/A public void setToolTipText(final String text) {
4632N/A final MenuComponentPeer peer = getPeer();
4632N/A if (!(peer instanceof CMenuItem)) return;
4632N/A
4632N/A final CMenuItem cmi = (CMenuItem)peer;
4632N/A cmi.setToolTipText(text);
4632N/A }
4632N/A
4632N/A public void setIcon(final Icon i) {
4632N/A final MenuComponentPeer peer = getPeer();
4632N/A if (!(peer instanceof CMenuItem)) return;
4632N/A
4632N/A final CMenuItem cmi = (CMenuItem)peer;
4632N/A Image img = null;
4632N/A
4632N/A if (i != null) {
4632N/A if (i.getIconWidth() > 0 && i.getIconHeight() > 0) {
4632N/A img = AquaIcon.getImageForIcon(i);
4632N/A }
4632N/A }
4632N/A cmi.setImage(img);
4632N/A }
4632N/A
4632N/A
4632N/A /**
4632N/A * Gets a hashCode for a JMenu or JMenuItem or subclass so that we can compare for
4632N/A * changes in the Menu.
4632N/A *
4632N/A */
4632N/A static int getHashCode(final Component m) {
4632N/A int hashCode = m.hashCode();
4632N/A
4632N/A if (m instanceof JMenuItem) {
4632N/A final JMenuItem mi = (JMenuItem) m;
4632N/A
4632N/A final String text = mi.getText();
4632N/A if (text != null) hashCode ^= text.hashCode();
4632N/A
4632N/A final Icon icon = mi.getIcon();
4632N/A if (icon != null) hashCode ^= icon.hashCode();
4632N/A
4632N/A final Icon disabledIcon = mi.getDisabledIcon();
4632N/A if (disabledIcon != null) hashCode ^= disabledIcon.hashCode();
4632N/A
4632N/A final Action action = mi.getAction();
4632N/A if (action != null) hashCode ^= action.hashCode();
4632N/A
4632N/A final KeyStroke ks = mi.getAccelerator();
4632N/A if (ks != null) hashCode ^= ks.hashCode();
4632N/A
4632N/A hashCode ^= Boolean.valueOf(mi.isVisible()).hashCode();
4632N/A hashCode ^= Boolean.valueOf(mi.isEnabled()).hashCode();
4632N/A hashCode ^= Boolean.valueOf(mi.isSelected()).hashCode();
4632N/A
4632N/A } else if (m instanceof JSeparator) {
4632N/A hashCode ^= "-".hashCode();
4632N/A }
4632N/A
4632N/A return hashCode;
4632N/A }
4632N/A
4632N/A void addItem(final Component m) {
4632N/A if (!m.isVisible()) return;
4632N/A MenuItem sm = fItems.get(m);
4632N/A
4632N/A if (sm == null) {
4632N/A if (m instanceof JMenu) {
4632N/A sm = new ScreenMenu((JMenu)m);
4632N/A } else if (m instanceof JCheckBoxMenuItem) {
4632N/A sm = new ScreenMenuItemCheckbox((JCheckBoxMenuItem)m);
4632N/A } else if (m instanceof JRadioButtonMenuItem) {
4632N/A sm = new ScreenMenuItemCheckbox((JRadioButtonMenuItem)m);
4632N/A } else if (m instanceof JMenuItem) {
4632N/A sm = new ScreenMenuItem((JMenuItem)m);
4632N/A } else if (m instanceof JPopupMenu.Separator || m instanceof JSeparator) {
4632N/A sm = new MenuItem("-"); // This is what java.awt.Menu.addSeparator does
4632N/A }
4632N/A
4632N/A // Only place the menu item in the hashtable if we just created it.
4632N/A if (sm != null) {
4632N/A fItems.put(m, sm);
4632N/A }
4632N/A }
4632N/A
4632N/A if (sm != null) {
4632N/A add(sm);
4632N/A }
4632N/A }
4632N/A}