001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing;
026
027 import java.util.EventListener;
028
029 import java.awt.*;
030 import java.awt.event.*;
031 import java.awt.image.*;
032
033 import java.io.ObjectOutputStream;
034 import java.io.ObjectInputStream;
035 import java.io.IOException;
036
037 import javax.swing.plaf.*;
038 import javax.accessibility.*;
039
040 /**
041 * A menu item that can be selected or deselected. If selected, the menu
042 * item typically appears with a checkmark next to it. If unselected or
043 * deselected, the menu item appears without a checkmark. Like a regular
044 * menu item, a check box menu item can have either text or a graphic
045 * icon associated with it, or both.
046 * <p>
047 * Either <code>isSelected</code>/<code>setSelected</code> or
048 * <code>getState</code>/<code>setState</code> can be used
049 * to determine/specify the menu item's selection state. The
050 * preferred methods are <code>isSelected</code> and
051 * <code>setSelected</code>, which work for all menus and buttons.
052 * The <code>getState</code> and <code>setState</code> methods exist for
053 * compatibility with other component sets.
054 * <p>
055 * Menu items can be configured, and to some degree controlled, by
056 * <code><a href="Action.html">Action</a></code>s. Using an
057 * <code>Action</code> with a menu item has many benefits beyond directly
058 * configuring a menu item. Refer to <a href="Action.html#buttonActions">
059 * Swing Components Supporting <code>Action</code></a> for more
060 * details, and you can find more information in <a
061 * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
062 * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
063 * <p>
064 * For further information and examples of using check box menu items,
065 * see <a
066 href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
067 * a section in <em>The Java Tutorial.</em>
068 * <p>
069 * <strong>Warning:</strong> Swing is not thread safe. For more
070 * information see <a
071 * href="package-summary.html#threading">Swing's Threading
072 * Policy</a>.
073 * <p>
074 * <strong>Warning:</strong>
075 * Serialized objects of this class will not be compatible with
076 * future Swing releases. The current serialization support is
077 * appropriate for short term storage or RMI between applications running
078 * the same version of Swing. As of 1.4, support for long term storage
079 * of all JavaBeans<sup><font size="-2">TM</font></sup>
080 * has been added to the <code>java.beans</code> package.
081 * Please see {@link java.beans.XMLEncoder}.
082 *
083 * @beaninfo
084 * attribute: isContainer false
085 * description: A menu item which can be selected or deselected.
086 *
087 * @version 1.64 05/05/07
088 * @author Georges Saab
089 * @author David Karlton
090 */
091 public class JCheckBoxMenuItem extends JMenuItem implements
092 SwingConstants, Accessible {
093 /**
094 * @see #getUIClassID
095 * @see #readObject
096 */
097 private static final String uiClassID = "CheckBoxMenuItemUI";
098
099 /**
100 * Creates an initially unselected check box menu item with no set text or icon.
101 */
102 public JCheckBoxMenuItem() {
103 this (null, null, false);
104 }
105
106 /**
107 * Creates an initially unselected check box menu item with an icon.
108 *
109 * @param icon the icon of the CheckBoxMenuItem.
110 */
111 public JCheckBoxMenuItem(Icon icon) {
112 this (null, icon, false);
113 }
114
115 /**
116 * Creates an initially unselected check box menu item with text.
117 *
118 * @param text the text of the CheckBoxMenuItem
119 */
120 public JCheckBoxMenuItem(String text) {
121 this (text, null, false);
122 }
123
124 /**
125 * Creates a menu item whose properties are taken from the
126 * Action supplied.
127 *
128 * @since 1.3
129 */
130 public JCheckBoxMenuItem(Action a) {
131 this ();
132 setAction(a);
133 }
134
135 /**
136 * Creates an initially unselected check box menu item with the specified text and icon.
137 *
138 * @param text the text of the CheckBoxMenuItem
139 * @param icon the icon of the CheckBoxMenuItem
140 */
141 public JCheckBoxMenuItem(String text, Icon icon) {
142 this (text, icon, false);
143 }
144
145 /**
146 * Creates a check box menu item with the specified text and selection state.
147 *
148 * @param text the text of the check box menu item.
149 * @param b the selected state of the check box menu item
150 */
151 public JCheckBoxMenuItem(String text, boolean b) {
152 this (text, null, b);
153 }
154
155 /**
156 * Creates a check box menu item with the specified text, icon, and selection state.
157 *
158 * @param text the text of the check box menu item
159 * @param icon the icon of the check box menu item
160 * @param b the selected state of the check box menu item
161 */
162 public JCheckBoxMenuItem(String text, Icon icon, boolean b) {
163 super (text, icon);
164 setModel(new JToggleButton.ToggleButtonModel());
165 setSelected(b);
166 setFocusable(false);
167 }
168
169 /**
170 * Returns the name of the L&F class
171 * that renders this component.
172 *
173 * @return "CheckBoxMenuItemUI"
174 * @see JComponent#getUIClassID
175 * @see UIDefaults#getUI
176 */
177 public String getUIClassID() {
178 return uiClassID;
179 }
180
181 /**
182 * Returns the selected-state of the item. This method
183 * exists for AWT compatibility only. New code should
184 * use isSelected() instead.
185 *
186 * @return true if the item is selected
187 */
188 public boolean getState() {
189 return isSelected();
190 }
191
192 /**
193 * Sets the selected-state of the item. This method
194 * exists for AWT compatibility only. New code should
195 * use setSelected() instead.
196 *
197 * @param b a boolean value indicating the item's
198 * selected-state, where true=selected
199 * @beaninfo
200 * description: The selection state of the check box menu item
201 * hidden: true
202 */
203 public synchronized void setState(boolean b) {
204 setSelected(b);
205 }
206
207 /**
208 * Returns an array (length 1) containing the check box menu item
209 * label or null if the check box is not selected.
210 *
211 * @return an array containing one Object -- the text of the menu item
212 * -- if the item is selected; otherwise null
213 */
214 public Object[] getSelectedObjects() {
215 if (isSelected() == false)
216 return null;
217 Object[] selectedObjects = new Object[1];
218 selectedObjects[0] = getText();
219 return selectedObjects;
220 }
221
222 /**
223 * See readObject() and writeObject() in JComponent for more
224 * information about serialization in Swing.
225 */
226 private void writeObject(ObjectOutputStream s) throws IOException {
227 s.defaultWriteObject();
228 if (getUIClassID().equals(uiClassID)) {
229 byte count = JComponent.getWriteObjCounter(this );
230 JComponent.setWriteObjCounter(this , --count);
231 if (count == 0 && ui != null) {
232 ui.installUI(this );
233 }
234 }
235 }
236
237 /**
238 * Returns a string representation of this JCheckBoxMenuItem. This method
239 * is intended to be used only for debugging purposes, and the
240 * content and format of the returned string may vary between
241 * implementations. The returned string may be empty but may not
242 * be <code>null</code>.
243 *
244 * @return a string representation of this JCheckBoxMenuItem.
245 */
246 protected String paramString() {
247 return super .paramString();
248 }
249
250 /**
251 * Overriden to return true, JCheckBoxMenuItem supports
252 * the selected state.
253 */
254 boolean shouldUpdateSelectedStateFromAction() {
255 return true;
256 }
257
258 /////////////////
259 // Accessibility support
260 ////////////////
261
262 /**
263 * Gets the AccessibleContext associated with this JCheckBoxMenuItem.
264 * For JCheckBoxMenuItems, the AccessibleContext takes the form of an
265 * AccessibleJCheckBoxMenuItem.
266 * A new AccessibleJCheckBoxMenuItem instance is created if necessary.
267 *
268 * @return an AccessibleJCheckBoxMenuItem that serves as the
269 * AccessibleContext of this AccessibleJCheckBoxMenuItem
270 */
271 public AccessibleContext getAccessibleContext() {
272 if (accessibleContext == null) {
273 accessibleContext = new AccessibleJCheckBoxMenuItem();
274 }
275 return accessibleContext;
276 }
277
278 /**
279 * This class implements accessibility support for the
280 * <code>JCheckBoxMenuItem</code> class. It provides an implementation
281 * of the Java Accessibility API appropriate to checkbox menu item
282 * user-interface elements.
283 * <p>
284 * <strong>Warning:</strong>
285 * Serialized objects of this class will not be compatible with
286 * future Swing releases. The current serialization support is
287 * appropriate for short term storage or RMI between applications running
288 * the same version of Swing. As of 1.4, support for long term storage
289 * of all JavaBeans<sup><font size="-2">TM</font></sup>
290 * has been added to the <code>java.beans</code> package.
291 * Please see {@link java.beans.XMLEncoder}.
292 */
293 protected class AccessibleJCheckBoxMenuItem extends
294 AccessibleJMenuItem {
295 /**
296 * Get the role of this object.
297 *
298 * @return an instance of AccessibleRole describing the role of the
299 * object
300 */
301 public AccessibleRole getAccessibleRole() {
302 return AccessibleRole.CHECK_BOX;
303 }
304 } // inner class AccessibleJCheckBoxMenuItem
305 }
|