src/macosx/classes/com/apple/laf/ScreenMenuItem.java

Print this page




  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.laf;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.awt.peer.MenuComponentPeer;
  31 
  32 import javax.swing.*;
  33 import javax.swing.plaf.ComponentUI;
  34 
  35 import sun.lwawt.macosx.CMenuItem;
  36 
  37 class ScreenMenuItem extends MenuItem implements ActionListener, ComponentListener, ScreenMenuPropertyHandler {
  38     ScreenMenuPropertyListener fListener;
  39     JMenuItem fMenuItem;
  40 
  41     ScreenMenuItem(final JMenuItem mi) {
  42         super(mi.getText());
  43         fMenuItem = mi;
  44         setEnabled(fMenuItem.isEnabled());
  45         final ComponentUI ui = fMenuItem.getUI();
  46 
  47         if (ui instanceof ScreenMenuItemUI) {
  48             ((ScreenMenuItemUI)ui).updateListenersForScreenMenuItem();
  49             // SAK:  Not calling this means that mouse and mouse motion listeners don't get
  50             // installed.  Not a problem because the menu manager handles tracking for us.
  51     }
  52     }
  53 
  54     public void addNotify() {
  55         super.addNotify();
  56 
  57         fMenuItem.addComponentListener(this);


  79             this.setToolTipText(tooltipText);
  80         }
  81 
  82         if (fMenuItem instanceof JRadioButtonMenuItem) {
  83             final ComponentUI ui = fMenuItem.getUI();
  84 
  85             if (ui instanceof ScreenMenuItemUI) {
  86                 ((ScreenMenuItemUI)ui).updateListenersForScreenMenuItem();
  87             }
  88         }
  89     }
  90 
  91     public void removeNotify() {
  92         super.removeNotify();
  93         removeActionListener(this);
  94         fMenuItem.removePropertyChangeListener(fListener);
  95         fListener = null;
  96         fMenuItem.removeComponentListener(this);
  97     }
  98 
  99     public void setAccelerator(final KeyStroke ks) {
 100         if (ks == null) {
 101             setShortcut(null);

 102             return;
 103         }
 104 
 105         final MenuComponentPeer peer = getPeer();
 106         if (peer instanceof CMenuItem) {
 107             final CMenuItem ourPeer = (CMenuItem)peer;
 108             ourPeer.setLabel(fMenuItem.getText(), ks.getKeyChar(), ks.getKeyCode(), ks.getModifiers());
 109         } else {
 110             setShortcut(new MenuShortcut(ks.getKeyCode(), (ks.getModifiers() & InputEvent.SHIFT_MASK) != 0));


 111         }









 112     }
 113 
 114     public void actionPerformed(final ActionEvent e) {
 115         fMenuItem.doClick(0); // This takes care of all the different events
 116     }
 117 
 118     /**
 119      * Invoked when the component's size changes.
 120      */
 121     public void componentResized(final ComponentEvent e) {}
 122 
 123     /**
 124      * Invoked when the component's position changes.
 125      */
 126     public void componentMoved(final ComponentEvent e) {}
 127 
 128     /**
 129      * Invoked when the component has been made visible.
 130      * See componentHidden - we should still have a MenuItem
 131      * it just isn't inserted




  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.apple.laf;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.awt.peer.MenuComponentPeer;
  31 
  32 import javax.swing.*;
  33 import javax.swing.plaf.ComponentUI;
  34 
  35 import sun.lwawt.macosx.CMenuItem;
  36 
  37 final class ScreenMenuItem extends MenuItem implements ActionListener, ComponentListener, ScreenMenuPropertyHandler {
  38     ScreenMenuPropertyListener fListener;
  39     JMenuItem fMenuItem;
  40 
  41     ScreenMenuItem(final JMenuItem mi) {
  42         super(mi.getText());
  43         fMenuItem = mi;
  44         setEnabled(fMenuItem.isEnabled());
  45         final ComponentUI ui = fMenuItem.getUI();
  46 
  47         if (ui instanceof ScreenMenuItemUI) {
  48             ((ScreenMenuItemUI)ui).updateListenersForScreenMenuItem();
  49             // SAK:  Not calling this means that mouse and mouse motion listeners don't get
  50             // installed.  Not a problem because the menu manager handles tracking for us.
  51     }
  52     }
  53 
  54     public void addNotify() {
  55         super.addNotify();
  56 
  57         fMenuItem.addComponentListener(this);


  79             this.setToolTipText(tooltipText);
  80         }
  81 
  82         if (fMenuItem instanceof JRadioButtonMenuItem) {
  83             final ComponentUI ui = fMenuItem.getUI();
  84 
  85             if (ui instanceof ScreenMenuItemUI) {
  86                 ((ScreenMenuItemUI)ui).updateListenersForScreenMenuItem();
  87             }
  88         }
  89     }
  90 
  91     public void removeNotify() {
  92         super.removeNotify();
  93         removeActionListener(this);
  94         fMenuItem.removePropertyChangeListener(fListener);
  95         fListener = null;
  96         fMenuItem.removeComponentListener(this);
  97     }
  98 
  99     static void syncLabelAndKS(MenuItem menuItem, String label, KeyStroke ks) {
 100         final MenuComponentPeer peer = menuItem.getPeer();
 101         if (!(peer instanceof CMenuItem)) {
 102             //Is it possible?
 103             return;
 104         }
 105         final CMenuItem cmi = (CMenuItem) peer;
 106         if (ks == null) {
 107             cmi.setLabel(label);


 108         } else {
 109             cmi.setLabel(label, ks.getKeyChar(), ks.getKeyCode(),
 110                          ks.getModifiers());
 111         }
 112     }
 113 
 114     @Override
 115     public synchronized void setLabel(final String label) {
 116         syncLabelAndKS(this, label, fMenuItem.getAccelerator());
 117     }
 118 
 119     @Override
 120     public void setAccelerator(final KeyStroke ks) {
 121         syncLabelAndKS(this, fMenuItem.getText(), ks);
 122     }
 123 
 124     public void actionPerformed(final ActionEvent e) {
 125         fMenuItem.doClick(0); // This takes care of all the different events
 126     }
 127 
 128     /**
 129      * Invoked when the component's size changes.
 130      */
 131     public void componentResized(final ComponentEvent e) {}
 132 
 133     /**
 134      * Invoked when the component's position changes.
 135      */
 136     public void componentMoved(final ComponentEvent e) {}
 137 
 138     /**
 139      * Invoked when the component has been made visible.
 140      * See componentHidden - we should still have a MenuItem
 141      * it just isn't inserted