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