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