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 * An implementation of a radio button menu item.
042 * A <code>JRadioButtonMenuItem</code> is
043 * a menu item that is part of a group of menu items in which only one
044 * item in the group can be selected. The selected item displays its
045 * selected state. Selecting it causes any other selected item to
046 * switch to the unselected state.
047 * To control the selected state of a group of radio button menu items,
048 * use a <code>ButtonGroup</code> object.
049 * <p>
050 * Menu items can be configured, and to some degree controlled, by
051 * <code><a href="Action.html">Action</a></code>s. Using an
052 * <code>Action</code> with a menu item has many benefits beyond directly
053 * configuring a menu item. Refer to <a href="Action.html#buttonActions">
054 * Swing Components Supporting <code>Action</code></a> for more
055 * details, and you can find more information in <a
056 * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
057 * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
058 * <p>
059 * For further documentation and examples see
060 * <a
061 href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
062 * a section in <em>The Java Tutorial.</em>
063 * <p>
064 * <strong>Warning:</strong> Swing is not thread safe. For more
065 * information see <a
066 * href="package-summary.html#threading">Swing's Threading
067 * Policy</a>.
068 * <p>
069 * <strong>Warning:</strong>
070 * Serialized objects of this class will not be compatible with
071 * future Swing releases. The current serialization support is
072 * appropriate for short term storage or RMI between applications running
073 * the same version of Swing. As of 1.4, support for long term storage
074 * of all JavaBeans<sup><font size="-2">TM</font></sup>
075 * has been added to the <code>java.beans</code> package.
076 * Please see {@link java.beans.XMLEncoder}.
077 *
078 * @beaninfo
079 * attribute: isContainer false
080 * description: A component within a group of menu items which can be selected.
081 *
082 * @version 1.58 05/05/07
083 * @author Georges Saab
084 * @author David Karlton
085 * @see ButtonGroup
086 */
087 public class JRadioButtonMenuItem extends JMenuItem implements
088 Accessible {
089 /**
090 * @see #getUIClassID
091 * @see #readObject
092 */
093 private static final String uiClassID = "RadioButtonMenuItemUI";
094
095 /**
096 * Creates a <code>JRadioButtonMenuItem</code> with no set text or icon.
097 */
098 public JRadioButtonMenuItem() {
099 this (null, null, false);
100 }
101
102 /**
103 * Creates a <code>JRadioButtonMenuItem</code> with an icon.
104 *
105 * @param icon the <code>Icon</code> to display on the
106 * <code>JRadioButtonMenuItem</code>
107 */
108 public JRadioButtonMenuItem(Icon icon) {
109 this (null, icon, false);
110 }
111
112 /**
113 * Creates a <code>JRadioButtonMenuItem</code> with text.
114 *
115 * @param text the text of the <code>JRadioButtonMenuItem</code>
116 */
117 public JRadioButtonMenuItem(String text) {
118 this (text, null, false);
119 }
120
121 /**
122 * Creates a radio button menu item whose properties are taken from the
123 * <code>Action</code> supplied.
124 *
125 * @param a the <code>Action</code> on which to base the radio
126 * button menu item
127 *
128 * @since 1.3
129 */
130 public JRadioButtonMenuItem(Action a) {
131 this ();
132 setAction(a);
133 }
134
135 /**
136 * Creates a radio button menu item with the specified text
137 * and <code>Icon</code>.
138 *
139 * @param text the text of the <code>JRadioButtonMenuItem</code>
140 * @param icon the icon to display on the <code>JRadioButtonMenuItem</code>
141 */
142 public JRadioButtonMenuItem(String text, Icon icon) {
143 this (text, icon, false);
144 }
145
146 /**
147 * Creates a radio button menu item with the specified text
148 * and selection state.
149 *
150 * @param text the text of the <code>CheckBoxMenuItem</code>
151 * @param selected the selected state of the <code>CheckBoxMenuItem</code>
152 */
153 public JRadioButtonMenuItem(String text, boolean selected) {
154 this (text);
155 setSelected(selected);
156 }
157
158 /**
159 * Creates a radio button menu item with the specified image
160 * and selection state, but no text.
161 *
162 * @param icon the image that the button should display
163 * @param selected if true, the button is initially selected;
164 * otherwise, the button is initially unselected
165 */
166 public JRadioButtonMenuItem(Icon icon, boolean selected) {
167 this (null, icon, selected);
168 }
169
170 /**
171 * Creates a radio button menu item that has the specified
172 * text, image, and selection state. All other constructors
173 * defer to this one.
174 *
175 * @param text the string displayed on the radio button
176 * @param icon the image that the button should display
177 */
178 public JRadioButtonMenuItem(String text, Icon icon, boolean selected) {
179 super (text, icon);
180 setModel(new JToggleButton.ToggleButtonModel());
181 setSelected(selected);
182 setFocusable(false);
183 }
184
185 /**
186 * Returns the name of the L&F class that renders this component.
187 *
188 * @return the string "RadioButtonMenuItemUI"
189 * @see JComponent#getUIClassID
190 * @see UIDefaults#getUI
191 */
192 public String getUIClassID() {
193 return uiClassID;
194 }
195
196 /**
197 * See <code>readObject</code> and <code>writeObject</code> in
198 * <code>JComponent</code> for more
199 * information about serialization in Swing.
200 */
201 private void writeObject(ObjectOutputStream s) throws IOException {
202 s.defaultWriteObject();
203 if (getUIClassID().equals(uiClassID)) {
204 byte count = JComponent.getWriteObjCounter(this );
205 JComponent.setWriteObjCounter(this , --count);
206 if (count == 0 && ui != null) {
207 ui.installUI(this );
208 }
209 }
210 }
211
212 /**
213 * Returns a string representation of this
214 * <code>JRadioButtonMenuItem</code>. This method
215 * is intended to be used only for debugging purposes, and the
216 * content and format of the returned string may vary between
217 * implementations. The returned string may be empty but may not
218 * be <code>null</code>.
219 *
220 * @return a string representation of this
221 * <code>JRadioButtonMenuItem</code>
222 */
223 protected String paramString() {
224 return super .paramString();
225 }
226
227 /**
228 * Overriden to return true, JRadioButtonMenuItem supports
229 * the selected state.
230 */
231 boolean shouldUpdateSelectedStateFromAction() {
232 return true;
233 }
234
235 /////////////////
236 // Accessibility support
237 ////////////////
238
239 /**
240 * Gets the AccessibleContext associated with this JRadioButtonMenuItem.
241 * For JRadioButtonMenuItems, the AccessibleContext takes the form of an
242 * AccessibleJRadioButtonMenuItem.
243 * A new AccessibleJRadioButtonMenuItem instance is created if necessary.
244 *
245 * @return an AccessibleJRadioButtonMenuItem that serves as the
246 * AccessibleContext of this JRadioButtonMenuItem
247 */
248 public AccessibleContext getAccessibleContext() {
249 if (accessibleContext == null) {
250 accessibleContext = new AccessibleJRadioButtonMenuItem();
251 }
252 return accessibleContext;
253 }
254
255 /**
256 * This class implements accessibility support for the
257 * <code>JRadioButtonMenuItem</code> class. It provides an
258 * implementation of the Java Accessibility API appropriate to
259 * <code>JRadioButtonMenuItem</code> user-interface elements.
260 * <p>
261 * <strong>Warning:</strong>
262 * Serialized objects of this class will not be compatible with
263 * future Swing releases. The current serialization support is
264 * appropriate for short term storage or RMI between applications running
265 * the same version of Swing. As of 1.4, support for long term storage
266 * of all JavaBeans<sup><font size="-2">TM</font></sup>
267 * has been added to the <code>java.beans</code> package.
268 * Please see {@link java.beans.XMLEncoder}.
269 */
270 protected class AccessibleJRadioButtonMenuItem extends
271 AccessibleJMenuItem {
272 /**
273 * Get the role of this object.
274 *
275 * @return an instance of AccessibleRole describing the role of the
276 * object
277 */
278 public AccessibleRole getAccessibleRole() {
279 return AccessibleRole.RADIO_BUTTON;
280 }
281 } // inner class AccessibleJRadioButtonMenuItem
282 }
|