jdk/src/share/classes/java/awt/MenuItem.java
author tbell
Wed, 07 Oct 2009 14:15:01 -0700
changeset 3965 63aae8ce7f47
parent 2473 3f4bbd3be2f1
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2473
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 1964
diff changeset
     2
 * Copyright 1995-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.peer.MenuItemPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.EventListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * All items in a menu must belong to the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <code>MenuItem</code>, or one of its subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The default <code>MenuItem</code> object embodies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * a simple labeled menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This picture of a menu bar shows five menu items:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <IMG SRC="doc-files/MenuBar-1.gif" alt="The following text describes this graphic."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * ALIGN=CENTER HSPACE=10 VSPACE=7>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <br CLEAR=LEFT>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The first two items are simple menu items, labeled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <code>"Basic"</code> and <code>"Simple"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Following these two items is a separator, which is itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * a menu item, created with the label <code>"-"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Next is an instance of <code>CheckboxMenuItem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * labeled <code>"Check"</code>. The final menu item is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * submenu labeled <code>"More&nbsp;Examples"</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * and this submenu is an instance of <code>Menu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * When a menu item is selected, AWT sends an action event to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * the menu item. Since the event is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * instance of <code>ActionEvent</code>, the <code>processEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * method examines the event and passes it along to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <code>processActionEvent</code>. The latter method redirects the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * event to any <code>ActionListener</code> objects that have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * registered an interest in action events generated by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Note that the subclass <code>Menu</code> overrides this behavior and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * does not send any event to the frame until one of its subitems is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @author Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
public class MenuItem extends MenuComponent implements Accessible {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * A value to indicate whether a menu item is enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * or not.  If it is enabled, <code>enabled</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * be set to true.  Else <code>enabled</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * be set to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @see #isEnabled()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @see #setEnabled(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    boolean enabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <code>label</code> is the label of a menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * It can be any string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @see #getLabel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @see #setLabel(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    String label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * This field indicates the command tha has been issued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * by a  particular menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * By default the <code>actionCommand</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * is the label of the menu item, unless it has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * set using setActionCommand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see #setActionCommand(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see #getActionCommand()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    String actionCommand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * The eventMask is ONLY set by subclasses via enableEvents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The mask should NOT be set when listeners are registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * so that we can distinguish the difference between when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * listeners request events and subclasses request them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    long eventMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    transient ActionListener actionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * A sequence of key stokes that ia associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * a menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Note :in 1.1.2 you must use setActionCommand()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * on a menu item in order for its shortcut to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @see #getShortcut()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see #setShortcut(MenuShortcut)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see #deleteShortcut()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private MenuShortcut shortcut = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static final String base = "menuitem";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static int nameCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private static final long serialVersionUID = -21757335363267194L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Constructs a new MenuItem with an empty label and no keyboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * shortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @since    JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public MenuItem() throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this("", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Constructs a new MenuItem with the specified label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * and no keyboard shortcut. Note that use of "-" in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * a label is reserved to indicate a separator between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * menu items. By default, all menu items except for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * separators are enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param       label the label for this menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @since       JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public MenuItem(String label) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        this(label, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Create a menu item with an associated keyboard shortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Note that use of "-" in a label is reserved to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * a separator between menu items. By default, all menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * items except for separators are enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param       label the label for this menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param       s the instance of <code>MenuShortcut</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *                       associated with this menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public MenuItem(String label, MenuShortcut s) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        this.label = label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        this.shortcut = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Construct a name for this MenuComponent.  Called by getName() when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    String constructComponentName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        synchronized (MenuItem.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return base + nameCounter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Creates the menu item's peer.  The peer allows us to modify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * appearance of the menu item without changing its functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public void addNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (peer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                peer = Toolkit.getDefaultToolkit().createMenuItem(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Gets the label for this menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @return  the label of this menu item, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                       if this menu item has no label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @see     java.awt.MenuItem#setLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public String getLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Sets the label for this menu item to the specified label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param     label   the new label, or <code>null</code> for no label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @see       java.awt.MenuItem#getLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @since     JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public synchronized void setLabel(String label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        this.label = label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        MenuItemPeer peer = (MenuItemPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            peer.setLabel(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Checks whether this menu item is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @see        java.awt.MenuItem#setEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @since      JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Sets whether or not this menu item can be chosen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param      b  if <code>true</code>, enables this menu item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *                       if <code>false</code>, disables it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @see        java.awt.MenuItem#isEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @since      JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public synchronized void setEnabled(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        enable(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * replaced by <code>setEnabled(boolean)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public synchronized void enable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        enabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        MenuItemPeer peer = (MenuItemPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (peer != null) {
1964
934568dfe859 6749920: Cleanup AWT peer interfaces
rkennke
parents: 715
diff changeset
   271
            peer.setEnabled(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * replaced by <code>setEnabled(boolean)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public void enable(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            disable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * replaced by <code>setEnabled(boolean)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public synchronized void disable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        enabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        MenuItemPeer peer = (MenuItemPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (peer != null) {
1964
934568dfe859 6749920: Cleanup AWT peer interfaces
rkennke
parents: 715
diff changeset
   297
            peer.setEnabled(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Get the <code>MenuShortcut</code> object associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * menu item,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @return      the menu shortcut associated with this menu item,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *                   or <code>null</code> if none has been specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @see         java.awt.MenuItem#setShortcut
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public MenuShortcut getShortcut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return shortcut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Set the <code>MenuShortcut</code> object associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * menu item. If a menu shortcut is already associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * this menu item, it is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @param       s  the menu shortcut to associate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *                           with this menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @see         java.awt.MenuItem#getShortcut
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public void setShortcut(MenuShortcut s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        shortcut = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        MenuItemPeer peer = (MenuItemPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            peer.setLabel(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Delete any <code>MenuShortcut</code> object associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * with this menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @since      JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public void deleteShortcut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        shortcut = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        MenuItemPeer peer = (MenuItemPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            peer.setLabel(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Delete a matching MenuShortcut associated with this MenuItem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Used when iterating Menus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    void deleteShortcut(MenuShortcut s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (s.equals(shortcut)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            shortcut = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            MenuItemPeer peer = (MenuItemPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                peer.setLabel(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * The main goal of this method is to post an appropriate event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * to the event queue when menu shortcut is pressed. However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * in subclasses this method may do more than just posting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * an event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    void doMenuEvent(long when, int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        Toolkit.getEventQueue().postEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                            getActionCommand(), when, modifiers));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Returns true if the item and all its ancestors are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * enabled, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private final boolean isItemEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // Fix For 6185151: Menu shortcuts of all menuitems within a menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // should be disabled when the menu itself is disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (!isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        MenuContainer container = getParent_NoClientCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (!(container instanceof Menu)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            Menu menu = (Menu)container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (!menu.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            container = menu.getParent_NoClientCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        } while (container != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Post an ActionEvent to the target (on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * keydown) and the item is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Returns true if there is an associated shortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    boolean handleShortcut(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        MenuShortcut s = new MenuShortcut(e.getKeyCode(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                             (e.getModifiers() & InputEvent.SHIFT_MASK) > 0);
2473
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 1964
diff changeset
   401
        MenuShortcut sE = new MenuShortcut(e.getExtendedKeyCode(),
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 1964
diff changeset
   402
                             (e.getModifiers() & InputEvent.SHIFT_MASK) > 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        // Fix For 6185151: Menu shortcuts of all menuitems within a menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // should be disabled when the menu itself is disabled
2473
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 1964
diff changeset
   405
        if ((s.equals(shortcut) || sE.equals(shortcut)) && isItemEnabled()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            // MenuShortcut match -- issue an event on keydown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (e.getID() == KeyEvent.KEY_PRESSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                doMenuEvent(e.getWhen(), e.getModifiers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                // silently eat key release.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    MenuItem getShortcutMenuItem(MenuShortcut s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return (s.equals(shortcut)) ? this : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Enables event delivery to this menu item for events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * to be defined by the specified event mask parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Since event types are automatically enabled when a listener for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * that type is added to the menu item, this method only needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * to be invoked by subclasses of <code>MenuItem</code> which desire to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * have the specified event types delivered to <code>processEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * regardless of whether a listener is registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @param       eventsToEnable the event mask defining the event types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @see         java.awt.MenuItem#processEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @see         java.awt.MenuItem#disableEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @see         java.awt.Component#enableEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    protected final void enableEvents(long eventsToEnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        eventMask |= eventsToEnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        newEventsOnly = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Disables event delivery to this menu item for events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * defined by the specified event mask parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param       eventsToDisable the event mask defining the event types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @see         java.awt.MenuItem#processEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @see         java.awt.MenuItem#enableEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @see         java.awt.Component#disableEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    protected final void disableEvents(long eventsToDisable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        eventMask &= ~eventsToDisable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Sets the command name of the action event that is fired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * by this menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * By default, the action command is set to the label of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * the menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @param       command   the action command to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *                                for this menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @see         java.awt.MenuItem#getActionCommand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public void setActionCommand(String command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        actionCommand = command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Gets the command name of the action event that is fired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * by this menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @see         java.awt.MenuItem#setActionCommand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public String getActionCommand() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        return getActionCommandImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    // This is final so it can be called on the Toolkit thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    final String getActionCommandImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        return (actionCommand == null? label : actionCommand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Adds the specified action listener to receive action events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * from this menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * If l is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param      l the action listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @see        #removeActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @see        #getActionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @see        java.awt.event.ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @see        java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @since      JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    public synchronized void addActionListener(ActionListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        actionListener = AWTEventMulticaster.add(actionListener, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        newEventsOnly = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Removes the specified action listener so it no longer receives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * action events from this menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * If l is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @param      l the action listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @see        #addActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @see        #getActionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @see        java.awt.event.ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @see        java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @since      JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public synchronized void removeActionListener(ActionListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        actionListener = AWTEventMulticaster.remove(actionListener, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Returns an array of all the action listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * registered on this menu item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @return all of this menu item's <code>ActionListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *         or an empty array if no action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *         listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @see        #addActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @see        #removeActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @see        java.awt.event.ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @see        java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    public synchronized ActionListener[] getActionListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        return (ActionListener[])(getListeners(ActionListener.class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Returns an array of all the objects currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * as <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * upon this <code>MenuItem</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * <code><em>Foo</em>Listener</code>s are registered using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * with a class literal, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * For example, you can query a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * <code>MenuItem</code> <code>m</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * for its action listeners with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * <pre>ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * If no such listeners exist, this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @param listenerType the type of listeners requested; this parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *          should specify an interface that descends from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *          <code><em>Foo</em>Listener</code>s on this menu item,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @exception ClassCastException if <code>listenerType</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *          doesn't specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @see #getActionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        EventListener l = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        if  (listenerType == ActionListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            l = actionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        return AWTEventMulticaster.getListeners(l, listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * Processes events on this menu item. If the event is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * instance of <code>ActionEvent</code>, it invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * <code>processActionEvent</code>, another method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * defined by <code>MenuItem</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * Currently, menu items only support action events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * <p>Note that if the event parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * the behavior is unspecified and may result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @param       e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @see         java.awt.MenuItem#processActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    protected void processEvent(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (e instanceof ActionEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            processActionEvent((ActionEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    // REMIND: remove when filtering is done at lower level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    boolean eventEnabled(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (e.id == ActionEvent.ACTION_PERFORMED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                actionListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return super.eventEnabled(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Processes action events occurring on this menu item,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * by dispatching them to any registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * <code>ActionListener</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * This method is not called unless action events are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * enabled for this component. Action events are enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * when one of the following occurs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * <p><ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <li>An <code>ActionListener</code> object is registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * via <code>addActionListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * <li>Action events are enabled via <code>enableEvents</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * <p>Note that if the event parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * the behavior is unspecified and may result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @param       e the action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @see         java.awt.event.ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @see         java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @see         java.awt.MenuItem#enableEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    protected void processActionEvent(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        ActionListener listener = actionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            listener.actionPerformed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * Returns a string representing the state of this <code>MenuItem</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * This method is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * implementations. The returned string may be empty but may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @return the parameter string of this menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    public String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        String str = ",label=" + label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if (shortcut != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            str += ",shortcut=" + shortcut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return super.paramString() + str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    /* Serialization support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * Menu item serialized data version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    private int menuItemSerializedDataVersion = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * Writes default serializable fields to stream.  Writes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * a list of serializable <code>ActionListeners</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * as optional data. The non-serializable listeners are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * detected and no attempt is made to serialize them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @param s the <code>ObjectOutputStream</code> to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @serialData <code>null</code> terminated sequence of 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *   or more pairs; the pair consists of a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *   and an <code>Object</code>; the <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *   indicates the type of object and is one of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *   <code>actionListenerK</code> indicating an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *     <code>ActionListener</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @see #readObject(ObjectInputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
      throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
      s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
      AWTEventMulticaster.save(s, actionListenerK, actionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
      s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * Reads the <code>ObjectInputStream</code> and if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * isn't <code>null</code> adds a listener to receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * action events fired by the <code>Menu</code> Item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * Unrecognized keys or values will be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @param s the <code>ObjectInputStream</code> to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @exception HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *   <code>GraphicsEnvironment.isHeadless</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *   <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * @see #removeActionListener(ActionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @see #addActionListener(ActionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * @see #writeObject(ObjectOutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
      throws ClassNotFoundException, IOException, HeadlessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
      // HeadlessException will be thrown from MenuComponent's readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
      s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
      Object keyOrNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
      while(null != (keyOrNull = s.readObject())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        String key = ((String)keyOrNull).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if (actionListenerK == key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
          addActionListener((ActionListener)(s.readObject()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        else // skip value for unrecognized key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
          s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * Initialize JNI field and method IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * Gets the AccessibleContext associated with this MenuItem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * For menu items, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * AccessibleAWTMenuItem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * A new AccessibleAWTMenuItem instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @return an AccessibleAWTMenuItem that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *         AccessibleContext of this MenuItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            accessibleContext = new AccessibleAWTMenuItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * Inner class of MenuItem used to provide default support for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * accessibility.  This class is not meant to be used directly by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * application developers, but is instead meant only to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * subclassed by menu component developers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * <code>MenuItem</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * Java Accessibility API appropriate to menu item user-interface elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    protected class AccessibleAWTMenuItem extends AccessibleAWTMenuComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        implements AccessibleAction, AccessibleValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
         * JDK 1.3 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        private static final long serialVersionUID = -217847831945965825L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         * Get the accessible name of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
         * @return the localized name of the object -- can be null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
         * object does not have a name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        public String getAccessibleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            if (accessibleName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                return accessibleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                if (getLabel() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    return super.getAccessibleName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    return getLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            return AccessibleRole.MENU_ITEM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
         * Get the AccessibleAction associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
         * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
         * AccessibleAction interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        public AccessibleAction getAccessibleAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
         * Get the AccessibleValue associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
         * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         * AccessibleValue interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        public AccessibleValue getAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
         * Returns the number of Actions available in this object.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
         * default behavior of a menu item is to have one action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         * @return 1, the number of Actions in this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        public int getAccessibleActionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
         * Return a description of the specified action of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
         * @param i zero-based index of the actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        public String getAccessibleActionDescription(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                // [[[PENDING:  WDW -- need to provide a localized string]]]
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   852
                return "click";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
         * Perform the specified Action on the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
         * @param i zero-based index of actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
         * @return true if the action was performed; otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        public boolean doAccessibleAction(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                // Simulate a button click
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                Toolkit.getEventQueue().postEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                        new ActionEvent(MenuItem.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                                        ActionEvent.ACTION_PERFORMED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                                        MenuItem.this.getActionCommand(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                                        EventQueue.getMostRecentEventTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                                        0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
         * Get the value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
         * @return An Integer of 0 if this isn't selected or an Integer of 1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
         * this is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
         * @see javax.swing.AbstractButton#isSelected()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        public Number getCurrentAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            return Integer.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         * Set the value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
         * @return True if the value was set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        public boolean setCurrentAccessibleValue(Number n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
         * Get the minimum value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
         * @return An Integer of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        public Number getMinimumAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            return Integer.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
         * Get the maximum value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
         * @return An Integer of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        public Number getMaximumAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            return Integer.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    } // class AccessibleAWTMenuItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
}