Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/javax/swing/JCheckBoxMenuItem.java
+++ new/src/share/classes/javax/swing/JCheckBoxMenuItem.java
1 1 /*
2 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25 package javax.swing;
26 26
27 27 import java.util.EventListener;
28 28
29 29 import java.awt.*;
30 30 import java.awt.event.*;
31 31 import java.awt.image.*;
32 32
33 33 import java.io.ObjectOutputStream;
34 34 import java.io.ObjectInputStream;
35 35 import java.io.IOException;
36 36
37 37 import javax.swing.plaf.*;
38 38 import javax.accessibility.*;
39 39
40 40
41 41 /**
42 42 * A menu item that can be selected or deselected. If selected, the menu
43 43 * item typically appears with a checkmark next to it. If unselected or
44 44 * deselected, the menu item appears without a checkmark. Like a regular
45 45 * menu item, a check box menu item can have either text or a graphic
46 46 * icon associated with it, or both.
47 47 * <p>
48 48 * Either <code>isSelected</code>/<code>setSelected</code> or
49 49 * <code>getState</code>/<code>setState</code> can be used
50 50 * to determine/specify the menu item's selection state. The
51 51 * preferred methods are <code>isSelected</code> and
↓ open down ↓ |
51 lines elided |
↑ open up ↑ |
52 52 * <code>setSelected</code>, which work for all menus and buttons.
53 53 * The <code>getState</code> and <code>setState</code> methods exist for
54 54 * compatibility with other component sets.
55 55 * <p>
56 56 * Menu items can be configured, and to some degree controlled, by
57 57 * <code><a href="Action.html">Action</a></code>s. Using an
58 58 * <code>Action</code> with a menu item has many benefits beyond directly
59 59 * configuring a menu item. Refer to <a href="Action.html#buttonActions">
60 60 * Swing Components Supporting <code>Action</code></a> for more
61 61 * details, and you can find more information in <a
62 - * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
62 + * href="https://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
63 63 * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
64 64 * <p>
65 65 * For further information and examples of using check box menu items,
66 66 * see <a
67 - href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
67 + href="https://docs.oracle.com/javase/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
68 68 * a section in <em>The Java Tutorial.</em>
69 69 * <p>
70 70 * <strong>Warning:</strong> Swing is not thread safe. For more
71 71 * information see <a
72 72 * href="package-summary.html#threading">Swing's Threading
73 73 * Policy</a>.
74 74 * <p>
75 75 * <strong>Warning:</strong>
76 76 * Serialized objects of this class will not be compatible with
77 77 * future Swing releases. The current serialization support is
78 78 * appropriate for short term storage or RMI between applications running
79 79 * the same version of Swing. As of 1.4, support for long term storage
80 80 * of all JavaBeans™
81 81 * has been added to the <code>java.beans</code> package.
82 82 * Please see {@link java.beans.XMLEncoder}.
83 83 *
84 84 * @beaninfo
85 85 * attribute: isContainer false
86 86 * description: A menu item which can be selected or deselected.
87 87 *
88 88 * @author Georges Saab
89 89 * @author David Karlton
90 90 */
91 91 public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants,
92 92 Accessible
93 93 {
94 94 /**
95 95 * @see #getUIClassID
96 96 * @see #readObject
97 97 */
98 98 private static final String uiClassID = "CheckBoxMenuItemUI";
99 99
100 100 /**
101 101 * Creates an initially unselected check box menu item with no set text or icon.
102 102 */
103 103 public JCheckBoxMenuItem() {
104 104 this(null, null, false);
105 105 }
106 106
107 107 /**
108 108 * Creates an initially unselected check box menu item with an icon.
109 109 *
110 110 * @param icon the icon of the CheckBoxMenuItem.
111 111 */
112 112 public JCheckBoxMenuItem(Icon icon) {
113 113 this(null, icon, false);
114 114 }
115 115
116 116 /**
117 117 * Creates an initially unselected check box menu item with text.
118 118 *
119 119 * @param text the text of the CheckBoxMenuItem
120 120 */
121 121 public JCheckBoxMenuItem(String text) {
122 122 this(text, null, false);
123 123 }
124 124
125 125 /**
126 126 * Creates a menu item whose properties are taken from the
127 127 * Action supplied.
128 128 *
129 129 * @since 1.3
130 130 */
131 131 public JCheckBoxMenuItem(Action a) {
132 132 this();
133 133 setAction(a);
134 134 }
135 135
136 136 /**
137 137 * Creates an initially unselected check box menu item with the specified text and icon.
138 138 *
139 139 * @param text the text of the CheckBoxMenuItem
140 140 * @param icon the icon of the CheckBoxMenuItem
141 141 */
142 142 public JCheckBoxMenuItem(String text, Icon icon) {
143 143 this(text, icon, false);
144 144 }
145 145
146 146 /**
147 147 * Creates a check box menu item with the specified text and selection state.
148 148 *
149 149 * @param text the text of the check box menu item.
150 150 * @param b the selected state of the check box menu item
151 151 */
152 152 public JCheckBoxMenuItem(String text, boolean b) {
153 153 this(text, null, b);
154 154 }
155 155
156 156 /**
157 157 * Creates a check box menu item with the specified text, icon, and selection state.
158 158 *
159 159 * @param text the text of the check box menu item
160 160 * @param icon the icon of the check box menu item
161 161 * @param b the selected state of the check box menu item
162 162 */
163 163 public JCheckBoxMenuItem(String text, Icon icon, boolean b) {
164 164 super(text, icon);
165 165 setModel(new JToggleButton.ToggleButtonModel());
166 166 setSelected(b);
167 167 setFocusable(false);
168 168 }
169 169
170 170 /**
171 171 * Returns the name of the L&F class
172 172 * that renders this component.
173 173 *
174 174 * @return "CheckBoxMenuItemUI"
175 175 * @see JComponent#getUIClassID
176 176 * @see UIDefaults#getUI
177 177 */
178 178 public String getUIClassID() {
179 179 return uiClassID;
180 180 }
181 181
182 182 /**
183 183 * Returns the selected-state of the item. This method
184 184 * exists for AWT compatibility only. New code should
185 185 * use isSelected() instead.
186 186 *
187 187 * @return true if the item is selected
188 188 */
189 189 public boolean getState() {
190 190 return isSelected();
191 191 }
192 192
193 193 /**
194 194 * Sets the selected-state of the item. This method
195 195 * exists for AWT compatibility only. New code should
196 196 * use setSelected() instead.
197 197 *
198 198 * @param b a boolean value indicating the item's
199 199 * selected-state, where true=selected
200 200 * @beaninfo
201 201 * description: The selection state of the check box menu item
202 202 * hidden: true
203 203 */
204 204 public synchronized void setState(boolean b) {
205 205 setSelected(b);
206 206 }
207 207
208 208
209 209 /**
210 210 * Returns an array (length 1) containing the check box menu item
211 211 * label or null if the check box is not selected.
212 212 *
213 213 * @return an array containing one Object -- the text of the menu item
214 214 * -- if the item is selected; otherwise null
215 215 */
216 216 public Object[] getSelectedObjects() {
217 217 if (isSelected() == false)
218 218 return null;
219 219 Object[] selectedObjects = new Object[1];
220 220 selectedObjects[0] = getText();
221 221 return selectedObjects;
222 222 }
223 223
224 224 /**
225 225 * See readObject() and writeObject() in JComponent for more
226 226 * information about serialization in Swing.
227 227 */
228 228 private void writeObject(ObjectOutputStream s) throws IOException {
229 229 s.defaultWriteObject();
230 230 if (getUIClassID().equals(uiClassID)) {
231 231 byte count = JComponent.getWriteObjCounter(this);
232 232 JComponent.setWriteObjCounter(this, --count);
233 233 if (count == 0 && ui != null) {
234 234 ui.installUI(this);
235 235 }
236 236 }
237 237 }
238 238
239 239
240 240 /**
241 241 * Returns a string representation of this JCheckBoxMenuItem. This method
242 242 * is intended to be used only for debugging purposes, and the
243 243 * content and format of the returned string may vary between
244 244 * implementations. The returned string may be empty but may not
245 245 * be <code>null</code>.
246 246 *
247 247 * @return a string representation of this JCheckBoxMenuItem.
248 248 */
249 249 protected String paramString() {
250 250 return super.paramString();
251 251 }
252 252
253 253 /**
254 254 * Overriden to return true, JCheckBoxMenuItem supports
255 255 * the selected state.
256 256 */
257 257 boolean shouldUpdateSelectedStateFromAction() {
258 258 return true;
259 259 }
260 260
261 261 /////////////////
262 262 // Accessibility support
263 263 ////////////////
264 264
265 265 /**
266 266 * Gets the AccessibleContext associated with this JCheckBoxMenuItem.
267 267 * For JCheckBoxMenuItems, the AccessibleContext takes the form of an
268 268 * AccessibleJCheckBoxMenuItem.
269 269 * A new AccessibleJCheckBoxMenuItem instance is created if necessary.
270 270 *
271 271 * @return an AccessibleJCheckBoxMenuItem that serves as the
272 272 * AccessibleContext of this AccessibleJCheckBoxMenuItem
273 273 */
274 274 public AccessibleContext getAccessibleContext() {
275 275 if (accessibleContext == null) {
276 276 accessibleContext = new AccessibleJCheckBoxMenuItem();
277 277 }
278 278 return accessibleContext;
279 279 }
280 280
281 281 /**
282 282 * This class implements accessibility support for the
283 283 * <code>JCheckBoxMenuItem</code> class. It provides an implementation
284 284 * of the Java Accessibility API appropriate to checkbox menu item
285 285 * user-interface elements.
286 286 * <p>
287 287 * <strong>Warning:</strong>
288 288 * Serialized objects of this class will not be compatible with
289 289 * future Swing releases. The current serialization support is
290 290 * appropriate for short term storage or RMI between applications running
291 291 * the same version of Swing. As of 1.4, support for long term storage
292 292 * of all JavaBeans™
293 293 * has been added to the <code>java.beans</code> package.
294 294 * Please see {@link java.beans.XMLEncoder}.
295 295 */
296 296 protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem {
297 297 /**
298 298 * Get the role of this object.
299 299 *
300 300 * @return an instance of AccessibleRole describing the role of the
301 301 * object
302 302 */
303 303 public AccessibleRole getAccessibleRole() {
304 304 return AccessibleRole.CHECK_BOX;
305 305 }
306 306 } // inner class AccessibleJCheckBoxMenuItem
307 307 }
↓ open down ↓ |
230 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX