1 /*
2 * Copyright (c) 2011, 2014, 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 @SuppressWarnings("serial") // JDK implementation class
40 final class ScreenMenuItemCheckbox extends CheckboxMenuItem implements ActionListener, ComponentListener, ScreenMenuPropertyHandler, ItemListener {
41 JMenuItem fMenuItem;
42 MenuContainer fParent;
43
44 ScreenMenuItemCheckbox(final JCheckBoxMenuItem mi) {
45 super(mi.getText(), mi.getState());
46 init(mi);
47 }
48
49 ScreenMenuItemCheckbox(final JRadioButtonMenuItem mi) {
50 super(mi.getText(), mi.getModel().isSelected());
51 init(mi);
52 }
53
54 public void init(final JMenuItem mi) {
55 fMenuItem = mi;
56 setEnabled(fMenuItem.isEnabled());
57 }
58
59 ScreenMenuPropertyListener fPropertyListener;
60 @SuppressWarnings("deprecation")
61 public void addNotify() {
62 super.addNotify();
63
64 // Avoid the Auto toggle behavior of AWT CheckBoxMenuItem
65 CCheckboxMenuItem ccb = (CCheckboxMenuItem) getPeer();
66 ccb.setAutoToggle(false);
67
68 fMenuItem.addComponentListener(this);
69 fPropertyListener = new ScreenMenuPropertyListener(this);
70 fMenuItem.addPropertyChangeListener(fPropertyListener);
71 addActionListener(this);
72 addItemListener(this);
73 fMenuItem.addItemListener(this);
74 setIndeterminate(IndeterminateListener.isIndeterminate(fMenuItem));
75
76 // can't setState or setAccelerator or setIcon till we have a peer
77 setAccelerator(fMenuItem.getAccelerator());
78
79 final Icon icon = fMenuItem.getIcon();
80 if (icon != null) {
81 this.setIcon(icon);
82 }
83
84 final String tooltipText = fMenuItem.getToolTipText();
85 if (tooltipText != null) {
138
139 /**
140 * Invoked when the component has been made visible.
141 * See componentHidden - we should still have a MenuItem
142 * it just isn't inserted
143 */
144 public void componentShown(final ComponentEvent e) {
145 setVisible(true);
146 }
147
148 /**
149 * Invoked when the component has been made invisible.
150 * MenuComponent.setVisible does nothing,
151 * so we remove the ScreenMenuItem from the ScreenMenu
152 * but leave it in fItems
153 */
154 public void componentHidden(final ComponentEvent e) {
155 setVisible(false);
156 }
157
158 @SuppressWarnings("deprecation")
159 public void setToolTipText(final String text) {
160 final MenuComponentPeer peer = getPeer();
161 if (!(peer instanceof CMenuItem)) return;
162
163 ((CMenuItem)peer).setToolTipText(text);
164 }
165
166 @SuppressWarnings("deprecation")
167 public void setIcon(final Icon i) {
168 final MenuComponentPeer peer = getPeer();
169 if (!(peer instanceof CMenuItem)) return;
170
171 final CMenuItem cmi = (CMenuItem)peer;
172 Image img = null;
173
174 if (i != null) {
175 if (i.getIconWidth() > 0 && i.getIconHeight() > 0) {
176 img = AquaIcon.getImageForIcon(i);
177 }
178 }
179 cmi.setImage(img);
180 }
181
182 public void setVisible(final boolean b) {
183 // Tell our parent to add/remove us
184 // Hang on to our parent
185 if (fParent == null) fParent = getParent();
186 ((ScreenMenuPropertyHandler)fParent).setChildVisible(fMenuItem, b);
187 }
188
191
192 /**
193 * Invoked when an item's state has been changed.
194 */
195 public void itemStateChanged(final ItemEvent e) {
196 if (e.getSource() == this) {
197 fMenuItem.doClick(0);
198 return;
199 }
200
201 switch (e.getStateChange()) {
202 case ItemEvent.SELECTED:
203 forceSetState(true);
204 break;
205 case ItemEvent.DESELECTED:
206 forceSetState(false);
207 break;
208 }
209 }
210
211 @SuppressWarnings("deprecation")
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
219 /*
220 * The CCheckboxMenuItem peer is calling setState unconditionally every time user clicks the menu
221 * However for Swing controls in the screen menu bar it is wrong - the state should be changed only
222 * in response to the ITEM_STATE_CHANGED event. So the setState is overridden to no-op and all the
223 * correct state changes are made with forceSetState
224 */
225
226 @Override
227 public synchronized void setState(boolean b) {
228 // No Op
229 }
230
231 private void forceSetState(boolean b) {
232 super.setState(b);
233 }
|
1 /*
2 * Copyright (c) 2011, 2015, 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
31 import javax.swing.*;
32 import javax.swing.plaf.ButtonUI;
33
34 import com.apple.laf.AquaMenuItemUI.IndeterminateListener;
35
36 import sun.awt.AWTAccessor;
37 import sun.lwawt.macosx.*;
38
39 @SuppressWarnings("serial") // JDK implementation class
40 final class ScreenMenuItemCheckbox extends CheckboxMenuItem
41 implements ActionListener, ComponentListener, ScreenMenuPropertyHandler,
42 ItemListener {
43
44 JMenuItem fMenuItem;
45 MenuContainer fParent;
46
47 ScreenMenuItemCheckbox(final JCheckBoxMenuItem mi) {
48 super(mi.getText(), mi.getState());
49 init(mi);
50 }
51
52 ScreenMenuItemCheckbox(final JRadioButtonMenuItem mi) {
53 super(mi.getText(), mi.getModel().isSelected());
54 init(mi);
55 }
56
57 public void init(final JMenuItem mi) {
58 fMenuItem = mi;
59 setEnabled(fMenuItem.isEnabled());
60 }
61
62 ScreenMenuPropertyListener fPropertyListener;
63
64 public void addNotify() {
65 super.addNotify();
66
67 // Avoid the Auto toggle behavior of AWT CheckBoxMenuItem
68 CCheckboxMenuItem ccb = AWTAccessor.getMenuComponentAccessor().getPeer(this);
69 ccb.setAutoToggle(false);
70
71 fMenuItem.addComponentListener(this);
72 fPropertyListener = new ScreenMenuPropertyListener(this);
73 fMenuItem.addPropertyChangeListener(fPropertyListener);
74 addActionListener(this);
75 addItemListener(this);
76 fMenuItem.addItemListener(this);
77 setIndeterminate(IndeterminateListener.isIndeterminate(fMenuItem));
78
79 // can't setState or setAccelerator or setIcon till we have a peer
80 setAccelerator(fMenuItem.getAccelerator());
81
82 final Icon icon = fMenuItem.getIcon();
83 if (icon != null) {
84 this.setIcon(icon);
85 }
86
87 final String tooltipText = fMenuItem.getToolTipText();
88 if (tooltipText != null) {
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 Object peer = AWTAccessor.getMenuComponentAccessor().getPeer(this);
163 if (!(peer instanceof CMenuItem)) return;
164
165 ((CMenuItem)peer).setToolTipText(text);
166 }
167
168 public void setIcon(final Icon i) {
169 Object peer = AWTAccessor.getMenuComponentAccessor().getPeer(this);
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
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 forceSetState(true);
205 break;
206 case ItemEvent.DESELECTED:
207 forceSetState(false);
208 break;
209 }
210 }
211
212 public void setIndeterminate(final boolean indeterminate) {
213 Object peer = AWTAccessor.getMenuComponentAccessor().getPeer(this);
214 if (peer instanceof CCheckboxMenuItem) {
215 ((CCheckboxMenuItem)peer).setIsIndeterminate(indeterminate);
216 }
217 }
218
219 /*
220 * The CCheckboxMenuItem peer is calling setState unconditionally every time user clicks the menu
221 * However for Swing controls in the screen menu bar it is wrong - the state should be changed only
222 * in response to the ITEM_STATE_CHANGED event. So the setState is overridden to no-op and all the
223 * correct state changes are made with forceSetState
224 */
225
226 @Override
227 public synchronized void setState(boolean b) {
228 // No Op
229 }
230
231 private void forceSetState(boolean b) {
232 super.setState(b);
233 }
|