JMenuItem.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /* JMenuItem.java --
  2. Copyright (C) 2002, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 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 javax.swing;
  32. import java.awt.Component;
  33. import java.awt.event.KeyEvent;
  34. import java.awt.event.MouseEvent;
  35. import java.beans.PropertyChangeEvent;
  36. import java.beans.PropertyChangeListener;
  37. import java.util.EventListener;
  38. import javax.accessibility.Accessible;
  39. import javax.accessibility.AccessibleContext;
  40. import javax.accessibility.AccessibleRole;
  41. import javax.accessibility.AccessibleState;
  42. import javax.swing.event.ChangeEvent;
  43. import javax.swing.event.ChangeListener;
  44. import javax.swing.event.MenuDragMouseEvent;
  45. import javax.swing.event.MenuDragMouseListener;
  46. import javax.swing.event.MenuKeyEvent;
  47. import javax.swing.event.MenuKeyListener;
  48. import javax.swing.plaf.MenuItemUI;
  49. /**
  50. * JMenuItem represents element in the menu. It inherits most of
  51. * its functionality from AbstractButton, however its behavior somewhat
  52. * varies from it. JMenuItem fire different kinds of events.
  53. * PropertyChangeEvents are fired when menuItems properties are modified;
  54. * ChangeEvents are fired when menuItem's state changes and actionEvents are
  55. * fired when menu item is selected. In addition to this events menuItem also
  56. * fire MenuDragMouseEvent and MenuKeyEvents when mouse is dragged over
  57. * the menu item or associated key with menu item is invoked respectively.
  58. */
  59. public class JMenuItem extends AbstractButton implements Accessible,
  60. MenuElement
  61. {
  62. private static final long serialVersionUID = -1681004643499461044L;
  63. /** Combination of keyboard keys that can be used to activate this menu item */
  64. private KeyStroke accelerator;
  65. /**
  66. * Indicates if we are currently dragging the mouse.
  67. */
  68. private boolean isDragging;
  69. /**
  70. * Creates a new JMenuItem object.
  71. */
  72. public JMenuItem()
  73. {
  74. this(null, null);
  75. }
  76. /**
  77. * Creates a new JMenuItem with the given icon.
  78. *
  79. * @param icon Icon that will be displayed on the menu item
  80. */
  81. public JMenuItem(Icon icon)
  82. {
  83. // FIXME: The requestedFocusEnabled property should
  84. // be set to false, when only icon is set for menu item.
  85. this(null, icon);
  86. }
  87. /**
  88. * Creates a new JMenuItem with the given label.
  89. *
  90. * @param text label for the menu item
  91. */
  92. public JMenuItem(String text)
  93. {
  94. this(text, null);
  95. }
  96. /**
  97. * Creates a new JMenuItem associated with the specified action.
  98. *
  99. * @param action action for this menu item
  100. */
  101. public JMenuItem(Action action)
  102. {
  103. super();
  104. super.setAction(action);
  105. setModel(new DefaultButtonModel());
  106. init(null, null);
  107. if (action != null)
  108. {
  109. String name = (String) action.getValue(Action.NAME);
  110. if (name != null)
  111. setName(name);
  112. KeyStroke accel = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
  113. if (accel != null)
  114. setAccelerator(accel);
  115. Integer mnemonic = (Integer) action.getValue(Action.MNEMONIC_KEY);
  116. if (mnemonic != null)
  117. setMnemonic(mnemonic.intValue());
  118. String command = (String) action.getValue(Action.ACTION_COMMAND_KEY);
  119. if (command != null)
  120. setActionCommand(command);
  121. }
  122. }
  123. /**
  124. * Creates a new JMenuItem with specified text and icon.
  125. * Text is displayed to the left of icon by default.
  126. *
  127. * @param text label for this menu item
  128. * @param icon icon that will be displayed on this menu item
  129. */
  130. public JMenuItem(String text, Icon icon)
  131. {
  132. super();
  133. setModel(new DefaultButtonModel());
  134. init(text, icon);
  135. }
  136. /**
  137. * Creates a new JMenuItem object.
  138. *
  139. * @param text label for this menu item
  140. * @param mnemonic - Single key that can be used with a
  141. * look-and-feel meta key to activate this menu item. However
  142. * menu item should be visible on the screen when mnemonic is used.
  143. */
  144. public JMenuItem(String text, int mnemonic)
  145. {
  146. this(text, null);
  147. setMnemonic(mnemonic);
  148. }
  149. /**
  150. * Initializes this menu item
  151. *
  152. * @param text label for this menu item
  153. * @param icon icon to be displayed for this menu item
  154. */
  155. protected void init(String text, Icon icon)
  156. {
  157. super.init(text, icon);
  158. // Initializes properties for this menu item, that are different
  159. // from Abstract button properties.
  160. /* NOTE: According to java specifications paint_border should be set to false,
  161. since menu item should not have a border. However running few java programs
  162. it seems that menu items and menues can have a border. Commenting
  163. out statement below for now. */
  164. //borderPainted = false;
  165. focusPainted = false;
  166. horizontalAlignment = JButton.LEADING;
  167. horizontalTextPosition = JButton.TRAILING;
  168. }
  169. /**
  170. * Set the "UI" property of the menu item, which is a look and feel class
  171. * responsible for handling menuItem's input events and painting it.
  172. *
  173. * @param ui The new "UI" property
  174. */
  175. public void setUI(MenuItemUI ui)
  176. {
  177. super.setUI(ui);
  178. }
  179. /**
  180. * This method sets this menuItem's UI to the UIManager's default for the
  181. * current look and feel.
  182. */
  183. public void updateUI()
  184. {
  185. setUI((MenuItemUI) UIManager.getUI(this));
  186. }
  187. /**
  188. * This method returns a name to identify which look and feel class will be
  189. * the UI delegate for the menuItem.
  190. *
  191. * @return The Look and Feel classID. "MenuItemUI"
  192. */
  193. public String getUIClassID()
  194. {
  195. return "MenuItemUI";
  196. }
  197. /**
  198. * Returns true if button's model is armed and false otherwise. The
  199. * button model is armed if menu item has focus or it is selected.
  200. *
  201. * @return $boolean$ true if button's model is armed and false otherwise
  202. */
  203. public boolean isArmed()
  204. {
  205. return getModel().isArmed();
  206. }
  207. /**
  208. * Sets menuItem's "ARMED" property
  209. *
  210. * @param armed DOCUMENT ME!
  211. */
  212. public void setArmed(boolean armed)
  213. {
  214. getModel().setArmed(armed);
  215. }
  216. /**
  217. * Enable or disable menu item. When menu item is disabled,
  218. * its text and icon are grayed out if they exist.
  219. *
  220. * @param enabled if true enable menu item, and disable otherwise.
  221. */
  222. public void setEnabled(boolean enabled)
  223. {
  224. super.setEnabled(enabled);
  225. }
  226. /**
  227. * Return accelerator for this menu item.
  228. *
  229. * @return $KeyStroke$ accelerator for this menu item.
  230. */
  231. public KeyStroke getAccelerator()
  232. {
  233. return accelerator;
  234. }
  235. /**
  236. * Sets the key combination which invokes the menu item's action
  237. * listeners without navigating the menu hierarchy. Note that when the
  238. * keyboard accelerator is typed, it will work whether or not the
  239. * menu is currently displayed.
  240. *
  241. * @param keystroke accelerator for this menu item.
  242. */
  243. public void setAccelerator(KeyStroke keystroke)
  244. {
  245. KeyStroke old = this.accelerator;
  246. this.accelerator = keystroke;
  247. firePropertyChange ("accelerator", old, keystroke);
  248. }
  249. /**
  250. * Configures menu items' properties from properties of the specified action.
  251. * This method overrides configurePropertiesFromAction from AbstractButton
  252. * to also set accelerator property.
  253. *
  254. * @param action action to configure properties from
  255. */
  256. protected void configurePropertiesFromAction(Action action)
  257. {
  258. super.configurePropertiesFromAction(action);
  259. if (! (this instanceof JMenu) && action != null)
  260. {
  261. setAccelerator((KeyStroke) (action.getValue(Action.ACCELERATOR_KEY)));
  262. if (accelerator != null)
  263. super.registerKeyboardAction(action, accelerator,
  264. JComponent.WHEN_IN_FOCUSED_WINDOW);
  265. }
  266. }
  267. /**
  268. * Creates PropertyChangeListener to listen for the changes in action
  269. * properties.
  270. *
  271. * @param action action to listen to for property changes
  272. *
  273. * @return $PropertyChangeListener$ Listener that listens to changes in
  274. * action properties.
  275. */
  276. protected PropertyChangeListener createActionPropertyChangeListener(Action action)
  277. {
  278. return new PropertyChangeListener()
  279. {
  280. public void propertyChange(PropertyChangeEvent e)
  281. {
  282. Action act = (Action) (e.getSource());
  283. configurePropertiesFromAction(act);
  284. }
  285. };
  286. }
  287. /**
  288. * Process mouse events forwarded from MenuSelectionManager.
  289. *
  290. * @param ev event forwarded from MenuSelectionManager
  291. * @param path path to the menu element from which event was generated
  292. * @param manager MenuSelectionManager for the current menu hierarchy
  293. */
  294. public void processMouseEvent(MouseEvent ev, MenuElement[] path,
  295. MenuSelectionManager manager)
  296. {
  297. MenuDragMouseEvent e = new MenuDragMouseEvent(ev.getComponent(),
  298. ev.getID(), ev.getWhen(),
  299. ev.getModifiers(), ev.getX(),
  300. ev.getY(),
  301. ev.getClickCount(),
  302. ev.isPopupTrigger(), path,
  303. manager);
  304. processMenuDragMouseEvent(e);
  305. }
  306. /**
  307. * Process key events forwarded from MenuSelectionManager.
  308. *
  309. * @param event event forwarded from MenuSelectionManager
  310. * @param path path to the menu element from which event was generated
  311. * @param manager MenuSelectionManager for the current menu hierarchy
  312. */
  313. public void processKeyEvent(KeyEvent event, MenuElement[] path,
  314. MenuSelectionManager manager)
  315. {
  316. MenuKeyEvent e = new MenuKeyEvent(event.getComponent(), event.getID(),
  317. event.getWhen(), event.getModifiers(),
  318. event.getKeyCode(), event.getKeyChar(),
  319. path, manager);
  320. processMenuKeyEvent(e);
  321. // Consume original key event, if the menu key event has been consumed.
  322. if (e.isConsumed())
  323. event.consume();
  324. }
  325. /**
  326. * This method fires MenuDragMouseEvents to registered listeners.
  327. * Different types of MenuDragMouseEvents are fired depending
  328. * on the observed mouse event.
  329. *
  330. * @param event Mouse
  331. */
  332. public void processMenuDragMouseEvent(MenuDragMouseEvent event)
  333. {
  334. switch (event.getID())
  335. {
  336. case MouseEvent.MOUSE_ENTERED:
  337. isDragging = false;
  338. fireMenuDragMouseEntered(event);
  339. break;
  340. case MouseEvent.MOUSE_EXITED:
  341. isDragging = false;
  342. fireMenuDragMouseExited(event);
  343. break;
  344. case MouseEvent.MOUSE_DRAGGED:
  345. isDragging = true;
  346. fireMenuDragMouseDragged(event);
  347. break;
  348. case MouseEvent.MOUSE_RELEASED:
  349. if (isDragging)
  350. fireMenuDragMouseReleased(event);
  351. break;
  352. }
  353. }
  354. /**
  355. * This method fires MenuKeyEvent to registered listeners.
  356. * Different types of MenuKeyEvents are fired depending
  357. * on the observed key event.
  358. *
  359. * @param event DOCUMENT ME!
  360. */
  361. public void processMenuKeyEvent(MenuKeyEvent event)
  362. {
  363. switch (event.getID())
  364. {
  365. case KeyEvent.KEY_PRESSED:
  366. fireMenuKeyPressed(event);
  367. break;
  368. case KeyEvent.KEY_RELEASED:
  369. fireMenuKeyReleased(event);
  370. break;
  371. case KeyEvent.KEY_TYPED:
  372. fireMenuKeyTyped(event);
  373. break;
  374. default:
  375. break;
  376. }
  377. }
  378. /**
  379. * Fires MenuDragMouseEvent to all of the menuItem's MouseInputListeners.
  380. *
  381. * @param event The event signifying that mouse entered menuItem while it was dragged
  382. */
  383. protected void fireMenuDragMouseEntered(MenuDragMouseEvent event)
  384. {
  385. EventListener[] ll = listenerList.getListeners(MenuDragMouseListener.class);
  386. for (int i = 0; i < ll.length; i++)
  387. ((MenuDragMouseListener) ll[i]).menuDragMouseEntered(event);
  388. }
  389. /**
  390. * Fires MenuDragMouseEvent to all of the menuItem's MouseInputListeners.
  391. *
  392. * @param event The event signifying that mouse has exited menu item, while it was dragged
  393. */
  394. protected void fireMenuDragMouseExited(MenuDragMouseEvent event)
  395. {
  396. EventListener[] ll = listenerList.getListeners(MenuDragMouseListener.class);
  397. for (int i = 0; i < ll.length; i++)
  398. ((MenuDragMouseListener) ll[i]).menuDragMouseExited(event);
  399. }
  400. /**
  401. * Fires MenuDragMouseEvent to all of the menuItem's MouseInputListeners.
  402. *
  403. * @param event The event signifying that mouse is being dragged over the menuItem
  404. */
  405. protected void fireMenuDragMouseDragged(MenuDragMouseEvent event)
  406. {
  407. EventListener[] ll = listenerList.getListeners(MenuDragMouseListener.class);
  408. for (int i = 0; i < ll.length; i++)
  409. ((MenuDragMouseListener) ll[i]).menuDragMouseDragged(event);
  410. }
  411. /**
  412. * This method fires a MenuDragMouseEvent to all the MenuItem's MouseInputListeners.
  413. *
  414. * @param event The event signifying that mouse was released while it was dragged over the menuItem
  415. */
  416. protected void fireMenuDragMouseReleased(MenuDragMouseEvent event)
  417. {
  418. EventListener[] ll = listenerList.getListeners(MenuDragMouseListener.class);
  419. for (int i = 0; i < ll.length; i++)
  420. ((MenuDragMouseListener) ll[i]).menuDragMouseReleased(event);
  421. }
  422. /**
  423. * This method fires a MenuKeyEvent to all the MenuItem's MenuKeyListeners.
  424. *
  425. * @param event The event signifying that key associated with this menu was pressed
  426. */
  427. protected void fireMenuKeyPressed(MenuKeyEvent event)
  428. {
  429. EventListener[] ll = listenerList.getListeners(MenuKeyListener.class);
  430. for (int i = 0; i < ll.length; i++)
  431. ((MenuKeyListener) ll[i]).menuKeyPressed(event);
  432. }
  433. /**
  434. * This method fires a MenuKeyEvent to all the MenuItem's MenuKeyListeners.
  435. *
  436. * @param event The event signifying that key associated with this menu was released
  437. */
  438. protected void fireMenuKeyReleased(MenuKeyEvent event)
  439. {
  440. EventListener[] ll = listenerList.getListeners(MenuKeyListener.class);
  441. for (int i = 0; i < ll.length; i++)
  442. ((MenuKeyListener) ll[i]).menuKeyTyped(event);
  443. }
  444. /**
  445. * This method fires a MenuKeyEvent to all the MenuItem's MenuKeyListeners.
  446. *
  447. * @param event The event signifying that key associated with this menu was typed.
  448. * The key is typed when it was pressed and then released
  449. */
  450. protected void fireMenuKeyTyped(MenuKeyEvent event)
  451. {
  452. EventListener[] ll = listenerList.getListeners(MenuKeyListener.class);
  453. for (int i = 0; i < ll.length; i++)
  454. ((MenuKeyListener) ll[i]).menuKeyTyped(event);
  455. }
  456. /**
  457. * Method of the MenuElement interface.
  458. * This method is invoked by MenuSelectionManager when selection of
  459. * this menu item has changed. If this menu item was selected then
  460. * arm it's model, and disarm the model otherwise. The menu item
  461. * is considered to be selected, and thus highlighted when its model
  462. * is armed.
  463. *
  464. * @param changed indicates selection status of this menu item. If changed is
  465. * true then menu item is selected and deselected otherwise.
  466. */
  467. public void menuSelectionChanged(boolean changed)
  468. {
  469. Component parent = this.getParent();
  470. if (changed)
  471. {
  472. model.setArmed(true);
  473. if (parent != null && parent instanceof JPopupMenu)
  474. ((JPopupMenu) parent).setSelected(this);
  475. }
  476. else
  477. {
  478. model.setArmed(false);
  479. if (parent != null && parent instanceof JPopupMenu)
  480. ((JPopupMenu) parent).getSelectionModel().clearSelection();
  481. }
  482. }
  483. /**
  484. * Method of the MenuElement interface.
  485. *
  486. * @return $MenuElement[]$ Returns array of sub-components for this menu
  487. * item. By default menuItem doesn't have any subcomponents and so
  488. * empty array is returned instead.
  489. */
  490. public MenuElement[] getSubElements()
  491. {
  492. return new MenuElement[0];
  493. }
  494. /**
  495. * Returns reference to the component that will paint this menu item.
  496. *
  497. * @return $Component$ Component that will paint this menu item.
  498. * Simply returns reference to this menu item.
  499. */
  500. public Component getComponent()
  501. {
  502. return this;
  503. }
  504. /**
  505. * Adds a MenuDragMouseListener to this menu item. When mouse
  506. * is dragged over the menu item the MenuDragMouseEvents will be
  507. * fired, and these listeners will be called.
  508. *
  509. * @param listener The new listener to add
  510. */
  511. public void addMenuDragMouseListener(MenuDragMouseListener listener)
  512. {
  513. listenerList.add(MenuDragMouseListener.class, listener);
  514. }
  515. /**
  516. * Removes a MenuDragMouseListener from the menuItem's listener list.
  517. *
  518. * @param listener The listener to remove
  519. */
  520. public void removeMenuDragMouseListener(MenuDragMouseListener listener)
  521. {
  522. listenerList.remove(MenuDragMouseListener.class, listener);
  523. }
  524. /**
  525. * Returns all added MenuDragMouseListener objects.
  526. *
  527. * @return an array of listeners
  528. *
  529. * @since 1.4
  530. */
  531. public MenuDragMouseListener[] getMenuDragMouseListeners()
  532. {
  533. return (MenuDragMouseListener[]) listenerList.getListeners(MenuDragMouseListener.class);
  534. }
  535. /**
  536. * Adds an MenuKeyListener to this menu item. This listener will be
  537. * invoked when MenuKeyEvents will be fired by this menu item.
  538. *
  539. * @param listener The new listener to add
  540. */
  541. public void addMenuKeyListener(MenuKeyListener listener)
  542. {
  543. listenerList.add(MenuKeyListener.class, listener);
  544. }
  545. /**
  546. * Removes an MenuKeyListener from the menuItem's listener list.
  547. *
  548. * @param listener The listener to remove
  549. */
  550. public void removeMenuKeyListener(MenuKeyListener listener)
  551. {
  552. listenerList.remove(MenuKeyListener.class, listener);
  553. }
  554. /**
  555. * Returns all added MenuKeyListener objects.
  556. *
  557. * @return an array of listeners
  558. *
  559. * @since 1.4
  560. */
  561. public MenuKeyListener[] getMenuKeyListeners()
  562. {
  563. return (MenuKeyListener[]) listenerList.getListeners(MenuKeyListener.class);
  564. }
  565. /**
  566. * Returns a string describing the attributes for the <code>JMenuItem</code>
  567. * component, for use in debugging. The return value is guaranteed to be
  568. * non-<code>null</code>, but the format of the string may vary between
  569. * implementations.
  570. *
  571. * @return A string describing the attributes of the <code>JMenuItem</code>.
  572. */
  573. protected String paramString()
  574. {
  575. // calling super seems to be sufficient here...
  576. return super.paramString();
  577. }
  578. /**
  579. * Returns the object that provides accessibility features for this
  580. * <code>JMenuItem</code> component.
  581. *
  582. * @return The accessible context (an instance of
  583. * {@link AccessibleJMenuItem}).
  584. */
  585. public AccessibleContext getAccessibleContext()
  586. {
  587. if (accessibleContext == null)
  588. {
  589. AccessibleJMenuItem ctx = new AccessibleJMenuItem();
  590. addChangeListener(ctx);
  591. accessibleContext = ctx;
  592. }
  593. return accessibleContext;
  594. }
  595. /**
  596. * Provides the accessibility features for the <code>JMenuItem</code>
  597. * component.
  598. *
  599. * @see JMenuItem#getAccessibleContext()
  600. */
  601. protected class AccessibleJMenuItem extends AccessibleAbstractButton
  602. implements ChangeListener
  603. {
  604. private static final long serialVersionUID = 6748924232082076534L;
  605. private boolean armed;
  606. private boolean focusOwner;
  607. private boolean pressed;
  608. private boolean selected;
  609. /**
  610. * Creates a new <code>AccessibleJMenuItem</code> instance.
  611. */
  612. AccessibleJMenuItem()
  613. {
  614. //super(component);
  615. }
  616. /**
  617. * Receives notification when the menu item's state changes and fires
  618. * appropriate property change events to registered listeners.
  619. *
  620. * @param event the change event
  621. */
  622. public void stateChanged(ChangeEvent event)
  623. {
  624. // This is fired in all cases.
  625. firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
  626. Boolean.FALSE, Boolean.TRUE);
  627. ButtonModel model = getModel();
  628. // Handle the armed property.
  629. if (model.isArmed())
  630. {
  631. if (! armed)
  632. {
  633. armed = true;
  634. firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  635. AccessibleState.ARMED, null);
  636. }
  637. }
  638. else
  639. {
  640. if (armed)
  641. {
  642. armed = false;
  643. firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  644. null, AccessibleState.ARMED);
  645. }
  646. }
  647. // Handle the pressed property.
  648. if (model.isPressed())
  649. {
  650. if (! pressed)
  651. {
  652. pressed = true;
  653. firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  654. AccessibleState.PRESSED, null);
  655. }
  656. }
  657. else
  658. {
  659. if (pressed)
  660. {
  661. pressed = false;
  662. firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  663. null, AccessibleState.PRESSED);
  664. }
  665. }
  666. // Handle the selected property.
  667. if (model.isSelected())
  668. {
  669. if (! selected)
  670. {
  671. selected = true;
  672. firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  673. AccessibleState.SELECTED, null);
  674. }
  675. }
  676. else
  677. {
  678. if (selected)
  679. {
  680. selected = false;
  681. firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  682. null, AccessibleState.SELECTED);
  683. }
  684. }
  685. // Handle the focusOwner property.
  686. if (isFocusOwner())
  687. {
  688. if (! focusOwner)
  689. {
  690. focusOwner = true;
  691. firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  692. AccessibleState.FOCUSED, null);
  693. }
  694. }
  695. else
  696. {
  697. if (focusOwner)
  698. {
  699. focusOwner = false;
  700. firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  701. null, AccessibleState.FOCUSED);
  702. }
  703. }
  704. }
  705. /**
  706. * Returns the accessible role for the <code>JMenuItem</code> component.
  707. *
  708. * @return {@link AccessibleRole#MENU_ITEM}.
  709. */
  710. public AccessibleRole getAccessibleRole()
  711. {
  712. return AccessibleRole.MENU_ITEM;
  713. }
  714. }
  715. /**
  716. * Returns <code>true</code> if the component is guaranteed to be painted
  717. * on top of others. This returns false by default and is overridden by
  718. * components like JMenuItem, JPopupMenu and JToolTip to return true for
  719. * added efficiency.
  720. *
  721. * @return <code>true</code> if the component is guaranteed to be painted
  722. * on top of others
  723. */
  724. boolean onTop()
  725. {
  726. return SwingUtilities.getAncestorOfClass(JInternalFrame.class, this)
  727. == null;
  728. }
  729. }