0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing;
0N/A
0N/Aimport java.util.EventListener;
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.awt.image.*;
0N/A
0N/Aimport java.beans.PropertyChangeEvent;
0N/Aimport java.beans.PropertyChangeListener;
0N/A
0N/Aimport java.io.Serializable;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.IOException;
0N/A
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.swing.plaf.basic.*;
0N/Aimport javax.swing.event.*;
0N/Aimport javax.accessibility.*;
0N/A
0N/A/**
0N/A * An implementation of an item in a menu. A menu item is essentially a button
0N/A * sitting in a list. When the user selects the "button", the action
0N/A * associated with the menu item is performed. A <code>JMenuItem</code>
0N/A * contained in a <code>JPopupMenu</code> performs exactly that function.
0N/A * <p>
0N/A * Menu items can be configured, and to some degree controlled, by
0N/A * <code><a href="Action.html">Action</a></code>s. Using an
0N/A * <code>Action</code> with a menu item has many benefits beyond directly
0N/A * configuring a menu item. Refer to <a href="Action.html#buttonActions">
0N/A * Swing Components Supporting <code>Action</code></a> for more
0N/A * details, and you can find more information in <a
0N/A * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
0N/A * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
0N/A * <p>
0N/A * For further documentation and for examples, see
0N/A * <a
0N/A href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>
0N/A * in <em>The Java Tutorial.</em>
0N/A * <p>
0N/A * <strong>Warning:</strong> Swing is not thread safe. For more
0N/A * information see <a
0N/A * href="package-summary.html#threading">Swing's Threading
0N/A * Policy</a>.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @beaninfo
0N/A * attribute: isContainer false
0N/A * description: An item which can be selected in a menu.
0N/A *
0N/A * @author Georges Saab
0N/A * @author David Karlton
0N/A * @see JPopupMenu
0N/A * @see JMenu
0N/A * @see JCheckBoxMenuItem
0N/A * @see JRadioButtonMenuItem
0N/A */
0N/Apublic class JMenuItem extends AbstractButton implements Accessible,MenuElement {
0N/A
0N/A /**
0N/A * @see #getUIClassID
0N/A * @see #readObject
0N/A */
0N/A private static final String uiClassID = "MenuItemUI";
0N/A
0N/A /* diagnostic aids -- should be false for production builds. */
0N/A private static final boolean TRACE = false; // trace creates and disposes
0N/A private static final boolean VERBOSE = false; // show reuse hits/misses
0N/A private static final boolean DEBUG = false; // show bad params, misc.
0N/A
0N/A private boolean isMouseDragged = false;
0N/A
0N/A /**
0N/A * Creates a <code>JMenuItem</code> with no set text or icon.
0N/A */
0N/A public JMenuItem() {
0N/A this(null, (Icon)null);
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>JMenuItem</code> with the specified icon.
0N/A *
0N/A * @param icon the icon of the <code>JMenuItem</code>
0N/A */
0N/A public JMenuItem(Icon icon) {
0N/A this(null, icon);
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>JMenuItem</code> with the specified text.
0N/A *
0N/A * @param text the text of the <code>JMenuItem</code>
0N/A */
0N/A public JMenuItem(String text) {
0N/A this(text, (Icon)null);
0N/A }
0N/A
0N/A /**
0N/A * Creates a menu item whose properties are taken from the
0N/A * specified <code>Action</code>.
0N/A *
0N/A * @param a the action of the <code>JMenuItem</code>
0N/A * @since 1.3
0N/A */
0N/A public JMenuItem(Action a) {
0N/A this();
0N/A setAction(a);
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>JMenuItem</code> with the specified text and icon.
0N/A *
0N/A * @param text the text of the <code>JMenuItem</code>
0N/A * @param icon the icon of the <code>JMenuItem</code>
0N/A */
0N/A public JMenuItem(String text, Icon icon) {
0N/A setModel(new DefaultButtonModel());
0N/A init(text, icon);
0N/A initFocusability();
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>JMenuItem</code> with the specified text and
0N/A * keyboard mnemonic.
0N/A *
0N/A * @param text the text of the <code>JMenuItem</code>
0N/A * @param mnemonic the keyboard mnemonic for the <code>JMenuItem</code>
0N/A */
0N/A public JMenuItem(String text, int mnemonic) {
0N/A setModel(new DefaultButtonModel());
0N/A init(text, null);
0N/A setMnemonic(mnemonic);
0N/A initFocusability();
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public void setModel(ButtonModel newModel) {
0N/A super.setModel(newModel);
0N/A if(newModel instanceof DefaultButtonModel) {
0N/A ((DefaultButtonModel)newModel).setMenuItem(true);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Inititalizes the focusability of the the <code>JMenuItem</code>.
0N/A * <code>JMenuItem</code>'s are focusable, but subclasses may
0N/A * want to be, this provides them the opportunity to override this
0N/A * and invoke something else, or nothing at all. Refer to
0N/A * {@link javax.swing.JMenu#initFocusability} for the motivation of
0N/A * this.
0N/A */
0N/A void initFocusability() {
0N/A setFocusable(false);
0N/A }
0N/A
0N/A /**
0N/A * Initializes the menu item with the specified text and icon.
0N/A *
0N/A * @param text the text of the <code>JMenuItem</code>
0N/A * @param icon the icon of the <code>JMenuItem</code>
0N/A */
0N/A protected void init(String text, Icon icon) {
0N/A if(text != null) {
0N/A setText(text);
0N/A }
0N/A
0N/A if(icon != null) {
0N/A setIcon(icon);
0N/A }
0N/A
0N/A // Listen for Focus events
0N/A addFocusListener(new MenuItemFocusListener());
0N/A setUIProperty("borderPainted", Boolean.FALSE);
0N/A setFocusPainted(false);
0N/A setHorizontalTextPosition(JButton.TRAILING);
0N/A setHorizontalAlignment(JButton.LEADING);
0N/A updateUI();
0N/A }
0N/A
0N/A private static class MenuItemFocusListener implements FocusListener,
0N/A Serializable {
0N/A public void focusGained(FocusEvent event) {}
0N/A public void focusLost(FocusEvent event) {
0N/A // When focus is lost, repaint if
0N/A // the focus information is painted
0N/A JMenuItem mi = (JMenuItem)event.getSource();
0N/A if(mi.isFocusPainted()) {
0N/A mi.repaint();
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the look and feel object that renders this component.
0N/A *
0N/A * @param ui the <code>JMenuItemUI</code> L&F object
0N/A * @see UIDefaults#getUI
0N/A * @beaninfo
0N/A * bound: true
0N/A * hidden: true
0N/A * attribute: visualUpdate true
0N/A * description: The UI object that implements the Component's LookAndFeel.
0N/A */
0N/A public void setUI(MenuItemUI ui) {
0N/A super.setUI(ui);
0N/A }
0N/A
0N/A /**
0N/A * Resets the UI property with a value from the current look and feel.
0N/A *
0N/A * @see JComponent#updateUI
0N/A */
0N/A public void updateUI() {
0N/A setUI((MenuItemUI)UIManager.getUI(this));
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the suffix used to construct the name of the L&F class used to
0N/A * render this component.
0N/A *
0N/A * @return the string "MenuItemUI"
0N/A * @see JComponent#getUIClassID
0N/A * @see UIDefaults#getUI
0N/A */
0N/A public String getUIClassID() {
0N/A return uiClassID;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Identifies the menu item as "armed". If the mouse button is
0N/A * released while it is over this item, the menu's action event
0N/A * will fire. If the mouse button is released elsewhere, the
0N/A * event will not fire and the menu item will be disarmed.
0N/A *
0N/A * @param b true to arm the menu item so it can be selected
0N/A * @beaninfo
0N/A * description: Mouse release will fire an action event
0N/A * hidden: true
0N/A */
0N/A public void setArmed(boolean b) {
625N/A ButtonModel model = getModel();
0N/A
0N/A boolean oldValue = model.isArmed();
0N/A if(model.isArmed() != b) {
0N/A model.setArmed(b);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns whether the menu item is "armed".
0N/A *
0N/A * @return true if the menu item is armed, and it can be selected
0N/A * @see #setArmed
0N/A */
0N/A public boolean isArmed() {
625N/A ButtonModel model = getModel();
0N/A return model.isArmed();
0N/A }
0N/A
0N/A /**
0N/A * Enables or disables the menu item.
0N/A *
0N/A * @param b true to enable the item
0N/A * @beaninfo
0N/A * description: Does the component react to user interaction
0N/A * bound: true
0N/A * preferred: true
0N/A */
0N/A public void setEnabled(boolean b) {
0N/A // Make sure we aren't armed!
0N/A if (!b && !UIManager.getBoolean("MenuItem.disabledAreNavigable")) {
0N/A setArmed(false);
0N/A }
0N/A super.setEnabled(b);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns true since <code>Menu</code>s, by definition,
0N/A * should always be on top of all other windows. If the menu is
0N/A * in an internal frame false is returned due to the rollover effect
0N/A * for windows laf where the menu is not always on top.
0N/A */
0N/A // package private
0N/A boolean alwaysOnTop() {
0N/A // Fix for bug #4482165
0N/A if (SwingUtilities.getAncestorOfClass(JInternalFrame.class, this) !=
0N/A null) {
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A
0N/A /* The keystroke which acts as the menu item's accelerator
0N/A */
0N/A private KeyStroke accelerator;
0N/A
0N/A /**
0N/A * Sets the key combination which invokes the menu item's
0N/A * action listeners without navigating the menu hierarchy. It is the
0N/A * UI's responsibility to install the correct action. Note that
0N/A * when the keyboard accelerator is typed, it will work whether or
0N/A * not the menu is currently displayed.
0N/A *
0N/A * @param keyStroke the <code>KeyStroke</code> which will
0N/A * serve as an accelerator
0N/A * @beaninfo
0N/A * description: The keystroke combination which will invoke the
0N/A * JMenuItem's actionlisteners without navigating the
0N/A * menu hierarchy
0N/A * bound: true
0N/A * preferred: true
0N/A */
0N/A public void setAccelerator(KeyStroke keyStroke) {
0N/A KeyStroke oldAccelerator = accelerator;
0N/A this.accelerator = keyStroke;
0N/A repaint();
0N/A revalidate();
0N/A firePropertyChange("accelerator", oldAccelerator, accelerator);
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>KeyStroke</code> which serves as an accelerator
0N/A * for the menu item.
0N/A * @return a <code>KeyStroke</code> object identifying the
0N/A * accelerator key
0N/A */
0N/A public KeyStroke getAccelerator() {
0N/A return this.accelerator;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A *
0N/A * @since 1.3
0N/A */
0N/A protected void configurePropertiesFromAction(Action a) {
0N/A super.configurePropertiesFromAction(a);
0N/A configureAcceleratorFromAction(a);
0N/A }
0N/A
0N/A void setIconFromAction(Action a) {
0N/A Icon icon = null;
0N/A if (a != null) {
0N/A icon = (Icon)a.getValue(Action.SMALL_ICON);
0N/A }
0N/A setIcon(icon);
0N/A }
0N/A
0N/A void largeIconChanged(Action a) {
0N/A }
0N/A
0N/A void smallIconChanged(Action a) {
0N/A setIconFromAction(a);
0N/A }
0N/A
0N/A void configureAcceleratorFromAction(Action a) {
0N/A KeyStroke ks = (a==null) ? null :
0N/A (KeyStroke)a.getValue(Action.ACCELERATOR_KEY);
0N/A setAccelerator(ks);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.6
0N/A */
0N/A protected void actionPropertyChanged(Action action, String propertyName) {
0N/A if (propertyName == Action.ACCELERATOR_KEY) {
0N/A configureAcceleratorFromAction(action);
0N/A }
0N/A else {
0N/A super.actionPropertyChanged(action, propertyName);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Processes a mouse event forwarded from the
0N/A * <code>MenuSelectionManager</code> and changes the menu
0N/A * selection, if necessary, by using the
0N/A * <code>MenuSelectionManager</code>'s API.
0N/A * <p>
0N/A * Note: you do not have to forward the event to sub-components.
0N/A * This is done automatically by the <code>MenuSelectionManager</code>.
0N/A *
0N/A * @param e a <code>MouseEvent</code>
0N/A * @param path the <code>MenuElement</code> path array
0N/A * @param manager the <code>MenuSelectionManager</code>
0N/A */
0N/A public void processMouseEvent(MouseEvent e,MenuElement path[],MenuSelectionManager manager) {
0N/A processMenuDragMouseEvent(
0N/A new MenuDragMouseEvent(e.getComponent(), e.getID(),
0N/A e.getWhen(),
0N/A e.getModifiers(), e.getX(), e.getY(),
0N/A e.getXOnScreen(), e.getYOnScreen(),
0N/A e.getClickCount(), e.isPopupTrigger(),
0N/A path, manager));
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Processes a key event forwarded from the
0N/A * <code>MenuSelectionManager</code> and changes the menu selection,
0N/A * if necessary, by using <code>MenuSelectionManager</code>'s API.
0N/A * <p>
0N/A * Note: you do not have to forward the event to sub-components.
0N/A * This is done automatically by the <code>MenuSelectionManager</code>.
0N/A *
0N/A * @param e a <code>KeyEvent</code>
0N/A * @param path the <code>MenuElement</code> path array
0N/A * @param manager the <code>MenuSelectionManager</code>
0N/A */
0N/A public void processKeyEvent(KeyEvent e,MenuElement path[],MenuSelectionManager manager) {
0N/A if (DEBUG) {
0N/A System.out.println("in JMenuItem.processKeyEvent/3 for " + getText() +
0N/A " " + KeyStroke.getKeyStrokeForEvent(e));
0N/A }
0N/A MenuKeyEvent mke = new MenuKeyEvent(e.getComponent(), e.getID(),
0N/A e.getWhen(), e.getModifiers(),
0N/A e.getKeyCode(), e.getKeyChar(),
0N/A path, manager);
0N/A processMenuKeyEvent(mke);
0N/A
0N/A if (mke.isConsumed()) {
0N/A e.consume();
0N/A }
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Handles mouse drag in a menu.
0N/A *
0N/A * @param e a <code>MenuDragMouseEvent</code> object
0N/A */
0N/A public void processMenuDragMouseEvent(MenuDragMouseEvent e) {
0N/A switch (e.getID()) {
0N/A case MouseEvent.MOUSE_ENTERED:
0N/A isMouseDragged = false; fireMenuDragMouseEntered(e); break;
0N/A case MouseEvent.MOUSE_EXITED:
0N/A isMouseDragged = false; fireMenuDragMouseExited(e); break;
0N/A case MouseEvent.MOUSE_DRAGGED:
0N/A isMouseDragged = true; fireMenuDragMouseDragged(e); break;
0N/A case MouseEvent.MOUSE_RELEASED:
0N/A if(isMouseDragged) fireMenuDragMouseReleased(e); break;
0N/A default:
0N/A break;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Handles a keystroke in a menu.
0N/A *
0N/A * @param e a <code>MenuKeyEvent</code> object
0N/A */
0N/A public void processMenuKeyEvent(MenuKeyEvent e) {
0N/A if (DEBUG) {
0N/A System.out.println("in JMenuItem.processMenuKeyEvent for " + getText()+
0N/A " " + KeyStroke.getKeyStrokeForEvent(e));
0N/A }
0N/A switch (e.getID()) {
0N/A case KeyEvent.KEY_PRESSED:
0N/A fireMenuKeyPressed(e); break;
0N/A case KeyEvent.KEY_RELEASED:
0N/A fireMenuKeyReleased(e); break;
0N/A case KeyEvent.KEY_TYPED:
0N/A fireMenuKeyTyped(e); break;
0N/A default:
0N/A break;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type.
0N/A *
0N/A * @param event a <code>MenuMouseDragEvent</code>
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireMenuDragMouseEntered(MenuDragMouseEvent event) {
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==MenuDragMouseListener.class) {
0N/A // Lazily create the event:
0N/A ((MenuDragMouseListener)listeners[i+1]).menuDragMouseEntered(event);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type.
0N/A *
0N/A * @param event a <code>MenuDragMouseEvent</code>
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireMenuDragMouseExited(MenuDragMouseEvent event) {
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==MenuDragMouseListener.class) {
0N/A // Lazily create the event:
0N/A ((MenuDragMouseListener)listeners[i+1]).menuDragMouseExited(event);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type.
0N/A *
0N/A * @param event a <code>MenuDragMouseEvent</code>
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireMenuDragMouseDragged(MenuDragMouseEvent event) {
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==MenuDragMouseListener.class) {
0N/A // Lazily create the event:
0N/A ((MenuDragMouseListener)listeners[i+1]).menuDragMouseDragged(event);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type.
0N/A *
0N/A * @param event a <code>MenuDragMouseEvent</code>
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireMenuDragMouseReleased(MenuDragMouseEvent event) {
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==MenuDragMouseListener.class) {
0N/A // Lazily create the event:
0N/A ((MenuDragMouseListener)listeners[i+1]).menuDragMouseReleased(event);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type.
0N/A *
0N/A * @param event a <code>MenuKeyEvent</code>
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireMenuKeyPressed(MenuKeyEvent event) {
0N/A if (DEBUG) {
0N/A System.out.println("in JMenuItem.fireMenuKeyPressed for " + getText()+
0N/A " " + KeyStroke.getKeyStrokeForEvent(event));
0N/A }
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==MenuKeyListener.class) {
0N/A // Lazily create the event:
0N/A ((MenuKeyListener)listeners[i+1]).menuKeyPressed(event);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type.
0N/A *
0N/A * @param event a <code>MenuKeyEvent</code>
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireMenuKeyReleased(MenuKeyEvent event) {
0N/A if (DEBUG) {
0N/A System.out.println("in JMenuItem.fireMenuKeyReleased for " + getText()+
0N/A " " + KeyStroke.getKeyStrokeForEvent(event));
0N/A }
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==MenuKeyListener.class) {
0N/A // Lazily create the event:
0N/A ((MenuKeyListener)listeners[i+1]).menuKeyReleased(event);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type.
0N/A *
0N/A * @param event a <code>MenuKeyEvent</code>
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireMenuKeyTyped(MenuKeyEvent event) {
0N/A if (DEBUG) {
0N/A System.out.println("in JMenuItem.fireMenuKeyTyped for " + getText()+
0N/A " " + KeyStroke.getKeyStrokeForEvent(event));
0N/A }
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==MenuKeyListener.class) {
0N/A // Lazily create the event:
0N/A ((MenuKeyListener)listeners[i+1]).menuKeyTyped(event);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Called by the <code>MenuSelectionManager</code> when the
0N/A * <code>MenuElement</code> is selected or unselected.
0N/A *
0N/A * @param isIncluded true if this menu item is on the part of the menu
0N/A * path that changed, false if this menu is part of the
0N/A * a menu path that changed, but this particular part of
0N/A * that path is still the same
0N/A * @see MenuSelectionManager#setSelectedPath(MenuElement[])
0N/A */
0N/A public void menuSelectionChanged(boolean isIncluded) {
0N/A setArmed(isIncluded);
0N/A }
0N/A
0N/A /**
0N/A * This method returns an array containing the sub-menu
0N/A * components for this menu component.
0N/A *
0N/A * @return an array of <code>MenuElement</code>s
0N/A */
0N/A public MenuElement[] getSubElements() {
0N/A return new MenuElement[0];
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>java.awt.Component</code> used to paint
0N/A * this object. The returned component will be used to convert
0N/A * events and detect if an event is inside a menu component.
0N/A *
0N/A * @return the <code>Component</code> that paints this menu item
0N/A */
0N/A public Component getComponent() {
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Adds a <code>MenuDragMouseListener</code> to the menu item.
0N/A *
0N/A * @param l the <code>MenuDragMouseListener</code> to be added
0N/A */
0N/A public void addMenuDragMouseListener(MenuDragMouseListener l) {
0N/A listenerList.add(MenuDragMouseListener.class, l);
0N/A }
0N/A
0N/A /**
0N/A * Removes a <code>MenuDragMouseListener</code> from the menu item.
0N/A *
0N/A * @param l the <code>MenuDragMouseListener</code> to be removed
0N/A */
0N/A public void removeMenuDragMouseListener(MenuDragMouseListener l) {
0N/A listenerList.remove(MenuDragMouseListener.class, l);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the <code>MenuDragMouseListener</code>s added
0N/A * to this JMenuItem with addMenuDragMouseListener().
0N/A *
0N/A * @return all of the <code>MenuDragMouseListener</code>s added or an empty
0N/A * array if no listeners have been added
0N/A * @since 1.4
0N/A */
0N/A public MenuDragMouseListener[] getMenuDragMouseListeners() {
625N/A return listenerList.getListeners(MenuDragMouseListener.class);
0N/A }
0N/A
0N/A /**
0N/A * Adds a <code>MenuKeyListener</code> to the menu item.
0N/A *
0N/A * @param l the <code>MenuKeyListener</code> to be added
0N/A */
0N/A public void addMenuKeyListener(MenuKeyListener l) {
0N/A listenerList.add(MenuKeyListener.class, l);
0N/A }
0N/A
0N/A /**
0N/A * Removes a <code>MenuKeyListener</code> from the menu item.
0N/A *
0N/A * @param l the <code>MenuKeyListener</code> to be removed
0N/A */
0N/A public void removeMenuKeyListener(MenuKeyListener l) {
0N/A listenerList.remove(MenuKeyListener.class, l);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the <code>MenuKeyListener</code>s added
0N/A * to this JMenuItem with addMenuKeyListener().
0N/A *
0N/A * @return all of the <code>MenuKeyListener</code>s added or an empty
0N/A * array if no listeners have been added
0N/A * @since 1.4
0N/A */
0N/A public MenuKeyListener[] getMenuKeyListeners() {
625N/A return listenerList.getListeners(MenuKeyListener.class);
0N/A }
0N/A
0N/A /**
0N/A * See JComponent.readObject() for information about serialization
0N/A * in Swing.
0N/A */
0N/A private void readObject(ObjectInputStream s)
0N/A throws IOException, ClassNotFoundException
0N/A {
0N/A s.defaultReadObject();
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A updateUI();
0N/A }
0N/A }
0N/A
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A byte count = JComponent.getWriteObjCounter(this);
0N/A JComponent.setWriteObjCounter(this, --count);
0N/A if (count == 0 && ui != null) {
0N/A ui.installUI(this);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string representation of this <code>JMenuItem</code>.
0N/A * This method is intended to be used only for debugging purposes,
0N/A * and the content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not
0N/A * be <code>null</code>.
0N/A *
0N/A * @return a string representation of this <code>JMenuItem</code>
0N/A */
0N/A protected String paramString() {
0N/A return super.paramString();
0N/A }
0N/A
0N/A/////////////////
0N/A// Accessibility support
0N/A////////////////
0N/A
0N/A /**
0N/A * Returns the <code>AccessibleContext</code> associated with this
0N/A * <code>JMenuItem</code>. For <code>JMenuItem</code>s,
0N/A * the <code>AccessibleContext</code> takes the form of an
0N/A * <code>AccessibleJMenuItem</code>.
0N/A * A new AccessibleJMenuItme instance is created if necessary.
0N/A *
0N/A * @return an <code>AccessibleJMenuItem</code> that serves as the
0N/A * <code>AccessibleContext</code> of this <code>JMenuItem</code>
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleJMenuItem();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>JMenuItem</code> class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to menu item user-interface
0N/A * elements.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected class AccessibleJMenuItem extends AccessibleAbstractButton implements ChangeListener {
0N/A
0N/A private boolean isArmed = false;
0N/A private boolean hasFocus = false;
0N/A private boolean isPressed = false;
0N/A private boolean isSelected = false;
0N/A
0N/A AccessibleJMenuItem() {
0N/A super();
0N/A JMenuItem.this.addChangeListener(this);
0N/A }
0N/A
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the
0N/A * object
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.MENU_ITEM;
0N/A }
0N/A
0N/A private void fireAccessibilityFocusedEvent(JMenuItem toCheck) {
0N/A MenuElement [] path =
0N/A MenuSelectionManager.defaultManager().getSelectedPath();
0N/A if (path.length > 0) {
0N/A Object menuItem = path[path.length - 1];
0N/A if (toCheck == menuItem) {
0N/A firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A null, AccessibleState.FOCUSED);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Supports the change listener interface and fires property changes.
0N/A */
0N/A public void stateChanged(ChangeEvent e) {
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
0N/A Boolean.valueOf(false), Boolean.valueOf(true));
0N/A if (JMenuItem.this.getModel().isArmed()) {
0N/A if (!isArmed) {
0N/A isArmed = true;
0N/A firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A null, AccessibleState.ARMED);
0N/A // Fix for 4848220 moved here to avoid major memory leak
0N/A // Here we will fire the event in case of JMenuItem
0N/A // See bug 4910323 for details [zav]
0N/A fireAccessibilityFocusedEvent(JMenuItem.this);
0N/A }
0N/A } else {
0N/A if (isArmed) {
0N/A isArmed = false;
0N/A firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A AccessibleState.ARMED, null);
0N/A }
0N/A }
0N/A if (JMenuItem.this.isFocusOwner()) {
0N/A if (!hasFocus) {
0N/A hasFocus = true;
0N/A firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A null, AccessibleState.FOCUSED);
0N/A }
0N/A } else {
0N/A if (hasFocus) {
0N/A hasFocus = false;
0N/A firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A AccessibleState.FOCUSED, null);
0N/A }
0N/A }
0N/A if (JMenuItem.this.getModel().isPressed()) {
0N/A if (!isPressed) {
0N/A isPressed = true;
0N/A firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A null, AccessibleState.PRESSED);
0N/A }
0N/A } else {
0N/A if (isPressed) {
0N/A isPressed = false;
0N/A firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A AccessibleState.PRESSED, null);
0N/A }
0N/A }
0N/A if (JMenuItem.this.getModel().isSelected()) {
0N/A if (!isSelected) {
0N/A isSelected = true;
0N/A firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A null, AccessibleState.CHECKED);
0N/A
0N/A // Fix for 4848220 moved here to avoid major memory leak
0N/A // Here we will fire the event in case of JMenu
0N/A // See bug 4910323 for details [zav]
0N/A fireAccessibilityFocusedEvent(JMenuItem.this);
0N/A }
0N/A } else {
0N/A if (isSelected) {
0N/A isSelected = false;
0N/A firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A AccessibleState.CHECKED, null);
0N/A }
0N/A }
0N/A
0N/A }
0N/A } // inner class AccessibleJMenuItem
0N/A
0N/A
0N/A}