jdk/src/java.desktop/share/classes/javax/swing/PopupFactory.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 25568 jdk/src/share/classes/javax/swing/PopupFactory.java@b906a74c6882
child 39001 d446b5342c35
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
     2
 * Copyright (c) 1999, 2014, 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: 2496
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: 2496
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: 2496
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2496
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2496
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
25120
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
    28
import sun.awt.EmbeddedFrame;
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
    29
import sun.awt.OSInfo;
25132
a778c3c853bb 8044516: [macosx] ScreenPopupFactory uses native method that could be avoided
pchelko
parents: 25120
diff changeset
    30
import sun.swing.SwingAccessor;
25120
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
    31
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.applet.Applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.event.WindowAdapter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.event.WindowEvent;
25120
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
    36
import java.security.AccessController;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import static javax.swing.ClientPropertyKey.PopupFactory_FORCE_HEAVYWEIGHT_POPUP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>PopupFactory</code>, as the name implies, is used to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * instances of <code>Popup</code>s. <code>Popup</code>s are used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * display a <code>Component</code> above all other <code>Component</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * in a particular containment hierarchy. The general contract is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * once you have obtained a <code>Popup</code> from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <code>PopupFactory</code>, you must invoke <code>hide</code> on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <code>Popup</code>. The typical usage is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   PopupFactory factory = PopupFactory.getSharedInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   Popup popup = factory.getPopup(owner, contents, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   popup.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   popup.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see Popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class PopupFactory {
25132
a778c3c853bb 8044516: [macosx] ScreenPopupFactory uses native method that could be avoided
pchelko
parents: 25120
diff changeset
    64
a778c3c853bb 8044516: [macosx] ScreenPopupFactory uses native method that could be avoided
pchelko
parents: 25120
diff changeset
    65
    static {
a778c3c853bb 8044516: [macosx] ScreenPopupFactory uses native method that could be avoided
pchelko
parents: 25120
diff changeset
    66
        SwingAccessor.setPopupFactoryAccessor(new SwingAccessor.PopupFactoryAccessor() {
a778c3c853bb 8044516: [macosx] ScreenPopupFactory uses native method that could be avoided
pchelko
parents: 25120
diff changeset
    67
            @Override
a778c3c853bb 8044516: [macosx] ScreenPopupFactory uses native method that could be avoided
pchelko
parents: 25120
diff changeset
    68
            public Popup getHeavyWeightPopup(PopupFactory factory, Component owner,
a778c3c853bb 8044516: [macosx] ScreenPopupFactory uses native method that could be avoided
pchelko
parents: 25120
diff changeset
    69
                                             Component contents, int ownerX, int ownerY) {
a778c3c853bb 8044516: [macosx] ScreenPopupFactory uses native method that could be avoided
pchelko
parents: 25120
diff changeset
    70
                return factory.getPopup(owner, contents, ownerX, ownerY, HEAVY_WEIGHT_POPUP);
a778c3c853bb 8044516: [macosx] ScreenPopupFactory uses native method that could be avoided
pchelko
parents: 25120
diff changeset
    71
            }
a778c3c853bb 8044516: [macosx] ScreenPopupFactory uses native method that could be avoided
pchelko
parents: 25120
diff changeset
    72
        });
a778c3c853bb 8044516: [macosx] ScreenPopupFactory uses native method that could be avoided
pchelko
parents: 25120
diff changeset
    73
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The shared instanceof <code>PopupFactory</code> is per
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <code>AppContext</code>. This is the key used in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <code>AppContext</code> to locate the <code>PopupFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final Object SharedInstanceKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        new StringBuffer("PopupFactory.SharedInstanceKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Max number of items to store in any one particular cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final int MAX_CACHE_SIZE = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Key used to indicate a light weight popup should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static final int LIGHT_WEIGHT_POPUP   = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Key used to indicate a medium weight Popup should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    static final int MEDIUM_WEIGHT_POPUP  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Key used to indicate a heavy weight Popup should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static final int HEAVY_WEIGHT_POPUP   = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Default type of Popup to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private int popupType = LIGHT_WEIGHT_POPUP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Sets the <code>PopupFactory</code> that will be used to obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <code>Popup</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * This will throw an <code>IllegalArgumentException</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <code>factory</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param factory Shared PopupFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @exception IllegalArgumentException if <code>factory</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @see #getPopup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static void setSharedInstance(PopupFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (factory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new IllegalArgumentException("PopupFactory can not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        SwingUtilities.appContextPut(SharedInstanceKey, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Returns the shared <code>PopupFactory</code> which can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * to obtain <code>Popup</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @return Shared PopupFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static PopupFactory getSharedInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        PopupFactory factory = (PopupFactory)SwingUtilities.appContextGet(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                         SharedInstanceKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (factory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            factory = new PopupFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            setSharedInstance(factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Provides a hint as to the type of <code>Popup</code> that should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    void setPopupType(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        popupType = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Returns the preferred type of Popup to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    int getPopupType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return popupType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Creates a <code>Popup</code> for the Component <code>owner</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * containing the Component <code>contents</code>. <code>owner</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * is used to determine which <code>Window</code> the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <code>Popup</code> will parent the <code>Component</code> the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <code>Popup</code> creates to. A null <code>owner</code> implies there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * is no valid parent. <code>x</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <code>y</code> specify the preferred initial location to place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * the <code>Popup</code> at. Based on screen size, or other paramaters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * the <code>Popup</code> may not display at <code>x</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <code>y</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param owner    Component mouse coordinates are relative to, may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param contents Contents of the Popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param x        Initial x screen coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param y        Initial y screen coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @exception IllegalArgumentException if contents is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return Popup containing Contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public Popup getPopup(Component owner, Component contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                          int x, int y) throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (contents == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                          "Popup.getPopup must be passed non-null contents");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        int popupType = getPopupType(owner, contents, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        Popup popup = getPopup(owner, contents, x, y, popupType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (popup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // Didn't fit, force to heavy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            popup = getPopup(owner, contents, x, y, HEAVY_WEIGHT_POPUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Returns the popup type to use for the specified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private int getPopupType(Component owner, Component contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                             int ownerX, int ownerY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int popupType = getPopupType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (owner == null || invokerInHeavyWeightPopup(owner)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            popupType = HEAVY_WEIGHT_POPUP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        else if (popupType == LIGHT_WEIGHT_POPUP &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                 !(contents instanceof JToolTip) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                 !(contents instanceof JPopupMenu)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            popupType = MEDIUM_WEIGHT_POPUP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // Check if the parent component is an option pane.  If so we need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // force a heavy weight popup in order to have event dispatching work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        // correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        Component c = owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        while (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (c instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                if (((JComponent)c).getClientProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                            PopupFactory_FORCE_HEAVYWEIGHT_POPUP) == Boolean.TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    popupType = HEAVY_WEIGHT_POPUP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            c = c.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return popupType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Obtains the appropriate <code>Popup</code> based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <code>popupType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private Popup getPopup(Component owner, Component contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                           int ownerX, int ownerY, int popupType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return getHeadlessPopup(owner, contents, ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        switch(popupType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        case LIGHT_WEIGHT_POPUP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return getLightWeightPopup(owner, contents, ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        case MEDIUM_WEIGHT_POPUP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return getMediumWeightPopup(owner, contents, ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        case HEAVY_WEIGHT_POPUP:
25120
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   244
            Popup popup = getHeavyWeightPopup(owner, contents, ownerX, ownerY);
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   245
            if ((AccessController.doPrivileged(OSInfo.getOSTypeAction()) ==
25558
6ecf203ea747 8048506: [macosx] javax.swing.PopupFactory issue with null owner
dmarkov
parents: 25132
diff changeset
   246
                OSInfo.OSType.MACOSX) && (owner != null) &&
6ecf203ea747 8048506: [macosx] javax.swing.PopupFactory issue with null owner
dmarkov
parents: 25132
diff changeset
   247
                (EmbeddedFrame.getAppletIfAncestorOf(owner) != null)) {
25120
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   248
                ((HeavyWeightPopup)popup).setCacheEnabled(false);
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   249
            }
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   250
            return popup;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Creates a headless popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private Popup getHeadlessPopup(Component owner, Component contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                   int ownerX, int ownerY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return HeadlessPopup.getHeadlessPopup(owner, contents, ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Creates a light weight popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private Popup getLightWeightPopup(Component owner, Component contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                         int ownerX, int ownerY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return LightWeightPopup.getLightWeightPopup(owner, contents, ownerX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                                    ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Creates a medium weight popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private Popup getMediumWeightPopup(Component owner, Component contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                                          int ownerX, int ownerY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return MediumWeightPopup.getMediumWeightPopup(owner, contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                                      ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Creates a heavy weight popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    private Popup getHeavyWeightPopup(Component owner, Component contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                         int ownerX, int ownerY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return getMediumWeightPopup(owner, contents, ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return HeavyWeightPopup.getHeavyWeightPopup(owner, contents, ownerX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                                    ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Returns true if the Component <code>i</code> inside a heavy weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <code>Popup</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private boolean invokerInHeavyWeightPopup(Component i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (i != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            Container parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            for(parent = i.getParent() ; parent != null ; parent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    parent.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                if (parent instanceof Popup.HeavyWeightWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Popup implementation that uses a Window as the popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private static class HeavyWeightPopup extends Popup {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        private static final Object heavyWeightPopupCacheKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                 new StringBuffer("PopupFactory.heavyWeightPopupCache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
25120
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   318
        private volatile boolean isCacheEnabled = true;
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   319
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * Returns either a new or recycled <code>Popup</code> containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * the specified children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        static Popup getHeavyWeightPopup(Component owner, Component contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                         int ownerX, int ownerY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            Window window = (owner != null) ? SwingUtilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                              getWindowAncestor(owner) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            HeavyWeightPopup popup = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (window != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                popup = getRecycledHeavyWeightPopup(window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            boolean focusPopup = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if(contents != null && contents.isFocusable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if(contents instanceof JPopupMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    JPopupMenu jpm = (JPopupMenu) contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    Component popComps[] = jpm.getComponents();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   339
                    for (Component popComp : popComps) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   340
                        if (!(popComp instanceof MenuElement) &&
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   341
                                !(popComp instanceof JSeparator)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                            focusPopup = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (popup == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                ((JWindow) popup.getComponent())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                 .getFocusableWindowState() != focusPopup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if(popup != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    // The recycled popup can't serve us well
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    // dispose it and create new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    popup._dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                popup = new HeavyWeightPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            popup.reset(owner, contents, ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            if(focusPopup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                JWindow wnd = (JWindow) popup.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                wnd.setFocusableWindowState(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                // Set window name. We need this in BasicPopupMenuUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                // to identify focusable popup window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                wnd.setName("###focusableSwingPopup###");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         * Returns a previously disposed heavy weight <code>Popup</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         * associated with <code>window</code>. This will return null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         * there is no <code>HeavyWeightPopup</code> associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
         * <code>window</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        private static HeavyWeightPopup getRecycledHeavyWeightPopup(Window w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            synchronized (HeavyWeightPopup.class) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   383
                List<HeavyWeightPopup> cache;
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   384
                Map<Window, List<HeavyWeightPopup>> heavyPopupCache = getHeavyWeightPopupCache();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                if (heavyPopupCache.containsKey(w)) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   387
                    cache = heavyPopupCache.get(w);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                }
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   391
                if (cache.size() > 0) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   392
                    HeavyWeightPopup r = cache.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    cache.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         * Returns the cache to use for heavy weight popups. Maps from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         * <code>Window</code> to a <code>List</code> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         * <code>HeavyWeightPopup</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 25558
diff changeset
   405
        @SuppressWarnings("unchecked")
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   406
        private static Map<Window, List<HeavyWeightPopup>> getHeavyWeightPopupCache() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            synchronized (HeavyWeightPopup.class) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   408
                Map<Window, List<HeavyWeightPopup>> cache = (Map<Window, List<HeavyWeightPopup>>)SwingUtilities.appContextGet(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                  heavyWeightPopupCacheKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                if (cache == null) {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 25558
diff changeset
   412
                    cache = new HashMap<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    SwingUtilities.appContextPut(heavyWeightPopupCacheKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                                                 cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                return cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         * Recycles the passed in <code>HeavyWeightPopup</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        private static void recycleHeavyWeightPopup(HeavyWeightPopup popup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            synchronized (HeavyWeightPopup.class) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   425
                List<HeavyWeightPopup> cache;
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   426
                Window window = SwingUtilities.getWindowAncestor(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                                     popup.getComponent());
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   428
                Map<Window, List<HeavyWeightPopup>> heavyPopupCache = getHeavyWeightPopupCache();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                if (window instanceof Popup.DefaultFrame ||
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   431
                                      !window.isVisible()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    // If the Window isn't visible, we don't cache it as we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    // likely won't ever get a windowClosed event to clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    // We also don't cache DefaultFrames as this indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    // there wasn't a valid Window parent, and thus we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    // know when to clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    popup._dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                } else if (heavyPopupCache.containsKey(window)) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   440
                    cache = heavyPopupCache.get(window);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                } else {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   442
                    cache = new ArrayList<HeavyWeightPopup>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    heavyPopupCache.put(window, cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    // Clean up if the Window is closed
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   445
                    final Window w = window;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    w.addWindowListener(new WindowAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        public void windowClosed(WindowEvent e) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   449
                            List<HeavyWeightPopup> popups;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                            synchronized(HeavyWeightPopup.class) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   452
                                Map<Window, List<HeavyWeightPopup>> heavyPopupCache2 =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                              getHeavyWeightPopupCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   455
                                popups = heavyPopupCache2.remove(w);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                            if (popups != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                for (int counter = popups.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                                   counter >= 0; counter--) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   460
                                    popups.get(counter)._dispose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                if(cache.size() < MAX_CACHE_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    cache.add(popup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    popup._dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
25120
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   475
        /**
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   476
         * Enables or disables cache for current object.
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   477
         */
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   478
        void setCacheEnabled(boolean enable) {
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   479
            isCacheEnabled = enable;
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   480
        }
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   481
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        // Popup methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        public void hide() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            super.hide();
25120
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   487
            if (isCacheEnabled) {
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   488
                recycleHeavyWeightPopup(this);
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   489
            } else {
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   490
                this._dispose();
8b6a41c3752f 8042465: Applet menus not rendering when browser is full screen on Mac
dmarkov
parents: 23697
diff changeset
   491
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         * As we recycle the <code>Window</code>, we don't want to dispose it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
         * thus this method does nothing, instead use <code>_dipose</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         * which will handle the disposing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        void _dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            super.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * ContainerPopup consolidates the common code used in the light/medium
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * weight implementations of <code>Popup</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    private static class ContainerPopup extends Popup {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        /** Component we are to be added to. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        Component owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        /** Desired x location. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        int x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        /** Desired y location. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        int y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        public void hide() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            Component component = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            if (component != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                Container parent = component.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    Rectangle bounds = component.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    parent.remove(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    parent.repaint(bounds.x, bounds.y, bounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                                   bounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            owner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        public void pack() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            Component component = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if (component != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                component.setSize(component.getPreferredSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        void reset(Component owner, Component contents, int ownerX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                   int ownerY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            if ((owner instanceof JFrame) || (owner instanceof JDialog) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                                                 (owner instanceof JWindow)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                // Force the content to be added to the layered pane, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                // we'll get an exception when adding to the RootPaneContainer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                owner = ((RootPaneContainer)owner).getLayeredPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            super.reset(owner, contents, ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            x = ownerX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            y = ownerY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            this.owner = owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        boolean overlappedByOwnedWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            Component component = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            if(owner != null && component != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                Window w = SwingUtilities.getWindowAncestor(owner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                if (w == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                Window[] ownedWindows = w.getOwnedWindows();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                if(ownedWindows != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    Rectangle bnd = component.getBounds();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   570
                    for (Window window : ownedWindows) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   571
                        if (window.isVisible() &&
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   572
                                bnd.intersects(window.getBounds())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        /**
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   583
         * Returns true if popup can fit the screen and the owner's top parent.
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   584
         * It determines can popup be lightweight or mediumweight.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        boolean fitsOnScreen() {
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   587
            boolean result = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            Component component = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            if (owner != null && component != null) {
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   590
                int popupWidth = component.getWidth();
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   591
                int popupHeight = component.getHeight();
2496
c0dfcd9b27f7 6740974: api/javax_swing/PopupFactory/index.html#Ctor[PopupFactory2002] fails with NPE
peterz
parents: 1301
diff changeset
   592
c0dfcd9b27f7 6740974: api/javax_swing/PopupFactory/index.html#Ctor[PopupFactory2002] fails with NPE
peterz
parents: 1301
diff changeset
   593
                Container parent = (Container) SwingUtilities.getRoot(owner);
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   594
                if (parent instanceof JFrame ||
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   595
                    parent instanceof JDialog ||
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   596
                    parent instanceof JWindow) {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   597
2496
c0dfcd9b27f7 6740974: api/javax_swing/PopupFactory/index.html#Ctor[PopupFactory2002] fails with NPE
peterz
parents: 1301
diff changeset
   598
                    Rectangle parentBounds = parent.getBounds();
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   599
                    Insets i = parent.getInsets();
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   600
                    parentBounds.x += i.left;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   601
                    parentBounds.y += i.top;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   602
                    parentBounds.width -= i.left + i.right;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   603
                    parentBounds.height -= i.top + i.bottom;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   605
                    if (JPopupMenu.canPopupOverlapTaskBar()) {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   606
                        GraphicsConfiguration gc =
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   607
                                parent.getGraphicsConfiguration();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        Rectangle popupArea = getContainerPopupArea(gc);
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   609
                        result = parentBounds.intersection(popupArea)
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   610
                                .contains(x, y, popupWidth, popupHeight);
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   611
                    } else {
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   612
                        result = parentBounds
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   613
                                .contains(x, y, popupWidth, popupHeight);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    }
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   615
                } else if (parent instanceof JApplet) {
2496
c0dfcd9b27f7 6740974: api/javax_swing/PopupFactory/index.html#Ctor[PopupFactory2002] fails with NPE
peterz
parents: 1301
diff changeset
   616
                    Rectangle parentBounds = parent.getBounds();
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   617
                    Point p = parent.getLocationOnScreen();
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   618
                    parentBounds.x = p.x;
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   619
                    parentBounds.y = p.y;
2496
c0dfcd9b27f7 6740974: api/javax_swing/PopupFactory/index.html#Ctor[PopupFactory2002] fails with NPE
peterz
parents: 1301
diff changeset
   620
                    result = parentBounds.contains(x, y, popupWidth, popupHeight);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
675
4f26ea16c5c1 6694823: A popup menu can be partially hidden under the task bar in applets
mlapshin
parents: 2
diff changeset
   623
            return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        Rectangle getContainerPopupArea(GraphicsConfiguration gc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            Rectangle screenBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            Toolkit toolkit = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            Insets insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            if(gc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                // If we have GraphicsConfiguration use it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                // to get screen bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                screenBounds = gc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                insets = toolkit.getScreenInsets(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                // If we don't have GraphicsConfiguration use primary screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                screenBounds = new Rectangle(toolkit.getScreenSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                insets = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            // Take insets into account
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            screenBounds.x += insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            screenBounds.y += insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            screenBounds.width -= (insets.left + insets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            screenBounds.height -= (insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            return screenBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * Popup implementation that is used in headless environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    private static class HeadlessPopup extends ContainerPopup {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        static Popup getHeadlessPopup(Component owner, Component contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                                      int ownerX, int ownerY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            HeadlessPopup popup = new HeadlessPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            popup.reset(owner, contents, ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            return popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        Component createComponent(Component owner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            return new Panel(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        public void show() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        public void hide() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * Popup implementation that uses a JPanel as the popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    private static class LightWeightPopup extends ContainerPopup {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        private static final Object lightWeightPopupCacheKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                         new StringBuffer("PopupFactory.lightPopupCache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
         * Returns a light weight <code>Popup</code> implementation. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
         * the <code>Popup</code> needs more space that in available in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
         * <code>owner</code>, this will return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        static Popup getLightWeightPopup(Component owner, Component contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                                         int ownerX, int ownerY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            LightWeightPopup popup = getRecycledLightWeightPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            if (popup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                popup = new LightWeightPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            popup.reset(owner, contents, ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            if (!popup.fitsOnScreen() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                 popup.overlappedByOwnedWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                popup.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            return popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
         * Returns the cache to use for heavy weight popups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 25558
diff changeset
   703
        @SuppressWarnings("unchecked")
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   704
        private static List<LightWeightPopup> getLightWeightPopupCache() {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   705
            List<LightWeightPopup> cache = (List<LightWeightPopup>)SwingUtilities.appContextGet(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                                   lightWeightPopupCacheKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            if (cache == null) {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 25558
diff changeset
   708
                cache = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                SwingUtilities.appContextPut(lightWeightPopupCacheKey, cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            return cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
         * Recycles the LightWeightPopup <code>popup</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        private static void recycleLightWeightPopup(LightWeightPopup popup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            synchronized (LightWeightPopup.class) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   719
                List<LightWeightPopup> lightPopupCache = getLightWeightPopupCache();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                if (lightPopupCache.size() < MAX_CACHE_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    lightPopupCache.add(popup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
         * Returns a previously used <code>LightWeightPopup</code>, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
         * if none of the popups have been recycled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        private static LightWeightPopup getRecycledLightWeightPopup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            synchronized (LightWeightPopup.class) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   732
                List<LightWeightPopup> lightPopupCache = getLightWeightPopupCache();
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   733
                if (lightPopupCache.size() > 0) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   734
                    LightWeightPopup r = lightPopupCache.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    lightPopupCache.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        // Popup methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        public void hide() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            super.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            Container component = (Container)getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            component.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            recycleLightWeightPopup(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        public void show() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            Container parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            if (owner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                parent = (owner instanceof Container? (Container)owner : owner.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            // Try to find a JLayeredPane and Window to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            for (Container p = parent; p != null; p = p.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                if (p instanceof JRootPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                    if (p.getParent() instanceof JInternalFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    parent = ((JRootPane)p).getLayeredPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    // Continue, so that if there is a higher JRootPane, we'll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                    // pick it up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                } else if(p instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    if (parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                        parent = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                } else if (p instanceof JApplet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                    // Painting code stops at Applets, we don't want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    // to add to a Component above an Applet otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                    // you'll never see it painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            Point p = SwingUtilities.convertScreenLocationToParent(parent, x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                                                                   y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            Component component = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            component.setLocation(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            if (parent instanceof JLayeredPane) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   790
                parent.add(component, JLayeredPane.POPUP_LAYER, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                parent.add(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        Component createComponent(Component owner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            JComponent component = new JPanel(new BorderLayout(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            component.setOpaque(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            return component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        // Local methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
         * Resets the <code>Popup</code> to an initial state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        void reset(Component owner, Component contents, int ownerX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                   int ownerY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            super.reset(owner, contents, ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            JComponent component = (JComponent)getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            component.setOpaque(contents.isOpaque());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            component.setLocation(ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            component.add(contents, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            contents.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * Popup implementation that uses a Panel as the popup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    private static class MediumWeightPopup extends ContainerPopup {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        private static final Object mediumWeightPopupCacheKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                             new StringBuffer("PopupFactory.mediumPopupCache");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        /** Child of the panel. The contents are added to this. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        private JRootPane rootPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         * Returns a medium weight <code>Popup</code> implementation. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         * the <code>Popup</code> needs more space that in available in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         * <code>owner</code>, this will return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        static Popup getMediumWeightPopup(Component owner, Component contents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                                          int ownerX, int ownerY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            MediumWeightPopup popup = getRecycledMediumWeightPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            if (popup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                popup = new MediumWeightPopup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            popup.reset(owner, contents, ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            if (!popup.fitsOnScreen() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                 popup.overlappedByOwnedWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                popup.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            return popup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
         * Returns the cache to use for medium weight popups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
         */
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 25558
diff changeset
   860
        @SuppressWarnings("unchecked")
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   861
        private static List<MediumWeightPopup> getMediumWeightPopupCache() {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   862
            List<MediumWeightPopup> cache = (List<MediumWeightPopup>)SwingUtilities.appContextGet(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                                    mediumWeightPopupCacheKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            if (cache == null) {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 25558
diff changeset
   866
                cache = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                SwingUtilities.appContextPut(mediumWeightPopupCacheKey, cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            return cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
         * Recycles the MediumWeightPopup <code>popup</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        private static void recycleMediumWeightPopup(MediumWeightPopup popup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            synchronized (MediumWeightPopup.class) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   877
                List<MediumWeightPopup> mediumPopupCache = getMediumWeightPopupCache();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                if (mediumPopupCache.size() < MAX_CACHE_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                    mediumPopupCache.add(popup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
         * Returns a previously used <code>MediumWeightPopup</code>, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
         * if none of the popups have been recycled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        private static MediumWeightPopup getRecycledMediumWeightPopup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            synchronized (MediumWeightPopup.class) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   890
                List<MediumWeightPopup> mediumPopupCache = getMediumWeightPopupCache();
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   891
                if (mediumPopupCache.size() > 0) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   892
                    MediumWeightPopup r = mediumPopupCache.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                    mediumPopupCache.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                    return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        // Popup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        public void hide() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            super.hide();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            rootPane.getContentPane().removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            recycleMediumWeightPopup(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        public void show() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            Component component = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            Container parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            if (owner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                parent = owner.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
              Find the top level window,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
              if it has a layered pane,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
              add to that, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
              add to the window. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            while (!(parent instanceof Window || parent instanceof Applet) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                   (parent!=null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                parent = parent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            // Set the visibility to false before adding to workaround a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            // bug in Solaris in which the Popup gets added at the wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            // location, which will result in a mouseExit, which will then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            // result in the ToolTip being removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            if (parent instanceof RootPaneContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                parent = ((RootPaneContainer)parent).getLayeredPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                Point p = SwingUtilities.convertScreenLocationToParent(parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                                                                       x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                component.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                component.setLocation(p.x, p.y);
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 715
diff changeset
   936
                parent.add(component, JLayeredPane.POPUP_LAYER,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                                           0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                Point p = SwingUtilities.convertScreenLocationToParent(parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                                                                       x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                component.setLocation(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                component.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                parent.add(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            component.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        Component createComponent(Component owner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            Panel component = new MediumWeightComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            rootPane = new JRootPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            // NOTE: this uses setOpaque vs LookAndFeel.installProperty as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            // there is NO reason for the RootPane not to be opaque. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            // painting to work the contentPane must be opaque, therefor the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            // RootPane can also be opaque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            rootPane.setOpaque(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            component.add(rootPane, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            return component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
         * Resets the <code>Popup</code> to an initial state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        void reset(Component owner, Component contents, int ownerX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                   int ownerY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            super.reset(owner, contents, ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            Component component = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            component.setLocation(ownerX, ownerY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            rootPane.getContentPane().add(contents, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            contents.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            component.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        // This implements SwingHeavyWeight so that repaints on it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        // are processed by the RepaintManager and SwingPaintEventDispatcher.
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
   981
        @SuppressWarnings("serial") // JDK-implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        private static class MediumWeightComponent extends Panel implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                                                           SwingHeavyWeight {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            MediumWeightComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                super(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
}