jdk/src/share/classes/java/awt/Menu.java
author anthony
Tue, 05 May 2009 17:56:31 +0400
changeset 3444 18840bd1c784
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6818787: It is possible to reposition the security icon too far from the border of the window on X11 Summary: The constraints for the position of the icon are moved to the shared code Reviewed-by: art, dcherepanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1995-2006 Sun Microsystems, Inc.  All Rights Reserved.
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.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.peer.MenuPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.event.KeyEvent;
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
 * A <code>Menu</code> object is a pull-down menu component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * that is deployed from a menu bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * A menu can optionally be a <i>tear-off</i> menu. A tear-off menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * can be opened and dragged away from its parent menu bar or menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * It remains on the screen after the mouse button has been released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The mechanism for tearing off a menu is platform dependent, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the look and feel of the tear-off menu is determined by its peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * On platforms that do not support tear-off menus, the tear-off
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * property is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Each item in a menu must belong to the <code>MenuItem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * class. It can be an instance of <code>MenuItem</code>, a submenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * (an instance of <code>Menu</code>), or a check box (an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <code>CheckboxMenuItem</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @see     java.awt.MenuItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @see     java.awt.CheckboxMenuItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class Menu extends MenuItem implements MenuContainer, Accessible {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * A vector of the items that will be part of the Menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @see #countItems()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    Vector              items = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * This field indicates whether the menu has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * tear of property or not.  It will be set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <code>true</code> if the menu has the tear off
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * property and it will be set to <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * if it does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * A torn off menu can be deleted by a user when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * it is no longer needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @see #isTearOff()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    boolean             tearOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * This field will be set to <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * if the Menu in question is actually a help
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * menu.  Otherwise it will be set to <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    boolean             isHelpMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final String base = "menu";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static int nameCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     private static final long serialVersionUID = -8809584163345499784L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Constructs a new menu with an empty label. This menu is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * a tear-off menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @since      JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public Menu() throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this("", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Constructs a new menu with the specified label. This menu is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * a tear-off menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param       label the menu's label in the menu bar, or in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *                   another menu of which this menu is a submenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public Menu(String label) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this(label, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Constructs a new menu with the specified label,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * indicating whether the menu can be torn off.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Tear-off functionality may not be supported by all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * implementations of AWT.  If a particular implementation doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * support tear-off menus, this value is silently ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param       label the menu's label in the menu bar, or in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *                   another menu of which this menu is a submenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param       tearOff   if <code>true</code>, the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *                   is a tear-off menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @since       JDK1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public Menu(String label, boolean tearOff) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        super(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.tearOff = tearOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Construct a name for this MenuComponent.  Called by getName() when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * the name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    String constructComponentName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        synchronized (Menu.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return base + nameCounter++;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Creates the menu's peer.  The peer allows us to modify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * appearance of the menu without changing its functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public void addNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (peer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                peer = Toolkit.getDefaultToolkit().createMenu(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            int nitems = getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            for (int i = 0 ; i < nitems ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                MenuItem mi = getItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                mi.parent = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                mi.addNotify();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Removes the menu's peer.  The peer allows us to modify the appearance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * of the menu without changing its functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public void removeNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            int nitems = getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            for (int i = 0 ; i < nitems ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                getItem(i).removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            super.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Indicates whether this menu is a tear-off menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Tear-off functionality may not be supported by all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * implementations of AWT.  If a particular implementation doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * support tear-off menus, this value is silently ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return      <code>true</code> if this is a tear-off menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *                         <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public boolean isTearOff() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return tearOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
      * Get the number of items in this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
      * @return     the number of items in this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
      * @since      JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public int getItemCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return countItems();
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
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * replaced by <code>getItemCount()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public int countItems() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return countItemsImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * This is called by the native code, so client code can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * be called on the toolkit thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    final int countItemsImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return items.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Gets the item located at the specified index of this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param     index the position of the item to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @return    the item located at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public MenuItem getItem(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return getItemImpl(index);
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
     * This is called by the native code, so client code can't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * be called on the toolkit thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    final MenuItem getItemImpl(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return (MenuItem)items.elementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Adds the specified menu item to this menu. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * menu item has been part of another menu, removes it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * from that menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param       mi   the menu item to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @return      the menu item added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @see         java.awt.Menu#insert(java.lang.String, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @see         java.awt.Menu#insert(java.awt.MenuItem, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public MenuItem add(MenuItem mi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (mi.parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                mi.parent.remove(mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            items.addElement(mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            mi.parent = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            MenuPeer peer = (MenuPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                mi.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                peer.addItem(mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return mi;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Adds an item with the specified label to this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param       label   the text on the item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @see         java.awt.Menu#insert(java.lang.String, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @see         java.awt.Menu#insert(java.awt.MenuItem, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public void add(String label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        add(new MenuItem(label));
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
     * Inserts a menu item into this menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * at the specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param         menuitem  the menu item to be inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param         index     the position at which the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *                          item should be inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @see           java.awt.Menu#add(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @see           java.awt.Menu#add(java.awt.MenuItem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @exception     IllegalArgumentException if the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *                    <code>index</code> is less than zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @since         JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public void insert(MenuItem menuitem, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            int nitems = getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            Vector tempItems = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            /* Remove the item at index, nitems-index times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
               storing them in a temporary vector in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
               order they appear on the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            for (int i = index ; i < nitems; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                tempItems.addElement(getItem(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            add(menuitem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            /* Add the removed items back to the menu, they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
               already in the correct order in the temp vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            for (int i = 0; i < tempItems.size()  ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                add((MenuItem)tempItems.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
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
     * Inserts a menu item with the specified label into this menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * at the specified position.  This is a convenience method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <code>insert(menuItem, index)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param       label the text on the item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param       index the position at which the menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *                      should be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @see         java.awt.Menu#add(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @see         java.awt.Menu#add(java.awt.MenuItem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @exception     IllegalArgumentException if the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *                    <code>index</code> is less than zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public void insert(String label, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        insert(new MenuItem(label), index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Adds a separator line, or a hypen, to the menu at the current position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @see         java.awt.Menu#insertSeparator(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public void addSeparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        add("-");
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
     * Inserts a separator at the specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param       index the position at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *                       menu separator should be inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @exception   IllegalArgumentException if the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *                       <code>index</code> is less than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @see         java.awt.Menu#addSeparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public void insertSeparator(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            int nitems = getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            Vector tempItems = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            /* Remove the item at index, nitems-index times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
               storing them in a temporary vector in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
               order they appear on the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            for (int i = index ; i < nitems; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                tempItems.addElement(getItem(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            /* Add the removed items back to the menu, they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
               already in the correct order in the temp vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            for (int i = 0; i < tempItems.size()  ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                add((MenuItem)tempItems.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Removes the menu item at the specified index from this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @param       index the position of the item to be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public void remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            MenuItem mi = getItem(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            items.removeElementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            MenuPeer peer = (MenuPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                mi.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                mi.parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                peer.delItem(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Removes the specified menu item from this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @param  item the item to be removed from the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *         If <code>item</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *         or is not in this menu, this method does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *         nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public void remove(MenuComponent item) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            int index = items.indexOf(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Removes all items from this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @since       JDK1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public void removeAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            int nitems = getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            for (int i = nitems-1 ; i >= 0 ; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
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
     * Post an ActionEvent to the target of the MenuPeer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * associated with the specified keyboard event (on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * keydown).  Returns true if there is an associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * keyboard event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    boolean handleShortcut(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        int nitems = getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        for (int i = 0 ; i < nitems ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            MenuItem mi = getItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (mi.handleShortcut(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    MenuItem getShortcutMenuItem(MenuShortcut s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        int nitems = getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        for (int i = 0 ; i < nitems ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            MenuItem mi = getItem(i).getShortcutMenuItem(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            if (mi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                return mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    synchronized Enumeration shortcuts() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        Vector shortcuts = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        int nitems = getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        for (int i = 0 ; i < nitems ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            MenuItem mi = getItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (mi instanceof Menu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                Enumeration e = ((Menu)mi).shortcuts();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    shortcuts.addElement(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                MenuShortcut ms = mi.getShortcut();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                if (ms != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    shortcuts.addElement(ms);
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
        return shortcuts.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    void deleteShortcut(MenuShortcut s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        int nitems = getItemCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        for (int i = 0 ; i < nitems ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            getItem(i).deleteShortcut(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    /* Serialization support.  A MenuContainer is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * restoring the parent fields of its children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * The menu serialized Data Version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    private int menuSerializedDataVersion = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Writes default serializable fields to stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param s the <code>ObjectOutputStream</code> to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @see #readObject(ObjectInputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
      throws java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
      s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Reads the <code>ObjectInputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Unrecognized keys or values will be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @param s the <code>ObjectInputStream</code> to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @exception HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *   <code>GraphicsEnvironment.isHeadless</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *   <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @see #writeObject(ObjectOutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
      throws IOException, ClassNotFoundException, HeadlessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
      // HeadlessException will be thrown from MenuComponent's readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
      s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
      for(int i = 0; i < items.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        MenuItem item = (MenuItem)items.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        item.parent = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * Returns a string representing the state of this <code>Menu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * This method is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * implementations. The returned string may be empty but may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @return the parameter string of this menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    public String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        String str = ",tearOff=" + tearOff+",isHelpMenu=" + isHelpMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        return super.paramString() + str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Initialize JNI field and method IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Gets the AccessibleContext associated with this Menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * For menus, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * AccessibleAWTMenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * A new AccessibleAWTMenu instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @return an AccessibleAWTMenu that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *         AccessibleContext of this Menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            accessibleContext = new AccessibleAWTMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * Defined in MenuComponent. Overridden here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    int getAccessibleChildIndex(MenuComponent child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        return items.indexOf(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * Inner class of Menu used to provide default support for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * accessibility.  This class is not meant to be used directly by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * application developers, but is instead meant only to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * subclassed by menu component developers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * <code>Menu</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * Java Accessibility API appropriate to menu user-interface elements.
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
    protected class AccessibleAWTMenu extends AccessibleAWTMenuItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         * JDK 1.3 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        private static final long serialVersionUID = 5228160894980069094L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            return AccessibleRole.MENU;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    } // class AccessibleAWTMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
}