jdk/src/share/classes/javax/swing/JMenu.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1301 15e81207e1f2
permissions -rw-r--r--
Initial load
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 1997-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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.AWTEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.ComponentOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.GraphicsConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.GraphicsDevice;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.GraphicsEnvironment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.Polygon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.swing.plaf.basic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * An implementation of a menu -- a popup window containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <code>JMenuItem</code>s that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * is displayed when the user selects an item on the <code>JMenuBar</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * In addition to <code>JMenuItem</code>s, a <code>JMenu</code> can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * also contain <code>JSeparator</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * In essence, a menu is a button with an associated <code>JPopupMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * When the "button" is pressed, the <code>JPopupMenu</code> appears. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * "button" is on the <code>JMenuBar</code>, the menu is a top-level window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * If the "button" is another menu item, then the <code>JPopupMenu</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * "pull-right" menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * Menus can be configured, and to some degree controlled, by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <code><a href="Action.html">Action</a></code>s.  Using an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <code>Action</code> with a menu has many benefits beyond directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * configuring a menu.  Refer to <a href="Action.html#buttonActions">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Swing Components Supporting <code>Action</code></a> for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * details, and you can find more information in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * For information and examples of using menus see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <a href="http://java.sun.com/doc/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * a section in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *   attribute: isContainer true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * description: A popup window containing menu items displayed in a menu bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * @author Georges Saab
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * @author David Karlton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * @see JMenuItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * @see JSeparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * @see JMenuBar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * @see JPopupMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
public class JMenu extends JMenuItem implements Accessible,MenuElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static final String uiClassID = "MenuUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * The popup menu portion of the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private JPopupMenu popupMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * The button's model listeners.  Default is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private ChangeListener menuChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Only one <code>MenuEvent</code> is needed for each menu since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * event's only state is the source property.  The source of events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * generated is always "this".  Default is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private MenuEvent menuEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /* Registry of listeners created for <code>Action-JMenuItem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * linkage.  This is needed so that references can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * be cleaned up at remove time to allow garbage collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Default is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static Hashtable listenerRegistry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Used by the look and feel (L&F) code to handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * implementation specific menu behaviors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private int delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
      * Location of the popup component. Location is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
      * if it was not customized by <code>setMenuLocation</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     private Point customMenuLocation = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /* Diagnostic aids -- should be false for production builds. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static final boolean TRACE =   false; // trace creates and disposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static final boolean VERBOSE = false; // show reuse hits/misses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private static final boolean DEBUG =   false;  // show bad params, misc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Constructs a new <code>JMenu</code> with no text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public JMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        this("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Constructs a new <code>JMenu</code> with the supplied string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * as its text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param s  the text for the menu label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public JMenu(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        super(s);
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
     * Constructs a menu whose properties are taken from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <code>Action</code> supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param a an <code>Action</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public JMenu(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        setAction(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Constructs a new <code>JMenu</code> with the supplied string as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * its text and specified as a tear-off menu or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param s the text for the menu label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param b can the menu be torn off (not yet implemented)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public JMenu(String s, boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        this(s);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Overriden to do nothing. We want JMenu to be focusable, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <code>JMenuItem</code> doesn't want to be, thus we override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * do nothing. We don't invoke <code>setFocusable(true)</code> after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * super's constructor has completed as this has the side effect that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <code>JMenu</code> will be considered traversable via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * keyboard, which we don't want. Making a Component traversable by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * the keyboard after invoking <code>setFocusable(true)</code> is OK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * as <code>setFocusable</code> is new API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * and is speced as such, but internally we don't want to use it like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * this else we change the keyboard traversability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    void initFocusability() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Resets the UI property with a value from the current look and feel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @see JComponent#updateUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        setUI((MenuItemUI)UIManager.getUI(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if ( popupMenu != null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
          {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            popupMenu.setUI((PopupMenuUI)UIManager.getUI(popupMenu));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
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
     * Returns the name of the L&F class that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @return the string "MenuUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    //    public void repaint(long tm, int x, int y, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    //        Thread.currentThread().dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    //        super.repaint(tm,x,y,width,height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    //    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Sets the data model for the "menu button" -- the label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * that the user clicks to open or close the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param newModel the <code>ButtonModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * description: The menu's model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *      expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *      hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public void setModel(ButtonModel newModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        ButtonModel oldModel = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        super.setModel(newModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (oldModel != null && menuChangeListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            oldModel.removeChangeListener(menuChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            menuChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        model = newModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (newModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            menuChangeListener = createMenuChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            newModel.addChangeListener(menuChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Returns true if the menu is currently selected (highlighted).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @return true if the menu is selected, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public boolean isSelected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return getModel().isSelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Sets the selection status of the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param b  true to select (highlight) the menu; false to de-select
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *          the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *      description: When the menu is selected, its popup child is shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *           expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *           hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public void setSelected(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        ButtonModel model = getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        boolean oldValue = model.isSelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // TIGER - 4840653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // Removed code which fired an AccessibleState.SELECTED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // PropertyChangeEvent since this resulted in two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // identical events being fired since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // AbstractButton.fireItemStateChanged also fires the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // same event. This caused screen readers to speak the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        // name of the item twice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (b != model.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            getModel().setSelected(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Returns true if the menu's popup window is visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @return true if the menu is visible, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public boolean isPopupMenuVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return popupMenu.isVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Sets the visibility of the menu's popup.  If the menu is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * not enabled, this method will have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param b  a boolean value -- true to make the menu visible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *           false to hide it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *      description: The popup menu's visibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *           expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *           hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public void setPopupMenuVisible(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            System.out.println("in JMenu.setPopupMenuVisible " + b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            // Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        boolean isVisible = isPopupMenuVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (b != isVisible && (isEnabled() || !b)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if ((b==true) && isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                // Set location of popupMenu (pulldown or pullright)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                Point p = getCustomMenuLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    p = getPopupMenuOrigin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                getPopupMenu().show(this, p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                getPopupMenu().setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Computes the origin for the <code>JMenu</code>'s popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * This method uses Look and Feel properties named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <code>Menu.menuPopupOffsetX</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <code>Menu.menuPopupOffsetY</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <code>Menu.submenuPopupOffsetX</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * <code>Menu.submenuPopupOffsetY</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * to adjust the exact location of popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @return a <code>Point</code> in the coordinate space of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *          menu which should be used as the origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *          of the <code>JMenu</code>'s popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    protected Point getPopupMenuOrigin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        int y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        JPopupMenu pm = getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        // Figure out the sizes needed to caclulate the menu position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        Dimension s = getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        Dimension pmSize = pm.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // For the first time the menu is popped up,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // the size has not yet been initiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (pmSize.width==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            pmSize = pm.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        Point position = getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        Toolkit toolkit = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        GraphicsConfiguration gc = getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        GraphicsEnvironment ge =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            GraphicsEnvironment.getLocalGraphicsEnvironment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        GraphicsDevice[] gd = ge.getScreenDevices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        for(int i = 0; i < gd.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if(gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                GraphicsConfiguration dgc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    gd[i].getDefaultConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if(dgc.getBounds().contains(position)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    gc = dgc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if (gc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            screenBounds = gc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            // take screen insets (e.g. taskbar) into account
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            Insets screenInsets = toolkit.getScreenInsets(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            screenBounds.width -=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                        Math.abs(screenInsets.left + screenInsets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            screenBounds.height -=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        Math.abs(screenInsets.top + screenInsets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            position.x -= Math.abs(screenInsets.left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            position.y -= Math.abs(screenInsets.top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        Container parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (parent instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            // We are a submenu (pull-right)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            int xOffset = UIManager.getInt("Menu.submenuPopupOffsetX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            int yOffset = UIManager.getInt("Menu.submenuPopupOffsetY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if( SwingUtilities.isLeftToRight(this) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                // First determine x:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                x = s.width + xOffset;   // Prefer placement to the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                if (position.x + x + pmSize.width >= screenBounds.width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                                                     + screenBounds.x &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    // popup doesn't fit - place it wherever there's more room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    screenBounds.width - s.width < 2*(position.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                                                    - screenBounds.x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    x = 0 - xOffset - pmSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                // First determine x:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                x = 0 - xOffset - pmSize.width; // Prefer placement to the left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                if (position.x + x < screenBounds.x &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    // popup doesn't fit - place it wherever there's more room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    screenBounds.width - s.width > 2*(position.x -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                                                    screenBounds.x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    x = s.width + xOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            // Then the y:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            y = yOffset;                     // Prefer dropping down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if (position.y + y + pmSize.height >= screenBounds.height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                                                  + screenBounds.y &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                // popup doesn't fit - place it wherever there's more room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                screenBounds.height - s.height < 2*(position.y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                                                  - screenBounds.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                y = s.height - yOffset - pmSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            // We are a toplevel menu (pull-down)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            int xOffset = UIManager.getInt("Menu.menuPopupOffsetX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            int yOffset = UIManager.getInt("Menu.menuPopupOffsetY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            if( SwingUtilities.isLeftToRight(this) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                // First determine the x:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                x = xOffset;                   // Extend to the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                if (position.x + x + pmSize.width >= screenBounds.width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                                     + screenBounds.x &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    // popup doesn't fit - place it wherever there's more room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    screenBounds.width - s.width < 2*(position.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                                                    - screenBounds.x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    x = s.width - xOffset - pmSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                // First determine the x:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                x = s.width - xOffset - pmSize.width; // Extend to the left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                if (position.x + x < screenBounds.x &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    // popup doesn't fit - place it wherever there's more room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    screenBounds.width - s.width > 2*(position.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                                    - screenBounds.x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    x = xOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            // Then the y:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            y = s.height + yOffset;    // Prefer dropping down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            if (position.y + y + pmSize.height >= screenBounds.height &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                // popup doesn't fit - place it wherever there's more room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                screenBounds.height - s.height < 2*(position.y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                                  - screenBounds.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                y = 0 - yOffset - pmSize.height;   // Otherwise drop 'up'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        return new Point(x,y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
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
     * Returns the suggested delay, in milliseconds, before submenus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * are popped up or down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * Each look and feel (L&F) may determine its own policy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * observing the <code>delay</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * In most cases, the delay is not observed for top level menus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * or while dragging.  The default for <code>delay</code> is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * This method is a property of the look and feel code and is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * to manage the idiosyncracies of the various UI implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @return the <code>delay</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    public int getDelay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        return delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Sets the suggested delay before the menu's <code>PopupMenu</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * is popped up or down.  Each look and feel (L&F) may determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * it's own policy for observing the delay property.  In most cases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * the delay is not observed for top level menus or while dragging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * This method is a property of the look and feel code and is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * to manage the idiosyncracies of the various UI implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @param       d the number of milliseconds to delay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @exception   IllegalArgumentException if <code>d</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *                       is less than 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *      description: The delay between menu selection and making the popup menu visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *           expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    public void setDelay(int d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if (d < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            throw new IllegalArgumentException("Delay must be a positive integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        delay = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * The window-closing listener for the popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @see WinListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    protected WinListener popupListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    private void ensurePopupMenuCreated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (popupMenu == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            final JMenu thisMenu = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            this.popupMenu = new JPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            popupMenu.setInvoker(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            popupListener = createWinListener(popupMenu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Return the customized location of the popup component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    private Point getCustomMenuLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        return customMenuLocation;
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
     * Sets the location of the popup component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @param x the x coordinate of the popup's new position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @param y the y coordinate of the popup's new position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    public void setMenuLocation(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        customMenuLocation = new Point(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            popupMenu.setLocation(x, y);
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
     * Appends a menu item to the end of this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Returns the menu item added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @param menuItem the <code>JMenuitem</code> to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @return the <code>JMenuItem</code> added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public JMenuItem add(JMenuItem menuItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        return popupMenu.add(menuItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * Appends a component to the end of this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * Returns the component added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @param c the <code>Component</code> to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @return the <code>Component</code> added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public Component add(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        popupMenu.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Adds the specified component to this container at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * position. If <code>index</code> equals -1, the component will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * be appended to the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @param     c   the <code>Component</code> to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @param     index    the position at which to insert the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @return    the <code>Component</code> added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @see       #remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @see java.awt.Container#add(Component, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public Component add(Component c, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        popupMenu.add(c, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return c;
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
     * Creates a new menu item with the specified text and appends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * it to the end of this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @param s the string for the menu item to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    public JMenuItem add(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return add(new JMenuItem(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Creates a new menu item attached to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * <code>Action</code> object and appends it to the end of this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @param a the <code>Action</code> for the menu item to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @see Action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public JMenuItem add(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        JMenuItem mi = createActionComponent(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        mi.setAction(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        add(mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        return mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Factory method which creates the <code>JMenuItem</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * <code>Action</code>s added to the <code>JMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @param a the <code>Action</code> for the menu item to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @return the new menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @see Action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    protected JMenuItem createActionComponent(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        JMenuItem mi = new JMenuItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                PropertyChangeListener pcl = createActionChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                if (pcl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    pcl = super.createActionPropertyChangeListener(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                return pcl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        mi.setHorizontalTextPosition(JButton.TRAILING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        mi.setVerticalTextPosition(JButton.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * Returns a properly configured <code>PropertyChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * which updates the control as changes to the <code>Action</code> occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    protected PropertyChangeListener createActionChangeListener(JMenuItem b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        return b.createActionPropertyChangeListener0(b.getAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * Appends a new separator to the end of the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    public void addSeparator()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        popupMenu.addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Inserts a new menu item with the specified text at a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * given position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @param s the text for the menu item to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @param pos an integer specifying the position at which to add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *               new menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @exception IllegalArgumentException when the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *                  <code>pos</code> < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public void insert(String s, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        popupMenu.insert(new JMenuItem(s), pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * Inserts the specified <code>JMenuitem</code> at a given position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @param mi the <code>JMenuitem</code> to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @param pos an integer specifying the position at which to add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *               new <code>JMenuitem</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * @return the new menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @exception IllegalArgumentException if the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *                  <code>pos</code> < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    public JMenuItem insert(JMenuItem mi, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        popupMenu.insert(mi, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        return mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * Inserts a new menu item attached to the specified <code>Action</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * object at a given position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @param a the <code>Action</code> object for the menu item to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @param pos an integer specifying the position at which to add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *               new menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @exception IllegalArgumentException if the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *                  <code>pos</code> < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    public JMenuItem insert(Action a, int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        JMenuItem mi = new JMenuItem(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        mi.setHorizontalTextPosition(JButton.TRAILING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        mi.setVerticalTextPosition(JButton.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        popupMenu.insert(mi, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        return mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * Inserts a separator at the specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @param       index an integer specifying the position at which to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *                    insert the menu separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @exception   IllegalArgumentException if the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *                       <code>index</code> < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    public void insertSeparator(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        popupMenu.insert( new JPopupMenu.Separator(), index );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * Returns the <code>JMenuItem</code> at the specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * If the component at <code>pos</code> is not a menu item,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * <code>null</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * This method is included for AWT compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * @param pos    an integer specifying the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @exception   IllegalArgumentException if the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *                       <code>pos</code> < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @return  the menu item at the specified position; or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *          if the item as the specified position is not a menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    public JMenuItem getItem(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        Component c = getMenuComponent(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        if (c instanceof JMenuItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            JMenuItem mi = (JMenuItem) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            return mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        // 4173633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * Returns the number of items on the menu, including separators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * This method is included for AWT compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @return an integer equal to the number of items on the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @see #getMenuComponentCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    public int getItemCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        return getMenuComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * Returns true if the menu can be torn off.  This method is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * yet implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @return true if the menu can be torn off, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @exception  Error  if invoked -- this method is not yet implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    public boolean isTearOff() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        throw new Error("boolean isTearOff() {} not yet implemented");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * Removes the specified menu item from this menu.  If there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * popup menu, this method will have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @param    item the <code>JMenuItem</code> to be removed from the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    public void remove(JMenuItem item) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            popupMenu.remove(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Removes the menu item at the specified index from this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @param       pos the position of the item to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @exception   IllegalArgumentException if the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *                       <code>pos</code> < 0, or if <code>pos</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *                       is greater than the number of menu items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    public void remove(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        if (pos > getItemCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            throw new IllegalArgumentException("index greater than the number of items.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            popupMenu.remove(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * Removes the component <code>c</code> from this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @param       c the component to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    public void remove(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            popupMenu.remove(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * Removes all menu items from this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    public void removeAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            popupMenu.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Returns the number of components on the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @return an integer containing the number of components on the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    public int getMenuComponentCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        int componentCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            componentCount = popupMenu.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        return componentCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * Returns the component at position <code>n</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * @param n the position of the component to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * @return the component requested, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *                  if there is no popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    public Component getMenuComponent(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            return popupMenu.getComponent(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * Returns an array of <code>Component</code>s of the menu's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * subcomponents.  Note that this returns all <code>Component</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * in the popup menu, including separators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * @return an array of <code>Component</code>s or an empty array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *          if there is no popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    public Component[] getMenuComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        if (popupMenu != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            return popupMenu.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        return new Component[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * Returns true if the menu is a 'top-level menu', that is, if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * the direct child of a menubar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * @return true if the menu is activated from the menu bar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     *         false if the menu is activated from a menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *         on another menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    public boolean isTopLevelMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        if (getParent() instanceof JMenuBar)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * Returns true if the specified component exists in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * submenu hierarchy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @param c the <code>Component</code> to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @return true if the <code>Component</code> exists, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    public boolean isMenuComponent(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        // Are we in the MenuItem part of the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        // Are we in the PopupMenu?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        if (c instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            JPopupMenu comp = (JPopupMenu) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            if (comp == this.getPopupMenu())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        // Are we in a Component on the PopupMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        int ncomponents = this.getMenuComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        Component[] component = this.getMenuComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            // Are we in the current component?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            if (comp == c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            // Hmmm, what about Non-menu containers?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            // Recursive call for the Menu case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            if (comp instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                JMenu subMenu = (JMenu) comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                if (subMenu.isMenuComponent(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * Returns a point in the coordinate space of this menu's popupmenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * which corresponds to the point <code>p</code> in the menu's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * coordinate space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @param p the point to be translated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * @return the point in the coordinate space of this menu's popupmenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    private Point translateToPopupMenu(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        return translateToPopupMenu(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * Returns a point in the coordinate space of this menu's popupmenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * which corresponds to the point (x,y) in the menu's coordinate space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * @param x the x coordinate of the point to be translated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @param y the y coordinate of the point to be translated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * @return the point in the coordinate space of this menu's popupmenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    private Point translateToPopupMenu(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            int newX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            int newY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            if (getParent() instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                newX = x - getSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                newY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                newX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                newY = y - getSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            return new Point(newX, newY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * Returns the popupmenu associated with this menu.  If there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * no popupmenu, it will create one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    public JPopupMenu getPopupMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        ensurePopupMenuCreated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        return popupMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * Adds a listener for menu events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * @param l the listener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    public void addMenuListener(MenuListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        listenerList.add(MenuListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * Removes a listener for menu events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @param l the listener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    public void removeMenuListener(MenuListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        listenerList.remove(MenuListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * Returns an array of all the <code>MenuListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * to this JMenu with addMenuListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * @return all of the <code>MenuListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    public MenuListener[] getMenuListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        return (MenuListener[])listenerList.getListeners(MenuListener.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * is created lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * @exception Error  if there is a <code>null</code> listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    protected void fireMenuSelected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            System.out.println("In JMenu.fireMenuSelected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            if (listeners[i]==MenuListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                if (listeners[i+1]== null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                    throw new Error(getText() +" has a NULL Listener!! " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                    if (menuEvent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                        menuEvent = new MenuEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    ((MenuListener)listeners[i+1]).menuSelected(menuEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * is created lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * @exception Error if there is a <code>null</code> listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    protected void fireMenuDeselected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            System.out.println("In JMenu.fireMenuDeselected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            if (listeners[i]==MenuListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                if (listeners[i+1]== null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                    throw new Error(getText() +" has a NULL Listener!! " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                    // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                    if (menuEvent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                        menuEvent = new MenuEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    ((MenuListener)listeners[i+1]).menuDeselected(menuEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * notification on this event type.  The event instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * is created lazily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * @exception Error if there is a <code>null</code> listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    protected void fireMenuCanceled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            System.out.println("In JMenu.fireMenuCanceled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            if (listeners[i]==MenuListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                if (listeners[i+1]== null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    throw new Error(getText() +" has a NULL Listener!! "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                                       + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                    // Lazily create the event:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                    if (menuEvent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                        menuEvent = new MenuEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                    ((MenuListener)listeners[i+1]).menuCanceled(menuEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    // Overriden to do nothing, JMenu doesn't support an accelerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    void configureAcceleratorFromAction(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    class MenuChangeListener implements ChangeListener, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        boolean isSelected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            ButtonModel model = (ButtonModel) e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            boolean modelSelected = model.isSelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            if (modelSelected != isSelected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                if (modelSelected == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    fireMenuSelected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                    fireMenuDeselected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                isSelected = modelSelected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    private ChangeListener createMenuChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        return new MenuChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * Creates a window-closing listener for the popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * @param p the <code>JPopupMenu</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * @return the new window-closing listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * @see WinListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    protected WinListener createWinListener(JPopupMenu p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        return new WinListener(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * A listener class that watches for a popup window closing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * When the popup is closing, the listener deselects the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    protected class WinListener extends WindowAdapter implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        JPopupMenu popupMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
         *  Create the window listener for the specified popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        public WinListener(JPopupMenu p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            this.popupMenu = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
         * Deselect the menu when the popup is closed from outside.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        public void windowClosing(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            setSelected(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * Messaged when the menubar selection changes to activate or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * deactivate this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * Overrides <code>JMenuItem.menuSelectionChanged</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * @param isIncluded  true if this menu is active, false if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     *        it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    public void menuSelectionChanged(boolean isIncluded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            System.out.println("In JMenu.menuSelectionChanged to " + isIncluded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        setSelected(isIncluded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * Returns an array of <code>MenuElement</code>s containing the submenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * for this menu component.  If popup menu is <code>null</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * an empty array.  This method is required to conform to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * <code>MenuElement</code> interface.  Note that since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * <code>JSeparator</code>s do not conform to the <code>MenuElement</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * interface, this array will only contain <code>JMenuItem</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * @return an array of <code>MenuElement</code> objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    public MenuElement[] getSubElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        if(popupMenu == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            return new MenuElement[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            MenuElement result[] = new MenuElement[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            result[0] = popupMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    // implements javax.swing.MenuElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * Returns the <code>java.awt.Component</code> used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * paint this <code>MenuElement</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * The returned component is used to convert events and detect if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * an event is inside a menu component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    public Component getComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * Sets the <code>ComponentOrientation</code> property of this menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * and all components contained within it. This includes all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * components returned by {@link #getMenuComponents getMenuComponents}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * @param o the new component orientation of this menu and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     *        the components contained within it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * @exception NullPointerException if <code>orientation</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * @see java.awt.Component#setComponentOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * @see java.awt.Component#getComponentOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    public void applyComponentOrientation(ComponentOrientation o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        super.applyComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        if ( popupMenu != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            int ncomponents = getMenuComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            for (int i = 0 ; i < ncomponents ; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                getMenuComponent(i).applyComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            popupMenu.setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    public void setComponentOrientation(ComponentOrientation o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        super.setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        if ( popupMenu != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            popupMenu.setComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * <code>setAccelerator</code> is not defined for <code>JMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * Use <code>setMnemonic</code> instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * @param keyStroke  the keystroke combination which will invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     *                  the <code>JMenuItem</code>'s actionlisteners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     *                  without navigating the menu hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * @exception Error  if invoked -- this method is not defined for JMenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     *                  Use <code>setMnemonic</code> instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *     description: The keystroke combination which will invoke the JMenuItem's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     *                  actionlisteners without navigating the menu hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *          hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    public void setAccelerator(KeyStroke keyStroke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        throw new Error("setAccelerator() is not defined for JMenu.  Use setMnemonic() instead.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * Processes key stroke events such as mnemonics and accelerators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * @param evt  the key event to be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    protected void processKeyEvent(KeyEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        MenuSelectionManager.defaultManager().processKeyEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        if (evt.isConsumed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        super.processKeyEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * Programmatically performs a "click".  This overrides the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * <code>AbstractButton.doClick</code> in order to make the menu pop up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * @param pressTime  indicates the number of milliseconds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *          button was pressed for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    public void doClick(int pressTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        MenuElement me[] = buildMenuElementArray(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        MenuSelectionManager.defaultManager().setSelectedPath(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * Build an array of menu elements - from <code>PopupMenu</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * the root <code>JMenuBar</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * @param  leaf  the leaf node from which to start building up the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * @return the array of menu items
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    private MenuElement[] buildMenuElementArray(JMenu leaf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        Vector elements = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        Component current = leaf.getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        JPopupMenu pop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        JMenu menu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        JMenuBar bar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            if (current instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                pop = (JPopupMenu) current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                elements.insertElementAt(pop, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                current = pop.getInvoker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            } else if (current instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                menu = (JMenu) current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                elements.insertElementAt(menu, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                current = menu.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            } else if (current instanceof JMenuBar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                bar = (JMenuBar) current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                elements.insertElementAt(bar, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                MenuElement me[] = new MenuElement[elements.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                elements.copyInto(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                return me;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * See <code>readObject</code> and <code>writeObject</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * <code>JComponent</code> for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * Returns a string representation of this <code>JMenu</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * method is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * @return  a string representation of this JMenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        return super.paramString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * Gets the AccessibleContext associated with this JMenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * For JMenus, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * AccessibleJMenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * A new AccessibleJMenu instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * @return an AccessibleJMenu that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     *         AccessibleContext of this JMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            accessibleContext = new AccessibleJMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * <code>JMenu</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * Java Accessibility API appropriate to menu user-interface elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    protected class AccessibleJMenu extends AccessibleJMenuItem
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        implements AccessibleSelection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
         * Returns the number of accessible children in the object.  If all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
         * of the children of this object implement Accessible, than this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
         * method should return the number of children of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
         * @return the number of accessible children in the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        public int getAccessibleChildrenCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            Component[] children = getMenuComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            for (int j = 0; j < children.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                if (children[j] instanceof Accessible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
         * Returns the nth Accessible child of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
         * @param i zero-based index of child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
         * @return the nth Accessible child of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        public Accessible getAccessibleChild(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
            Component[] children = getMenuComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            for (int j = 0; j < children.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                if (children[j] instanceof Accessible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                    if (count == i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                        if (children[j] instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                            // FIXME:  [[[WDW - probably should set this when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                            // the component is added to the menu.  I tried
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                            // to do this in most cases, but the separators
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                            // added by addSeparator are hard to get to.]]]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                            AccessibleContext ac = ((Accessible) children[j]).getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                            ac.setAccessibleParent(JMenu.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                        return (Accessible) children[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            return AccessibleRole.MENU;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
         * Get the AccessibleSelection associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
         * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
         * AccessibleSelection interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        public AccessibleSelection getAccessibleSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
         * Returns 1 if a sub-menu is currently selected in this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
         * @return 1 if a menu is currently selected, else 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        public int getAccessibleSelectionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            MenuElement me[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                MenuSelectionManager.defaultManager().getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            if (me != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                for (int i = 0; i < me.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                    if (me[i] == JMenu.this) {   // this menu is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                        if (i+1 < me.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
         * Returns the currently selected sub-menu if one is selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
         * otherwise null (there can only be one selection, and it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
         * only be a sub-menu, as otherwise menu items don't remain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
         * selected).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        public Accessible getAccessibleSelection(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            // if i is a sub-menu & popped, return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            if (i < 0 || i >= getItemCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            MenuElement me[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                MenuSelectionManager.defaultManager().getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            if (me != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                for (int j = 0; j < me.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                    if (me[j] == JMenu.this) {   // this menu is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                        // so find the next JMenuItem in the MenuElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                        // array, and return it!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                        while (++j < me.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                            if (me[j] instanceof JMenuItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                                return (Accessible) me[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
         * Returns true if the current child of this object is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
         * (that is, if this child is a popped-up submenu).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
         * @param i the zero-based index of the child in this Accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
         * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
         * @see AccessibleContext#getAccessibleChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        public boolean isAccessibleChildSelected(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            // if i is a sub-menu and is pop-ed up, return true, else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            MenuElement me[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                MenuSelectionManager.defaultManager().getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            if (me != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                JMenuItem mi = JMenu.this.getItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                for (int j = 0; j < me.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                    if (me[j] == mi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
         * Selects the <code>i</code>th menu in the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
         * If that item is a submenu,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
         * it will pop up in response.  If a different item is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
         * popped up, this will force it to close.  If this is a sub-menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
         * that is already popped up (selected), this method has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
         * effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
         * @param i the index of the item to be selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
         * @see #getAccessibleStateSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        public void addAccessibleSelection(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            if (i < 0 || i >= getItemCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            JMenuItem mi = getItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            if (mi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                if (mi instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                    MenuElement me[] = buildMenuElementArray((JMenu) mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                    MenuSelectionManager.defaultManager().setSelectedPath(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                    MenuSelectionManager.defaultManager().setSelectedPath(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
         * Removes the nth item from the selection.  In general, menus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
         * can only have one item within them selected at a time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
         * (e.g. one sub-menu popped open).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
         * @param i the zero-based index of the selected item
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        public void removeAccessibleSelection(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            if (i < 0 || i >= getItemCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            JMenuItem mi = getItem(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            if (mi != null && mi instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                if (((JMenu) mi).isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                    MenuElement old[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                        MenuSelectionManager.defaultManager().getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                    MenuElement me[] = new MenuElement[old.length-2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                    for (int j = 0; j < old.length -2; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                        me[j] = old[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                    MenuSelectionManager.defaultManager().setSelectedPath(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
         * Clears the selection in the object, so that nothing in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
         * object is selected.  This will close any open sub-menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        public void clearAccessibleSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            // if this menu is selected, reset selection to only go
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            // to this menu; else do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            MenuElement old[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                MenuSelectionManager.defaultManager().getSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
            if (old != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                for (int j = 0; j < old.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                    if (old[j] == JMenu.this) {  // menu is in the selection!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                        MenuElement me[] = new MenuElement[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                        System.arraycopy(old, 0, me, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                        me[j] = JMenu.this.getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                        MenuSelectionManager.defaultManager().setSelectedPath(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
         * Normally causes every selected item in the object to be selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
         * if the object supports multiple selections.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
         * makes no sense in a menu bar, and so does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        public void selectAllAccessibleSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
    } // inner class AccessibleJMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
}