001 /* JRadioButtonMenuItem.java --
002 Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package javax.swing;
040
041 import javax.accessibility.Accessible;
042 import javax.accessibility.AccessibleContext;
043 import javax.accessibility.AccessibleRole;
044
045 /**
046 * This class represents JRadioButtonMenuItem. Its behaviour is very similar
047 * to JRadioButton. Just like JRadioButton, user can check and uncheck this
048 * menu item by clicking on it. JRadioButtonMenuItem uses ToggleButtonModel
049 * to keep track of its selection. If the JRadioButtonMenuItem is included in
050 * the button group, then only one JRadioButtonMenuItem can be selected at
051 * one time.
052 */
053 public class JRadioButtonMenuItem extends JMenuItem implements Accessible
054 {
055 private static final long serialVersionUID = 8482658191548521743L;
056
057 /** name for the UI delegate for this radio button menu item. */
058 private static final String uiClassID = "RadioButtonMenuItemUI";
059
060 /**
061 * Creates a new JRadioButtonMenuItem object.
062 */
063 public JRadioButtonMenuItem()
064 {
065 this(null, null);
066 }
067
068 /**
069 * Creates a new JRadioButtonMenuItem with specified icon
070 *
071 * @param icon Icon to be used for this menu item
072 */
073 public JRadioButtonMenuItem(Icon icon)
074 {
075 this(null, icon);
076 }
077
078 /**
079 * Creates a new JRadioButtonMenuItem with specified label
080 *
081 * @param text Label for this menu item
082 */
083 public JRadioButtonMenuItem(String text)
084 {
085 this(text, null);
086 }
087
088 /**
089 * Creates a new JRadioButtonMenuItem using specified action
090 *
091 * @param action Action for this menu item
092 */
093 public JRadioButtonMenuItem(Action action)
094 {
095 this();
096 setAction(action);
097 }
098
099 /**
100 * Creates a new JRadioButtonMenuItem with specified label and icon
101 *
102 * @param text Label for this menu item
103 * @param icon Icon for this menu item
104 */
105 public JRadioButtonMenuItem(String text, Icon icon)
106 {
107 this(text, icon, false);
108 }
109
110 /**
111 * Creates a new JRadioButtonMenuItem with specified label
112 * and marked selected if 'selected' is true.
113 *
114 * @param text Text for this menu item
115 * @param selected Selected state of this menu item
116 */
117 public JRadioButtonMenuItem(String text, boolean selected)
118 {
119 this(text, null, selected);
120 }
121
122 /**
123 * Creates a new JRadioButtonMenuItem with specified icon
124 * and given selected state
125 *
126 * @param icon Icon for this menu item
127 * @param selected Selected state for this menu item
128 */
129 public JRadioButtonMenuItem(Icon icon, boolean selected)
130 {
131 this(null, icon, selected);
132 }
133
134 /**
135 * Creates a new JRadioButtonMenuItem with specified label,
136 * icon and selected state.
137 *
138 * @param text Label for this menu item
139 * @param icon Icon to be use for this menu item
140 * @param selected selected state of this menu item
141 */
142 public JRadioButtonMenuItem(String text, Icon icon, boolean selected)
143 {
144 super(text, icon);
145 setModel(new JToggleButton.ToggleButtonModel());
146 model.setSelected(selected);
147 setFocusable(false);
148 }
149
150 /**
151 * This method returns a name to identify which look and feel class will be
152 * the UI delegate for the menuItem.
153 *
154 * @return The Look and Feel classID. "JRadioButtonMenuItemUI"
155 */
156 public String getUIClassID()
157 {
158 return uiClassID;
159 }
160
161 /**
162 * This method overrides JComponent.requestFocus with an empty
163 * implementation, since JRadioButtonMenuItems should not
164 * receve focus in general.
165 */
166 public void requestFocus()
167 {
168 // Should do nothing here
169 }
170
171 /**
172 * Returns a string describing the attributes for the
173 * <code>JRadioButtonMenuItem</code> component, for use in debugging. The
174 * return value is guaranteed to be non-<code>null</code>, but the format of
175 * the string may vary between implementations.
176 *
177 * @return A string describing the attributes of the
178 * <code>JRadioButtonMenuItem</code>.
179 */
180 protected String paramString()
181 {
182 // calling super seems to be sufficient here...
183 return super.paramString();
184 }
185
186 /**
187 * Returns the object that provides accessibility features for this
188 * <code>JRadioButtonMenuItem</code> component.
189 *
190 * @return The accessible context (an instance of
191 * {@link AccessibleJRadioButtonMenuItem}).
192 */
193 public AccessibleContext getAccessibleContext()
194 {
195 if (accessibleContext == null)
196 accessibleContext = new AccessibleJRadioButtonMenuItem();
197
198 return accessibleContext;
199 }
200
201 /**
202 * Provides the accessibility features for the
203 * <code>JRadioButtonMenuItem</code> component.
204 *
205 * @see JRadioButtonMenuItem#getAccessibleContext()
206 */
207 protected class AccessibleJRadioButtonMenuItem extends AccessibleJMenuItem
208 {
209 private static final long serialVersionUID = 4381471510145292179L;
210
211 /**
212 * Creates a new <code>AccessibleJRadioButtonMenuItem</code> instance.
213 */
214 protected AccessibleJRadioButtonMenuItem()
215 {
216 // Nothing to do here.
217 }
218
219 /**
220 * Returns the accessible role for the <code>JRadioButtonMenuItem</code>
221 * component.
222 *
223 * @return {@link AccessibleRole#RADIO_BUTTON}.
224 */
225 public AccessibleRole getAccessibleRole()
226 {
227 return AccessibleRole.RADIO_BUTTON;
228 }
229 }
230 }