1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  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);
  58         fListener = new ScreenMenuPropertyListener(this);
  59         fMenuItem.addPropertyChangeListener(fListener);
  60         addActionListener(this);
  61 
  62         setEnabled(fMenuItem.isEnabled());
  63 
  64         // can't setState or setAccelerator or setIcon till we have a peer
  65         setAccelerator(fMenuItem.getAccelerator());
  66 
  67         final String label = fMenuItem.getText();
  68         if (label != null) {
  69             setLabel(label);
  70         }
  71 
  72         final Icon icon = fMenuItem.getIcon();
  73         if (icon != null) {
  74             this.setIcon(icon);
  75         }
  76 
  77         final String tooltipText = fMenuItem.getToolTipText();
  78         if (tooltipText != null) {
  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
 132      */
 133     public void componentShown(final ComponentEvent e) {
 134         setVisible(true);
 135     }
 136 
 137     /**
 138      * Invoked when the component has been made invisible.
 139      * MenuComponent.setVisible does nothing,
 140      * so we remove the ScreenMenuItem from the ScreenMenu
 141      * but leave it in fItems
 142      */
 143     public void componentHidden(final ComponentEvent e) {
 144         setVisible(false);
 145     }
 146 
 147     public void setVisible(final boolean b) {
 148         // Tell our parent to add/remove us -- parent may be nil if we aren't set up yet.
 149         // Hang on to our parent
 150         final MenuContainer parent = getParent();
 151 
 152         if (parent != null) {
 153             ((ScreenMenuPropertyHandler)parent).setChildVisible(fMenuItem, b);
 154         }
 155     }
 156 
 157     public void setToolTipText(final String text) {
 158         final MenuComponentPeer peer = getPeer();
 159         if (!(peer instanceof CMenuItem)) return;
 160 
 161         final CMenuItem cmi = (CMenuItem)peer;
 162         cmi.setToolTipText(text);
 163     }
 164 
 165     public void setIcon(final Icon i) {
 166         final MenuComponentPeer peer = getPeer();
 167         if (!(peer instanceof CMenuItem)) return;
 168 
 169         final CMenuItem cmi = (CMenuItem)peer;
 170             Image img = null;
 171 
 172         if (i != null) {
 173             if (i.getIconWidth() > 0 && i.getIconHeight() > 0) {
 174                     img = AquaIcon.getImageForIcon(i);
 175                 }
 176         }
 177             cmi.setImage(img);
 178         }
 179 
 180     // we have no children
 181     public void setChildVisible(final JMenuItem child, final boolean b) {}
 182 
 183     // only check and radio items can be indeterminate
 184     public void setIndeterminate(boolean indeterminate) { }
 185 }