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