2 Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.awt.Component;
42 import java.awt.event.KeyEvent;
43 import java.awt.event.MouseEvent;
44 import java.beans.PropertyChangeEvent;
45 import java.beans.PropertyChangeListener;
46 import java.util.EventListener;
48 import javax.accessibility.Accessible;
49 import javax.accessibility.AccessibleContext;
50 import javax.accessibility.AccessibleRole;
51 import javax.accessibility.AccessibleState;
52 import javax.swing.event.ChangeEvent;
53 import javax.swing.event.ChangeListener;
54 import javax.swing.event.MenuDragMouseEvent;
55 import javax.swing.event.MenuDragMouseListener;
56 import javax.swing.event.MenuKeyEvent;
57 import javax.swing.event.MenuKeyListener;
58 import javax.swing.plaf.MenuItemUI;
61 * JMenuItem represents element in the menu. It inherits most of
62 * its functionality from AbstractButton, however its behavior somewhat
63 * varies from it. JMenuItem fire different kinds of events.
64 * PropertyChangeEvents are fired when menuItems properties are modified;
65 * ChangeEvents are fired when menuItem's state changes and actionEvents are
66 * fired when menu item is selected. In addition to this events menuItem also
67 * fire MenuDragMouseEvent and MenuKeyEvents when mouse is dragged over
68 * the menu item or associated key with menu item is invoked respectively.
70 public class JMenuItem extends AbstractButton implements Accessible,
73 private static final long serialVersionUID = -1681004643499461044L;
75 /** Combination of keyboard keys that can be used to activate this menu item */
76 private KeyStroke accelerator;
79 * Indicates if we are currently dragging the mouse.
81 private boolean isDragging;
84 * Creates a new JMenuItem object.
92 * Creates a new JMenuItem with the given icon.
94 * @param icon Icon that will be displayed on the menu item
96 public JMenuItem(Icon icon)
98 // FIXME: The requestedFocusEnabled property should
99 // be set to false, when only icon is set for menu item.
104 * Creates a new JMenuItem with the given label.
106 * @param text label for the menu item
108 public JMenuItem(String text)
114 * Creates a new JMenuItem associated with the specified action.
116 * @param action action for this menu item
118 public JMenuItem(Action action)
121 super.setAction(action);
122 setModel(new DefaultButtonModel());
126 String name = (String) action.getValue(Action.NAME);
130 KeyStroke accel = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
132 setAccelerator(accel);
134 Integer mnemonic = (Integer) action.getValue(Action.MNEMONIC_KEY);
135 if (mnemonic != null)
136 setMnemonic(mnemonic.intValue());
138 String command = (String) action.getValue(Action.ACTION_COMMAND_KEY);
140 setActionCommand(command);
145 * Creates a new JMenuItem with specified text and icon.
146 * Text is displayed to the left of icon by default.
148 * @param text label for this menu item
149 * @param icon icon that will be displayed on this menu item
151 public JMenuItem(String text, Icon icon)
154 setModel(new DefaultButtonModel());
159 * Creates a new JMenuItem object.
161 * @param text label for this menu item
162 * @param mnemonic - Single key that can be used with a
163 * look-and-feel meta key to activate this menu item. However
164 * menu item should be visible on the screen when mnemonic is used.
166 public JMenuItem(String text, int mnemonic)
169 setMnemonic(mnemonic);
173 * Initializes this menu item
175 * @param text label for this menu item
176 * @param icon icon to be displayed for this menu item
178 protected void init(String text, Icon icon)
180 super.init(text, icon);
182 // Initializes properties for this menu item, that are different
183 // from Abstract button properties.
184 /* NOTE: According to java specifications paint_border should be set to false,
185 since menu item should not have a border. However running few java programs
186 it seems that menu items and menues can have a border. Commenting
187 out statement below for now. */
188 //borderPainted = false;
189 focusPainted = false;
190 horizontalAlignment = JButton.LEADING;
191 horizontalTextPosition = JButton.TRAILING;
195 * Set the "UI" property of the menu item, which is a look and feel class
196 * responsible for handling menuItem's input events and painting it.
198 * @param ui The new "UI" property
200 public void setUI(MenuItemUI ui)
206 * This method sets this menuItem's UI to the UIManager's default for the
207 * current look and feel.
209 public void updateUI()
211 setUI((MenuItemUI) UIManager.getUI(this));
215 * This method returns a name to identify which look and feel class will be
216 * the UI delegate for the menuItem.
218 * @return The Look and Feel classID. "MenuItemUI"
220 public String getUIClassID()
226 * Returns true if button's model is armed and false otherwise. The
227 * button model is armed if menu item has focus or it is selected.
229 * @return $boolean$ true if button's model is armed and false otherwise
231 public boolean isArmed()
233 return getModel().isArmed();
237 * Sets menuItem's "ARMED" property
239 * @param armed DOCUMENT ME!
241 public void setArmed(boolean armed)
243 getModel().setArmed(armed);
247 * Enable or disable menu item. When menu item is disabled,
248 * its text and icon are grayed out if they exist.
250 * @param enabled if true enable menu item, and disable otherwise.
252 public void setEnabled(boolean enabled)
254 super.setEnabled(enabled);
258 * Return accelerator for this menu item.
260 * @return $KeyStroke$ accelerator for this menu item.
262 public KeyStroke getAccelerator()
268 * Sets the key combination which invokes the menu item's action
269 * listeners without navigating the menu hierarchy. Note that when the
270 * keyboard accelerator is typed, it will work whether or not the
271 * menu is currently displayed.
273 * @param keystroke accelerator for this menu item.
275 public void setAccelerator(KeyStroke keystroke)
277 KeyStroke old = this.accelerator;
278 this.accelerator = keystroke;
279 firePropertyChange ("accelerator", old, keystroke);
283 * Configures menu items' properties from properties of the specified action.
284 * This method overrides configurePropertiesFromAction from AbstractButton
285 * to also set accelerator property.
287 * @param action action to configure properties from
289 protected void configurePropertiesFromAction(Action action)
291 super.configurePropertiesFromAction(action);
293 if (! (this instanceof JMenu) && action != null)
295 setAccelerator((KeyStroke) (action.getValue(Action.ACCELERATOR_KEY)));
296 if (accelerator != null)
297 super.registerKeyboardAction(action, accelerator,
298 JComponent.WHEN_IN_FOCUSED_WINDOW);
303 * Creates PropertyChangeListener to listen for the changes in action
306 * @param action action to listen to for property changes
308 * @return $PropertyChangeListener$ Listener that listens to changes in
311 protected PropertyChangeListener createActionPropertyChangeListener(Action action)
313 return new PropertyChangeListener()
315 public void propertyChange(PropertyChangeEvent e)
317 Action act = (Action) (e.getSource());
318 configurePropertiesFromAction(act);
324 * Process mouse events forwarded from MenuSelectionManager.
326 * @param ev event forwarded from MenuSelectionManager
327 * @param path path to the menu element from which event was generated
328 * @param manager MenuSelectionManager for the current menu hierarchy
330 public void processMouseEvent(MouseEvent ev, MenuElement[] path,
331 MenuSelectionManager manager)
333 MenuDragMouseEvent e = new MenuDragMouseEvent(ev.getComponent(),
334 ev.getID(), ev.getWhen(),
335 ev.getModifiers(), ev.getX(),
338 ev.isPopupTrigger(), path,
340 processMenuDragMouseEvent(e);
344 * Process key events forwarded from MenuSelectionManager.
346 * @param event event forwarded from MenuSelectionManager
347 * @param path path to the menu element from which event was generated
348 * @param manager MenuSelectionManager for the current menu hierarchy
350 public void processKeyEvent(KeyEvent event, MenuElement[] path,
351 MenuSelectionManager manager)
353 MenuKeyEvent e = new MenuKeyEvent(event.getComponent(), event.getID(),
354 event.getWhen(), event.getModifiers(),
355 event.getKeyCode(), event.getKeyChar(),
357 processMenuKeyEvent(e);
359 // Consume original key event, if the menu key event has been consumed.
365 * This method fires MenuDragMouseEvents to registered listeners.
366 * Different types of MenuDragMouseEvents are fired depending
367 * on the observed mouse event.
371 public void processMenuDragMouseEvent(MenuDragMouseEvent event)
373 switch (event.getID())
375 case MouseEvent.MOUSE_ENTERED:
377 fireMenuDragMouseEntered(event);
379 case MouseEvent.MOUSE_EXITED:
381 fireMenuDragMouseExited(event);
383 case MouseEvent.MOUSE_DRAGGED:
385 fireMenuDragMouseDragged(event);
387 case MouseEvent.MOUSE_RELEASED:
389 fireMenuDragMouseReleased(event);
395 * This method fires MenuKeyEvent to registered listeners.
396 * Different types of MenuKeyEvents are fired depending
397 * on the observed key event.
399 * @param event DOCUMENT ME!
401 public void processMenuKeyEvent(MenuKeyEvent event)
403 switch (event.getID())
405 case KeyEvent.KEY_PRESSED:
406 fireMenuKeyPressed(event);
408 case KeyEvent.KEY_RELEASED:
409 fireMenuKeyReleased(event);
411 case KeyEvent.KEY_TYPED:
412 fireMenuKeyTyped(event);
420 * Fires MenuDragMouseEvent to all of the menuItem's MouseInputListeners.
422 * @param event The event signifying that mouse entered menuItem while it was dragged
424 protected void fireMenuDragMouseEntered(MenuDragMouseEvent event)
426 EventListener[] ll = listenerList.getListeners(MenuDragMouseListener.class);
428 for (int i = 0; i < ll.length; i++)
429 ((MenuDragMouseListener) ll[i]).menuDragMouseEntered(event);
433 * Fires MenuDragMouseEvent to all of the menuItem's MouseInputListeners.
435 * @param event The event signifying that mouse has exited menu item, while it was dragged
437 protected void fireMenuDragMouseExited(MenuDragMouseEvent event)
439 EventListener[] ll = listenerList.getListeners(MenuDragMouseListener.class);
441 for (int i = 0; i < ll.length; i++)
442 ((MenuDragMouseListener) ll[i]).menuDragMouseExited(event);
446 * Fires MenuDragMouseEvent to all of the menuItem's MouseInputListeners.
448 * @param event The event signifying that mouse is being dragged over the menuItem
450 protected void fireMenuDragMouseDragged(MenuDragMouseEvent event)
452 EventListener[] ll = listenerList.getListeners(MenuDragMouseListener.class);
454 for (int i = 0; i < ll.length; i++)
455 ((MenuDragMouseListener) ll[i]).menuDragMouseDragged(event);
459 * This method fires a MenuDragMouseEvent to all the MenuItem's MouseInputListeners.
461 * @param event The event signifying that mouse was released while it was dragged over the menuItem
463 protected void fireMenuDragMouseReleased(MenuDragMouseEvent event)
465 EventListener[] ll = listenerList.getListeners(MenuDragMouseListener.class);
467 for (int i = 0; i < ll.length; i++)
468 ((MenuDragMouseListener) ll[i]).menuDragMouseReleased(event);
472 * This method fires a MenuKeyEvent to all the MenuItem's MenuKeyListeners.
474 * @param event The event signifying that key associated with this menu was pressed
476 protected void fireMenuKeyPressed(MenuKeyEvent event)
478 EventListener[] ll = listenerList.getListeners(MenuKeyListener.class);
480 for (int i = 0; i < ll.length; i++)
481 ((MenuKeyListener) ll[i]).menuKeyPressed(event);
485 * This method fires a MenuKeyEvent to all the MenuItem's MenuKeyListeners.
487 * @param event The event signifying that key associated with this menu was released
489 protected void fireMenuKeyReleased(MenuKeyEvent event)
491 EventListener[] ll = listenerList.getListeners(MenuKeyListener.class);
493 for (int i = 0; i < ll.length; i++)
494 ((MenuKeyListener) ll[i]).menuKeyTyped(event);
498 * This method fires a MenuKeyEvent to all the MenuItem's MenuKeyListeners.
500 * @param event The event signifying that key associated with this menu was typed.
501 * The key is typed when it was pressed and then released
503 protected void fireMenuKeyTyped(MenuKeyEvent event)
505 EventListener[] ll = listenerList.getListeners(MenuKeyListener.class);
507 for (int i = 0; i < ll.length; i++)
508 ((MenuKeyListener) ll[i]).menuKeyTyped(event);
512 * Method of the MenuElement interface.
513 * This method is invoked by MenuSelectionManager when selection of
514 * this menu item has changed. If this menu item was selected then
515 * arm it's model, and disarm the model otherwise. The menu item
516 * is considered to be selected, and thus highlighted when its model
519 * @param changed indicates selection status of this menu item. If changed is
520 * true then menu item is selected and deselected otherwise.
522 public void menuSelectionChanged(boolean changed)
524 Component parent = this.getParent();
527 model.setArmed(true);
529 if (parent != null && parent instanceof JPopupMenu)
530 ((JPopupMenu) parent).setSelected(this);
534 model.setArmed(false);
536 if (parent != null && parent instanceof JPopupMenu)
537 ((JPopupMenu) parent).getSelectionModel().clearSelection();
542 * Method of the MenuElement interface.
544 * @return $MenuElement[]$ Returns array of sub-components for this menu
545 * item. By default menuItem doesn't have any subcomponents and so
546 * empty array is returned instead.
548 public MenuElement[] getSubElements()
550 return new MenuElement[0];
554 * Returns reference to the component that will paint this menu item.
556 * @return $Component$ Component that will paint this menu item.
557 * Simply returns reference to this menu item.
559 public Component getComponent()
565 * Adds a MenuDragMouseListener to this menu item. When mouse
566 * is dragged over the menu item the MenuDragMouseEvents will be
567 * fired, and these listeners will be called.
569 * @param listener The new listener to add
571 public void addMenuDragMouseListener(MenuDragMouseListener listener)
573 listenerList.add(MenuDragMouseListener.class, listener);
577 * Removes a MenuDragMouseListener from the menuItem's listener list.
579 * @param listener The listener to remove
581 public void removeMenuDragMouseListener(MenuDragMouseListener listener)
583 listenerList.remove(MenuDragMouseListener.class, listener);
587 * Returns all added MenuDragMouseListener objects.
589 * @return an array of listeners
593 public MenuDragMouseListener[] getMenuDragMouseListeners()
595 return (MenuDragMouseListener[]) listenerList.getListeners(MenuDragMouseListener.class);
599 * Adds an MenuKeyListener to this menu item. This listener will be
600 * invoked when MenuKeyEvents will be fired by this menu item.
602 * @param listener The new listener to add
604 public void addMenuKeyListener(MenuKeyListener listener)
606 listenerList.add(MenuKeyListener.class, listener);
610 * Removes an MenuKeyListener from the menuItem's listener list.
612 * @param listener The listener to remove
614 public void removeMenuKeyListener(MenuKeyListener listener)
616 listenerList.remove(MenuKeyListener.class, listener);
620 * Returns all added MenuKeyListener objects.
622 * @return an array of listeners
626 public MenuKeyListener[] getMenuKeyListeners()
628 return (MenuKeyListener[]) listenerList.getListeners(MenuKeyListener.class);
632 * Returns a string describing the attributes for the <code>JMenuItem</code>
633 * component, for use in debugging. The return value is guaranteed to be
634 * non-<code>null</code>, but the format of the string may vary between
637 * @return A string describing the attributes of the <code>JMenuItem</code>.
639 protected String paramString()
641 // calling super seems to be sufficient here...
642 return super.paramString();
646 * Returns the object that provides accessibility features for this
647 * <code>JMenuItem</code> component.
649 * @return The accessible context (an instance of
650 * {@link AccessibleJMenuItem}).
652 public AccessibleContext getAccessibleContext()
654 if (accessibleContext == null)
656 AccessibleJMenuItem ctx = new AccessibleJMenuItem();
657 addChangeListener(ctx);
658 accessibleContext = ctx;
661 return accessibleContext;
665 * Provides the accessibility features for the <code>JMenuItem</code>
668 * @see JMenuItem#getAccessibleContext()
670 protected class AccessibleJMenuItem extends AccessibleAbstractButton
671 implements ChangeListener
673 private static final long serialVersionUID = 6748924232082076534L;
675 private boolean armed;
676 private boolean focusOwner;
677 private boolean pressed;
678 private boolean selected;
681 * Creates a new <code>AccessibleJMenuItem</code> instance.
683 AccessibleJMenuItem()
689 * Receives notification when the menu item's state changes and fires
690 * appropriate property change events to registered listeners.
692 * @param event the change event
694 public void stateChanged(ChangeEvent event)
696 // This is fired in all cases.
697 firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
698 Boolean.FALSE, Boolean.TRUE);
700 ButtonModel model = getModel();
702 // Handle the armed property.
708 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
709 AccessibleState.ARMED, null);
717 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
718 null, AccessibleState.ARMED);
722 // Handle the pressed property.
723 if (model.isPressed())
728 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
729 AccessibleState.PRESSED, null);
737 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
738 null, AccessibleState.PRESSED);
742 // Handle the selected property.
743 if (model.isSelected())
748 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
749 AccessibleState.SELECTED, null);
757 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
758 null, AccessibleState.SELECTED);
762 // Handle the focusOwner property.
768 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
769 AccessibleState.FOCUSED, null);
777 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
778 null, AccessibleState.FOCUSED);
784 * Returns the accessible role for the <code>JMenuItem</code> component.
786 * @return {@link AccessibleRole#MENU_ITEM}.
788 public AccessibleRole getAccessibleRole()
790 return AccessibleRole.MENU_ITEM;
795 * Returns <code>true</code> if the component is guaranteed to be painted
796 * on top of others. This returns false by default and is overridden by
797 * components like JMenuItem, JPopupMenu and JToolTip to return true for
800 * @return <code>true</code> if the component is guaranteed to be painted
805 return SwingUtilities.getAncestorOfClass(JInternalFrame.class, this)