blob: c1075f26d1cc408372f6009d85419dd886f681ca [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2006 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package javax.swing;
26
27import java.util.EventListener;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.awt.image.*;
32
33import java.io.ObjectOutputStream;
34import java.io.ObjectInputStream;
35import java.io.IOException;
36
37import javax.swing.plaf.*;
38import javax.accessibility.*;
39
40/**
41 * An implementation of a radio button menu item.
42 * A <code>JRadioButtonMenuItem</code> is
43 * a menu item that is part of a group of menu items in which only one
44 * item in the group can be selected. The selected item displays its
45 * selected state. Selecting it causes any other selected item to
46 * switch to the unselected state.
47 * To control the selected state of a group of radio button menu items,
48 * use a <code>ButtonGroup</code> object.
49 * <p>
50 * Menu items can be configured, and to some degree controlled, by
51 * <code><a href="Action.html">Action</a></code>s. Using an
52 * <code>Action</code> with a menu item has many benefits beyond directly
53 * configuring a menu item. Refer to <a href="Action.html#buttonActions">
54 * Swing Components Supporting <code>Action</code></a> for more
55 * details, and you can find more information in <a
56 * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
57 * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
58 * <p>
59 * For further documentation and examples see
60 * <a
61 href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
62 * a section in <em>The Java Tutorial.</em>
63 * <p>
64 * <strong>Warning:</strong> Swing is not thread safe. For more
65 * information see <a
66 * href="package-summary.html#threading">Swing's Threading
67 * Policy</a>.
68 * <p>
69 * <strong>Warning:</strong>
70 * Serialized objects of this class will not be compatible with
71 * future Swing releases. The current serialization support is
72 * appropriate for short term storage or RMI between applications running
73 * the same version of Swing. As of 1.4, support for long term storage
74 * of all JavaBeans<sup><font size="-2">TM</font></sup>
75 * has been added to the <code>java.beans</code> package.
76 * Please see {@link java.beans.XMLEncoder}.
77 *
78 * @beaninfo
79 * attribute: isContainer false
80 * description: A component within a group of menu items which can be selected.
81 *
82 * @author Georges Saab
83 * @author David Karlton
84 * @see ButtonGroup
85 */
86public class JRadioButtonMenuItem extends JMenuItem implements Accessible {
87 /**
88 * @see #getUIClassID
89 * @see #readObject
90 */
91 private static final String uiClassID = "RadioButtonMenuItemUI";
92
93 /**
94 * Creates a <code>JRadioButtonMenuItem</code> with no set text or icon.
95 */
96 public JRadioButtonMenuItem() {
97 this(null, null, false);
98 }
99
100 /**
101 * Creates a <code>JRadioButtonMenuItem</code> with an icon.
102 *
103 * @param icon the <code>Icon</code> to display on the
104 * <code>JRadioButtonMenuItem</code>
105 */
106 public JRadioButtonMenuItem(Icon icon) {
107 this(null, icon, false);
108 }
109
110 /**
111 * Creates a <code>JRadioButtonMenuItem</code> with text.
112 *
113 * @param text the text of the <code>JRadioButtonMenuItem</code>
114 */
115 public JRadioButtonMenuItem(String text) {
116 this(text, null, false);
117 }
118
119 /**
120 * Creates a radio button menu item whose properties are taken from the
121 * <code>Action</code> supplied.
122 *
123 * @param a the <code>Action</code> on which to base the radio
124 * button menu item
125 *
126 * @since 1.3
127 */
128 public JRadioButtonMenuItem(Action a) {
129 this();
130 setAction(a);
131 }
132
133 /**
134 * Creates a radio button menu item with the specified text
135 * and <code>Icon</code>.
136 *
137 * @param text the text of the <code>JRadioButtonMenuItem</code>
138 * @param icon the icon to display on the <code>JRadioButtonMenuItem</code>
139 */
140 public JRadioButtonMenuItem(String text, Icon icon) {
141 this(text, icon, false);
142 }
143
144 /**
145 * Creates a radio button menu item with the specified text
146 * and selection state.
147 *
148 * @param text the text of the <code>CheckBoxMenuItem</code>
149 * @param selected the selected state of the <code>CheckBoxMenuItem</code>
150 */
151 public JRadioButtonMenuItem(String text, boolean selected) {
152 this(text);
153 setSelected(selected);
154 }
155
156 /**
157 * Creates a radio button menu item with the specified image
158 * and selection state, but no text.
159 *
160 * @param icon the image that the button should display
161 * @param selected if true, the button is initially selected;
162 * otherwise, the button is initially unselected
163 */
164 public JRadioButtonMenuItem(Icon icon, boolean selected) {
165 this(null, icon, selected);
166 }
167
168 /**
169 * Creates a radio button menu item that has the specified
170 * text, image, and selection state. All other constructors
171 * defer to this one.
172 *
173 * @param text the string displayed on the radio button
174 * @param icon the image that the button should display
175 */
176 public JRadioButtonMenuItem(String text, Icon icon, boolean selected) {
177 super(text, icon);
178 setModel(new JToggleButton.ToggleButtonModel());
179 setSelected(selected);
180 setFocusable(false);
181 }
182
183 /**
184 * Returns the name of the L&F class that renders this component.
185 *
186 * @return the string "RadioButtonMenuItemUI"
187 * @see JComponent#getUIClassID
188 * @see UIDefaults#getUI
189 */
190 public String getUIClassID() {
191 return uiClassID;
192 }
193
194 /**
195 * See <code>readObject</code> and <code>writeObject</code> in
196 * <code>JComponent</code> for more
197 * information about serialization in Swing.
198 */
199 private void writeObject(ObjectOutputStream s) throws IOException {
200 s.defaultWriteObject();
201 if (getUIClassID().equals(uiClassID)) {
202 byte count = JComponent.getWriteObjCounter(this);
203 JComponent.setWriteObjCounter(this, --count);
204 if (count == 0 && ui != null) {
205 ui.installUI(this);
206 }
207 }
208 }
209
210
211 /**
212 * Returns a string representation of this
213 * <code>JRadioButtonMenuItem</code>. This method
214 * is intended to be used only for debugging purposes, and the
215 * content and format of the returned string may vary between
216 * implementations. The returned string may be empty but may not
217 * be <code>null</code>.
218 *
219 * @return a string representation of this
220 * <code>JRadioButtonMenuItem</code>
221 */
222 protected String paramString() {
223 return super.paramString();
224 }
225
226 /**
227 * Overriden to return true, JRadioButtonMenuItem supports
228 * the selected state.
229 */
230 boolean shouldUpdateSelectedStateFromAction() {
231 return true;
232 }
233
234/////////////////
235// Accessibility support
236////////////////
237
238 /**
239 * Gets the AccessibleContext associated with this JRadioButtonMenuItem.
240 * For JRadioButtonMenuItems, the AccessibleContext takes the form of an
241 * AccessibleJRadioButtonMenuItem.
242 * A new AccessibleJRadioButtonMenuItem instance is created if necessary.
243 *
244 * @return an AccessibleJRadioButtonMenuItem that serves as the
245 * AccessibleContext of this JRadioButtonMenuItem
246 */
247 public AccessibleContext getAccessibleContext() {
248 if (accessibleContext == null) {
249 accessibleContext = new AccessibleJRadioButtonMenuItem();
250 }
251 return accessibleContext;
252 }
253
254 /**
255 * This class implements accessibility support for the
256 * <code>JRadioButtonMenuItem</code> class. It provides an
257 * implementation of the Java Accessibility API appropriate to
258 * <code>JRadioButtonMenuItem</code> user-interface elements.
259 * <p>
260 * <strong>Warning:</strong>
261 * Serialized objects of this class will not be compatible with
262 * future Swing releases. The current serialization support is
263 * appropriate for short term storage or RMI between applications running
264 * the same version of Swing. As of 1.4, support for long term storage
265 * of all JavaBeans<sup><font size="-2">TM</font></sup>
266 * has been added to the <code>java.beans</code> package.
267 * Please see {@link java.beans.XMLEncoder}.
268 */
269 protected class AccessibleJRadioButtonMenuItem extends AccessibleJMenuItem {
270 /**
271 * Get the role of this object.
272 *
273 * @return an instance of AccessibleRole describing the role of the
274 * object
275 */
276 public AccessibleRole getAccessibleRole() {
277 return AccessibleRole.RADIO_BUTTON;
278 }
279 } // inner class AccessibleJRadioButtonMenuItem
280}