1. /*
  2. * @(#)JCheckBoxMenuItem.java 1.41 01/11/29
  3. *
  4. * Copyright 2002 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. * A menu item that can be selected or deselected. If selected, the menu
  19. * item typically appears with a checkmark next to it. If unselected or
  20. * deselected, the menu item appears without a checkmark. Like a regular
  21. * menu item, a checkbox menu item can have either text or a graphic
  22. * icon associated with it, or both.
  23. * <p>
  24. * Either <code>isSelected</code>/<code>setSelected</code> or
  25. * <code>getState</code>/<code>setState</code> can be used
  26. * to determine/specify the menu item's selection state. (The
  27. * Swing-standard methods are <code>isSelected</code> and
  28. * <code>setSelected</code>. These methods work for all menus and buttons.
  29. * The <code>getState</code> and <code>setState</code> methods exist for
  30. * compatibility with other component sets.)
  31. * <p>
  32. * For the keyboard keys used by this component in the standard Look and
  33. * Feel (L&F) renditions, see the
  34. * <a href="doc-files/Key-Index.html#JCheckBoxMenuItem">JCheckBoxMenuItem</a> key assignments.
  35. * <p>
  36. * <strong>Warning:</strong>
  37. * Serialized objects of this class will not be compatible with
  38. * future Swing releases. The current serialization support is appropriate
  39. * for short term storage or RMI between applications running the same
  40. * version of Swing. A future release of Swing will provide support for
  41. * long term persistence.
  42. *
  43. * @beaninfo
  44. * attribute: isContainer false
  45. *
  46. * @version 1.41 11/29/01
  47. * @author Georges Saab
  48. * @author David Karlton
  49. */
  50. public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants,
  51. Accessible
  52. {
  53. /**
  54. * @see #getUIClassID
  55. * @see #readObject
  56. */
  57. private static final String uiClassID = "CheckBoxMenuItemUI";
  58. /**
  59. * Creates an initially unselected checkboxMenuItem with no set text or icon.
  60. */
  61. public JCheckBoxMenuItem() {
  62. this(null, null, false);
  63. }
  64. /**
  65. * Creates an initially unselected checkboxMenuItem with an icon.
  66. *
  67. * @param icon the icon of the CheckBoxMenuItem.
  68. */
  69. public JCheckBoxMenuItem(Icon icon) {
  70. this(null, icon, false);
  71. }
  72. /**
  73. * Creates an initially unselected checkboxMenuItem with text.
  74. *
  75. * @param text the text of the CheckBoxMenuItem
  76. */
  77. public JCheckBoxMenuItem(String text) {
  78. this(text, null, false);
  79. }
  80. /**
  81. * Creates an initially unselected checkboxMenuItem with the specified text and icon.
  82. *
  83. * @param text the text of the CheckBoxMenuItem
  84. * @param icon the icon of the CheckBoxMenuItem
  85. */
  86. public JCheckBoxMenuItem(String text, Icon icon) {
  87. this(text, icon, false);
  88. }
  89. /**
  90. * Creates a checkboxMenuItem with the specified text and selection state.
  91. *
  92. * @param text the text of the CheckBoxMenuItem.
  93. * @param b the selected state of the checkboxmenuitem
  94. */
  95. public JCheckBoxMenuItem(String text, boolean b) {
  96. this(text, null, b);
  97. }
  98. /**
  99. * Creates a checkboxMenuItem with the specified text, icon, and selection state.
  100. *
  101. * @param text the text of the CheckBoxMenuItem
  102. * @param icon the icon of the CheckBoxMenuItem
  103. * @param b the selected state of the checkboxmenuitem
  104. */
  105. public JCheckBoxMenuItem(String text, Icon icon, boolean b) {
  106. super(text, icon);
  107. setModel(new JToggleButton.ToggleButtonModel());
  108. setSelected(b);
  109. }
  110. /**
  111. * Returns the name of the L&F class
  112. * that renders this component.
  113. *
  114. * @return "CheckBoxMenuItemUI"
  115. * @see JComponent#getUIClassID
  116. * @see UIDefaults#getUI
  117. */
  118. public String getUIClassID() {
  119. return uiClassID;
  120. }
  121. /**
  122. * Returns the selected-state of the item. This method
  123. * exists for AWT compatibility only. New code should
  124. * use isSelected() instead.
  125. *
  126. * @return true if the item is selected
  127. */
  128. public boolean getState() {
  129. return isSelected();
  130. }
  131. /**
  132. * Sets the selected-state of the item. This method
  133. * exists for AWT compatibility only. New code should
  134. * use setSelected() instead.
  135. *
  136. * @param b a boolean value indicating the item's
  137. * selected-state, where true=selected
  138. * @beaninfo
  139. * description: The selection state of the Checkbox menu item
  140. * hidden: true
  141. */
  142. public synchronized void setState(boolean b) {
  143. setSelected(b);
  144. }
  145. /**
  146. * Returns an array (length 1) containing the checkbox menu item
  147. * label or null if the checkbox is not selected.
  148. *
  149. * @return an array containing 1 Object -- the text of the menu item
  150. * -- if the item is selected, otherwise null
  151. */
  152. public synchronized Object[] getSelectedObjects() {
  153. if (isSelected() == false)
  154. return null;
  155. Object[] selectedObjects = new Object[1];
  156. selectedObjects[0] = getText();
  157. return selectedObjects;
  158. }
  159. /**
  160. * Override <code>JComponent.requestFocus()</code> to prevent grabbing the focus.
  161. */
  162. public void requestFocus() {}
  163. /**
  164. * See readObject() and writeObject() in JComponent for more
  165. * information about serialization in Swing.
  166. */
  167. private void writeObject(ObjectOutputStream s) throws IOException {
  168. s.defaultWriteObject();
  169. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  170. ui.installUI(this);
  171. }
  172. }
  173. /**
  174. * Returns a string representation of this JCheckBoxMenuItem. This method
  175. * is intended to be used only for debugging purposes, and the
  176. * content and format of the returned string may vary between
  177. * implementations. The returned string may be empty but may not
  178. * be <code>null</code>.
  179. *
  180. * @return a string representation of this JCheckBoxMenuItem.
  181. */
  182. protected String paramString() {
  183. return super.paramString();
  184. }
  185. /////////////////
  186. // Accessibility support
  187. ////////////////
  188. /**
  189. * Get the AccessibleContext associated with this JComponent
  190. *
  191. * @return the AccessibleContext of this JComponent
  192. */
  193. public AccessibleContext getAccessibleContext() {
  194. if (accessibleContext == null) {
  195. accessibleContext = new AccessibleJCheckBoxMenuItem();
  196. }
  197. return accessibleContext;
  198. }
  199. /**
  200. * The class used to obtain the accessible role for this object.
  201. * <p>
  202. * <strong>Warning:</strong>
  203. * Serialized objects of this class will not be compatible with
  204. * future Swing releases. The current serialization support is appropriate
  205. * for short term storage or RMI between applications running the same
  206. * version of Swing. A future release of Swing will provide support for
  207. * long term persistence.
  208. */
  209. protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem {
  210. /**
  211. * Get the role of this object.
  212. *
  213. * @return an instance of AccessibleRole describing the role of the
  214. * object
  215. */
  216. public AccessibleRole getAccessibleRole() {
  217. return AccessibleRole.CHECK_BOX;
  218. }
  219. } // inner class AccessibleJCheckBoxMenuItem
  220. }