1. /*
  2. * @(#)MenuItem.java 1.83 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 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.83, 01/23/03
  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. public synchronized void enable() {
  228. enabled = true;
  229. MenuItemPeer peer = (MenuItemPeer)this.peer;
  230. if (peer != null) {
  231. peer.enable();
  232. }
  233. }
  234. /**
  235. * @deprecated As of JDK version 1.1,
  236. * replaced by <code>setEnabled(boolean)</code>.
  237. */
  238. public void enable(boolean b) {
  239. if (b) {
  240. enable();
  241. } else {
  242. disable();
  243. }
  244. }
  245. /**
  246. * @deprecated As of JDK version 1.1,
  247. * replaced by <code>setEnabled(boolean)</code>.
  248. */
  249. public synchronized void disable() {
  250. enabled = false;
  251. MenuItemPeer peer = (MenuItemPeer)this.peer;
  252. if (peer != null) {
  253. peer.disable();
  254. }
  255. }
  256. /**
  257. * Get the <code>MenuShortcut</code> object associated with this
  258. * menu item,
  259. * @return the menu shortcut associated with this menu item,
  260. * or <code>null</code> if none has been specified.
  261. * @see java.awt.MenuItem#setShortcut
  262. * @since JDK1.1
  263. */
  264. public MenuShortcut getShortcut() {
  265. return shortcut;
  266. }
  267. /**
  268. * Set the <code>MenuShortcut</code> object associated with this
  269. * menu item. If a menu shortcut is already associated with
  270. * this menu item, it is replaced.
  271. * @param s the menu shortcut to associate
  272. * with this menu item.
  273. * @see java.awt.MenuItem#getShortcut
  274. * @since JDK1.1
  275. */
  276. public void setShortcut(MenuShortcut s) {
  277. shortcut = s;
  278. MenuItemPeer peer = (MenuItemPeer)this.peer;
  279. if (peer != null) {
  280. peer.setLabel(label);
  281. }
  282. }
  283. /**
  284. * Delete any <code>MenuShortcut</code> object associated
  285. * with this menu item.
  286. * @since JDK1.1
  287. */
  288. public void deleteShortcut() {
  289. shortcut = null;
  290. MenuItemPeer peer = (MenuItemPeer)this.peer;
  291. if (peer != null) {
  292. peer.setLabel(label);
  293. }
  294. }
  295. /*
  296. * Delete a matching MenuShortcut associated with this MenuItem.
  297. * Used when iterating Menus.
  298. */
  299. void deleteShortcut(MenuShortcut s) {
  300. if (s.equals(shortcut)) {
  301. shortcut = null;
  302. MenuItemPeer peer = (MenuItemPeer)this.peer;
  303. if (peer != null) {
  304. peer.setLabel(label);
  305. }
  306. }
  307. }
  308. /*
  309. * The main goal of this method is to post an appropriate event
  310. * to the event queue when menu shortcut is pressed. However,
  311. * in subclasses this method may do more than just posting
  312. * an event.
  313. */
  314. void doMenuEvent(long when, int modifiers) {
  315. Toolkit.getEventQueue().postEvent(
  316. new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
  317. getActionCommand(), when, modifiers));
  318. }
  319. /*
  320. * Post an ActionEvent to the target (on
  321. * keydown). Returns true if there is an associated
  322. * shortcut.
  323. */
  324. boolean handleShortcut(KeyEvent e) {
  325. MenuShortcut s = new MenuShortcut(e.getKeyCode(),
  326. (e.getModifiers() & InputEvent.SHIFT_MASK) > 0);
  327. if (s.equals(shortcut) && enabled) {
  328. // MenuShortcut match -- issue an event on keydown.
  329. if (e.getID() == KeyEvent.KEY_PRESSED) {
  330. doMenuEvent(e.getWhen(), e.getModifiers());
  331. } else {
  332. // silently eat key release.
  333. }
  334. return true;
  335. }
  336. return false;
  337. }
  338. MenuItem getShortcutMenuItem(MenuShortcut s) {
  339. return (s.equals(shortcut)) ? this : null;
  340. }
  341. /**
  342. * Enables event delivery to this menu item for events
  343. * to be defined by the specified event mask parameter
  344. * <p>
  345. * Since event types are automatically enabled when a listener for
  346. * that type is added to the menu item, this method only needs
  347. * to be invoked by subclasses of <code>MenuItem</code> which desire to
  348. * have the specified event types delivered to <code>processEvent</code>
  349. * regardless of whether a listener is registered.
  350. *
  351. * @param eventsToEnable the event mask defining the event types
  352. * @see java.awt.MenuItem#processEvent
  353. * @see java.awt.MenuItem#disableEvents
  354. * @see java.awt.Component#enableEvents
  355. * @since JDK1.1
  356. */
  357. protected final void enableEvents(long eventsToEnable) {
  358. eventMask |= eventsToEnable;
  359. newEventsOnly = true;
  360. }
  361. /**
  362. * Disables event delivery to this menu item for events
  363. * defined by the specified event mask parameter.
  364. *
  365. * @param eventsToDisable the event mask defining the event types
  366. * @see java.awt.MenuItem#processEvent
  367. * @see java.awt.MenuItem#enableEvents
  368. * @see java.awt.Component#disableEvents
  369. * @since JDK1.1
  370. */
  371. protected final void disableEvents(long eventsToDisable) {
  372. eventMask &= ~eventsToDisable;
  373. }
  374. /**
  375. * Sets the command name of the action event that is fired
  376. * by this menu item.
  377. * <p>
  378. * By default, the action command is set to the label of
  379. * the menu item.
  380. * @param command the action command to be set
  381. * for this menu item.
  382. * @see java.awt.MenuItem#getActionCommand
  383. * @since JDK1.1
  384. */
  385. public void setActionCommand(String command) {
  386. actionCommand = command;
  387. }
  388. /**
  389. * Gets the command name of the action event that is fired
  390. * by this menu item.
  391. * @see java.awt.MenuItem#setActionCommand
  392. * @since JDK1.1
  393. */
  394. public String getActionCommand() {
  395. return (actionCommand == null? label : actionCommand);
  396. }
  397. /**
  398. * Adds the specified action listener to receive action events
  399. * from this menu item.
  400. * If l is null, no exception is thrown and no action is performed.
  401. *
  402. * @param l the action listener.
  403. * @see #removeActionListener
  404. * @see #getActionListeners
  405. * @see java.awt.event.ActionEvent
  406. * @see java.awt.event.ActionListener
  407. * @since JDK1.1
  408. */
  409. public synchronized void addActionListener(ActionListener l) {
  410. if (l == null) {
  411. return;
  412. }
  413. actionListener = AWTEventMulticaster.add(actionListener, l);
  414. newEventsOnly = true;
  415. }
  416. /**
  417. * Removes the specified action listener so it no longer receives
  418. * action events from this menu item.
  419. * If l is null, no exception is thrown and no action is performed.
  420. *
  421. * @param l the action listener.
  422. * @see #addActionListener
  423. * @see #getActionListeners
  424. * @see java.awt.event.ActionEvent
  425. * @see java.awt.event.ActionListener
  426. * @since JDK1.1
  427. */
  428. public synchronized void removeActionListener(ActionListener l) {
  429. if (l == null) {
  430. return;
  431. }
  432. actionListener = AWTEventMulticaster.remove(actionListener, l);
  433. }
  434. /**
  435. * Returns an array of all the action listeners
  436. * registered on this menu item.
  437. *
  438. * @return all of this menu item's <code>ActionListener</code>s
  439. * or an empty array if no action
  440. * listeners are currently registered
  441. *
  442. * @see #addActionListener
  443. * @see #removeActionListener
  444. * @see java.awt.event.ActionEvent
  445. * @see java.awt.event.ActionListener
  446. * @since 1.4
  447. */
  448. public synchronized ActionListener[] getActionListeners() {
  449. return (ActionListener[])(getListeners(ActionListener.class));
  450. }
  451. /**
  452. * Returns an array of all the objects currently registered
  453. * as <code><em>Foo</em>Listener</code>s
  454. * upon this <code>MenuItem</code>.
  455. * <code><em>Foo</em>Listener</code>s are registered using the
  456. * <code>add<em>Foo</em>Listener</code> method.
  457. *
  458. * <p>
  459. * You can specify the <code>listenerType</code> argument
  460. * with a class literal, such as
  461. * <code><em>Foo</em>Listener.class</code>.
  462. * For example, you can query a
  463. * <code>MenuItem</code> <code>m</code>
  464. * for its action listeners with the following code:
  465. *
  466. * <pre>ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));</pre>
  467. *
  468. * If no such listeners exist, this method returns an empty array.
  469. *
  470. * @param listenerType the type of listeners requested; this parameter
  471. * should specify an interface that descends from
  472. * <code>java.util.EventListener</code>
  473. * @return an array of all objects registered as
  474. * <code><em>Foo</em>Listener</code>s on this menu item,
  475. * or an empty array if no such
  476. * listeners have been added
  477. * @exception ClassCastException if <code>listenerType</code>
  478. * doesn't specify a class or interface that implements
  479. * <code>java.util.EventListener</code>
  480. *
  481. * @see #getActionListeners
  482. * @since 1.3
  483. */
  484. public EventListener[] getListeners(Class listenerType) {
  485. EventListener l = null;
  486. if (listenerType == ActionListener.class) {
  487. l = actionListener;
  488. }
  489. return AWTEventMulticaster.getListeners(l, listenerType);
  490. }
  491. /**
  492. * Processes events on this menu item. If the event is an
  493. * instance of <code>ActionEvent</code>, it invokes
  494. * <code>processActionEvent</code>, another method
  495. * defined by <code>MenuItem</code>.
  496. * <p>
  497. * Currently, menu items only support action events.
  498. * <p>Note that if the event parameter is <code>null</code>
  499. * the behavior is unspecified and may result in an
  500. * exception.
  501. *
  502. * @param e the event
  503. * @see java.awt.MenuItem#processActionEvent
  504. * @since JDK1.1
  505. */
  506. protected void processEvent(AWTEvent e) {
  507. if (e instanceof ActionEvent) {
  508. processActionEvent((ActionEvent)e);
  509. }
  510. }
  511. // REMIND: remove when filtering is done at lower level
  512. boolean eventEnabled(AWTEvent e) {
  513. if (e.id == ActionEvent.ACTION_PERFORMED) {
  514. if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 ||
  515. actionListener != null) {
  516. return true;
  517. }
  518. return false;
  519. }
  520. return super.eventEnabled(e);
  521. }
  522. /**
  523. * Processes action events occurring on this menu item,
  524. * by dispatching them to any registered
  525. * <code>ActionListener</code> objects.
  526. * This method is not called unless action events are
  527. * enabled for this component. Action events are enabled
  528. * when one of the following occurs:
  529. * <p><ul>
  530. * <li>An <code>ActionListener</code> object is registered
  531. * via <code>addActionListener</code>.
  532. * <li>Action events are enabled via <code>enableEvents</code>.
  533. * </ul>
  534. * <p>Note that if the event parameter is <code>null</code>
  535. * the behavior is unspecified and may result in an
  536. * exception.
  537. *
  538. * @param e the action event
  539. * @see java.awt.event.ActionEvent
  540. * @see java.awt.event.ActionListener
  541. * @see java.awt.MenuItem#enableEvents
  542. * @since JDK1.1
  543. */
  544. protected void processActionEvent(ActionEvent e) {
  545. ActionListener listener = actionListener;
  546. if (listener != null) {
  547. listener.actionPerformed(e);
  548. }
  549. }
  550. /**
  551. * Returns a string representing the state of this <code>MenuItem</code>.
  552. * This method is intended to be used only for debugging purposes, and the
  553. * content and format of the returned string may vary between
  554. * implementations. The returned string may be empty but may not be
  555. * <code>null</code>.
  556. *
  557. * @return the parameter string of this menu item
  558. */
  559. public String paramString() {
  560. String str = ",label=" + label;
  561. if (shortcut != null) {
  562. str += ",shortcut=" + shortcut;
  563. }
  564. return super.paramString() + str;
  565. }
  566. /* Serialization support.
  567. */
  568. /**
  569. * Menu item serialized data version.
  570. *
  571. * @serial
  572. */
  573. private int menuItemSerializedDataVersion = 1;
  574. /**
  575. * Writes default serializable fields to stream. Writes
  576. * a list of serializable <code>ActionListeners</code>
  577. * as optional data. The non-serializable listeners are
  578. * detected and no attempt is made to serialize them.
  579. *
  580. * @param s the <code>ObjectOutputStream</code> to write
  581. * @serialData <code>null</code> terminated sequence of 0
  582. * or more pairs; the pair consists of a <code>String</code>
  583. * and an <code>Object</code> the <code>String</code>
  584. * indicates the type of object and is one of the following:
  585. * <code>actionListenerK</code> indicating an
  586. * <code>ActionListener</code> object
  587. *
  588. * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
  589. * @see #readObject(ObjectInputStream)
  590. */
  591. private void writeObject(ObjectOutputStream s)
  592. throws IOException
  593. {
  594. s.defaultWriteObject();
  595. AWTEventMulticaster.save(s, actionListenerK, actionListener);
  596. s.writeObject(null);
  597. }
  598. /**
  599. * Reads the <code>ObjectInputStream</code> and if it
  600. * isn't <code>null</code> adds a listener to receive
  601. * action events fired by the <code>Menu</code> Item.
  602. * Unrecognized keys or values will be ignored.
  603. *
  604. * @param s the <code>ObjectInputStream</code> to read
  605. * @exception HeadlessException if
  606. * <code>GraphicsEnvironment.isHeadless</code> returns
  607. * <code>true</code>
  608. * @see #removeActionListener(actionListener)
  609. * @see #addActionListener(actionListener)
  610. * @see #writeObject(ObjectOutputStream)
  611. */
  612. private void readObject(ObjectInputStream s)
  613. throws ClassNotFoundException, IOException, HeadlessException
  614. {
  615. // HeadlessException will be thrown from MenuComponent's readObject
  616. s.defaultReadObject();
  617. Object keyOrNull;
  618. while(null != (keyOrNull = s.readObject())) {
  619. String key = ((String)keyOrNull).intern();
  620. if (actionListenerK == key)
  621. addActionListener((ActionListener)(s.readObject()));
  622. else // skip value for unrecognized key
  623. s.readObject();
  624. }
  625. }
  626. /**
  627. * Initialize JNI field and method IDs
  628. */
  629. private static native void initIDs();
  630. /////////////////
  631. // Accessibility support
  632. ////////////////
  633. /**
  634. * Gets the AccessibleContext associated with this MenuItem.
  635. * For menu items, the AccessibleContext takes the form of an
  636. * AccessibleAWTMenuItem.
  637. * A new AccessibleAWTMenuItem instance is created if necessary.
  638. *
  639. * @return an AccessibleAWTMenuItem that serves as the
  640. * AccessibleContext of this MenuItem
  641. */
  642. public AccessibleContext getAccessibleContext() {
  643. if (accessibleContext == null) {
  644. accessibleContext = new AccessibleAWTMenuItem();
  645. }
  646. return accessibleContext;
  647. }
  648. /**
  649. * Inner class of MenuItem used to provide default support for
  650. * accessibility. This class is not meant to be used directly by
  651. * application developers, but is instead meant only to be
  652. * subclassed by menu component developers.
  653. * <p>
  654. * This class implements accessibility support for the
  655. * <code>MenuItem</code> class. It provides an implementation of the
  656. * Java Accessibility API appropriate to menu item user-interface elements.
  657. */
  658. protected class AccessibleAWTMenuItem extends AccessibleAWTMenuComponent
  659. implements AccessibleAction, AccessibleValue {
  660. /**
  661. * Get the accessible name of this object.
  662. *
  663. * @return the localized name of the object -- can be null if this
  664. * object does not have a name
  665. */
  666. public String getAccessibleName() {
  667. if (accessibleName != null) {
  668. return accessibleName;
  669. } else {
  670. if (getLabel() == null) {
  671. return super.getAccessibleName();
  672. } else {
  673. return getLabel();
  674. }
  675. }
  676. }
  677. /**
  678. * Get the role of this object.
  679. *
  680. * @return an instance of AccessibleRole describing the role of the
  681. * object
  682. */
  683. public AccessibleRole getAccessibleRole() {
  684. return AccessibleRole.MENU_ITEM;
  685. }
  686. /**
  687. * Get the AccessibleAction associated with this object. In the
  688. * implementation of the Java Accessibility API for this class,
  689. * return this object, which is responsible for implementing the
  690. * AccessibleAction interface on behalf of itself.
  691. *
  692. * @return this object
  693. */
  694. public AccessibleAction getAccessibleAction() {
  695. return this;
  696. }
  697. /**
  698. * Get the AccessibleValue associated with this object. In the
  699. * implementation of the Java Accessibility API for this class,
  700. * return this object, which is responsible for implementing the
  701. * AccessibleValue interface on behalf of itself.
  702. *
  703. * @return this object
  704. */
  705. public AccessibleValue getAccessibleValue() {
  706. return this;
  707. }
  708. /**
  709. * Returns the number of Actions available in this object. The
  710. * default behavior of a menu item is to have one action.
  711. *
  712. * @return 1, the number of Actions in this object
  713. */
  714. public int getAccessibleActionCount() {
  715. return 1;
  716. }
  717. /**
  718. * Return a description of the specified action of the object.
  719. *
  720. * @param i zero-based index of the actions
  721. */
  722. public String getAccessibleActionDescription(int i) {
  723. if (i == 0) {
  724. // [[[PENDING: WDW -- need to provide a localized string]]]
  725. return new String("click");
  726. } else {
  727. return null;
  728. }
  729. }
  730. /**
  731. * Perform the specified Action on the object
  732. *
  733. * @param i zero-based index of actions
  734. * @return true if the action was performed; otherwise false.
  735. */
  736. public boolean doAccessibleAction(int i) {
  737. if (i == 0) {
  738. // Simulate a button click
  739. Toolkit.getEventQueue().postEvent(
  740. new ActionEvent(MenuItem.this,
  741. ActionEvent.ACTION_PERFORMED,
  742. MenuItem.this.getActionCommand(),
  743. EventQueue.getMostRecentEventTime(),
  744. 0));
  745. return true;
  746. } else {
  747. return false;
  748. }
  749. }
  750. /**
  751. * Get the value of this object as a Number.
  752. *
  753. * @return An Integer of 0 if this isn't selected or an Integer of 1 if
  754. * this is selected.
  755. * @see javax.swing.AbstractButton#isSelected()
  756. */
  757. public Number getCurrentAccessibleValue() {
  758. return new Integer(0);
  759. }
  760. /**
  761. * Set the value of this object as a Number.
  762. *
  763. * @return True if the value was set.
  764. */
  765. public boolean setCurrentAccessibleValue(Number n) {
  766. return false;
  767. }
  768. /**
  769. * Get the minimum value of this object as a Number.
  770. *
  771. * @return An Integer of 0.
  772. */
  773. public Number getMinimumAccessibleValue() {
  774. return new Integer(0);
  775. }
  776. /**
  777. * Get the maximum value of this object as a Number.
  778. *
  779. * @return An Integer of 0.
  780. */
  781. public Number getMaximumAccessibleValue() {
  782. return new Integer(0);
  783. }
  784. } // class AccessibleAWTMenuItem
  785. }