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