1. /*
  2. * @(#)JCheckBoxMenuItem.java 1.48 00/04/06
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing;
  11. import java.util.EventListener;
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import java.awt.image.*;
  15. import java.io.ObjectOutputStream;
  16. import java.io.ObjectInputStream;
  17. import java.io.IOException;
  18. import javax.swing.plaf.*;
  19. import javax.accessibility.*;
  20. /**
  21. * A menu item that can be selected or deselected. If selected, the menu
  22. * item typically appears with a checkmark next to it. If unselected or
  23. * deselected, the menu item appears without a checkmark. Like a regular
  24. * menu item, a check box menu item can have either text or a graphic
  25. * icon associated with it, or both.
  26. * <p>
  27. * Either <code>isSelected</code>/<code>setSelected</code> or
  28. * <code>getState</code>/<code>setState</code> can be used
  29. * to determine/specify the menu item's selection state. The
  30. * preferred methods are <code>isSelected</code> and
  31. * <code>setSelected</code>, which work for all menus and buttons.
  32. * The <code>getState</code> and <code>setState</code> methods exist for
  33. * compatibility with other component sets.
  34. * <p>
  35. * For further information and examples of using check box menu items,
  36. * see <a
  37. href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
  38. * a section in <em>The Java Tutorial.</em>
  39. * For the keyboard keys used by this component in the standard Look and
  40. * Feel (L&F) renditions, see the
  41. * <a href="doc-files/Key-Index.html#JCheckBoxMenuItem">JCheckBoxMenuItem</a> key assignments.
  42. * <p>
  43. * <strong>Warning:</strong>
  44. * Serialized objects of this class will not be compatible with
  45. * future Swing releases. The current serialization support is appropriate
  46. * for short term storage or RMI between applications running the same
  47. * version of Swing. A future release of Swing will provide support for
  48. * long term persistence.
  49. *
  50. * @beaninfo
  51. * attribute: isContainer false
  52. * description: A menu item which can be selected or deselected.
  53. *
  54. * @version 1.48 04/06/00
  55. * @author Georges Saab
  56. * @author David Karlton
  57. */
  58. public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants,
  59. Accessible
  60. {
  61. /**
  62. * @see #getUIClassID
  63. * @see #readObject
  64. */
  65. private static final String uiClassID = "CheckBoxMenuItemUI";
  66. /**
  67. * Creates an initially unselected check box menu item with no set text or icon.
  68. */
  69. public JCheckBoxMenuItem() {
  70. this(null, null, false);
  71. }
  72. /**
  73. * Creates an initially unselected check box menu item with an icon.
  74. *
  75. * @param icon the icon of the CheckBoxMenuItem.
  76. */
  77. public JCheckBoxMenuItem(Icon icon) {
  78. this(null, icon, false);
  79. }
  80. /**
  81. * Creates an initially unselected check box menu item with text.
  82. *
  83. * @param text the text of the CheckBoxMenuItem
  84. */
  85. public JCheckBoxMenuItem(String text) {
  86. this(text, null, false);
  87. }
  88. /**
  89. * Creates a menu item whose properties are taken from the
  90. * Action supplied.
  91. *
  92. * @since 1.3
  93. */
  94. public JCheckBoxMenuItem(Action a) {
  95. this();
  96. setAction(a);
  97. }
  98. /**
  99. * Creates an initially unselected check box menu item with the specified text and icon.
  100. *
  101. * @param text the text of the CheckBoxMenuItem
  102. * @param icon the icon of the CheckBoxMenuItem
  103. */
  104. public JCheckBoxMenuItem(String text, Icon icon) {
  105. this(text, icon, false);
  106. }
  107. /**
  108. * Creates a check box menu item with the specified text and selection state.
  109. *
  110. * @param text the text of the check box menu item.
  111. * @param b the selected state of the check box menu item
  112. */
  113. public JCheckBoxMenuItem(String text, boolean b) {
  114. this(text, null, b);
  115. }
  116. /**
  117. * Creates a check box menu item with the specified text, icon, and selection state.
  118. *
  119. * @param text the text of the check box menu item
  120. * @param icon the icon of the check box menu item
  121. * @param b the selected state of the check box menu item
  122. */
  123. public JCheckBoxMenuItem(String text, Icon icon, boolean b) {
  124. super(text, icon);
  125. setModel(new JToggleButton.ToggleButtonModel());
  126. setSelected(b);
  127. }
  128. /**
  129. * Returns the name of the L&F class
  130. * that renders this component.
  131. *
  132. * @return "CheckBoxMenuItemUI"
  133. * @see JComponent#getUIClassID
  134. * @see UIDefaults#getUI
  135. */
  136. public String getUIClassID() {
  137. return uiClassID;
  138. }
  139. /**
  140. * Returns the selected-state of the item. This method
  141. * exists for AWT compatibility only. New code should
  142. * use isSelected() instead.
  143. *
  144. * @return true if the item is selected
  145. */
  146. public boolean getState() {
  147. return isSelected();
  148. }
  149. /**
  150. * Sets the selected-state of the item. This method
  151. * exists for AWT compatibility only. New code should
  152. * use setSelected() instead.
  153. *
  154. * @param b a boolean value indicating the item's
  155. * selected-state, where true=selected
  156. * @beaninfo
  157. * description: The selection state of the check box menu item
  158. * hidden: true
  159. */
  160. public synchronized void setState(boolean b) {
  161. setSelected(b);
  162. }
  163. /**
  164. * Returns an array (length 1) containing the check box menu item
  165. * label or null if the check box is not selected.
  166. *
  167. * @return an array containing one Object -- the text of the menu item
  168. * -- if the item is selected; otherwise null
  169. */
  170. public Object[] getSelectedObjects() {
  171. if (isSelected() == false)
  172. return null;
  173. Object[] selectedObjects = new Object[1];
  174. selectedObjects[0] = getText();
  175. return selectedObjects;
  176. }
  177. /**
  178. * Override <code>JComponent.requestFocus()</code> to prevent grabbing the focus.
  179. */
  180. public void requestFocus() {}
  181. /**
  182. * See readObject() and writeObject() in JComponent for more
  183. * information about serialization in Swing.
  184. */
  185. private void writeObject(ObjectOutputStream s) throws IOException {
  186. s.defaultWriteObject();
  187. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  188. ui.installUI(this);
  189. }
  190. }
  191. /**
  192. * Returns a string representation of this JCheckBoxMenuItem. This method
  193. * is intended to be used only for debugging purposes, and the
  194. * content and format of the returned string may vary between
  195. * implementations. The returned string may be empty but may not
  196. * be <code>null</code>.
  197. *
  198. * @return a string representation of this JCheckBoxMenuItem.
  199. */
  200. protected String paramString() {
  201. return super.paramString();
  202. }
  203. /////////////////
  204. // Accessibility support
  205. ////////////////
  206. /**
  207. * Gets the AccessibleContext associated with this JCheckBoxMenuItem.
  208. * For JCheckBoxMenuItems, the AccessibleContext takes the form of an
  209. * AccessibleJCheckBoxMenuItem.
  210. * A new AccessibleJCheckBoxMenuItem instance is created if necessary.
  211. *
  212. * @return an AccessibleJCheckBoxMenuItem that serves as the
  213. * AccessibleContext of this AccessibleJCheckBoxMenuItem
  214. */
  215. public AccessibleContext getAccessibleContext() {
  216. if (accessibleContext == null) {
  217. accessibleContext = new AccessibleJCheckBoxMenuItem();
  218. }
  219. return accessibleContext;
  220. }
  221. /**
  222. * This class implements accessibility support for the
  223. * <code>JCheckBoxMenuItem</code> class. It provides an implementation
  224. * of the Java Accessibility API appropriate to checkbox menu item
  225. * user-interface elements.
  226. * <p>
  227. * <strong>Warning:</strong>
  228. * Serialized objects of this class will not be compatible with
  229. * future Swing releases. The current serialization support is appropriate
  230. * for short term storage or RMI between applications running the same
  231. * version of Swing. A future release of Swing will provide support for
  232. * long term persistence.
  233. */
  234. protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem {
  235. /**
  236. * Get the role of this object.
  237. *
  238. * @return an instance of AccessibleRole describing the role of the
  239. * object
  240. */
  241. public AccessibleRole getAccessibleRole() {
  242. return AccessibleRole.CHECK_BOX;
  243. }
  244. } // inner class AccessibleJCheckBoxMenuItem
  245. }