MenuItem.java revision 1067
0N/A/*
1067N/A * Copyright 1995-2009 Sun Microsystems, Inc. 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
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun 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 *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/Apackage java.awt;
0N/A
0N/Aimport java.awt.peer.MenuItemPeer;
0N/Aimport java.awt.event.*;
0N/Aimport java.util.EventListener;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.IOException;
0N/Aimport javax.accessibility.*;
0N/A
0N/A
0N/A/**
0N/A * All items in a menu must belong to the class
0N/A * <code>MenuItem</code>, or one of its subclasses.
0N/A * <p>
0N/A * The default <code>MenuItem</code> object embodies
0N/A * a simple labeled menu item.
0N/A * <p>
0N/A * This picture of a menu bar shows five menu items:
0N/A * <IMG SRC="doc-files/MenuBar-1.gif" alt="The following text describes this graphic."
0N/A * ALIGN=CENTER HSPACE=10 VSPACE=7>
0N/A * <br CLEAR=LEFT>
0N/A * The first two items are simple menu items, labeled
0N/A * <code>"Basic"</code> and <code>"Simple"</code>.
0N/A * Following these two items is a separator, which is itself
0N/A * a menu item, created with the label <code>"-"</code>.
0N/A * Next is an instance of <code>CheckboxMenuItem</code>
0N/A * labeled <code>"Check"</code>. The final menu item is a
0N/A * submenu labeled <code>"More&nbsp;Examples"</code>,
0N/A * and this submenu is an instance of <code>Menu</code>.
0N/A * <p>
0N/A * When a menu item is selected, AWT sends an action event to
0N/A * the menu item. Since the event is an
0N/A * instance of <code>ActionEvent</code>, the <code>processEvent</code>
0N/A * method examines the event and passes it along to
0N/A * <code>processActionEvent</code>. The latter method redirects the
0N/A * event to any <code>ActionListener</code> objects that have
0N/A * registered an interest in action events generated by this
0N/A * menu item.
0N/A * <P>
0N/A * Note that the subclass <code>Menu</code> overrides this behavior and
0N/A * does not send any event to the frame until one of its subitems is
0N/A * selected.
0N/A *
0N/A * @author Sami Shaio
0N/A */
0N/Apublic class MenuItem extends MenuComponent implements Accessible {
0N/A
0N/A static {
0N/A /* ensure that the necessary native libraries are loaded */
0N/A Toolkit.loadLibraries();
0N/A if (!GraphicsEnvironment.isHeadless()) {
0N/A initIDs();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * A value to indicate whether a menu item is enabled
0N/A * or not. If it is enabled, <code>enabled</code> will
0N/A * be set to true. Else <code>enabled</code> will
0N/A * be set to false.
0N/A *
0N/A * @serial
0N/A * @see #isEnabled()
0N/A * @see #setEnabled(boolean)
0N/A */
0N/A boolean enabled = true;
0N/A
0N/A /**
0N/A * <code>label</code> is the label of a menu item.
0N/A * It can be any string.
0N/A *
0N/A * @serial
0N/A * @see #getLabel()
0N/A * @see #setLabel(String)
0N/A */
0N/A String label;
0N/A
0N/A /**
0N/A * This field indicates the command tha has been issued
0N/A * by a particular menu item.
0N/A * By default the <code>actionCommand</code>
0N/A * is the label of the menu item, unless it has been
0N/A * set using setActionCommand.
0N/A *
0N/A * @serial
0N/A * @see #setActionCommand(String)
0N/A * @see #getActionCommand()
0N/A */
0N/A String actionCommand;
0N/A
0N/A /**
0N/A * The eventMask is ONLY set by subclasses via enableEvents.
0N/A * The mask should NOT be set when listeners are registered
0N/A * so that we can distinguish the difference between when
0N/A * listeners request events and subclasses request them.
0N/A *
0N/A * @serial
0N/A */
0N/A long eventMask;
0N/A
0N/A transient ActionListener actionListener;
0N/A
0N/A /**
0N/A * A sequence of key stokes that ia associated with
0N/A * a menu item.
0N/A * Note :in 1.1.2 you must use setActionCommand()
0N/A * on a menu item in order for its shortcut to
0N/A * work.
0N/A *
0N/A * @serial
0N/A * @see #getShortcut()
0N/A * @see #setShortcut(MenuShortcut)
0N/A * @see #deleteShortcut()
0N/A */
0N/A private MenuShortcut shortcut = null;
0N/A
0N/A private static final String base = "menuitem";
0N/A private static int nameCounter = 0;
0N/A
0N/A /*
0N/A * JDK 1.1 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = -21757335363267194L;
0N/A
0N/A /**
0N/A * Constructs a new MenuItem with an empty label and no keyboard
0N/A * shortcut.
0N/A * @exception HeadlessException if GraphicsEnvironment.isHeadless()
0N/A * returns true.
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A * @since JDK1.1
0N/A */
0N/A public MenuItem() throws HeadlessException {
0N/A this("", null);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new MenuItem with the specified label
0N/A * and no keyboard shortcut. Note that use of "-" in
0N/A * a label is reserved to indicate a separator between
0N/A * menu items. By default, all menu items except for
0N/A * separators are enabled.
0N/A * @param label the label for this menu item.
0N/A * @exception HeadlessException if GraphicsEnvironment.isHeadless()
0N/A * returns true.
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A * @since JDK1.0
0N/A */
0N/A public MenuItem(String label) throws HeadlessException {
0N/A this(label, null);
0N/A }
0N/A
0N/A /**
0N/A * Create a menu item with an associated keyboard shortcut.
0N/A * Note that use of "-" in a label is reserved to indicate
0N/A * a separator between menu items. By default, all menu
0N/A * items except for separators are enabled.
0N/A * @param label the label for this menu item.
0N/A * @param s the instance of <code>MenuShortcut</code>
0N/A * associated with this menu item.
0N/A * @exception HeadlessException if GraphicsEnvironment.isHeadless()
0N/A * returns true.
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A * @since JDK1.1
0N/A */
0N/A public MenuItem(String label, MenuShortcut s) throws HeadlessException {
0N/A this.label = label;
0N/A this.shortcut = s;
0N/A }
0N/A
0N/A /**
0N/A * Construct a name for this MenuComponent. Called by getName() when
0N/A * the name is null.
0N/A */
0N/A String constructComponentName() {
0N/A synchronized (MenuItem.class) {
0N/A return base + nameCounter++;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates the menu item's peer. The peer allows us to modify the
0N/A * appearance of the menu item without changing its functionality.
0N/A */
0N/A public void addNotify() {
0N/A synchronized (getTreeLock()) {
0N/A if (peer == null)
0N/A peer = Toolkit.getDefaultToolkit().createMenuItem(this);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the label for this menu item.
0N/A * @return the label of this menu item, or <code>null</code>
0N/A if this menu item has no label.
0N/A * @see java.awt.MenuItem#setLabel
0N/A * @since JDK1.0
0N/A */
0N/A public String getLabel() {
0N/A return label;
0N/A }
0N/A
0N/A /**
0N/A * Sets the label for this menu item to the specified label.
0N/A * @param label the new label, or <code>null</code> for no label.
0N/A * @see java.awt.MenuItem#getLabel
0N/A * @since JDK1.0
0N/A */
0N/A public synchronized void setLabel(String label) {
0N/A this.label = label;
0N/A MenuItemPeer peer = (MenuItemPeer)this.peer;
0N/A if (peer != null) {
0N/A peer.setLabel(label);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Checks whether this menu item is enabled.
0N/A * @see java.awt.MenuItem#setEnabled
0N/A * @since JDK1.0
0N/A */
0N/A public boolean isEnabled() {
0N/A return enabled;
0N/A }
0N/A
0N/A /**
0N/A * Sets whether or not this menu item can be chosen.
0N/A * @param b if <code>true</code>, enables this menu item;
0N/A * if <code>false</code>, disables it.
0N/A * @see java.awt.MenuItem#isEnabled
0N/A * @since JDK1.1
0N/A */
0N/A public synchronized void setEnabled(boolean b) {
0N/A enable(b);
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>setEnabled(boolean)</code>.
0N/A */
0N/A @Deprecated
0N/A public synchronized void enable() {
0N/A enabled = true;
0N/A MenuItemPeer peer = (MenuItemPeer)this.peer;
0N/A if (peer != null) {
872N/A peer.setEnabled(true);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>setEnabled(boolean)</code>.
0N/A */
0N/A @Deprecated
0N/A public void enable(boolean b) {
0N/A if (b) {
0N/A enable();
0N/A } else {
0N/A disable();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * @deprecated As of JDK version 1.1,
0N/A * replaced by <code>setEnabled(boolean)</code>.
0N/A */
0N/A @Deprecated
0N/A public synchronized void disable() {
0N/A enabled = false;
0N/A MenuItemPeer peer = (MenuItemPeer)this.peer;
0N/A if (peer != null) {
872N/A peer.setEnabled(false);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the <code>MenuShortcut</code> object associated with this
0N/A * menu item,
0N/A * @return the menu shortcut associated with this menu item,
0N/A * or <code>null</code> if none has been specified.
0N/A * @see java.awt.MenuItem#setShortcut
0N/A * @since JDK1.1
0N/A */
0N/A public MenuShortcut getShortcut() {
0N/A return shortcut;
0N/A }
0N/A
0N/A /**
0N/A * Set the <code>MenuShortcut</code> object associated with this
0N/A * menu item. If a menu shortcut is already associated with
0N/A * this menu item, it is replaced.
0N/A * @param s the menu shortcut to associate
0N/A * with this menu item.
0N/A * @see java.awt.MenuItem#getShortcut
0N/A * @since JDK1.1
0N/A */
0N/A public void setShortcut(MenuShortcut s) {
0N/A shortcut = s;
0N/A MenuItemPeer peer = (MenuItemPeer)this.peer;
0N/A if (peer != null) {
0N/A peer.setLabel(label);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Delete any <code>MenuShortcut</code> object associated
0N/A * with this menu item.
0N/A * @since JDK1.1
0N/A */
0N/A public void deleteShortcut() {
0N/A shortcut = null;
0N/A MenuItemPeer peer = (MenuItemPeer)this.peer;
0N/A if (peer != null) {
0N/A peer.setLabel(label);
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Delete a matching MenuShortcut associated with this MenuItem.
0N/A * Used when iterating Menus.
0N/A */
0N/A void deleteShortcut(MenuShortcut s) {
0N/A if (s.equals(shortcut)) {
0N/A shortcut = null;
0N/A MenuItemPeer peer = (MenuItemPeer)this.peer;
0N/A if (peer != null) {
0N/A peer.setLabel(label);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * The main goal of this method is to post an appropriate event
0N/A * to the event queue when menu shortcut is pressed. However,
0N/A * in subclasses this method may do more than just posting
0N/A * an event.
0N/A */
0N/A void doMenuEvent(long when, int modifiers) {
0N/A Toolkit.getEventQueue().postEvent(
0N/A new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
0N/A getActionCommand(), when, modifiers));
0N/A }
0N/A
0N/A /*
0N/A * Returns true if the item and all its ancestors are
0N/A * enabled, false otherwise
0N/A */
0N/A private final boolean isItemEnabled() {
0N/A // Fix For 6185151: Menu shortcuts of all menuitems within a menu
0N/A // should be disabled when the menu itself is disabled
0N/A if (!isEnabled()) {
0N/A return false;
0N/A }
0N/A MenuContainer container = getParent_NoClientCode();
0N/A do {
0N/A if (!(container instanceof Menu)) {
0N/A return true;
0N/A }
0N/A Menu menu = (Menu)container;
0N/A if (!menu.isEnabled()) {
0N/A return false;
0N/A }
0N/A container = menu.getParent_NoClientCode();
0N/A } while (container != null);
0N/A return true;
0N/A }
0N/A
0N/A /*
0N/A * Post an ActionEvent to the target (on
0N/A * keydown) and the item is enabled.
0N/A * Returns true if there is an associated shortcut.
0N/A */
0N/A boolean handleShortcut(KeyEvent e) {
0N/A MenuShortcut s = new MenuShortcut(e.getKeyCode(),
0N/A (e.getModifiers() & InputEvent.SHIFT_MASK) > 0);
1067N/A MenuShortcut sE = new MenuShortcut(e.getExtendedKeyCode(),
1067N/A (e.getModifiers() & InputEvent.SHIFT_MASK) > 0);
0N/A // Fix For 6185151: Menu shortcuts of all menuitems within a menu
0N/A // should be disabled when the menu itself is disabled
1067N/A if ((s.equals(shortcut) || sE.equals(shortcut)) && isItemEnabled()) {
0N/A // MenuShortcut match -- issue an event on keydown.
0N/A if (e.getID() == KeyEvent.KEY_PRESSED) {
0N/A doMenuEvent(e.getWhen(), e.getModifiers());
0N/A } else {
0N/A // silently eat key release.
0N/A }
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A MenuItem getShortcutMenuItem(MenuShortcut s) {
0N/A return (s.equals(shortcut)) ? this : null;
0N/A }
0N/A
0N/A /**
0N/A * Enables event delivery to this menu item for events
0N/A * to be defined by the specified event mask parameter
0N/A * <p>
0N/A * Since event types are automatically enabled when a listener for
0N/A * that type is added to the menu item, this method only needs
0N/A * to be invoked by subclasses of <code>MenuItem</code> which desire to
0N/A * have the specified event types delivered to <code>processEvent</code>
0N/A * regardless of whether a listener is registered.
0N/A *
0N/A * @param eventsToEnable the event mask defining the event types
0N/A * @see java.awt.MenuItem#processEvent
0N/A * @see java.awt.MenuItem#disableEvents
0N/A * @see java.awt.Component#enableEvents
0N/A * @since JDK1.1
0N/A */
0N/A protected final void enableEvents(long eventsToEnable) {
0N/A eventMask |= eventsToEnable;
0N/A newEventsOnly = true;
0N/A }
0N/A
0N/A /**
0N/A * Disables event delivery to this menu item for events
0N/A * defined by the specified event mask parameter.
0N/A *
0N/A * @param eventsToDisable the event mask defining the event types
0N/A * @see java.awt.MenuItem#processEvent
0N/A * @see java.awt.MenuItem#enableEvents
0N/A * @see java.awt.Component#disableEvents
0N/A * @since JDK1.1
0N/A */
0N/A protected final void disableEvents(long eventsToDisable) {
0N/A eventMask &= ~eventsToDisable;
0N/A }
0N/A
0N/A /**
0N/A * Sets the command name of the action event that is fired
0N/A * by this menu item.
0N/A * <p>
0N/A * By default, the action command is set to the label of
0N/A * the menu item.
0N/A * @param command the action command to be set
0N/A * for this menu item.
0N/A * @see java.awt.MenuItem#getActionCommand
0N/A * @since JDK1.1
0N/A */
0N/A public void setActionCommand(String command) {
0N/A actionCommand = command;
0N/A }
0N/A
0N/A /**
0N/A * Gets the command name of the action event that is fired
0N/A * by this menu item.
0N/A * @see java.awt.MenuItem#setActionCommand
0N/A * @since JDK1.1
0N/A */
0N/A public String getActionCommand() {
0N/A return getActionCommandImpl();
0N/A }
0N/A
0N/A // This is final so it can be called on the Toolkit thread.
0N/A final String getActionCommandImpl() {
0N/A return (actionCommand == null? label : actionCommand);
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified action listener to receive action events
0N/A * from this menu item.
0N/A * If l is null, no exception is thrown and no action is performed.
0N/A * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
0N/A * >AWT Threading Issues</a> for details on AWT's threading model.
0N/A *
0N/A * @param l the action listener.
0N/A * @see #removeActionListener
0N/A * @see #getActionListeners
0N/A * @see java.awt.event.ActionEvent
0N/A * @see java.awt.event.ActionListener
0N/A * @since JDK1.1
0N/A */
0N/A public synchronized void addActionListener(ActionListener l) {
0N/A if (l == null) {
0N/A return;
0N/A }
0N/A actionListener = AWTEventMulticaster.add(actionListener, l);
0N/A newEventsOnly = true;
0N/A }
0N/A
0N/A /**
0N/A * Removes the specified action listener so it no longer receives
0N/A * action events from this menu item.
0N/A * If l is null, no exception is thrown and no action is performed.
0N/A * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
0N/A * >AWT Threading Issues</a> for details on AWT's threading model.
0N/A *
0N/A * @param l the action listener.
0N/A * @see #addActionListener
0N/A * @see #getActionListeners
0N/A * @see java.awt.event.ActionEvent
0N/A * @see java.awt.event.ActionListener
0N/A * @since JDK1.1
0N/A */
0N/A public synchronized void removeActionListener(ActionListener l) {
0N/A if (l == null) {
0N/A return;
0N/A }
0N/A actionListener = AWTEventMulticaster.remove(actionListener, l);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the action listeners
0N/A * registered on this menu item.
0N/A *
0N/A * @return all of this menu item's <code>ActionListener</code>s
0N/A * or an empty array if no action
0N/A * listeners are currently registered
0N/A *
0N/A * @see #addActionListener
0N/A * @see #removeActionListener
0N/A * @see java.awt.event.ActionEvent
0N/A * @see java.awt.event.ActionListener
0N/A * @since 1.4
0N/A */
0N/A public synchronized ActionListener[] getActionListeners() {
0N/A return (ActionListener[])(getListeners(ActionListener.class));
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the objects currently registered
0N/A * as <code><em>Foo</em>Listener</code>s
0N/A * upon this <code>MenuItem</code>.
0N/A * <code><em>Foo</em>Listener</code>s are registered using the
0N/A * <code>add<em>Foo</em>Listener</code> method.
0N/A *
0N/A * <p>
0N/A * You can specify the <code>listenerType</code> argument
0N/A * with a class literal, such as
0N/A * <code><em>Foo</em>Listener.class</code>.
0N/A * For example, you can query a
0N/A * <code>MenuItem</code> <code>m</code>
0N/A * for its action listeners with the following code:
0N/A *
0N/A * <pre>ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));</pre>
0N/A *
0N/A * If no such listeners exist, this method returns an empty array.
0N/A *
0N/A * @param listenerType the type of listeners requested; this parameter
0N/A * should specify an interface that descends from
0N/A * <code>java.util.EventListener</code>
0N/A * @return an array of all objects registered as
0N/A * <code><em>Foo</em>Listener</code>s on this menu item,
0N/A * or an empty array if no such
0N/A * listeners have been added
0N/A * @exception ClassCastException if <code>listenerType</code>
0N/A * doesn't specify a class or interface that implements
0N/A * <code>java.util.EventListener</code>
0N/A *
0N/A * @see #getActionListeners
0N/A * @since 1.3
0N/A */
0N/A public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
0N/A EventListener l = null;
0N/A if (listenerType == ActionListener.class) {
0N/A l = actionListener;
0N/A }
0N/A return AWTEventMulticaster.getListeners(l, listenerType);
0N/A }
0N/A
0N/A /**
0N/A * Processes events on this menu item. If the event is an
0N/A * instance of <code>ActionEvent</code>, it invokes
0N/A * <code>processActionEvent</code>, another method
0N/A * defined by <code>MenuItem</code>.
0N/A * <p>
0N/A * Currently, menu items only support action events.
0N/A * <p>Note that if the event parameter is <code>null</code>
0N/A * the behavior is unspecified and may result in an
0N/A * exception.
0N/A *
0N/A * @param e the event
0N/A * @see java.awt.MenuItem#processActionEvent
0N/A * @since JDK1.1
0N/A */
0N/A protected void processEvent(AWTEvent e) {
0N/A if (e instanceof ActionEvent) {
0N/A processActionEvent((ActionEvent)e);
0N/A }
0N/A }
0N/A
0N/A // REMIND: remove when filtering is done at lower level
0N/A boolean eventEnabled(AWTEvent e) {
0N/A if (e.id == ActionEvent.ACTION_PERFORMED) {
0N/A if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 ||
0N/A actionListener != null) {
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A return super.eventEnabled(e);
0N/A }
0N/A
0N/A /**
0N/A * Processes action events occurring on this menu item,
0N/A * by dispatching them to any registered
0N/A * <code>ActionListener</code> objects.
0N/A * This method is not called unless action events are
0N/A * enabled for this component. Action events are enabled
0N/A * when one of the following occurs:
0N/A * <p><ul>
0N/A * <li>An <code>ActionListener</code> object is registered
0N/A * via <code>addActionListener</code>.
0N/A * <li>Action events are enabled via <code>enableEvents</code>.
0N/A * </ul>
0N/A * <p>Note that if the event parameter is <code>null</code>
0N/A * the behavior is unspecified and may result in an
0N/A * exception.
0N/A *
0N/A * @param e the action event
0N/A * @see java.awt.event.ActionEvent
0N/A * @see java.awt.event.ActionListener
0N/A * @see java.awt.MenuItem#enableEvents
0N/A * @since JDK1.1
0N/A */
0N/A protected void processActionEvent(ActionEvent e) {
0N/A ActionListener listener = actionListener;
0N/A if (listener != null) {
0N/A listener.actionPerformed(e);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representing the state of this <code>MenuItem</code>.
0N/A * This method is intended to be used only for debugging purposes, and the
0N/A * content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not be
0N/A * <code>null</code>.
0N/A *
0N/A * @return the parameter string of this menu item
0N/A */
0N/A public String paramString() {
0N/A String str = ",label=" + label;
0N/A if (shortcut != null) {
0N/A str += ",shortcut=" + shortcut;
0N/A }
0N/A return super.paramString() + str;
0N/A }
0N/A
0N/A
0N/A /* Serialization support.
0N/A */
0N/A
0N/A /**
0N/A * Menu item serialized data version.
0N/A *
0N/A * @serial
0N/A */
0N/A private int menuItemSerializedDataVersion = 1;
0N/A
0N/A /**
0N/A * Writes default serializable fields to stream. Writes
0N/A * a list of serializable <code>ActionListeners</code>
0N/A * as optional data. The non-serializable listeners are
0N/A * detected and no attempt is made to serialize them.
0N/A *
0N/A * @param s the <code>ObjectOutputStream</code> to write
0N/A * @serialData <code>null</code> terminated sequence of 0
0N/A * or more pairs; the pair consists of a <code>String</code>
0N/A * and an <code>Object</code>; the <code>String</code>
0N/A * indicates the type of object and is one of the following:
0N/A * <code>actionListenerK</code> indicating an
0N/A * <code>ActionListener</code> object
0N/A *
0N/A * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
0N/A * @see #readObject(ObjectInputStream)
0N/A */
0N/A private void writeObject(ObjectOutputStream s)
0N/A throws IOException
0N/A {
0N/A s.defaultWriteObject();
0N/A
0N/A AWTEventMulticaster.save(s, actionListenerK, actionListener);
0N/A s.writeObject(null);
0N/A }
0N/A
0N/A /**
0N/A * Reads the <code>ObjectInputStream</code> and if it
0N/A * isn't <code>null</code> adds a listener to receive
0N/A * action events fired by the <code>Menu</code> Item.
0N/A * Unrecognized keys or values will be ignored.
0N/A *
0N/A * @param s the <code>ObjectInputStream</code> to read
0N/A * @exception HeadlessException if
0N/A * <code>GraphicsEnvironment.isHeadless</code> returns
0N/A * <code>true</code>
0N/A * @see #removeActionListener(ActionListener)
0N/A * @see #addActionListener(ActionListener)
0N/A * @see #writeObject(ObjectOutputStream)
0N/A */
0N/A private void readObject(ObjectInputStream s)
0N/A throws ClassNotFoundException, IOException, HeadlessException
0N/A {
0N/A // HeadlessException will be thrown from MenuComponent's readObject
0N/A s.defaultReadObject();
0N/A
0N/A Object keyOrNull;
0N/A while(null != (keyOrNull = s.readObject())) {
0N/A String key = ((String)keyOrNull).intern();
0N/A
0N/A if (actionListenerK == key)
0N/A addActionListener((ActionListener)(s.readObject()));
0N/A
0N/A else // skip value for unrecognized key
0N/A s.readObject();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Initialize JNI field and method IDs
0N/A */
0N/A private static native void initIDs();
0N/A
0N/A
0N/A/////////////////
0N/A// Accessibility support
0N/A////////////////
0N/A
0N/A /**
0N/A * Gets the AccessibleContext associated with this MenuItem.
0N/A * For menu items, the AccessibleContext takes the form of an
0N/A * AccessibleAWTMenuItem.
0N/A * A new AccessibleAWTMenuItem instance is created if necessary.
0N/A *
0N/A * @return an AccessibleAWTMenuItem that serves as the
0N/A * AccessibleContext of this MenuItem
0N/A * @since 1.3
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleAWTMenuItem();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * Inner class of MenuItem used to provide default support for
0N/A * accessibility. This class is not meant to be used directly by
0N/A * application developers, but is instead meant only to be
0N/A * subclassed by menu component developers.
0N/A * <p>
0N/A * This class implements accessibility support for the
0N/A * <code>MenuItem</code> class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to menu item user-interface elements.
0N/A * @since 1.3
0N/A */
0N/A protected class AccessibleAWTMenuItem extends AccessibleAWTMenuComponent
0N/A implements AccessibleAction, AccessibleValue
0N/A {
0N/A /*
0N/A * JDK 1.3 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = -217847831945965825L;
0N/A
0N/A /**
0N/A * Get the accessible name of this object.
0N/A *
0N/A * @return the localized name of the object -- can be null if this
0N/A * object does not have a name
0N/A */
0N/A public String getAccessibleName() {
0N/A if (accessibleName != null) {
0N/A return accessibleName;
0N/A } else {
0N/A if (getLabel() == null) {
0N/A return super.getAccessibleName();
0N/A } else {
0N/A return getLabel();
0N/A }
0N/A }
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 /**
0N/A * Get the AccessibleAction associated with this object. In the
0N/A * implementation of the Java Accessibility API for this class,
0N/A * return this object, which is responsible for implementing the
0N/A * AccessibleAction interface on behalf of itself.
0N/A *
0N/A * @return this object
0N/A */
0N/A public AccessibleAction getAccessibleAction() {
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleValue associated with this object. In the
0N/A * implementation of the Java Accessibility API for this class,
0N/A * return this object, which is responsible for implementing the
0N/A * AccessibleValue interface on behalf of itself.
0N/A *
0N/A * @return this object
0N/A */
0N/A public AccessibleValue getAccessibleValue() {
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of Actions available in this object. The
0N/A * default behavior of a menu item is to have one action.
0N/A *
0N/A * @return 1, the number of Actions in this object
0N/A */
0N/A public int getAccessibleActionCount() {
0N/A return 1;
0N/A }
0N/A
0N/A /**
0N/A * Return a description of the specified action of the object.
0N/A *
0N/A * @param i zero-based index of the actions
0N/A */
0N/A public String getAccessibleActionDescription(int i) {
0N/A if (i == 0) {
0N/A // [[[PENDING: WDW -- need to provide a localized string]]]
215N/A return "click";
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Perform the specified Action on the object
0N/A *
0N/A * @param i zero-based index of actions
0N/A * @return true if the action was performed; otherwise false.
0N/A */
0N/A public boolean doAccessibleAction(int i) {
0N/A if (i == 0) {
0N/A // Simulate a button click
0N/A Toolkit.getEventQueue().postEvent(
0N/A new ActionEvent(MenuItem.this,
0N/A ActionEvent.ACTION_PERFORMED,
0N/A MenuItem.this.getActionCommand(),
0N/A EventQueue.getMostRecentEventTime(),
0N/A 0));
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the value of this object as a Number.
0N/A *
0N/A * @return An Integer of 0 if this isn't selected or an Integer of 1 if
0N/A * this is selected.
0N/A * @see javax.swing.AbstractButton#isSelected()
0N/A */
0N/A public Number getCurrentAccessibleValue() {
0N/A return Integer.valueOf(0);
0N/A }
0N/A
0N/A /**
0N/A * Set the value of this object as a Number.
0N/A *
0N/A * @return True if the value was set.
0N/A */
0N/A public boolean setCurrentAccessibleValue(Number n) {
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Get the minimum value of this object as a Number.
0N/A *
0N/A * @return An Integer of 0.
0N/A */
0N/A public Number getMinimumAccessibleValue() {
0N/A return Integer.valueOf(0);
0N/A }
0N/A
0N/A /**
0N/A * Get the maximum value of this object as a Number.
0N/A *
0N/A * @return An Integer of 0.
0N/A */
0N/A public Number getMaximumAccessibleValue() {
0N/A return Integer.valueOf(0);
0N/A }
0N/A
0N/A } // class AccessibleAWTMenuItem
0N/A
0N/A}