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