MenuItem.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /* MenuItem.java -- An item in a menu
  2. Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt;
  32. import java.awt.peer.MenuItemPeer;
  33. import java.awt.peer.MenuComponentPeer;
  34. import java.awt.event.ActionEvent;
  35. import java.awt.event.ActionListener;
  36. import java.lang.reflect.Array;
  37. import java.util.EventListener;
  38. /**
  39. * This class represents an item in a menu.
  40. *
  41. * @author Aaron M. Renn (arenn@urbanophile.com)
  42. */
  43. public class MenuItem extends MenuComponent implements java.io.Serializable
  44. {
  45. // FIXME: The enabled event mask is not used at this time.
  46. /*
  47. * Static Variables
  48. */
  49. // Serialization Constant
  50. private static final long serialVersionUID = -21757335363267194L;
  51. /*************************************************************************/
  52. /*
  53. * Instance Variables
  54. */
  55. /**
  56. * @serial The name of the action command generated by this item.
  57. */
  58. private String actionCommand;
  59. /**
  60. * @serial Indicates whether or not this menu item is enabled.
  61. */
  62. private boolean enabled;
  63. /**
  64. * @serial The mask of events that are enabled for this menu item.
  65. */
  66. long eventMask;
  67. /**
  68. * @serial This menu item's label
  69. */
  70. private String label;
  71. /**
  72. * @serial The shortcut for this menu item, if any
  73. */
  74. private MenuShortcut shortcut;
  75. // The list of action listeners for this menu item.
  76. private transient ActionListener action_listeners;
  77. /*************************************************************************/
  78. /*
  79. * Constructors
  80. */
  81. /**
  82. * Initializes a new instance of <code>MenuItem</code> with no label
  83. * and no shortcut.
  84. */
  85. public
  86. MenuItem()
  87. {
  88. }
  89. /*************************************************************************/
  90. /**
  91. * Initializes a new instance of <code>MenuItem</code> with the specified
  92. * label and no shortcut.
  93. *
  94. * @param label The label for this menu item.
  95. */
  96. public
  97. MenuItem(String label)
  98. {
  99. this.label = label;
  100. }
  101. /*************************************************************************/
  102. /**
  103. * Initializes a new instance of <code>MenuItem</code> with the specified
  104. * label and shortcut.
  105. *
  106. * @param label The label for this menu item.
  107. * @param shortcut The shortcut for this menu item.
  108. */
  109. public
  110. MenuItem(String label, MenuShortcut shortcut)
  111. {
  112. this.label = label;
  113. this.shortcut = shortcut;
  114. }
  115. /*************************************************************************/
  116. /*
  117. * Instance Methods
  118. */
  119. /**
  120. * Returns the label for this menu item, which may be <code>null</code>.
  121. *
  122. * @return The label for this menu item.
  123. */
  124. public String
  125. getLabel()
  126. {
  127. return(label);
  128. }
  129. /*************************************************************************/
  130. /**
  131. * This method sets the label for this menu to the specified value.
  132. *
  133. * @param label The new label for this menu item.
  134. */
  135. public synchronized void
  136. setLabel(String label)
  137. {
  138. this.label = label;
  139. if (peer != null)
  140. {
  141. MenuItemPeer mp = (MenuItemPeer) peer;
  142. mp.setLabel (label);
  143. }
  144. }
  145. /*************************************************************************/
  146. /**
  147. * Tests whether or not this menu item is enabled.
  148. *
  149. * @return <code>true</code> if this menu item is enabled, <code>false</code>
  150. * otherwise.
  151. */
  152. public boolean
  153. isEnabled()
  154. {
  155. return(enabled);
  156. }
  157. /*************************************************************************/
  158. /**
  159. * Sets the enabled status of this menu item.
  160. *
  161. * @param enabled <code>true</code> to enable this menu item,
  162. * <code>false</code> otherwise.
  163. */
  164. public synchronized void
  165. setEnabled(boolean enabled)
  166. {
  167. if (enabled == this.enabled)
  168. return;
  169. this.enabled = enabled;
  170. if (peer != null)
  171. {
  172. MenuItemPeer mp = (MenuItemPeer) peer;
  173. mp.setEnabled (enabled);
  174. }
  175. }
  176. /*************************************************************************/
  177. /**
  178. * Sets the enabled status of this menu item.
  179. *
  180. * @param enabled <code>true</code> to enable this menu item,
  181. * <code>false</code> otherwise.
  182. *
  183. * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
  184. */
  185. public void
  186. enable(boolean enabled)
  187. {
  188. setEnabled(enabled);
  189. }
  190. /*************************************************************************/
  191. /**
  192. * Enables this menu item.
  193. *
  194. * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
  195. */
  196. public void
  197. enable()
  198. {
  199. setEnabled(true);
  200. }
  201. /*************************************************************************/
  202. /**
  203. * Disables this menu item.
  204. *
  205. * @deprecated This method is deprecated in favor of <code>setEnabled()</code>.
  206. */
  207. public void
  208. disable()
  209. {
  210. setEnabled(false);
  211. }
  212. /*************************************************************************/
  213. /**
  214. * Returns the shortcut for this menu item, which may be <code>null</code>.
  215. *
  216. * @return The shortcut for this menu item.
  217. */
  218. public MenuShortcut
  219. getShortcut()
  220. {
  221. return(shortcut);
  222. }
  223. /*************************************************************************/
  224. /**
  225. * Sets the shortcut for this menu item to the specified value. This
  226. * must be done before the native peer is created.
  227. *
  228. * @param shortcut The new shortcut for this menu item.
  229. */
  230. public void
  231. setShortcut(MenuShortcut shortcut)
  232. {
  233. this.shortcut = shortcut;
  234. }
  235. /*************************************************************************/
  236. /**
  237. * Deletes the shortcut for this menu item if one exists. This must be
  238. * done before the native peer is created.
  239. */
  240. public void
  241. deleteShortcut()
  242. {
  243. shortcut = null;
  244. }
  245. /*************************************************************************/
  246. /**
  247. * Returns the name of the action command in the action events
  248. * generated by this menu item.
  249. *
  250. * @return The action command name
  251. */
  252. public String
  253. getActionCommand()
  254. {
  255. return(actionCommand);
  256. }
  257. /*************************************************************************/
  258. /**
  259. * Sets the name of the action command in the action events generated by
  260. * this menu item.
  261. *
  262. * @param actionCommand The new action command name.
  263. */
  264. public void
  265. setActionCommand(String actionCommand)
  266. {
  267. this.actionCommand = actionCommand;
  268. }
  269. /*************************************************************************/
  270. /**
  271. * Enables the specified events. This is done automatically when a
  272. * listener is added and does not normally need to be done by
  273. * application code.
  274. *
  275. * @param events The events to enable, which should be the bit masks
  276. * from <code>AWTEvent</code>.
  277. */
  278. protected final void
  279. enableEvents(long events)
  280. {
  281. eventMask |= events;
  282. // TODO: see comment in Component.enableEvents().
  283. }
  284. /*************************************************************************/
  285. /**
  286. * Disables the specified events.
  287. *
  288. * @param events The events to enable, which should be the bit masks
  289. * from <code>AWTEvent</code>.
  290. */
  291. protected final void
  292. disableEvents(long events)
  293. {
  294. eventMask &= ~events;
  295. }
  296. /*************************************************************************/
  297. /**
  298. * Creates the native peer for this object.
  299. */
  300. public void
  301. addNotify()
  302. {
  303. if (peer != null)
  304. peer = getToolkit ().createMenuItem (this);
  305. }
  306. /*************************************************************************/
  307. /**
  308. * Adds the specified listener to the list of registered action listeners
  309. * for this component.
  310. *
  311. * @param listener The listener to add.
  312. */
  313. public synchronized void
  314. addActionListener(ActionListener listener)
  315. {
  316. action_listeners = AWTEventMulticaster.add(action_listeners, listener);
  317. enableEvents(AWTEvent.ACTION_EVENT_MASK);
  318. }
  319. public synchronized void
  320. removeActionListener(ActionListener l)
  321. {
  322. action_listeners = AWTEventMulticaster.remove(action_listeners, l);
  323. }
  324. public synchronized ActionListener[] getActionListeners()
  325. {
  326. return (ActionListener[])
  327. AWTEventMulticaster.getListeners(action_listeners,
  328. ActionListener.class);
  329. }
  330. /** Returns all registered EventListers of the given listenerType.
  331. * listenerType must be a subclass of EventListener, or a
  332. * ClassClassException is thrown.
  333. * @since 1.3
  334. */
  335. public EventListener[] getListeners(Class listenerType)
  336. {
  337. if (listenerType == ActionListener.class)
  338. return getActionListeners();
  339. return (EventListener[]) Array.newInstance(listenerType, 0);
  340. }
  341. /*************************************************************************/
  342. void
  343. dispatchEventImpl(AWTEvent e)
  344. {
  345. if (e.id <= ActionEvent.ACTION_LAST
  346. && e.id >= ActionEvent.ACTION_FIRST
  347. && (action_listeners != null
  348. || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
  349. processEvent(e);
  350. }
  351. /**
  352. * Processes the specified event by calling <code>processActionEvent()</code>
  353. * if it is an instance of <code>ActionEvent</code>.
  354. *
  355. * @param event The event to process.
  356. */
  357. protected void
  358. processEvent(AWTEvent event)
  359. {
  360. if (event instanceof ActionEvent)
  361. processActionEvent((ActionEvent)event);
  362. }
  363. /*************************************************************************/
  364. /**
  365. * Processes the specified event by dispatching it to any registered listeners.
  366. *
  367. * @param event The event to process.
  368. */
  369. protected void
  370. processActionEvent(ActionEvent event)
  371. {
  372. if (action_listeners != null)
  373. action_listeners.actionPerformed(event);
  374. }
  375. /*************************************************************************/
  376. /**
  377. * Returns a debugging string for this object.
  378. *
  379. * @return A debugging string for this object.
  380. */
  381. public String
  382. paramString()
  383. {
  384. return ("label=" + label + ",enabled=" + enabled +
  385. ",actionCommand=" + actionCommand + "," + super.paramString());
  386. }
  387. // Accessibility API not yet implemented.
  388. // public AccessibleContext getAccessibleContext()
  389. } // class MenuItem