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 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);
  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     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
 142      */
 143     public void componentShown(final ComponentEvent e) {
 144         setVisible(true);
 145     }
 146 
 147     /**
 148      * Invoked when the component has been made invisible.
 149      * MenuComponent.setVisible does nothing,
 150      * so we remove the ScreenMenuItem from the ScreenMenu
 151      * but leave it in fItems
 152      */
 153     public void componentHidden(final ComponentEvent e) {
 154         setVisible(false);
 155     }
 156 
 157     public void setVisible(final boolean b) {
 158         // Tell our parent to add/remove us -- parent may be nil if we aren't set up yet.
 159         // Hang on to our parent
 160         final MenuContainer parent = getParent();
 161 
 162         if (parent != null) {
 163             ((ScreenMenuPropertyHandler)parent).setChildVisible(fMenuItem, b);
 164         }
 165     }
 166 
 167     public void setToolTipText(final String text) {
 168         final MenuComponentPeer peer = getPeer();
 169         if (!(peer instanceof CMenuItem)) return;
 170 
 171         final CMenuItem cmi = (CMenuItem)peer;
 172         cmi.setToolTipText(text);
 173     }
 174 
 175     public void setIcon(final Icon i) {
 176         final MenuComponentPeer peer = getPeer();
 177         if (!(peer instanceof CMenuItem)) return;
 178 
 179         final CMenuItem cmi = (CMenuItem)peer;
 180             Image img = null;
 181 
 182         if (i != null) {
 183             if (i.getIconWidth() > 0 && i.getIconHeight() > 0) {
 184                     img = AquaIcon.getImageForIcon(i);
 185                 }
 186         }
 187             cmi.setImage(img);
 188         }
 189 
 190     // we have no children
 191     public void setChildVisible(final JMenuItem child, final boolean b) {}
 192 
 193     // only check and radio items can be indeterminate
 194     public void setIndeterminate(boolean indeterminate) { }
 195 }