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.ButtonUI;
  34 
  35 import com.apple.laf.AquaMenuItemUI.IndeterminateListener;
  36 
  37 import sun.lwawt.macosx.*;
  38 
  39 class ScreenMenuItemCheckbox extends CheckboxMenuItem implements ActionListener, ComponentListener, ScreenMenuPropertyHandler, ItemListener {
  40     JMenuItem fMenuItem;
  41     MenuContainer fParent;
  42 
  43     ScreenMenuItemCheckbox(final JCheckBoxMenuItem mi) {
  44         super(mi.getText(), mi.getState());
  45         init(mi);
  46     }
  47 
  48     ScreenMenuItemCheckbox(final JRadioButtonMenuItem mi) {
  49         super(mi.getText(), mi.getModel().isSelected());
  50         init(mi);
  51     }
  52 
  53     public void init(final JMenuItem mi) {
  54         fMenuItem = mi;
  55         setEnabled(fMenuItem.isEnabled());
  56     }
  57 
  58     ScreenMenuPropertyListener fPropertyListener;
  59     public void addNotify() {
  60         super.addNotify();
  61 
  62         // Avoid the Auto toggle behavior of AWT CheckBoxMenuItem
  63         CCheckboxMenuItem ccb = (CCheckboxMenuItem) getPeer();
  64         ccb.setAutoToggle(false);
  65 
  66         fMenuItem.addComponentListener(this);
  67         fPropertyListener = new ScreenMenuPropertyListener(this);
  68         fMenuItem.addPropertyChangeListener(fPropertyListener);
  69         addActionListener(this);
  70         addItemListener(this);
  71         fMenuItem.addItemListener(this);
  72         setIndeterminate(IndeterminateListener.isIndeterminate(fMenuItem));
  73 
  74         // can't setState or setAccelerator or setIcon till we have a peer
  75         setAccelerator(fMenuItem.getAccelerator());
  76 
  77         final Icon icon = fMenuItem.getIcon();
  78         if (icon != null) {
  79             this.setIcon(icon);
  80         }
  81 
  82         final String tooltipText = fMenuItem.getToolTipText();
  83         if (tooltipText != null) {
  84             this.setToolTipText(tooltipText);
  85         }
  86 
  87         // sja fix is this needed?
  88         fMenuItem.addItemListener(this);
  89 
  90         final ButtonUI ui = fMenuItem.getUI();
  91         if (ui instanceof ScreenMenuItemUI) {
  92             ((ScreenMenuItemUI)ui).updateListenersForScreenMenuItem();
  93         }
  94 
  95         if (fMenuItem instanceof JCheckBoxMenuItem) {
  96             setState(((JCheckBoxMenuItem)fMenuItem).isSelected());
  97         } else {
  98             setState(fMenuItem.getModel().isSelected());
  99         }
 100     }
 101 
 102     public void removeNotify() {
 103         fMenuItem.removeComponentListener(this);
 104         fMenuItem.removePropertyChangeListener(fPropertyListener);
 105         fPropertyListener = null;
 106         removeActionListener(this);
 107         removeItemListener(this);
 108         fMenuItem.removeItemListener(this);
 109 
 110         super.removeNotify();
 111     }
 112 
 113     public void setAccelerator(final KeyStroke ks) {
 114         if (ks == null) {
 115             setShortcut(null);
 116             return;
 117         }
 118 
 119         final MenuComponentPeer peer = getPeer();
 120         if (peer instanceof CMenuItem) {
 121             final CMenuItem ourPeer = (CMenuItem)peer;
 122             ourPeer.setLabel(fMenuItem.getText(), ks.getKeyChar(), ks.getKeyCode(), ks.getModifiers());
 123         } else {
 124             setShortcut(new MenuShortcut(ks.getKeyCode(), (ks.getModifiers() & InputEvent.SHIFT_MASK) != 0));
 125         }
 126     }
 127 
 128     public void actionPerformed(final ActionEvent e) {
 129         fMenuItem.doClick(0); // This takes care of all the different events
 130     }
 131 
 132     /**
 133      * Invoked when the component's size changes.
 134      */
 135     public void componentResized(final ComponentEvent e) {}
 136 
 137     /**
 138      * Invoked when the component's position changes.
 139      */
 140     public void componentMoved(final ComponentEvent e) {}
 141 
 142     /**
 143      * Invoked when the component has been made visible.
 144      * See componentHidden - we should still have a MenuItem
 145      * it just isn't inserted
 146      */
 147     public void componentShown(final ComponentEvent e) {
 148         setVisible(true);
 149     }
 150 
 151     /**
 152      * Invoked when the component has been made invisible.
 153      * MenuComponent.setVisible does nothing,
 154      * so we remove the ScreenMenuItem from the ScreenMenu
 155      * but leave it in fItems
 156      */
 157     public void componentHidden(final ComponentEvent e) {
 158         setVisible(false);
 159     }
 160 
 161     public void setToolTipText(final String text) {
 162         final MenuComponentPeer peer = getPeer();
 163         if (!(peer instanceof CMenuItem)) return;
 164 
 165         ((CMenuItem)peer).setToolTipText(text);
 166     }
 167 
 168     public void setIcon(final Icon i) {
 169         final MenuComponentPeer peer = getPeer();
 170         if (!(peer instanceof CMenuItem)) return;
 171 
 172         final CMenuItem cmi = (CMenuItem)peer;
 173         Image img = null;
 174 
 175         if (i != null) {
 176             if (i.getIconWidth() > 0 && i.getIconHeight() > 0) {
 177                 img = AquaIcon.getImageForIcon(i);
 178             }
 179         }
 180         cmi.setImage(img);
 181     }
 182 
 183     public void setVisible(final boolean b) {
 184         // Tell our parent to add/remove us
 185         // Hang on to our parent
 186         if (fParent == null) fParent = getParent();
 187         ((ScreenMenuPropertyHandler)fParent).setChildVisible(fMenuItem, b);
 188     }
 189 
 190     // we have no children
 191     public void setChildVisible(final JMenuItem child, final boolean b) {}
 192 
 193     /**
 194      * Invoked when an item's state has been changed.
 195      */
 196     public void itemStateChanged(final ItemEvent e) {
 197         if (e.getSource() == this) {
 198             fMenuItem.doClick(0);
 199             return;
 200         }
 201 
 202             switch (e.getStateChange()) {
 203                 case ItemEvent.SELECTED:
 204                     setState(true);
 205                     break;
 206                 case ItemEvent.DESELECTED:
 207                     setState(false);
 208                     break;
 209             }
 210         }
 211 
 212     public void setIndeterminate(final boolean indeterminate) {
 213         final MenuComponentPeer peer = getPeer();
 214         if (peer instanceof CCheckboxMenuItem) {
 215             ((CCheckboxMenuItem)peer).setIsIndeterminate(indeterminate);
 216         }
 217     }
 218 }