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