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 // We call CMenuItem.setLabel(..,..,..) directly and does not initialize 122 // shortcut property. So shortcut property should not be used from the 123 // peers code directly or indirectly. 124 syncLabelAndKS(this, fMenuItem.getText(), ks); 125 } 126 127 public void actionPerformed(final ActionEvent e) { 128 fMenuItem.doClick(0); // This takes care of all the different events 129 } 130 131 /** 132 * Invoked when the component's size changes. 133 */ 134 public void componentResized(final ComponentEvent e) {} 135 136 /** 137 * Invoked when the component's position changes. 138 */ 139 public void componentMoved(final ComponentEvent e) {} 140 141 /** 142 * Invoked when the component has been made visible. 143 * See componentHidden - we should still have a MenuItem 144 * it just isn't inserted 145 */ 146 public void componentShown(final ComponentEvent e) { 147 setVisible(true); 148 } 149 150 /** 151 * Invoked when the component has been made invisible. 152 * MenuComponent.setVisible does nothing, 153 * so we remove the ScreenMenuItem from the ScreenMenu 154 * but leave it in fItems 155 */ 156 public void componentHidden(final ComponentEvent e) { 157 setVisible(false); 158 } 159 160 public void setVisible(final boolean b) { 161 // Tell our parent to add/remove us -- parent may be nil if we aren't set up yet. 162 // Hang on to our parent 163 final MenuContainer parent = getParent(); 164 165 if (parent != null) { 166 ((ScreenMenuPropertyHandler)parent).setChildVisible(fMenuItem, b); 167 } 168 } 169 170 public void setToolTipText(final String text) { 171 final MenuComponentPeer peer = getPeer(); 172 if (!(peer instanceof CMenuItem)) return; 173 174 final CMenuItem cmi = (CMenuItem)peer; 175 cmi.setToolTipText(text); 176 } 177 178 public void setIcon(final Icon i) { 179 final MenuComponentPeer peer = getPeer(); 180 if (!(peer instanceof CMenuItem)) return; 181 182 final CMenuItem cmi = (CMenuItem)peer; 183 Image img = null; 184 185 if (i != null) { 186 if (i.getIconWidth() > 0 && i.getIconHeight() > 0) { 187 img = AquaIcon.getImageForIcon(i); 188 } 189 } 190 cmi.setImage(img); 191 } 192 193 // we have no children 194 public void setChildVisible(final JMenuItem child, final boolean b) {} 195 196 // only check and radio items can be indeterminate 197 public void setIndeterminate(boolean indeterminate) { } 198 }