jdk/src/share/classes/javax/swing/JPopupMenu.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 675 4f26ea16c5c1
child 1301 15e81207e1f2
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 675
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.plaf.PopupMenuUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.plaf.ComponentUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.plaf.basic.BasicComboPopup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.event.*;
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
    44
import sun.security.util.SecurityConstants;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.applet.Applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * An implementation of a popup menu -- a small window that pops up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * and displays a series of choices. A <code>JPopupMenu</code> is used for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * menu that appears when the user selects an item on the menu bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * It is also used for "pull-right" menu that appears when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * selects a menu item that activates it. Finally, a <code>JPopupMenu</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * can also be used anywhere else you want a menu to appear.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * example, when the user right-clicks in a specified area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * For information and examples of using popup menus, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *   attribute: isContainer false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * description: A small window that pops up and displays a series of choices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @author Georges Saab
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @author David Karlton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
public class JPopupMenu extends JComponent implements Accessible,MenuElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static final String uiClassID = "PopupMenuUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Key used in AppContext to determine if light way popups are the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final Object defaultLWPopupEnabledKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        new StringBuffer("JPopupMenu.defaultLWPopupEnabledKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /** Bug#4425878-Property javax.swing.adjustPopupLocationToFit introduced */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static boolean popupPostionFixDisabled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        popupPostionFixDisabled = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                new sun.security.action.GetPropertyAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                "javax.swing.adjustPopupLocationToFit","")).equals("false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    transient  Component invoker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    transient  Popup popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    transient  Frame frame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private    int desiredLocationX,desiredLocationY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private    String     label                   = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private    boolean   paintBorder              = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private    Insets    margin                   = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Used to indicate if lightweight popups should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private    boolean   lightWeightPopup         = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Model for the selected subcontrol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private SingleSelectionModel selectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /* Lock object used in place of class object for synchronization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * (4187686)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final Object classLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /* diagnostic aids -- should be false for production builds. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static final boolean TRACE =   false; // trace creates and disposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private static final boolean VERBOSE = false; // show reuse hits/misses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static final boolean DEBUG =   false;  // show bad params, misc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *  Sets the default value of the <code>lightWeightPopupEnabled</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *  property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *  @param aFlag <code>true</code> if popups can be lightweight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *               otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *  @see #getDefaultLightWeightPopupEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *  @see #setLightWeightPopupEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public static void setDefaultLightWeightPopupEnabled(boolean aFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        SwingUtilities.appContextPut(defaultLWPopupEnabledKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                     Boolean.valueOf(aFlag));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *  Gets the <code>defaultLightWeightPopupEnabled</code> property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *  which by default is <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *  @return the value of the <code>defaultLightWeightPopupEnabled</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *          property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *  @see #setDefaultLightWeightPopupEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public static boolean getDefaultLightWeightPopupEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        Boolean b = (Boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            SwingUtilities.appContextGet(defaultLWPopupEnabledKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            SwingUtilities.appContextPut(defaultLWPopupEnabledKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                         Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return b.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Constructs a <code>JPopupMenu</code> without an "invoker".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public JPopupMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        this(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Constructs a <code>JPopupMenu</code> with the specified title.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param label  the string that a UI may use to display as a title
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * for the popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public JPopupMenu(String label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this.label = label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        lightWeightPopup = getDefaultLightWeightPopupEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        setSelectionModel(new DefaultSingleSelectionModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        setFocusTraversalKeysEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns the look and feel (L&F) object that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @return the <code>PopupMenuUI</code> object that renders this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public PopupMenuUI getUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return (PopupMenuUI)ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Sets the L&F object that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param ui the new <code>PopupMenuUI</code> L&F object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *       hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *  description: The UI object that implements the Component's LookAndFeel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public void setUI(PopupMenuUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        super.setUI(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Resets the UI property to a value from the current look and feel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @see JComponent#updateUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        setUI((PopupMenuUI)UIManager.getUI(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Returns the name of the L&F class that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @return the string "PopupMenuUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    protected void processFocusEvent(FocusEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        super.processFocusEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Processes key stroke events such as mnemonics and accelerators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param evt  the key event to be processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    protected void processKeyEvent(KeyEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        MenuSelectionManager.defaultManager().processKeyEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (evt.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        super.processKeyEvent(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Returns the model object that handles single selections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @return the <code>selectionModel</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @see SingleSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public SingleSelectionModel getSelectionModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return selectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Sets the model object to handle single selections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param model the new <code>SingleSelectionModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @see SingleSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * description: The selection model for the popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *      expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public void setSelectionModel(SingleSelectionModel model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        selectionModel = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Appends the specified menu item to the end of this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param menuItem the <code>JMenuItem</code> to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return the <code>JMenuItem</code> added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public JMenuItem add(JMenuItem menuItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        super.add(menuItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return menuItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Creates a new menu item with the specified text and appends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * it to the end of this menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @param s the string for the menu item to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public JMenuItem add(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return add(new JMenuItem(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Appends a new menu item to the end of the menu which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * dispatches the specified <code>Action</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @param a the <code>Action</code> to add to the menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @return the new menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @see Action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public JMenuItem add(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        JMenuItem mi = createActionComponent(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        mi.setAction(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        add(mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Returns an point which has been adjusted to take into account of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * desktop bounds, taskbar and multi-monitor configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * This adustment may be cancelled by invoking the application with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * -Djavax.swing.adjustPopupLocationToFit=false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   324
    Point adjustPopupLocationToFitScreen(int xPosition, int yPosition) {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   325
        Point popupLocation = new Point(xPosition, yPosition);
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   326
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   327
        if(popupPostionFixDisabled == true || GraphicsEnvironment.isHeadless()) {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   328
            return popupLocation;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   329
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   331
        // Get screen bounds
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   332
        Rectangle scrBounds;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   333
        GraphicsConfiguration gc = getCurrentGraphicsConfiguration(popupLocation);
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   334
        Toolkit toolkit = Toolkit.getDefaultToolkit();
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   335
        if(gc != null) {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   336
            // If we have GraphicsConfiguration use it to get screen bounds
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   337
            scrBounds = gc.getBounds();
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   338
        } else {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   339
            // If we don't have GraphicsConfiguration use primary screen
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   340
            scrBounds = new Rectangle(toolkit.getScreenSize());
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   341
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   343
        // Calculate the screen size that popup should fit
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   344
        Dimension popupSize = JPopupMenu.this.getPreferredSize();
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   345
        int popupRightX = popupLocation.x + popupSize.width;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   346
        int popupBottomY = popupLocation.y + popupSize.height;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   347
        int scrWidth = scrBounds.width;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   348
        int scrHeight = scrBounds.height;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   349
        if (!canPopupOverlapTaskBar()) {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   350
            // Insets include the task bar. Take them into account.
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   351
            Insets scrInsets = toolkit.getScreenInsets(gc);
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   352
            scrBounds.x += scrInsets.left;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   353
            scrBounds.y += scrInsets.top;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   354
            scrWidth -= scrInsets.left + scrInsets.right;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   355
            scrHeight -= scrInsets.top + scrInsets.bottom;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   356
        }
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   357
        int scrRightX = scrBounds.x + scrWidth;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   358
        int scrBottomY = scrBounds.y + scrHeight;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   359
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   360
        // Ensure that popup menu fits the screen
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   361
        if (popupRightX > scrRightX) {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   362
            popupLocation.x = scrRightX - popupSize.width;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   363
            if( popupLocation.x < scrBounds.x ) {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   364
                popupLocation.x = scrBounds.x ;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   365
            }
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   366
        }
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   367
        if (popupBottomY > scrBottomY) {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   368
            popupLocation.y = scrBottomY - popupSize.height;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   369
            if( popupLocation.y < scrBounds.y ) {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   370
                popupLocation.y = scrBounds.y;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   371
            }
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   372
        }
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   373
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   374
        return popupLocation;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   375
    }
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   376
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   377
    /**
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   378
     * Tries to find GraphicsConfiguration
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   379
     * that contains the mouse cursor position.
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   380
     * Can return null.
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   381
     */
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   382
    private GraphicsConfiguration getCurrentGraphicsConfiguration(
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   383
            Point popupLocation) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        GraphicsConfiguration gc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        GraphicsEnvironment ge =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            GraphicsEnvironment.getLocalGraphicsEnvironment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        GraphicsDevice[] gd = ge.getScreenDevices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        for(int i = 0; i < gd.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if(gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                GraphicsConfiguration dgc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    gd[i].getDefaultConfiguration();
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   392
                if(dgc.getBounds().contains(popupLocation)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    gc = dgc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        // If not found and we have invoker, ask invoker about his gc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if(gc == null && getInvoker() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            gc = getInvoker().getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   402
        return gc;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   403
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   405
    /**
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   406
     * Checks that there are enough security permissions
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   407
     * to make popup "always on top", which allows to show it above the task bar.
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   408
     */
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   409
    static boolean canPopupOverlapTaskBar() {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   410
        boolean result = true;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   411
        try {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   412
            SecurityManager sm = System.getSecurityManager();
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   413
            if (sm != null) {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   414
                sm.checkPermission(
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   415
                        SecurityConstants.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION);
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   416
            }
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   417
        } catch (SecurityException se) {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   418
            // There is no permission to show popups over the task bar
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   419
            result = false;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   420
        }
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   421
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * Factory method which creates the <code>JMenuItem</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <code>Actions</code> added to the <code>JPopupMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @param a the <code>Action</code> for the menu item to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @return the new menu item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @see Action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    protected JMenuItem createActionComponent(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        JMenuItem mi = new JMenuItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                PropertyChangeListener pcl = createActionChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                if (pcl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    pcl = super.createActionPropertyChangeListener(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                return pcl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        mi.setHorizontalTextPosition(JButton.TRAILING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        mi.setVerticalTextPosition(JButton.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return mi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Returns a properly configured <code>PropertyChangeListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * which updates the control as changes to the <code>Action</code> occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    protected PropertyChangeListener createActionChangeListener(JMenuItem b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return b.createActionPropertyChangeListener0(b.getAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Removes the component at the specified index from this popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @param       pos the position of the item to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @exception   IllegalArgumentException if the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *                          <code>pos</code> < 0, or if the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *                          <code>pos</code> is greater than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *                          number of items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public void remove(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (pos > getComponentCount() -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            throw new IllegalArgumentException("index greater than the number of items.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        super.remove(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Sets the value of the <code>lightWeightPopupEnabled</code> property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * which by default is <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * By default, when a look and feel displays a popup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * it can choose to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * use a lightweight (all-Java) popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Lightweight popup windows are more efficient than heavyweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * (native peer) windows,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * but lightweight and heavyweight components do not mix well in a GUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * If your application mixes lightweight and heavyweight components,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * you should disable lightweight popups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Some look and feels might always use heavyweight popups,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * no matter what the value of this property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param aFlag  <code>false</code> to disable lightweight popups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * description: Determines whether lightweight popups are used when possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *      expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @see #isLightWeightPopupEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public void setLightWeightPopupEnabled(boolean aFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        // NOTE: this use to set the flag on a shared JPopupMenu, which meant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // this effected ALL JPopupMenus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        lightWeightPopup = aFlag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Gets the <code>lightWeightPopupEnabled</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @return the value of the <code>lightWeightPopupEnabled</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @see #setLightWeightPopupEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public boolean isLightWeightPopupEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        return lightWeightPopup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Returns the popup menu's label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @return a string containing the popup menu's label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @see #setLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public String getLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        return label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Sets the popup menu's label.  Different look and feels may choose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * to display or not display this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @param label a string specifying the label for the popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @see #setLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * description: The label for the popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public void setLabel(String label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        String oldValue = this.label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        this.label = label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        firePropertyChange("label", oldValue, label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (accessibleContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            accessibleContext.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                oldValue, label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Appends a new separator at the end of the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    public void addSeparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        add( new JPopupMenu.Separator() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Inserts a menu item for the specified <code>Action</code> object at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * a given position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @param a  the <code>Action</code> object to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @param index      specifies the position at which to insert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *                   <code>Action</code>, where 0 is the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @exception IllegalArgumentException if <code>index</code> < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @see Action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    public void insert(Action a, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        JMenuItem mi = createActionComponent(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        mi.setAction(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        insert(mi, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Inserts the specified component into the menu at a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @param component  the <code>Component</code> to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @param index      specifies the position at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *                   to insert the component, where 0 is the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @exception IllegalArgumentException if <code>index</code> < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    public void insert(Component component, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            throw new IllegalArgumentException("index less than zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        int nitems = getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        // PENDING(ges): Why not use an array?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        Vector tempItems = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        /* Remove the item at index, nitems-index times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
           storing them in a temporary vector in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
           order they appear on the menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        for (int i = index ; i < nitems; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            tempItems.addElement(getComponent(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            remove(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        add(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        /* Add the removed items back to the menu, they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
           already in the correct order in the temp vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        for (int i = 0; i < tempItems.size()  ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            add((Component)tempItems.elementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *  Adds a <code>PopupMenu</code> listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *  @param l  the <code>PopupMenuListener</code> to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    public void addPopupMenuListener(PopupMenuListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        listenerList.add(PopupMenuListener.class,l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * Removes a <code>PopupMenu</code> listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @param l  the <code>PopupMenuListener</code> to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    public void removePopupMenuListener(PopupMenuListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        listenerList.remove(PopupMenuListener.class,l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Returns an array of all the <code>PopupMenuListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * to this JMenuItem with addPopupMenuListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @return all of the <code>PopupMenuListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    public PopupMenuListener[] getPopupMenuListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        return (PopupMenuListener[])listenerList.getListeners(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                PopupMenuListener.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Adds a <code>MenuKeyListener</code> to the popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param l the <code>MenuKeyListener</code> to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    public void addMenuKeyListener(MenuKeyListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        listenerList.add(MenuKeyListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * Removes a <code>MenuKeyListener</code> from the popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @param l the <code>MenuKeyListener</code> to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    public void removeMenuKeyListener(MenuKeyListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        listenerList.remove(MenuKeyListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * Returns an array of all the <code>MenuKeyListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * to this JPopupMenu with addMenuKeyListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @return all of the <code>MenuKeyListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    public MenuKeyListener[] getMenuKeyListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        return (MenuKeyListener[])listenerList.getListeners(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                MenuKeyListener.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * Notifies <code>PopupMenuListener</code>s that this popup menu will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * become visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    protected void firePopupMenuWillBecomeVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        PopupMenuEvent e=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            if (listeners[i]==PopupMenuListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    e = new PopupMenuEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeVisible(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * Notifies <code>PopupMenuListener</code>s that this popup menu will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * become invisible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    protected void firePopupMenuWillBecomeInvisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        PopupMenuEvent e=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            if (listeners[i]==PopupMenuListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    e = new PopupMenuEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeInvisible(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * Notifies <code>PopupMenuListeners</code> that this popup menu is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    protected void firePopupMenuCanceled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        PopupMenuEvent e=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            if (listeners[i]==PopupMenuListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    e = new PopupMenuEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                ((PopupMenuListener)listeners[i+1]).popupMenuCanceled(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * Always returns true since popups, by definition, should always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * be on top of all other windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @return true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    // package private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    boolean alwaysOnTop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * Lays out the container so that it uses the minimum space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * needed to display its contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    public void pack() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if(popup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            Dimension pref = getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (pref == null || pref.width != getWidth() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                                pref.height != getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                popup = getPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * Sets the visibility of the popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @param b true to make the popup visible, or false to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *          hide it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *           bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *     description: Makes the popup visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    public void setVisible(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            System.out.println("JPopupMenu.setVisible " + b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        // Is it a no-op?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        if (b == isVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        // if closing, first close all Submenus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if (b == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            // 4234793: This is a workaround because JPopupMenu.firePopupMenuCanceled is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            // a protected method and cannot be called from BasicPopupMenuUI directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            // The real solution could be to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            // firePopupMenuCanceled public and call it directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            Boolean doCanceled = (Boolean)getClientProperty("JPopupMenu.firePopupMenuCanceled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            if (doCanceled != null && doCanceled == Boolean.TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                putClientProperty("JPopupMenu.firePopupMenuCanceled", Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                firePopupMenuCanceled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            getSelectionModel().clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            // This is a popup menu with MenuElement children,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            // set selection path before popping up!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            if (isPopupMenu()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                MenuElement me[] = new MenuElement[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                me[0] = (MenuElement) this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                MenuSelectionManager.defaultManager().setSelectedPath(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if(b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            firePopupMenuWillBecomeVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            popup = getPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            firePropertyChange("visible", Boolean.FALSE, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        } else if(popup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            firePopupMenuWillBecomeInvisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            popup.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            popup = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            firePropertyChange("visible", Boolean.TRUE, Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            // 4694797: When popup menu is made invisible, selected path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            // should be cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            if (isPopupMenu()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                MenuSelectionManager.defaultManager().clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * Returns a <code>Popup</code> instance from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * <code>PopupMenuUI</code> that has had <code>show</code> invoked on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * it. If the current <code>popup</code> is non-null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * this will invoke <code>dispose</code> of it, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * <code>show</code> the new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * This does NOT fire any events, it is up the caller to dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * the necessary events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    private Popup getPopup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        Popup oldPopup = popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        if (oldPopup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            oldPopup.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        PopupFactory popupFactory = PopupFactory.getSharedInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (isLightWeightPopupEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            popupFactory.setPopupType(PopupFactory.LIGHT_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            popupFactory.setPopupType(PopupFactory.MEDIUM_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        // adjust the location of the popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        Point p = adjustPopupLocationToFitScreen(desiredLocationX,desiredLocationY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        desiredLocationX = p.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        desiredLocationY = p.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        Popup newPopup = getUI().getPopup(this, desiredLocationX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                                          desiredLocationY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        popupFactory.setPopupType(PopupFactory.LIGHT_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        newPopup.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        return newPopup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * Returns true if the popup menu is visible (currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * being displayed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    public boolean isVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        if(popup != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * Sets the location of the upper left corner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * popup menu using x, y coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @param x the x coordinate of the popup's new position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *          in the screen's coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @param y the y coordinate of the popup's new position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *          in the screen's coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * description: The location of the popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    public void setLocation(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        int oldX = desiredLocationX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        int oldY = desiredLocationY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        desiredLocationX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        desiredLocationY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        if(popup != null && (x != oldX || y != oldY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            popup = getPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * Returns true if the popup menu is a standalone popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * rather than the submenu of a <code>JMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * @return true if this menu is a standalone popup menu, otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    private boolean isPopupMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        return  ((invoker != null) && !(invoker instanceof JMenu));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * Returns the component which is the 'invoker' of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @return the <code>Component</code> in which the popup menu is displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    public Component getInvoker() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        return this.invoker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * Sets the invoker of this popup menu -- the component in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * the popup menu menu is to be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * @param invoker the <code>Component</code> in which the popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *          menu is displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * description: The invoking component for the popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     *      expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    public void setInvoker(Component invoker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        Component oldInvoker = this.invoker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        this.invoker = invoker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        if ((oldInvoker != this.invoker) && (ui != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            ui.uninstallUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * Displays the popup menu at the position x,y in the coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * space of the component invoker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @param invoker the component in whose space the popup menu is to appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * @param x the x coordinate in invoker's coordinate space at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * the popup menu is to be displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @param y the y coordinate in invoker's coordinate space at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * the popup menu is to be displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    public void show(Component invoker, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            System.out.println("in JPopupMenu.show " );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        setInvoker(invoker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        Frame newFrame = getFrame(invoker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        if (newFrame != frame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            // Use the invoker's frame so that events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            // are propagated properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            if (newFrame!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                this.frame = newFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                if(popup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        Point invokerOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        if (invoker != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            invokerOrigin = invoker.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            // To avoid integer overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            long lx, ly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            lx = ((long) invokerOrigin.x) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                 ((long) x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            ly = ((long) invokerOrigin.y) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                 ((long) y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            if(lx > Integer.MAX_VALUE) lx = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            if(lx < Integer.MIN_VALUE) lx = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            if(ly > Integer.MAX_VALUE) ly = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            if(ly < Integer.MIN_VALUE) ly = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            setLocation((int) lx, (int) ly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            setLocation(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * Returns the popup menu which is at the root of the menu system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * for this popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @return the topmost grandparent <code>JPopupMenu</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    JPopupMenu getRootPopupMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        JPopupMenu mp = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        while((mp!=null) && (mp.isPopupMenu()!=true) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
              (mp.getInvoker() != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
              (mp.getInvoker().getParent() != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
              (mp.getInvoker().getParent() instanceof JPopupMenu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
              ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            mp = (JPopupMenu) mp.getInvoker().getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        return mp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * Returns the component at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * @param i  the index of the component, where 0 is the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @return the <code>Component</code> at that index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * @deprecated replaced by {@link java.awt.Container#getComponent(int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    public Component getComponentAtIndex(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        return getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * Returns the index of the specified component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * @param  c the <code>Component</code> to find
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @return the index of the component, where 0 is the first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *         or -1 if the component is not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    public int getComponentIndex(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        int ncomponents = this.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        Component[] component = this.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            if (comp == c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * Sets the size of the Popup window using a <code>Dimension</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * This is equivalent to <code>setPreferredSize(d)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * @param d   the <code>Dimension</code> specifying the new size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * of this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * description: The size of the popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    public void setPopupSize(Dimension d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        Dimension oldSize = getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        setPreferredSize(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        if (popup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            Dimension newSize = getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            if (!oldSize.equals(newSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                popup = getPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * Sets the size of the Popup window to the specified width and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * height. This is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     *  <code>setPreferredSize(new Dimension(width, height))</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @param width the new width of the Popup in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * @param height the new height of the Popup in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * description: The size of the popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    public void setPopupSize(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        setPopupSize(new Dimension(width, height));
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
     * Sets the currently selected component,  This will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * in a change to the selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * @param sel the <code>Component</code> to select
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * description: The selected component on the popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *      expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *      hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    public void setSelected(Component sel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        SingleSelectionModel model = getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        int index = getComponentIndex(sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        model.setSelectedIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * Checks whether the border should be painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @return true if the border is painted, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * @see #setBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    public boolean isBorderPainted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        return paintBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * Sets whether the border should be painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * @param b if true, the border is painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * @see #isBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * description: Is the border of the popup menu painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    public void setBorderPainted(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        paintBorder = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * Paints the popup menu's border if the <code>borderPainted</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * property is <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * @param g  the <code>Graphics</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * @see JComponent#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * @see JComponent#setBorder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    protected void paintBorder(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        if (isBorderPainted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            super.paintBorder(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * Returns the margin, in pixels, between the popup menu's border and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * its containees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * @return an <code>Insets</code> object containing the margin values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    public Insets getMargin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        if(margin == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            return new Insets(0,0,0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            return margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * Examines the list of menu items to determine whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * <code>popup</code> is a popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * @param popup  a <code>JPopupMenu</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @return true if <code>popup</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    boolean isSubPopupMenu(JPopupMenu popup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        int ncomponents = this.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        Component[] component = this.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            if (comp instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                JMenu menu = (JMenu)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                JPopupMenu subPopup = menu.getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                if (subPopup == popup)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                if (subPopup.isSubPopupMenu(popup))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    private static Frame getFrame(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        Component w = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        while(!(w instanceof Frame) && (w!=null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            w = w.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        return (Frame)w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * Returns a string representation of this <code>JPopupMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * @return  a string representation of this <code>JPopupMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        String labelString = (label != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                              label : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        String paintBorderString = (paintBorder ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                                    "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        String marginString = (margin != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                              margin.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        String lightWeightPopupEnabledString = (isLightWeightPopupEnabled() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                                                "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        return super.paramString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            ",desiredLocationX=" + desiredLocationX +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            ",desiredLocationY=" + desiredLocationY +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        ",label=" + labelString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        ",lightWeightPopupEnabled=" + lightWeightPopupEnabledString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        ",margin=" + marginString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        ",paintBorder=" + paintBorderString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * Gets the AccessibleContext associated with this JPopupMenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * For JPopupMenus, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * AccessibleJPopupMenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * A new AccessibleJPopupMenu instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * @return an AccessibleJPopupMenu that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     *         AccessibleContext of this JPopupMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            accessibleContext = new AccessibleJPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * <code>JPopupMenu</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * Java Accessibility API appropriate to popup menu user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    protected class AccessibleJPopupMenu extends AccessibleJComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        implements PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
         * AccessibleJPopupMenu constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
         * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        protected AccessibleJPopupMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            JPopupMenu.this.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
         * @return an instance of AccessibleRole describing the role of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
         * the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            return AccessibleRole.POPUP_MENU;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
         * This method gets called when a bound property is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
         * @param e A <code>PropertyChangeEvent</code> object describing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
         * the event source and the property that has changed. Must not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
         * @throws NullPointerException if the parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
         * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            String propertyName = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            if (propertyName == "visible") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                if (e.getOldValue() == Boolean.FALSE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                    e.getNewValue() == Boolean.TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                    handlePopupIsVisibleEvent(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                } else if (e.getOldValue() == Boolean.TRUE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                           e.getNewValue() == Boolean.FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                    handlePopupIsVisibleEvent(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                }
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
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
         * Handles popup "visible" PropertyChangeEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        private void handlePopupIsVisibleEvent(boolean visible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            if (visible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                // notify listeners that the popup became visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                firePropertyChange(ACCESSIBLE_STATE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                                   null, AccessibleState.VISIBLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                // notify listeners that a popup list item is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                fireActiveDescendant();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                // notify listeners that the popup became hidden
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                firePropertyChange(ACCESSIBLE_STATE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                                   AccessibleState.VISIBLE, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
         * Fires AccessibleActiveDescendant PropertyChangeEvent to notify listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
         * on the popup menu invoker that a popup list item has been selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        private void fireActiveDescendant() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            if (JPopupMenu.this instanceof BasicComboPopup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                // get the popup list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                JList popupList = ((BasicComboPopup)JPopupMenu.this).getList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                if (popupList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                // get the first selected item
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                AccessibleContext ac = popupList.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                AccessibleSelection selection = ac.getAccessibleSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                if (selection == null) {
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
                Accessible a = selection.getAccessibleSelection(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                AccessibleContext selectedItem = a.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                // fire the event with the popup invoker as the source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                if (selectedItem != null && invoker != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                    AccessibleContext invokerContext = invoker.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                    if (invokerContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                        // Check invokerContext because Component.getAccessibleContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                        // returns null. Classes that extend Component are responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                        // for returning a non-null AccessibleContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                        invokerContext.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                            ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                            null, selectedItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    } // inner class AccessibleJPopupMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
// Serialization support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        Vector      values = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        // Save the invoker, if its Serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        if(invoker != null && invoker instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            values.addElement("invoker");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            values.addElement(invoker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        // Save the popup, if its Serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        if(popup != null && popup instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            values.addElement("popup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            values.addElement(popup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        s.writeObject(values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    // implements javax.swing.MenuElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        Vector          values = (Vector)s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        int             indexCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        int             maxCounter = values.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        if(indexCounter < maxCounter && values.elementAt(indexCounter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
           equals("invoker")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            invoker = (Component)values.elementAt(++indexCounter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            indexCounter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        if(indexCounter < maxCounter && values.elementAt(indexCounter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
           equals("popup")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            popup = (Popup)values.elementAt(++indexCounter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            indexCounter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * This method is required to conform to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * <code>MenuElement</code> interface, but it not implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * @see MenuElement#processMouseEvent(MouseEvent, MenuElement[], MenuSelectionManager)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    public void processMouseEvent(MouseEvent event,MenuElement path[],MenuSelectionManager manager) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * Processes a key event forwarded from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * <code>MenuSelectionManager</code> and changes the menu selection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * if necessary, by using <code>MenuSelectionManager</code>'s API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * Note: you do not have to forward the event to sub-components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * This is done automatically by the <code>MenuSelectionManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * @param e  a <code>KeyEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * @param path the <code>MenuElement</code> path array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * @param manager   the <code>MenuSelectionManager</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    public void processKeyEvent(KeyEvent e, MenuElement path[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                                MenuSelectionManager manager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        MenuKeyEvent mke = new MenuKeyEvent(e.getComponent(), e.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                                             e.getWhen(), e.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                                             e.getKeyCode(), e.getKeyChar(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                                             path, manager);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        processMenuKeyEvent(mke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        if (mke.isConsumed())  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * Handles a keystroke in a menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * @param e  a <code>MenuKeyEvent</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
    private void processMenuKeyEvent(MenuKeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        switch (e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        case KeyEvent.KEY_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            fireMenuKeyPressed(e); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        case KeyEvent.KEY_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            fireMenuKeyReleased(e); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        case KeyEvent.KEY_TYPED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            fireMenuKeyTyped(e); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * notification on this event type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * @param event a <code>MenuKeyEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    private void fireMenuKeyPressed(MenuKeyEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            if (listeners[i]==MenuKeyListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                ((MenuKeyListener)listeners[i+1]).menuKeyPressed(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * notification on this event type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * @param event a <code>MenuKeyEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    private void fireMenuKeyReleased(MenuKeyEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            if (listeners[i]==MenuKeyListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                ((MenuKeyListener)listeners[i+1]).menuKeyReleased(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * notification on this event type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * @param event a <code>MenuKeyEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    private void fireMenuKeyTyped(MenuKeyEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            if (listeners[i]==MenuKeyListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                ((MenuKeyListener)listeners[i+1]).menuKeyTyped(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * Messaged when the menubar selection changes to activate or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * deactivate this menu. This implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * <code>javax.swing.MenuElement</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     * Overrides <code>MenuElement.menuSelectionChanged</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * @param isIncluded  true if this menu is active, false if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     *        it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * @see MenuElement#menuSelectionChanged(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    public void menuSelectionChanged(boolean isIncluded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            System.out.println("In JPopupMenu.menuSelectionChanged " + isIncluded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        if(invoker instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            JMenu m = (JMenu) invoker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            if(isIncluded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                m.setPopupMenuVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                m.setPopupMenuVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        if (isPopupMenu() && !isIncluded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
          setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * Returns an array of <code>MenuElement</code>s containing the submenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * for this menu component.  It will only return items conforming to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * the <code>JMenuElement</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * If popup menu is <code>null</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * an empty array.  This method is required to conform to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * <code>MenuElement</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * @return an array of <code>MenuElement</code> objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * @see MenuElement#getSubElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    public MenuElement[] getSubElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        MenuElement result[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        Vector tmp = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        int c = getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        Component m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        for(i=0 ; i < c ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            m = getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            if(m instanceof MenuElement)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                tmp.addElement(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        result = new MenuElement[tmp.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        for(i=0,c=tmp.size() ; i < c ; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            result[i] = (MenuElement) tmp.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * Returns this <code>JPopupMenu</code> component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * @return this <code>JPopupMenu</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * @see MenuElement#getComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    public Component getComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * A popup menu-specific separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    static public class Separator extends JSeparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        public Separator( )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            super( JSeparator.HORIZONTAL );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
         * Returns the name of the L&F class that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
         * @return the string "PopupMenuSeparatorUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
         * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
         * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        public String getUIClassID()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            return "PopupMenuSeparatorUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * Returns true if the <code>MouseEvent</code> is considered a popup trigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * by the <code>JPopupMenu</code>'s currently installed UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * @return true if the mouse event is a popup trigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
    public boolean isPopupTrigger(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        return getUI().isPopupTrigger(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
}