1. /*
  2. * @(#)JCheckBoxMenuItem.java 1.53 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. * 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 check box 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. * preferred methods are <code>isSelected</code> and
  28. * <code>setSelected</code>, which 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 further information and examples of using check box menu items,
  33. * see <a
  34. href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
  35. * a section in <em>The Java Tutorial.</em>
  36. * For the keyboard keys used by this component in the standard Look and
  37. * Feel (L&F) renditions, see the
  38. * <a href="doc-files/Key-Index.html#JCheckBoxMenuItem"><code>JCheckBoxMenuItem</code> key assignments</a>.
  39. * <p>
  40. * <strong>Warning:</strong>
  41. * Serialized objects of this class will not be compatible with
  42. * future Swing releases. The current serialization support is
  43. * appropriate for short term storage or RMI between applications running
  44. * the same version of Swing. As of 1.4, support for long term storage
  45. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  46. * has been added to the <code>java.beans</code> package.
  47. * Please see {@link java.beans.XMLEncoder}.
  48. *
  49. * @beaninfo
  50. * attribute: isContainer false
  51. * description: A menu item which can be selected or deselected.
  52. *
  53. * @version 1.53 01/23/03
  54. * @author Georges Saab
  55. * @author David Karlton
  56. */
  57. public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants,
  58. Accessible
  59. {
  60. /**
  61. * @see #getUIClassID
  62. * @see #readObject
  63. */
  64. private static final String uiClassID = "CheckBoxMenuItemUI";
  65. /**
  66. * Creates an initially unselected check box menu item with no set text or icon.
  67. */
  68. public JCheckBoxMenuItem() {
  69. this(null, null, false);
  70. }
  71. /**
  72. * Creates an initially unselected check box menu item with an icon.
  73. *
  74. * @param icon the icon of the CheckBoxMenuItem.
  75. */
  76. public JCheckBoxMenuItem(Icon icon) {
  77. this(null, icon, false);
  78. }
  79. /**
  80. * Creates an initially unselected check box menu item with text.
  81. *
  82. * @param text the text of the CheckBoxMenuItem
  83. */
  84. public JCheckBoxMenuItem(String text) {
  85. this(text, null, false);
  86. }
  87. /**
  88. * Creates a menu item whose properties are taken from the
  89. * Action supplied.
  90. *
  91. * @since 1.3
  92. */
  93. public JCheckBoxMenuItem(Action a) {
  94. this();
  95. setAction(a);
  96. }
  97. /**
  98. * Creates an initially unselected check box menu item with the specified text and icon.
  99. *
  100. * @param text the text of the CheckBoxMenuItem
  101. * @param icon the icon of the CheckBoxMenuItem
  102. */
  103. public JCheckBoxMenuItem(String text, Icon icon) {
  104. this(text, icon, false);
  105. }
  106. /**
  107. * Creates a check box menu item with the specified text and selection state.
  108. *
  109. * @param text the text of the check box menu item.
  110. * @param b the selected state of the check box menu item
  111. */
  112. public JCheckBoxMenuItem(String text, boolean b) {
  113. this(text, null, b);
  114. }
  115. /**
  116. * Creates a check box menu item with the specified text, icon, and selection state.
  117. *
  118. * @param text the text of the check box menu item
  119. * @param icon the icon of the check box menu item
  120. * @param b the selected state of the check box menu item
  121. */
  122. public JCheckBoxMenuItem(String text, Icon icon, boolean b) {
  123. super(text, icon);
  124. setModel(new JToggleButton.ToggleButtonModel());
  125. setSelected(b);
  126. setFocusable(false);
  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. * See readObject() and writeObject() in JComponent for more
  179. * information about serialization in Swing.
  180. */
  181. private void writeObject(ObjectOutputStream s) throws IOException {
  182. s.defaultWriteObject();
  183. if (getUIClassID().equals(uiClassID)) {
  184. byte count = JComponent.getWriteObjCounter(this);
  185. JComponent.setWriteObjCounter(this, --count);
  186. if (count == 0 && ui != null) {
  187. ui.installUI(this);
  188. }
  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
  230. * appropriate for short term storage or RMI between applications running
  231. * the same version of Swing. As of 1.4, support for long term storage
  232. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  233. * has been added to the <code>java.beans</code> package.
  234. * Please see {@link java.beans.XMLEncoder}.
  235. */
  236. protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem {
  237. /**
  238. * Get the role of this object.
  239. *
  240. * @return an instance of AccessibleRole describing the role of the
  241. * object
  242. */
  243. public AccessibleRole getAccessibleRole() {
  244. return AccessibleRole.CHECK_BOX;
  245. }
  246. } // inner class AccessibleJCheckBoxMenuItem
  247. }