jdk/src/share/classes/javax/swing/JPopupMenu.java
author rupashka
Tue, 26 Aug 2008 15:12:54 +0400
changeset 1301 15e81207e1f2
parent 715 f16baef3a20e
child 3956 2586d23078e4
permissions -rw-r--r--
6727662: Code improvement and warnings removing from swing packages Summary: Removed unnecessary castings and other warnings Reviewed-by: malenkov
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?
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   587
        Vector<Component> tempItems = new Vector<Component>();
2
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
           */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   603
        for (Component tempItem : tempItems) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   604
            add(tempItem);
2
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() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   635
        return listenerList.getListeners(PopupMenuListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * Adds a <code>MenuKeyListener</code> to the popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @param l the <code>MenuKeyListener</code> to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    public void addMenuKeyListener(MenuKeyListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        listenerList.add(MenuKeyListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * Removes a <code>MenuKeyListener</code> from the popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @param l the <code>MenuKeyListener</code> to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    public void removeMenuKeyListener(MenuKeyListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        listenerList.remove(MenuKeyListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Returns an array of all the <code>MenuKeyListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * to this JPopupMenu with addMenuKeyListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @return all of the <code>MenuKeyListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    public MenuKeyListener[] getMenuKeyListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   667
        return listenerList.getListeners(MenuKeyListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * Notifies <code>PopupMenuListener</code>s that this popup menu will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * become visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    protected void firePopupMenuWillBecomeVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        PopupMenuEvent e=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            if (listeners[i]==PopupMenuListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    e = new PopupMenuEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeVisible(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
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
     * Notifies <code>PopupMenuListener</code>s that this popup menu will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * become invisible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    protected void firePopupMenuWillBecomeInvisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        PopupMenuEvent e=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            if (listeners[i]==PopupMenuListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    e = new PopupMenuEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeInvisible(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
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
     * Notifies <code>PopupMenuListeners</code> that this popup menu is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    protected void firePopupMenuCanceled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        PopupMenuEvent e=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            if (listeners[i]==PopupMenuListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    e = new PopupMenuEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                ((PopupMenuListener)listeners[i+1]).popupMenuCanceled(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
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
     * Always returns true since popups, by definition, should always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * be on top of all other windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @return true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    // package private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    boolean alwaysOnTop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * Lays out the container so that it uses the minimum space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * needed to display its contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    public void pack() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        if(popup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            Dimension pref = getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            if (pref == null || pref.width != getWidth() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                                pref.height != getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                popup = getPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
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
     * Sets the visibility of the popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @param b true to make the popup visible, or false to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *          hide it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *           bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *     description: Makes the popup visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    public void setVisible(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            System.out.println("JPopupMenu.setVisible " + b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        // Is it a no-op?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        if (b == isVisible())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        // if closing, first close all Submenus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        if (b == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            // 4234793: This is a workaround because JPopupMenu.firePopupMenuCanceled is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            // a protected method and cannot be called from BasicPopupMenuUI directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            // The real solution could be to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            // firePopupMenuCanceled public and call it directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            Boolean doCanceled = (Boolean)getClientProperty("JPopupMenu.firePopupMenuCanceled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            if (doCanceled != null && doCanceled == Boolean.TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                putClientProperty("JPopupMenu.firePopupMenuCanceled", Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                firePopupMenuCanceled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            getSelectionModel().clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            // This is a popup menu with MenuElement children,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            // set selection path before popping up!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            if (isPopupMenu()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                MenuElement me[] = new MenuElement[1];
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   782
                me[0] = this;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                MenuSelectionManager.defaultManager().setSelectedPath(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        if(b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            firePopupMenuWillBecomeVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            popup = getPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            firePropertyChange("visible", Boolean.FALSE, Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        } else if(popup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            firePopupMenuWillBecomeInvisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            popup.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            popup = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            firePropertyChange("visible", Boolean.TRUE, Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            // 4694797: When popup menu is made invisible, selected path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            // should be cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            if (isPopupMenu()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                MenuSelectionManager.defaultManager().clearSelectedPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * Returns a <code>Popup</code> instance from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * <code>PopupMenuUI</code> that has had <code>show</code> invoked on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * it. If the current <code>popup</code> is non-null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * this will invoke <code>dispose</code> of it, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * <code>show</code> the new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * This does NOT fire any events, it is up the caller to dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * the necessary events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    private Popup getPopup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        Popup oldPopup = popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        if (oldPopup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            oldPopup.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        PopupFactory popupFactory = PopupFactory.getSharedInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (isLightWeightPopupEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            popupFactory.setPopupType(PopupFactory.LIGHT_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            popupFactory.setPopupType(PopupFactory.MEDIUM_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        // adjust the location of the popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        Point p = adjustPopupLocationToFitScreen(desiredLocationX,desiredLocationY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        desiredLocationX = p.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        desiredLocationY = p.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        Popup newPopup = getUI().getPopup(this, desiredLocationX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                                          desiredLocationY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        popupFactory.setPopupType(PopupFactory.LIGHT_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        newPopup.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        return newPopup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * Returns true if the popup menu is visible (currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * being displayed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    public boolean isVisible() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   849
        return popup != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Sets the location of the upper left corner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * popup menu using x, y coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @param x the x coordinate of the popup's new position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *          in the screen's coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @param y the y coordinate of the popup's new position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *          in the screen's coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * description: The location of the popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    public void setLocation(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        int oldX = desiredLocationX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        int oldY = desiredLocationY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        desiredLocationX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        desiredLocationY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        if(popup != null && (x != oldX || y != oldY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            popup = getPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * Returns true if the popup menu is a standalone popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * rather than the submenu of a <code>JMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @return true if this menu is a standalone popup menu, otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    private boolean isPopupMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        return  ((invoker != null) && !(invoker instanceof JMenu));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * Returns the component which is the 'invoker' of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @return the <code>Component</code> in which the popup menu is displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    public Component getInvoker() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        return this.invoker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * Sets the invoker of this popup menu -- the component in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * the popup menu menu is to be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * @param invoker the <code>Component</code> in which the popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     *          menu is displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * description: The invoking component for the popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *      expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    public void setInvoker(Component invoker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        Component oldInvoker = this.invoker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        this.invoker = invoker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        if ((oldInvoker != this.invoker) && (ui != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            ui.uninstallUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * Displays the popup menu at the position x,y in the coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * space of the component invoker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * @param invoker the component in whose space the popup menu is to appear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @param x the x coordinate in invoker's coordinate space at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * the popup menu is to be displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * @param y the y coordinate in invoker's coordinate space at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * the popup menu is to be displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    public void show(Component invoker, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            System.out.println("in JPopupMenu.show " );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        setInvoker(invoker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        Frame newFrame = getFrame(invoker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        if (newFrame != frame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            // Use the invoker's frame so that events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            // are propagated properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            if (newFrame!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                this.frame = newFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                if(popup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                    setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        Point invokerOrigin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        if (invoker != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            invokerOrigin = invoker.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            // To avoid integer overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            long lx, ly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            lx = ((long) invokerOrigin.x) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                 ((long) x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            ly = ((long) invokerOrigin.y) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                 ((long) y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            if(lx > Integer.MAX_VALUE) lx = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            if(lx < Integer.MIN_VALUE) lx = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            if(ly > Integer.MAX_VALUE) ly = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            if(ly < Integer.MIN_VALUE) ly = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            setLocation((int) lx, (int) ly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            setLocation(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * Returns the popup menu which is at the root of the menu system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * for this popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * @return the topmost grandparent <code>JPopupMenu</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    JPopupMenu getRootPopupMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        JPopupMenu mp = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        while((mp!=null) && (mp.isPopupMenu()!=true) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
              (mp.getInvoker() != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
              (mp.getInvoker().getParent() != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
              (mp.getInvoker().getParent() instanceof JPopupMenu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
              ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            mp = (JPopupMenu) mp.getInvoker().getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        return mp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * Returns the component at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * @param i  the index of the component, where 0 is the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * @return the <code>Component</code> at that index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * @deprecated replaced by {@link java.awt.Container#getComponent(int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    public Component getComponentAtIndex(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        return getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * Returns the index of the specified component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * @param  c the <code>Component</code> to find
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @return the index of the component, where 0 is the first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     *         or -1 if the component is not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    public int getComponentIndex(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        int ncomponents = this.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        Component[] component = this.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            if (comp == c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * Sets the size of the Popup window using a <code>Dimension</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * This is equivalent to <code>setPreferredSize(d)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * @param d   the <code>Dimension</code> specifying the new size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * of this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * description: The size of the popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    public void setPopupSize(Dimension d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        Dimension oldSize = getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        setPreferredSize(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        if (popup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            Dimension newSize = getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            if (!oldSize.equals(newSize)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                popup = getPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * Sets the size of the Popup window to the specified width and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * height. This is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     *  <code>setPreferredSize(new Dimension(width, height))</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * @param width the new width of the Popup in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * @param height the new height of the Popup in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * description: The size of the popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    public void setPopupSize(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        setPopupSize(new Dimension(width, height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * Sets the currently selected component,  This will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * in a change to the selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @param sel the <code>Component</code> to select
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * description: The selected component on the popup menu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     *      expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     *      hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    public void setSelected(Component sel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        SingleSelectionModel model = getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        int index = getComponentIndex(sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        model.setSelectedIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * Checks whether the border should be painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * @return true if the border is painted, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @see #setBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    public boolean isBorderPainted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        return paintBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * Sets whether the border should be painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * @param b if true, the border is painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @see #isBorderPainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * description: Is the border of the popup menu painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    public void setBorderPainted(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        paintBorder = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * Paints the popup menu's border if the <code>borderPainted</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * property is <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * @param g  the <code>Graphics</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * @see JComponent#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * @see JComponent#setBorder
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    protected void paintBorder(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        if (isBorderPainted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            super.paintBorder(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * Returns the margin, in pixels, between the popup menu's border and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * its containees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * @return an <code>Insets</code> object containing the margin values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    public Insets getMargin() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        if(margin == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            return new Insets(0,0,0,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            return margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * Examines the list of menu items to determine whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * <code>popup</code> is a popup menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * @param popup  a <code>JPopupMenu</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * @return true if <code>popup</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    boolean isSubPopupMenu(JPopupMenu popup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        int ncomponents = this.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        Component[] component = this.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            if (comp instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                JMenu menu = (JMenu)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                JPopupMenu subPopup = menu.getPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                if (subPopup == popup)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                if (subPopup.isSubPopupMenu(popup))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    private static Frame getFrame(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        Component w = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        while(!(w instanceof Frame) && (w!=null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            w = w.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        return (Frame)w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * Returns a string representation of this <code>JPopupMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * @return  a string representation of this <code>JPopupMenu</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        String labelString = (label != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                              label : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        String paintBorderString = (paintBorder ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                                    "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        String marginString = (margin != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                              margin.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        String lightWeightPopupEnabledString = (isLightWeightPopupEnabled() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                                                "true" : "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        return super.paramString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            ",desiredLocationX=" + desiredLocationX +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            ",desiredLocationY=" + desiredLocationY +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        ",label=" + labelString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        ",lightWeightPopupEnabled=" + lightWeightPopupEnabledString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        ",margin=" + marginString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        ",paintBorder=" + paintBorderString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * Gets the AccessibleContext associated with this JPopupMenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * For JPopupMenus, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * AccessibleJPopupMenu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * A new AccessibleJPopupMenu instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * @return an AccessibleJPopupMenu that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     *         AccessibleContext of this JPopupMenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            accessibleContext = new AccessibleJPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * <code>JPopupMenu</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * Java Accessibility API appropriate to popup menu user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    protected class AccessibleJPopupMenu extends AccessibleJComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        implements PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
         * AccessibleJPopupMenu constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
         * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        protected AccessibleJPopupMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            JPopupMenu.this.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
         * @return an instance of AccessibleRole describing the role of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
         * the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            return AccessibleRole.POPUP_MENU;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
         * This method gets called when a bound property is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
         * @param e A <code>PropertyChangeEvent</code> object describing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
         * the event source and the property that has changed. Must not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
         * @throws NullPointerException if the parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
         * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            String propertyName = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            if (propertyName == "visible") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                if (e.getOldValue() == Boolean.FALSE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                    e.getNewValue() == Boolean.TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                    handlePopupIsVisibleEvent(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                } else if (e.getOldValue() == Boolean.TRUE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                           e.getNewValue() == Boolean.FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                    handlePopupIsVisibleEvent(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
         * Handles popup "visible" PropertyChangeEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        private void handlePopupIsVisibleEvent(boolean visible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            if (visible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                // notify listeners that the popup became visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                firePropertyChange(ACCESSIBLE_STATE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                                   null, AccessibleState.VISIBLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                // notify listeners that a popup list item is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                fireActiveDescendant();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                // notify listeners that the popup became hidden
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                firePropertyChange(ACCESSIBLE_STATE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                                   AccessibleState.VISIBLE, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
         * Fires AccessibleActiveDescendant PropertyChangeEvent to notify listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
         * on the popup menu invoker that a popup list item has been selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        private void fireActiveDescendant() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            if (JPopupMenu.this instanceof BasicComboPopup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                // get the popup list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                JList popupList = ((BasicComboPopup)JPopupMenu.this).getList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                if (popupList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                // get the first selected item
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                AccessibleContext ac = popupList.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                AccessibleSelection selection = ac.getAccessibleSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                if (selection == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                Accessible a = selection.getAccessibleSelection(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                AccessibleContext selectedItem = a.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                // fire the event with the popup invoker as the source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                if (selectedItem != null && invoker != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                    AccessibleContext invokerContext = invoker.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                    if (invokerContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                        // Check invokerContext because Component.getAccessibleContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                        // returns null. Classes that extend Component are responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                        // for returning a non-null AccessibleContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                        invokerContext.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                            ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                            null, selectedItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    } // inner class AccessibleJPopupMenu
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
// Serialization support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    private void writeObject(ObjectOutputStream s) throws IOException {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1309
        Vector<Object> values = new Vector<Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        // Save the invoker, if its Serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        if(invoker != null && invoker instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            values.addElement("invoker");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            values.addElement(invoker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        // Save the popup, if its Serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        if(popup != null && popup instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            values.addElement("popup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            values.addElement(popup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        s.writeObject(values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    // implements javax.swing.MenuElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        Vector          values = (Vector)s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        int             indexCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        int             maxCounter = values.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        if(indexCounter < maxCounter && values.elementAt(indexCounter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
           equals("invoker")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            invoker = (Component)values.elementAt(++indexCounter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            indexCounter++;
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("popup")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            popup = (Popup)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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * This method is required to conform to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * <code>MenuElement</code> interface, but it not implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * @see MenuElement#processMouseEvent(MouseEvent, MenuElement[], MenuSelectionManager)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    public void processMouseEvent(MouseEvent event,MenuElement path[],MenuSelectionManager manager) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * Processes a key event forwarded from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * <code>MenuSelectionManager</code> and changes the menu selection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * if necessary, by using <code>MenuSelectionManager</code>'s API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * Note: you do not have to forward the event to sub-components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * This is done automatically by the <code>MenuSelectionManager</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * @param e  a <code>KeyEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * @param path the <code>MenuElement</code> path array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * @param manager   the <code>MenuSelectionManager</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    public void processKeyEvent(KeyEvent e, MenuElement path[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                                MenuSelectionManager manager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        MenuKeyEvent mke = new MenuKeyEvent(e.getComponent(), e.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                                             e.getWhen(), e.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                                             e.getKeyCode(), e.getKeyChar(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                                             path, manager);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        processMenuKeyEvent(mke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        if (mke.isConsumed())  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * Handles a keystroke in a menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * @param e  a <code>MenuKeyEvent</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    private void processMenuKeyEvent(MenuKeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        switch (e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        case KeyEvent.KEY_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            fireMenuKeyPressed(e); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        case KeyEvent.KEY_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            fireMenuKeyReleased(e); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        case KeyEvent.KEY_TYPED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            fireMenuKeyTyped(e); break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * notification on this event type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     * @param event a <code>MenuKeyEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    private void fireMenuKeyPressed(MenuKeyEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            if (listeners[i]==MenuKeyListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                ((MenuKeyListener)listeners[i+1]).menuKeyPressed(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * notification on this event type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * @param event a <code>MenuKeyEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    private void fireMenuKeyReleased(MenuKeyEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            if (listeners[i]==MenuKeyListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                ((MenuKeyListener)listeners[i+1]).menuKeyReleased(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * notification on this event type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * @param event a <code>MenuKeyEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    private void fireMenuKeyTyped(MenuKeyEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        for (int i = listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            if (listeners[i]==MenuKeyListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                ((MenuKeyListener)listeners[i+1]).menuKeyTyped(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * Messaged when the menubar selection changes to activate or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * deactivate this menu. This implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * <code>javax.swing.MenuElement</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * Overrides <code>MenuElement.menuSelectionChanged</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * @param isIncluded  true if this menu is active, false if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     *        it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * @see MenuElement#menuSelectionChanged(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    public void menuSelectionChanged(boolean isIncluded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            System.out.println("In JPopupMenu.menuSelectionChanged " + isIncluded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        if(invoker instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            JMenu m = (JMenu) invoker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            if(isIncluded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                m.setPopupMenuVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                m.setPopupMenuVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        if (isPopupMenu() && !isIncluded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
          setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * Returns an array of <code>MenuElement</code>s containing the submenu
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * for this menu component.  It will only return items conforming to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * the <code>JMenuElement</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * If popup menu is <code>null</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * an empty array.  This method is required to conform to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * <code>MenuElement</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * @return an array of <code>MenuElement</code> objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * @see MenuElement#getSubElements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
    public MenuElement[] getSubElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        MenuElement result[];
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1492
        Vector<MenuElement> tmp = new Vector<MenuElement>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        int c = getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        Component m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        for(i=0 ; i < c ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            m = getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            if(m instanceof MenuElement)
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1500
                tmp.addElement((MenuElement) m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        result = new MenuElement[tmp.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        for(i=0,c=tmp.size() ; i < c ; i++)
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
  1505
            result[i] = tmp.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * Returns this <code>JPopupMenu</code> component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     * @return this <code>JPopupMenu</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * @see MenuElement#getComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
    public Component getComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * A popup menu-specific separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    static public class Separator extends JSeparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        public Separator( )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            super( JSeparator.HORIZONTAL );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
         * Returns the name of the L&F class that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
         * @return the string "PopupMenuSeparatorUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
         * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
         * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        public String getUIClassID()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            return "PopupMenuSeparatorUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * Returns true if the <code>MouseEvent</code> is considered a popup trigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * by the <code>JPopupMenu</code>'s currently installed UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * @return true if the mouse event is a popup trigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    public boolean isPopupTrigger(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        return getUI().isPopupTrigger(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
}