1. /*
  2. * @(#)JRadioButtonMenuItem.java 1.47 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.util.EventListener;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.awt.image.*;
  12. import java.io.ObjectOutputStream;
  13. import java.io.ObjectInputStream;
  14. import java.io.IOException;
  15. import javax.swing.plaf.*;
  16. import javax.accessibility.*;
  17. /**
  18. * An implementation of a radio button menu item.
  19. * A <code>JRadioButtonMenuItem</code> is
  20. * a menu item that is part of a group of menu items in which only one
  21. * item in the group can be selected. The selected item displays its
  22. * selected state. Selecting it causes any other selected item to
  23. * switch to the unselected state.
  24. * To control the selected state of a group of radio button menu items,
  25. * use a <code>ButtonGroup</code> object.
  26. * <p>
  27. * For further documentation and examples see
  28. * <a
  29. href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
  30. * a section in <em>The Java Tutorial.</em>
  31. * For the keyboard keys used by this component in the standard Look and
  32. * Feel (L&F) renditions, see the
  33. * <a href="doc-files/Key-Index.html#JRadioButtonMenuItem"><code>JRadioButtonMenuItem</code> key assignments</a>.
  34. * <p>
  35. * <strong>Warning:</strong>
  36. * Serialized objects of this class will not be compatible with
  37. * future Swing releases. The current serialization support is
  38. * appropriate for short term storage or RMI between applications running
  39. * the same version of Swing. As of 1.4, support for long term storage
  40. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  41. * has been added to the <code>java.beans</code> package.
  42. * Please see {@link java.beans.XMLEncoder}.
  43. *
  44. * @beaninfo
  45. * attribute: isContainer false
  46. * description: A component within a group of menu items which can be selected.
  47. *
  48. * @version 1.47 01/23/03
  49. * @author Georges Saab
  50. * @author David Karlton
  51. * @see ButtonGroup
  52. */
  53. public class JRadioButtonMenuItem extends JMenuItem implements Accessible {
  54. /**
  55. * @see #getUIClassID
  56. * @see #readObject
  57. */
  58. private static final String uiClassID = "RadioButtonMenuItemUI";
  59. /**
  60. * Creates a <code>JRadioButtonMenuItem</code> with no set text or icon.
  61. */
  62. public JRadioButtonMenuItem() {
  63. this(null, null, false);
  64. }
  65. /**
  66. * Creates a <code>JRadioButtonMenuItem</code> with an icon.
  67. *
  68. * @param icon the <code>Icon</code> to display on the
  69. * <code>JRadioButtonMenuItem</code>
  70. */
  71. public JRadioButtonMenuItem(Icon icon) {
  72. this(null, icon, false);
  73. }
  74. /**
  75. * Creates a <code>JRadioButtonMenuItem</code> with text.
  76. *
  77. * @param text the text of the <code>JRadioButtonMenuItem</code>
  78. */
  79. public JRadioButtonMenuItem(String text) {
  80. this(text, null, false);
  81. }
  82. /**
  83. * Creates a radio button menu item whose properties are taken from the
  84. * <code>Action</code> supplied.
  85. *
  86. * @param a the <code>Action</code> on which to base the radio
  87. * button menu item
  88. *
  89. * @since 1.3
  90. */
  91. public JRadioButtonMenuItem(Action a) {
  92. this();
  93. setAction(a);
  94. }
  95. /**
  96. * Creates a radio button menu item with the specified text
  97. * and <code>Icon</code>.
  98. *
  99. * @param text the text of the <code>JRadioButtonMenuItem</code>
  100. * @param icon the icon to display on the <code>JRadioButtonMenuItem</code>
  101. */
  102. public JRadioButtonMenuItem(String text, Icon icon) {
  103. this(text, icon, false);
  104. }
  105. /**
  106. * Creates a radio button menu item with the specified text
  107. * and selection state.
  108. *
  109. * @param text the text of the <code>CheckBoxMenuItem</code>
  110. * @param selected the selected state of the <code>CheckBoxMenuItem</code>
  111. */
  112. public JRadioButtonMenuItem(String text, boolean selected) {
  113. this(text);
  114. setSelected(selected);
  115. }
  116. /**
  117. * Creates a radio button menu item with the specified image
  118. * and selection state, but no text.
  119. *
  120. * @param icon the image that the button should display
  121. * @param selected if true, the button is initially selected;
  122. * otherwise, the button is initially unselected
  123. */
  124. public JRadioButtonMenuItem(Icon icon, boolean selected) {
  125. this(null, icon, selected);
  126. }
  127. /**
  128. * Creates a radio button menu item that has the specified
  129. * text, image, and selection state. All other constructors
  130. * defer to this one.
  131. *
  132. * @param text the string displayed on the radio button
  133. * @param icon the image that the button should display
  134. */
  135. public JRadioButtonMenuItem(String text, Icon icon, boolean selected) {
  136. super(text, icon);
  137. setModel(new JToggleButton.ToggleButtonModel());
  138. setSelected(selected);
  139. setFocusable(false);
  140. }
  141. /**
  142. * Returns the name of the L&F class that renders this component.
  143. *
  144. * @return the string "RadioButtonMenuItemUI"
  145. * @see JComponent#getUIClassID
  146. * @see UIDefaults#getUI
  147. */
  148. public String getUIClassID() {
  149. return uiClassID;
  150. }
  151. /**
  152. * See <code>readObject</code> and <code>writeObject</code> in
  153. * <code>JComponent</code> for more
  154. * information about serialization in Swing.
  155. */
  156. private void writeObject(ObjectOutputStream s) throws IOException {
  157. s.defaultWriteObject();
  158. if (getUIClassID().equals(uiClassID)) {
  159. byte count = JComponent.getWriteObjCounter(this);
  160. JComponent.setWriteObjCounter(this, --count);
  161. if (count == 0 && ui != null) {
  162. ui.installUI(this);
  163. }
  164. }
  165. }
  166. /**
  167. * Returns a string representation of this
  168. * <code>JRadioButtonMenuItem</code>. This method
  169. * is intended to be used only for debugging purposes, and the
  170. * content and format of the returned string may vary between
  171. * implementations. The returned string may be empty but may not
  172. * be <code>null</code>.
  173. *
  174. * @return a string representation of this
  175. * <code>JRadioButtonMenuItem</code>
  176. */
  177. protected String paramString() {
  178. return super.paramString();
  179. }
  180. /////////////////
  181. // Accessibility support
  182. ////////////////
  183. /**
  184. * Gets the AccessibleContext associated with this JRadioButtonMenuItem.
  185. * For JRadioButtonMenuItems, the AccessibleContext takes the form of an
  186. * AccessibleJRadioButtonMenuItem.
  187. * A new AccessibleJRadioButtonMenuItem instance is created if necessary.
  188. *
  189. * @return an AccessibleJRadioButtonMenuItem that serves as the
  190. * AccessibleContext of this JRadioButtonMenuItem
  191. */
  192. public AccessibleContext getAccessibleContext() {
  193. if (accessibleContext == null) {
  194. accessibleContext = new AccessibleJRadioButtonMenuItem();
  195. }
  196. return accessibleContext;
  197. }
  198. /**
  199. * This class implements accessibility support for the
  200. * <code>JRadioButtonMenuItem</code> class. It provides an
  201. * implementation of the Java Accessibility API appropriate to
  202. * <code>JRadioButtonMenuItem</code> user-interface elements.
  203. * <p>
  204. * <strong>Warning:</strong>
  205. * Serialized objects of this class will not be compatible with
  206. * future Swing releases. The current serialization support is
  207. * appropriate for short term storage or RMI between applications running
  208. * the same version of Swing. As of 1.4, support for long term storage
  209. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  210. * has been added to the <code>java.beans</code> package.
  211. * Please see {@link java.beans.XMLEncoder}.
  212. */
  213. protected class AccessibleJRadioButtonMenuItem extends AccessibleJMenuItem {
  214. /**
  215. * Get the role of this object.
  216. *
  217. * @return an instance of AccessibleRole describing the role of the
  218. * object
  219. */
  220. public AccessibleRole getAccessibleRole() {
  221. return AccessibleRole.RADIO_BUTTON;
  222. }
  223. } // inner class AccessibleJRadioButtonMenuItem
  224. }