1. /*
  2. * @(#)MenuItem.java 1.88 04/05/18
  3. *
  4. * Copyright 2004 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.MenuItemPeer;
  9. import java.awt.event.*;
  10. import java.util.EventListener;
  11. import java.io.ObjectOutputStream;
  12. import java.io.ObjectInputStream;
  13. import java.io.IOException;
  14. import javax.accessibility.*;
  15. /**
  16. * All items in a menu must belong to the class
  17. * <code>MenuItem</code>, or one of its subclasses.
  18. * <p>
  19. * The default <code>MenuItem</code> object embodies
  20. * a simple labeled menu item.
  21. * <p>
  22. * This picture of a menu bar shows five menu items:
  23. * <IMG SRC="doc-files/MenuBar-1.gif" alt="The following text describes this graphic."
  24. * ALIGN=CENTER HSPACE=10 VSPACE=7>
  25. * <br CLEAR=LEFT>
  26. * The first two items are simple menu items, labeled
  27. * <code>"Basic"</code> and <code>"Simple"</code>.
  28. * Following these two items is a separator, which is itself
  29. * a menu item, created with the label <code>"-"</code>.
  30. * Next is an instance of <code>CheckboxMenuItem</code>
  31. * labeled <code>"Check"</code>. The final menu item is a
  32. * submenu labeled <code>"More Examples"</code>,
  33. * and this submenu is an instance of <code>Menu</code>.
  34. * <p>
  35. * When a menu item is selected, AWT sends an action event to
  36. * the menu item. Since the event is an
  37. * instance of <code>ActionEvent</code>, the <code>processEvent</code>
  38. * method examines the event and passes it along to
  39. * <code>processActionEvent</code>. The latter method redirects the
  40. * event to any <code>ActionListener</code> objects that have
  41. * registered an interest in action events generated by this
  42. * menu item.
  43. * <P>
  44. * Note that the subclass <code>Menu</code> overrides this behavior and
  45. * does not send any event to the frame until one of its subitems is
  46. * selected.
  47. *
  48. * @version 1.88, 05/18/04
  49. * @author Sami Shaio
  50. */
  51. public class MenuItem extends MenuComponent implements Accessible {
  52. static {
  53. /* ensure that the necessary native libraries are loaded */
  54. Toolkit.loadLibraries();
  55. if (!GraphicsEnvironment.isHeadless()) {
  56. initIDs();
  57. }
  58. }
  59. /**
  60. * A value to indicate whether a menu item is enabled
  61. * or not. If it is enabled, <code>enabled</code> will
  62. * be set to true. Else <code>enabled</code> will
  63. * be set to false.
  64. *
  65. * @serial
  66. * @see #isEnabled()
  67. * @see #setEnabled(boolean)
  68. */
  69. boolean enabled = true;
  70. /**
  71. * <code>label</code> is the label of a menu item.
  72. * It can be any string.
  73. *
  74. * @serial
  75. * @see #getLabel()
  76. * @see #setLabel(String)
  77. */
  78. String label;
  79. /**
  80. * This field indicates the command tha has been issued
  81. * by a particular menu item.
  82. * By default the <code>actionCommand</code>
  83. * is the label of the menu item, unless it has been
  84. * set using setActionCommand.
  85. *
  86. * @serial
  87. * @see #setActionCommand(String)
  88. * @see #getActionCommand()
  89. */
  90. String actionCommand;
  91. /**
  92. * The eventMask is ONLY set by subclasses via enableEvents.
  93. * The mask should NOT be set when listeners are registered
  94. * so that we can distinguish the difference between when
  95. * listeners request events and subclasses request them.
  96. *
  97. * @serial
  98. */
  99. long eventMask;
  100. transient ActionListener actionListener;
  101. /**
  102. * A sequence of key stokes that ia associated with
  103. * a menu item.
  104. * Note :in 1.1.2 you must use setActionCommand()
  105. * on a menu item in order for its shortcut to
  106. * work.
  107. *
  108. * @serial
  109. * @see #getShortcut()
  110. * @see #setShortcut(MenuShortcut)
  111. * @see #deleteShortcut()
  112. */
  113. private MenuShortcut shortcut = null;
  114. private static final String base = "menuitem";
  115. private static int nameCounter = 0;
  116. /*
  117. * JDK 1.1 serialVersionUID
  118. */
  119. private static final long serialVersionUID = -21757335363267194L;
  120. /**
  121. * Constructs a new MenuItem with an empty label and no keyboard
  122. * shortcut.
  123. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  124. * returns true.
  125. * @see java.awt.GraphicsEnvironment#isHeadless
  126. * @since JDK1.1
  127. */
  128. public MenuItem() throws HeadlessException {
  129. this("", null);
  130. }
  131. /**
  132. * Constructs a new MenuItem with the specified label
  133. * and no keyboard shortcut. Note that use of "-" in
  134. * a label is reserved to indicate a separator between
  135. * menu items. By default, all menu items except for
  136. * separators are enabled.
  137. * @param label the label for this menu item.
  138. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  139. * returns true.
  140. * @see java.awt.GraphicsEnvironment#isHeadless
  141. * @since JDK1.0
  142. */
  143. public MenuItem(String label) throws HeadlessException {
  144. this(label, null);
  145. }
  146. /**
  147. * Create a menu item with an associated keyboard shortcut.
  148. * Note that use of "-" in a label is reserved to indicate
  149. * a separator between menu items. By default, all menu
  150. * items except for separators are enabled.
  151. * @param label the label for this menu item.
  152. * @param s the instance of <code>MenuShortcut</code>
  153. * associated with this menu item.
  154. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  155. * returns true.
  156. * @see java.awt.GraphicsEnvironment#isHeadless
  157. * @since JDK1.1
  158. */
  159. public MenuItem(String label, MenuShortcut s) throws HeadlessException {
  160. this.label = label;
  161. this.shortcut = s;
  162. }
  163. /**
  164. * Construct a name for this MenuComponent. Called by getName() when
  165. * the name is null.
  166. */
  167. String constructComponentName() {
  168. synchronized (getClass()) {
  169. return base + nameCounter++;
  170. }
  171. }
  172. /**
  173. * Creates the menu item's peer. The peer allows us to modify the
  174. * appearance of the menu item without changing its functionality.
  175. */
  176. public void addNotify() {
  177. synchronized (getTreeLock()) {
  178. if (peer == null)
  179. peer = Toolkit.getDefaultToolkit().createMenuItem(this);
  180. }
  181. }
  182. /**
  183. * Gets the label for this menu item.
  184. * @return the label of this menu item, or <code>null</code>
  185. if this menu item has no label.
  186. * @see java.awt.MenuItem#setLabel
  187. * @since JDK1.0
  188. */
  189. public String getLabel() {
  190. return label;
  191. }
  192. /**
  193. * Sets the label for this menu item to the specified label.
  194. * @param label the new label, or <code>null</code> for no label.
  195. * @see java.awt.MenuItem#getLabel
  196. * @since JDK1.0
  197. */
  198. public synchronized void setLabel(String label) {
  199. this.label = label;
  200. MenuItemPeer peer = (MenuItemPeer)this.peer;
  201. if (peer != null) {
  202. peer.setLabel(label);
  203. }
  204. }
  205. /**
  206. * Checks whether this menu item is enabled.
  207. * @see java.awt.MenuItem#setEnabled
  208. * @since JDK1.0
  209. */
  210. public boolean isEnabled() {
  211. return enabled;
  212. }
  213. /**
  214. * Sets whether or not this menu item can be chosen.
  215. * @param b if <code>true</code>, enables this menu item;
  216. * if <code>false</code>, disables it.
  217. * @see java.awt.MenuItem#isEnabled
  218. * @since JDK1.1
  219. */
  220. public synchronized void setEnabled(boolean b) {
  221. enable(b);
  222. }
  223. /**
  224. * @deprecated As of JDK version 1.1,
  225. * replaced by <code>setEnabled(boolean)</code>.
  226. */
  227. @Deprecated
  228. public synchronized void enable() {
  229. enabled = true;
  230. MenuItemPeer peer = (MenuItemPeer)this.peer;
  231. if (peer != null) {
  232. peer.enable();
  233. }
  234. }
  235. /**
  236. * @deprecated As of JDK version 1.1,
  237. * replaced by <code>setEnabled(boolean)</code>.
  238. */
  239. @Deprecated
  240. public void enable(boolean b) {
  241. if (b) {
  242. enable();
  243. } else {
  244. disable();
  245. }
  246. }
  247. /**
  248. * @deprecated As of JDK version 1.1,
  249. * replaced by <code>setEnabled(boolean)</code>.
  250. */
  251. @Deprecated
  252. public synchronized void disable() {
  253. enabled = false;
  254. MenuItemPeer peer = (MenuItemPeer)this.peer;
  255. if (peer != null) {
  256. peer.disable();
  257. }
  258. }
  259. /**
  260. * Get the <code>MenuShortcut</code> object associated with this
  261. * menu item,
  262. * @return the menu shortcut associated with this menu item,
  263. * or <code>null</code> if none has been specified.
  264. * @see java.awt.MenuItem#setShortcut
  265. * @since JDK1.1
  266. */
  267. public MenuShortcut getShortcut() {
  268. return shortcut;
  269. }
  270. /**
  271. * Set the <code>MenuShortcut</code> object associated with this
  272. * menu item. If a menu shortcut is already associated with
  273. * this menu item, it is replaced.
  274. * @param s the menu shortcut to associate
  275. * with this menu item.
  276. * @see java.awt.MenuItem#getShortcut
  277. * @since JDK1.1
  278. */
  279. public void setShortcut(MenuShortcut s) {
  280. shortcut = s;
  281. MenuItemPeer peer = (MenuItemPeer)this.peer;
  282. if (peer != null) {
  283. peer.setLabel(label);
  284. }
  285. }
  286. /**
  287. * Delete any <code>MenuShortcut</code> object associated
  288. * with this menu item.
  289. * @since JDK1.1
  290. */
  291. public void deleteShortcut() {
  292. shortcut = null;
  293. MenuItemPeer peer = (MenuItemPeer)this.peer;
  294. if (peer != null) {
  295. peer.setLabel(label);
  296. }
  297. }
  298. /*
  299. * Delete a matching MenuShortcut associated with this MenuItem.
  300. * Used when iterating Menus.
  301. */
  302. void deleteShortcut(MenuShortcut s) {
  303. if (s.equals(shortcut)) {
  304. shortcut = null;
  305. MenuItemPeer peer = (MenuItemPeer)this.peer;
  306. if (peer != null) {
  307. peer.setLabel(label);
  308. }
  309. }
  310. }
  311. /*
  312. * The main goal of this method is to post an appropriate event
  313. * to the event queue when menu shortcut is pressed. However,
  314. * in subclasses this method may do more than just posting
  315. * an event.
  316. */
  317. void doMenuEvent(long when, int modifiers) {
  318. Toolkit.getEventQueue().postEvent(
  319. new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
  320. getActionCommand(), when, modifiers));
  321. }
  322. /*
  323. * Post an ActionEvent to the target (on
  324. * keydown). Returns true if there is an associated
  325. * shortcut.
  326. */
  327. boolean handleShortcut(KeyEvent e) {
  328. MenuShortcut s = new MenuShortcut(e.getKeyCode(),
  329. (e.getModifiers() & InputEvent.SHIFT_MASK) > 0);
  330. if (s.equals(shortcut) && enabled) {
  331. // MenuShortcut match -- issue an event on keydown.
  332. if (e.getID() == KeyEvent.KEY_PRESSED) {
  333. doMenuEvent(e.getWhen(), e.getModifiers());
  334. } else {
  335. // silently eat key release.
  336. }
  337. return true;
  338. }
  339. return false;
  340. }
  341. MenuItem getShortcutMenuItem(MenuShortcut s) {
  342. return (s.equals(shortcut)) ? this : null;
  343. }
  344. /**
  345. * Enables event delivery to this menu item for events
  346. * to be defined by the specified event mask parameter
  347. * <p>
  348. * Since event types are automatically enabled when a listener for
  349. * that type is added to the menu item, this method only needs
  350. * to be invoked by subclasses of <code>MenuItem</code> which desire to
  351. * have the specified event types delivered to <code>processEvent</code>
  352. * regardless of whether a listener is registered.
  353. *
  354. * @param eventsToEnable the event mask defining the event types
  355. * @see java.awt.MenuItem#processEvent
  356. * @see java.awt.MenuItem#disableEvents
  357. * @see java.awt.Component#enableEvents
  358. * @since JDK1.1
  359. */
  360. protected final void enableEvents(long eventsToEnable) {
  361. eventMask |= eventsToEnable;
  362. newEventsOnly = true;
  363. }
  364. /**
  365. * Disables event delivery to this menu item for events
  366. * defined by the specified event mask parameter.
  367. *
  368. * @param eventsToDisable the event mask defining the event types
  369. * @see java.awt.MenuItem#processEvent
  370. * @see java.awt.MenuItem#enableEvents
  371. * @see java.awt.Component#disableEvents
  372. * @since JDK1.1
  373. */
  374. protected final void disableEvents(long eventsToDisable) {
  375. eventMask &= ~eventsToDisable;
  376. }
  377. /**
  378. * Sets the command name of the action event that is fired
  379. * by this menu item.
  380. * <p>
  381. * By default, the action command is set to the label of
  382. * the menu item.
  383. * @param command the action command to be set
  384. * for this menu item.
  385. * @see java.awt.MenuItem#getActionCommand
  386. * @since JDK1.1
  387. */
  388. public void setActionCommand(String command) {
  389. actionCommand = command;
  390. }
  391. /**
  392. * Gets the command name of the action event that is fired
  393. * by this menu item.
  394. * @see java.awt.MenuItem#setActionCommand
  395. * @since JDK1.1
  396. */
  397. public String getActionCommand() {
  398. return getActionCommandImpl();
  399. }
  400. // This is final so it can be called on the Toolkit thread.
  401. final String getActionCommandImpl() {
  402. return (actionCommand == null? label : actionCommand);
  403. }
  404. /**
  405. * Adds the specified action listener to receive action events
  406. * from this menu item.
  407. * If l is null, no exception is thrown and no action is performed.
  408. *
  409. * @param l the action listener.
  410. * @see #removeActionListener
  411. * @see #getActionListeners
  412. * @see java.awt.event.ActionEvent
  413. * @see java.awt.event.ActionListener
  414. * @since JDK1.1
  415. */
  416. public synchronized void addActionListener(ActionListener l) {
  417. if (l == null) {
  418. return;
  419. }
  420. actionListener = AWTEventMulticaster.add(actionListener, l);
  421. newEventsOnly = true;
  422. }
  423. /**
  424. * Removes the specified action listener so it no longer receives
  425. * action events from this menu item.
  426. * If l is null, no exception is thrown and no action is performed.
  427. *
  428. * @param l the action listener.
  429. * @see #addActionListener
  430. * @see #getActionListeners
  431. * @see java.awt.event.ActionEvent
  432. * @see java.awt.event.ActionListener
  433. * @since JDK1.1
  434. */
  435. public synchronized void removeActionListener(ActionListener l) {
  436. if (l == null) {
  437. return;
  438. }
  439. actionListener = AWTEventMulticaster.remove(actionListener, l);
  440. }
  441. /**
  442. * Returns an array of all the action listeners
  443. * registered on this menu item.
  444. *
  445. * @return all of this menu item's <code>ActionListener</code>s
  446. * or an empty array if no action
  447. * listeners are currently registered
  448. *
  449. * @see #addActionListener
  450. * @see #removeActionListener
  451. * @see java.awt.event.ActionEvent
  452. * @see java.awt.event.ActionListener
  453. * @since 1.4
  454. */
  455. public synchronized ActionListener[] getActionListeners() {
  456. return (ActionListener[])(getListeners(ActionListener.class));
  457. }
  458. /**
  459. * Returns an array of all the objects currently registered
  460. * as <code><em>Foo</em>Listener</code>s
  461. * upon this <code>MenuItem</code>.
  462. * <code><em>Foo</em>Listener</code>s are registered using the
  463. * <code>add<em>Foo</em>Listener</code> method.
  464. *
  465. * <p>
  466. * You can specify the <code>listenerType</code> argument
  467. * with a class literal, such as
  468. * <code><em>Foo</em>Listener.class</code>.
  469. * For example, you can query a
  470. * <code>MenuItem</code> <code>m</code>
  471. * for its action listeners with the following code:
  472. *
  473. * <pre>ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));</pre>
  474. *
  475. * If no such listeners exist, this method returns an empty array.
  476. *
  477. * @param listenerType the type of listeners requested; this parameter
  478. * should specify an interface that descends from
  479. * <code>java.util.EventListener</code>
  480. * @return an array of all objects registered as
  481. * <code><em>Foo</em>Listener</code>s on this menu item,
  482. * or an empty array if no such
  483. * listeners have been added
  484. * @exception ClassCastException if <code>listenerType</code>
  485. * doesn't specify a class or interface that implements
  486. * <code>java.util.EventListener</code>
  487. *
  488. * @see #getActionListeners
  489. * @since 1.3
  490. */
  491. public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
  492. EventListener l = null;
  493. if (listenerType == ActionListener.class) {
  494. l = actionListener;
  495. }
  496. return AWTEventMulticaster.getListeners(l, listenerType);
  497. }
  498. /**
  499. * Processes events on this menu item. If the event is an
  500. * instance of <code>ActionEvent</code>, it invokes
  501. * <code>processActionEvent</code>, another method
  502. * defined by <code>MenuItem</code>.
  503. * <p>
  504. * Currently, menu items only support action events.
  505. * <p>Note that if the event parameter is <code>null</code>
  506. * the behavior is unspecified and may result in an
  507. * exception.
  508. *
  509. * @param e the event
  510. * @see java.awt.MenuItem#processActionEvent
  511. * @since JDK1.1
  512. */
  513. protected void processEvent(AWTEvent e) {
  514. if (e instanceof ActionEvent) {
  515. processActionEvent((ActionEvent)e);
  516. }
  517. }
  518. // REMIND: remove when filtering is done at lower level
  519. boolean eventEnabled(AWTEvent e) {
  520. if (e.id == ActionEvent.ACTION_PERFORMED) {
  521. if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 ||
  522. actionListener != null) {
  523. return true;
  524. }
  525. return false;
  526. }
  527. return super.eventEnabled(e);
  528. }
  529. /**
  530. * Processes action events occurring on this menu item,
  531. * by dispatching them to any registered
  532. * <code>ActionListener</code> objects.
  533. * This method is not called unless action events are
  534. * enabled for this component. Action events are enabled
  535. * when one of the following occurs:
  536. * <p><ul>
  537. * <li>An <code>ActionListener</code> object is registered
  538. * via <code>addActionListener</code>.
  539. * <li>Action events are enabled via <code>enableEvents</code>.
  540. * </ul>
  541. * <p>Note that if the event parameter is <code>null</code>
  542. * the behavior is unspecified and may result in an
  543. * exception.
  544. *
  545. * @param e the action event
  546. * @see java.awt.event.ActionEvent
  547. * @see java.awt.event.ActionListener
  548. * @see java.awt.MenuItem#enableEvents
  549. * @since JDK1.1
  550. */
  551. protected void processActionEvent(ActionEvent e) {
  552. ActionListener listener = actionListener;
  553. if (listener != null) {
  554. listener.actionPerformed(e);
  555. }
  556. }
  557. /**
  558. * Returns a string representing the state of this <code>MenuItem</code>.
  559. * This method is intended to be used only for debugging purposes, and the
  560. * content and format of the returned string may vary between
  561. * implementations. The returned string may be empty but may not be
  562. * <code>null</code>.
  563. *
  564. * @return the parameter string of this menu item
  565. */
  566. public String paramString() {
  567. String str = ",label=" + label;
  568. if (shortcut != null) {
  569. str += ",shortcut=" + shortcut;
  570. }
  571. return super.paramString() + str;
  572. }
  573. /* Serialization support.
  574. */
  575. /**
  576. * Menu item serialized data version.
  577. *
  578. * @serial
  579. */
  580. private int menuItemSerializedDataVersion = 1;
  581. /**
  582. * Writes default serializable fields to stream. Writes
  583. * a list of serializable <code>ActionListeners</code>
  584. * as optional data. The non-serializable listeners are
  585. * detected and no attempt is made to serialize them.
  586. *
  587. * @param s the <code>ObjectOutputStream</code> to write
  588. * @serialData <code>null</code> terminated sequence of 0
  589. * or more pairs; the pair consists of a <code>String</code>
  590. * and an <code>Object</code> the <code>String</code>
  591. * indicates the type of object and is one of the following:
  592. * <code>actionListenerK</code> indicating an
  593. * <code>ActionListener</code> object
  594. *
  595. * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
  596. * @see #readObject(ObjectInputStream)
  597. */
  598. private void writeObject(ObjectOutputStream s)
  599. throws IOException
  600. {
  601. s.defaultWriteObject();
  602. AWTEventMulticaster.save(s, actionListenerK, actionListener);
  603. s.writeObject(null);
  604. }
  605. /**
  606. * Reads the <code>ObjectInputStream</code> and if it
  607. * isn't <code>null</code> adds a listener to receive
  608. * action events fired by the <code>Menu</code> Item.
  609. * Unrecognized keys or values will be ignored.
  610. *
  611. * @param s the <code>ObjectInputStream</code> to read
  612. * @exception HeadlessException if
  613. * <code>GraphicsEnvironment.isHeadless</code> returns
  614. * <code>true</code>
  615. * @see #removeActionListener(actionListener)
  616. * @see #addActionListener(actionListener)
  617. * @see #writeObject(ObjectOutputStream)
  618. */
  619. private void readObject(ObjectInputStream s)
  620. throws ClassNotFoundException, IOException, HeadlessException
  621. {
  622. // HeadlessException will be thrown from MenuComponent's readObject
  623. s.defaultReadObject();
  624. Object keyOrNull;
  625. while(null != (keyOrNull = s.readObject())) {
  626. String key = ((String)keyOrNull).intern();
  627. if (actionListenerK == key)
  628. addActionListener((ActionListener)(s.readObject()));
  629. else // skip value for unrecognized key
  630. s.readObject();
  631. }
  632. }
  633. /**
  634. * Initialize JNI field and method IDs
  635. */
  636. private static native void initIDs();
  637. /////////////////
  638. // Accessibility support
  639. ////////////////
  640. /**
  641. * Gets the AccessibleContext associated with this MenuItem.
  642. * For menu items, the AccessibleContext takes the form of an
  643. * AccessibleAWTMenuItem.
  644. * A new AccessibleAWTMenuItem instance is created if necessary.
  645. *
  646. * @return an AccessibleAWTMenuItem that serves as the
  647. * AccessibleContext of this MenuItem
  648. */
  649. public AccessibleContext getAccessibleContext() {
  650. if (accessibleContext == null) {
  651. accessibleContext = new AccessibleAWTMenuItem();
  652. }
  653. return accessibleContext;
  654. }
  655. /**
  656. * Inner class of MenuItem used to provide default support for
  657. * accessibility. This class is not meant to be used directly by
  658. * application developers, but is instead meant only to be
  659. * subclassed by menu component developers.
  660. * <p>
  661. * This class implements accessibility support for the
  662. * <code>MenuItem</code> class. It provides an implementation of the
  663. * Java Accessibility API appropriate to menu item user-interface elements.
  664. */
  665. protected class AccessibleAWTMenuItem extends AccessibleAWTMenuComponent
  666. implements AccessibleAction, AccessibleValue
  667. {
  668. /*
  669. * JDK 1.3 serialVersionUID
  670. */
  671. private static final long serialVersionUID = -217847831945965825L;
  672. /**
  673. * Get the accessible name of this object.
  674. *
  675. * @return the localized name of the object -- can be null if this
  676. * object does not have a name
  677. */
  678. public String getAccessibleName() {
  679. if (accessibleName != null) {
  680. return accessibleName;
  681. } else {
  682. if (getLabel() == null) {
  683. return super.getAccessibleName();
  684. } else {
  685. return getLabel();
  686. }
  687. }
  688. }
  689. /**
  690. * Get the role of this object.
  691. *
  692. * @return an instance of AccessibleRole describing the role of the
  693. * object
  694. */
  695. public AccessibleRole getAccessibleRole() {
  696. return AccessibleRole.MENU_ITEM;
  697. }
  698. /**
  699. * Get the AccessibleAction associated with this object. In the
  700. * implementation of the Java Accessibility API for this class,
  701. * return this object, which is responsible for implementing the
  702. * AccessibleAction interface on behalf of itself.
  703. *
  704. * @return this object
  705. */
  706. public AccessibleAction getAccessibleAction() {
  707. return this;
  708. }
  709. /**
  710. * Get the AccessibleValue associated with this object. In the
  711. * implementation of the Java Accessibility API for this class,
  712. * return this object, which is responsible for implementing the
  713. * AccessibleValue interface on behalf of itself.
  714. *
  715. * @return this object
  716. */
  717. public AccessibleValue getAccessibleValue() {
  718. return this;
  719. }
  720. /**
  721. * Returns the number of Actions available in this object. The
  722. * default behavior of a menu item is to have one action.
  723. *
  724. * @return 1, the number of Actions in this object
  725. */
  726. public int getAccessibleActionCount() {
  727. return 1;
  728. }
  729. /**
  730. * Return a description of the specified action of the object.
  731. *
  732. * @param i zero-based index of the actions
  733. */
  734. public String getAccessibleActionDescription(int i) {
  735. if (i == 0) {
  736. // [[[PENDING: WDW -- need to provide a localized string]]]
  737. return new String("click");
  738. } else {
  739. return null;
  740. }
  741. }
  742. /**
  743. * Perform the specified Action on the object
  744. *
  745. * @param i zero-based index of actions
  746. * @return true if the action was performed; otherwise false.
  747. */
  748. public boolean doAccessibleAction(int i) {
  749. if (i == 0) {
  750. // Simulate a button click
  751. Toolkit.getEventQueue().postEvent(
  752. new ActionEvent(MenuItem.this,
  753. ActionEvent.ACTION_PERFORMED,
  754. MenuItem.this.getActionCommand(),
  755. EventQueue.getMostRecentEventTime(),
  756. 0));
  757. return true;
  758. } else {
  759. return false;
  760. }
  761. }
  762. /**
  763. * Get the value of this object as a Number.
  764. *
  765. * @return An Integer of 0 if this isn't selected or an Integer of 1 if
  766. * this is selected.
  767. * @see javax.swing.AbstractButton#isSelected()
  768. */
  769. public Number getCurrentAccessibleValue() {
  770. return new Integer(0);
  771. }
  772. /**
  773. * Set the value of this object as a Number.
  774. *
  775. * @return True if the value was set.
  776. */
  777. public boolean setCurrentAccessibleValue(Number n) {
  778. return false;
  779. }
  780. /**
  781. * Get the minimum value of this object as a Number.
  782. *
  783. * @return An Integer of 0.
  784. */
  785. public Number getMinimumAccessibleValue() {
  786. return new Integer(0);
  787. }
  788. /**
  789. * Get the maximum value of this object as a Number.
  790. *
  791. * @return An Integer of 0.
  792. */
  793. public Number getMaximumAccessibleValue() {
  794. return new Integer(0);
  795. }
  796. } // class AccessibleAWTMenuItem
  797. }