1. /*
  2. * @(#)CheckboxMenuItem.java 1.40 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 java.awt;
  8. import java.awt.peer.CheckboxMenuItemPeer;
  9. import java.awt.event.*;
  10. import java.io.ObjectOutputStream;
  11. import java.io.ObjectInputStream;
  12. import java.io.IOException;
  13. /**
  14. * This class represents a check box that can be included in a menu.
  15. * Clicking on the check box in the menu changes its state from
  16. * "on" to "off" or from "off" to "on."
  17. * <p>
  18. * The following picture depicts a menu which contains an instance
  19. * of <code>CheckBoxMenuItem</code>:
  20. * <p>
  21. * <img src="doc-files/MenuBar-1.gif"
  22. * ALIGN=center HSPACE=10 VSPACE=7>
  23. * <p>
  24. * The item labeled <code>Check</code> shows a check box menu item
  25. * in its "off" state.
  26. * <p>
  27. * When a check box menu item is selected, AWT sends an item event to
  28. * the item. Since the event is an instance of <code>ItemEvent</code>,
  29. * the <code>processEvent</code> method examines the event and passes
  30. * it along to <code>processItemEvent</code>. The latter method redirects
  31. * the event to any <code>ItemListener</code> objects that have
  32. * registered an interest in item events generated by this menu item.
  33. *
  34. * @version 1.40, 11/29/01
  35. * @author Sami Shaio
  36. * @see java.awt.event.ItemEvent
  37. * @see java.awt.event.ItemListener
  38. * @since JDK1.0
  39. */
  40. public class CheckboxMenuItem extends MenuItem implements ItemSelectable {
  41. static {
  42. /* ensure that the necessary native libraries are loaded */
  43. Toolkit.loadLibraries();
  44. initIDs();
  45. }
  46. /*
  47. * The state of a checkbox menu item
  48. * @serial
  49. * @see getState()
  50. * @see setState()
  51. */
  52. boolean state = false;
  53. transient ItemListener itemListener;
  54. private static final String base = "chkmenuitem";
  55. private static int nameCounter = 0;
  56. /*
  57. * JDK 1.1 serialVersionUID
  58. */
  59. private static final long serialVersionUID = 6190621106981774043L;
  60. /**
  61. * Create a check box menu item with an empty label.
  62. * The item's state is initially set to "off."
  63. * @since JDK1.1
  64. */
  65. public CheckboxMenuItem() {
  66. this("", false);
  67. }
  68. /**
  69. * Create a check box menu item with the specified label.
  70. * The item's state is initially set to "off."
  71. * @param label a string label for the check box menu item,
  72. * or <code>null</code> for an unlabeled menu item.
  73. */
  74. public CheckboxMenuItem(String label) {
  75. this(label, false);
  76. }
  77. /**
  78. * Create a check box menu item with the specified label and state.
  79. * @param label a string label for the check box menu item,
  80. * or <code>null</code> for an unlabeled menu item.
  81. * @param state the initial state of the menu item, where
  82. * <code>true</code> indicates "on" and
  83. * <code>false</code> indicates "off."
  84. * @since JDK1.1
  85. */
  86. public CheckboxMenuItem(String label, boolean state) {
  87. super(label);
  88. this.state = state;
  89. }
  90. /**
  91. * Construct a name for this MenuComponent. Called by getName() when
  92. * the name is null.
  93. */
  94. String constructComponentName() {
  95. synchronized (getClass()) {
  96. return base + nameCounter++;
  97. }
  98. }
  99. /**
  100. * Creates the peer of the checkbox item. This peer allows us to
  101. * change the look of the checkbox item without changing its
  102. * functionality.
  103. * Most applications do not call this method directly.
  104. * @see java.awt.Toolkit#createCheckboxMenuItem(java.awt.CheckboxMenuItem)
  105. * @see java.awt.Component#getToolkit()
  106. */
  107. public void addNotify() {
  108. synchronized (getTreeLock()) {
  109. if (peer == null)
  110. peer = Toolkit.getDefaultToolkit().createCheckboxMenuItem(this);
  111. super.addNotify();
  112. }
  113. }
  114. /**
  115. * Determines whether the state of this check box menu item
  116. * is "on" or "off."
  117. * @return the state of this check box menu item, where
  118. * <code>true</code> indicates "on" and
  119. * <code>false</code> indicates "off."
  120. * @see java.awt.CheckboxMenuItem#setState
  121. */
  122. public boolean getState() {
  123. return state;
  124. }
  125. /**
  126. * Sets this check box menu item to the specifed state.
  127. * The boolean value <code>true</code> indicates "on" while
  128. * <code>false</code> indicates "off."
  129. * @param b the boolean state of this
  130. * check box menu item.
  131. * @see java.awt.CheckboxMenuItem#getState
  132. */
  133. public synchronized void setState(boolean b) {
  134. state = b;
  135. CheckboxMenuItemPeer peer = (CheckboxMenuItemPeer)this.peer;
  136. if (peer != null) {
  137. peer.setState(b);
  138. }
  139. }
  140. /**
  141. * Returns the an array (length 1) containing the checkbox menu item
  142. * label or null if the checkbox is not selected.
  143. * @see ItemSelectable
  144. */
  145. public synchronized Object[] getSelectedObjects() {
  146. if (state) {
  147. Object[] items = new Object[1];
  148. items[0] = label;
  149. return items;
  150. }
  151. return null;
  152. }
  153. /**
  154. * Adds the specified item listener to receive item events from
  155. * this check box menu item.
  156. * If l is null, no exception is thrown and no action is performed.
  157. *
  158. * @param l the item listener
  159. * @see java.awt.event.ItemEvent
  160. * @see java.awt.event.ItemListener
  161. * @see java.awt.Choice#removeItemListener
  162. * @since JDK1.1
  163. */
  164. public synchronized void addItemListener(ItemListener l) {
  165. if (l == null) {
  166. return;
  167. }
  168. itemListener = AWTEventMulticaster.add(itemListener, l);
  169. newEventsOnly = true;
  170. }
  171. /**
  172. * Removes the specified item listener so that it no longer receives
  173. * item events from this check box menu item.
  174. * If l is null, no exception is thrown and no action is performed.
  175. *
  176. * @param l the item listener
  177. * @see java.awt.event.ItemEvent
  178. * @see java.awt.event.ItemListener
  179. * @see java.awt.Choice#addItemListener
  180. * @since JDK1.1
  181. */
  182. public synchronized void removeItemListener(ItemListener l) {
  183. if (l == null) {
  184. return;
  185. }
  186. itemListener = AWTEventMulticaster.remove(itemListener, l);
  187. }
  188. // REMIND: remove when filtering is done at lower level
  189. boolean eventEnabled(AWTEvent e) {
  190. if (e.id == ItemEvent.ITEM_STATE_CHANGED) {
  191. if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 ||
  192. itemListener != null) {
  193. return true;
  194. }
  195. return false;
  196. }
  197. return super.eventEnabled(e);
  198. }
  199. /**
  200. * Processes events on this check box menu item.
  201. * If the event is an instance of <code>ItemEvent</code>,
  202. * this method invokes the <code>processItemEvent</code> method.
  203. * If the event is not an item event,
  204. * it invokes <code>processEvent</code> on the superclass.
  205. * <p>
  206. * Check box menu items currently support only item events.
  207. * @param e the event
  208. * @see java.awt.event.ItemEvent
  209. * @see java.awt.CheckboxMenuItem#processItemEvent
  210. * @since JDK1.1
  211. */
  212. protected void processEvent(AWTEvent e) {
  213. if (e instanceof ItemEvent) {
  214. processItemEvent((ItemEvent)e);
  215. return;
  216. }
  217. super.processEvent(e);
  218. }
  219. /**
  220. * Processes item events occurring on this check box menu item by
  221. * dispatching them to any registered <code>ItemListener</code> objects.
  222. * <p>
  223. * This method is not called unless item events are
  224. * enabled for this menu item. Item events are enabled
  225. * when one of the following occurs:
  226. * <p><ul>
  227. * <li>An <code>ItemListener</code> object is registered
  228. * via <code>addItemListener</code>.
  229. * <li>Item events are enabled via <code>enableEvents</code>.
  230. * </ul>
  231. * @param e the item event.
  232. * @see java.awt.event.ItemEvent
  233. * @see java.awt.event.ItemListener
  234. * @see java.awt.CheckboxMenuItem#addItemListener
  235. * @see java.awt.MenuItem#enableEvents
  236. * @since JDK1.1
  237. */
  238. protected void processItemEvent(ItemEvent e) {
  239. if (itemListener != null) {
  240. itemListener.itemStateChanged(e);
  241. }
  242. }
  243. /**
  244. * Returns the parameter string representing the state of this check
  245. * box menu item. This string is useful for debugging.
  246. * @return the parameter string of this check box menu item.
  247. */
  248. public String paramString() {
  249. return super.paramString() + ",state=" + state;
  250. }
  251. /* Serialization support.
  252. */
  253. /*
  254. * Serial Data Version
  255. * @serial
  256. */
  257. private int checkboxMenuItemSerializedDataVersion = 1;
  258. /**
  259. * Writes default serializable fields to stream. Writes
  260. * a list of serializable ItemListener(s) as optional data.
  261. * The non-serializable ItemListner(s) are detected and
  262. * no attempt is made to serialize them.
  263. *
  264. * @serialData Null terminated sequence of 0 or more pairs.
  265. * The pair consists of a String and Object.
  266. * The String indicates the type of object and
  267. * is one of the following :
  268. * itemListenerK indicating and ItemListener object.
  269. *
  270. * @see AWTEventMulticaster.save(ObjectOutputStream, String, EventListener)
  271. * @see java.awt.Component.itemListenerK
  272. */
  273. private void writeObject(ObjectOutputStream s)
  274. throws java.io.IOException
  275. {
  276. s.defaultWriteObject();
  277. AWTEventMulticaster.save(s, itemListenerK, itemListener);
  278. s.writeObject(null);
  279. }
  280. /*
  281. * Read the ObjectInputStream and if it isnt null
  282. * add a listener to receive item events fired
  283. * by the Checkbox menu item.
  284. * Unrecognised keys or values will be Ignored.
  285. * @serial
  286. * @see removeActionListener()
  287. * @see addActionListener()
  288. */
  289. private void readObject(ObjectInputStream s)
  290. throws ClassNotFoundException, IOException
  291. {
  292. s.defaultReadObject();
  293. Object keyOrNull;
  294. while(null != (keyOrNull = s.readObject())) {
  295. String key = ((String)keyOrNull).intern();
  296. if (itemListenerK == key)
  297. addItemListener((ItemListener)(s.readObject()));
  298. else // skip value for unrecognized key
  299. s.readObject();
  300. }
  301. }
  302. /**
  303. * Initialize JNI field and method IDs
  304. */
  305. private static native void initIDs();
  306. }