1 /*
   2  * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package javax.swing;
  27 
  28 import java.awt.*;
  29 
  30 import sun.awt.ModalExclude;
  31 import sun.awt.SunToolkit;
  32 
  33 /**
  34  * Popups are used to display a <code>Component</code> to the user, typically
  35  * on top of all the other <code>Component</code>s in a particular containment
  36  * hierarchy. <code>Popup</code>s have a very small life cycle. Once you
  37  * have obtained a <code>Popup</code>, and hidden it (invoked the
  38  * <code>hide</code> method), you should no longer
  39  * invoke any methods on it. This allows the <code>PopupFactory</code> to cache
  40  * <code>Popup</code>s for later use.
  41  * <p>
  42  * The general contract is that if you need to change the size of the
  43  * <code>Component</code>, or location of the <code>Popup</code>, you should
  44  * obtain a new <code>Popup</code>.
  45  * <p>
  46  * <code>Popup</code> does not descend from <code>Component</code>, rather
  47  * implementations of <code>Popup</code> are responsible for creating
  48  * and maintaining their own <code>Component</code>s to render the
  49  * requested <code>Component</code> to the user.
  50  * <p>
  51  * You typically do not explicitly create an instance of <code>Popup</code>,
  52  * instead obtain one from a <code>PopupFactory</code>.
  53  *
  54  * @see PopupFactory
  55  *
  56  * @since 1.4
  57  */
  58 public class Popup {
  59     /**
  60      * The Component representing the Popup.
  61      */
  62     private Component component;
  63 
  64     /**
  65      * Creates a <code>Popup</code> for the Component <code>owner</code>
  66      * containing the Component <code>contents</code>. <code>owner</code>
  67      * is used to determine which <code>Window</code> the new
  68      * <code>Popup</code> will parent the <code>Component</code> the
  69      * <code>Popup</code> creates to.
  70      * A null <code>owner</code> implies there is no valid parent.
  71      * <code>x</code> and
  72      * <code>y</code> specify the preferred initial location to place
  73      * the <code>Popup</code> at. Based on screen size, or other paramaters,
  74      * the <code>Popup</code> may not display at <code>x</code> and
  75      * <code>y</code>.
  76      *
  77      * @param owner    Component mouse coordinates are relative to, may be null
  78      * @param contents Contents of the Popup
  79      * @param x        Initial x screen coordinate
  80      * @param y        Initial y screen coordinate
  81      * @exception IllegalArgumentException if contents is null
  82      */
  83     protected Popup(Component owner, Component contents, int x, int y) {
  84         this();
  85         if (contents == null) {
  86             throw new IllegalArgumentException("Contents must be non-null");
  87         }
  88         reset(owner, contents, x, y);
  89     }
  90 
  91     /**
  92      * Creates a <code>Popup</code>. This is provided for subclasses.
  93      */
  94     protected Popup() {
  95     }
  96 
  97     /**
  98      * Makes the <code>Popup</code> visible. If the <code>Popup</code> is
  99      * currently visible, this has no effect.
 100      */
 101     public void show() {
 102         Component component = getComponent();
 103 
 104         if (component != null) {
 105             component.show();
 106         }
 107     }
 108 
 109     /**
 110      * Hides and disposes of the <code>Popup</code>. Once a <code>Popup</code>
 111      * has been disposed you should no longer invoke methods on it. A
 112      * <code>dispose</code>d <code>Popup</code> may be reclaimed and later used
 113      * based on the <code>PopupFactory</code>. As such, if you invoke methods
 114      * on a <code>disposed</code> <code>Popup</code>, indeterminate
 115      * behavior will result.
 116      */
 117     public void hide() {
 118         Component component = getComponent();
 119 
 120         if (component instanceof JWindow) {
 121             component.hide();
 122             ((JWindow)component).getContentPane().removeAll();
 123         }
 124         dispose();
 125     }
 126 
 127     /**
 128      * Frees any resources the <code>Popup</code> may be holding onto.
 129      */
 130     void dispose() {
 131         Component component = getComponent();
 132         Window window = SwingUtilities.getWindowAncestor(component);
 133 
 134         if (component instanceof JWindow) {
 135             ((Window)component).dispose();
 136             component = null;
 137         }
 138         // If our parent is a DefaultFrame, we need to dispose it, too.
 139         if (window instanceof DefaultFrame) {
 140             window.dispose();
 141         }
 142     }
 143 
 144     /**
 145      * Resets the <code>Popup</code> to an initial state.
 146      */
 147     void reset(Component owner, Component contents, int ownerX, int ownerY) {
 148         if (getComponent() == null) {
 149             component = createComponent(owner);
 150         }
 151 
 152         Component c = getComponent();
 153 
 154         if (c instanceof JWindow) {
 155             JWindow component = (JWindow)getComponent();
 156 
 157             component.setLocation(ownerX, ownerY);
 158             component.getContentPane().add(contents, BorderLayout.CENTER);
 159             contents.invalidate();
 160             if(component.isVisible()) {
 161                 // Do not call pack() if window is not visible to
 162                 // avoid early native peer creation
 163                 pack();
 164             }
 165         }
 166     }
 167 
 168 
 169     /**
 170      * Causes the <code>Popup</code> to be sized to fit the preferred size
 171      * of the <code>Component</code> it contains.
 172      */
 173     void pack() {
 174         Component component = getComponent();
 175 
 176         if (component instanceof Window) {
 177             ((Window)component).pack();
 178         }
 179     }
 180 
 181     /**
 182      * Returns the <code>Window</code> to use as the parent of the
 183      * <code>Window</code> created for the <code>Popup</code>. This creates
 184      * a new <code>DefaultFrame</code>, if necessary.
 185      */
 186     private Window getParentWindow(Component owner) {
 187         Window window = null;
 188 
 189         if (owner instanceof Window) {
 190             window = (Window)owner;
 191         }
 192         else if (owner != null) {
 193             window = SwingUtilities.getWindowAncestor(owner);
 194         }
 195         if (window == null) {
 196             window = new DefaultFrame();
 197         }
 198         return window;
 199     }
 200 
 201     /**
 202      * Creates the Component to use as the parent of the <code>Popup</code>.
 203      * The default implementation creates a <code>Window</code>, subclasses
 204      * should override.
 205      */
 206     Component createComponent(Component owner) {
 207         if (GraphicsEnvironment.isHeadless()) {
 208             // Generally not useful, bail.
 209             return null;
 210         }
 211         return new HeavyWeightWindow(getParentWindow(owner));
 212     }
 213 
 214     /**
 215      * Returns the <code>Component</code> returned from
 216      * <code>createComponent</code> that will hold the <code>Popup</code>.
 217      */
 218     Component getComponent() {
 219         return component;
 220     }
 221 
 222 
 223     /**
 224      * Component used to house window.
 225      */
 226     static class HeavyWeightWindow extends JWindow implements ModalExclude {
 227         HeavyWeightWindow(Window parent) {
 228             super(parent);
 229             setFocusableWindowState(false);
 230             setType(Window.Type.POPUP);
 231 
 232             // Popups are typically transient and most likely won't benefit
 233             // from true double buffering.  Turn it off here.
 234             getRootPane().setUseTrueDoubleBuffering(false);
 235             // Try to set "always-on-top" for the popup window.
 236             // Applets usually don't have sufficient permissions to do it.
 237             // In this case simply ignore the exception.
 238             try {
 239                 setAlwaysOnTop(true);
 240             } catch (SecurityException se) {
 241                 // setAlwaysOnTop is restricted,
 242                 // the exception is ignored
 243             }
 244         }
 245 
 246         public void update(Graphics g) {
 247             paint(g);
 248         }
 249 
 250         public void show() {
 251             this.pack();
 252             if (getWidth() > 0 && getHeight() > 0) {
 253                 super.show();
 254             }
 255         }
 256     }
 257 
 258 
 259     /**
 260      * Used if no valid Window ancestor of the supplied owner is found.
 261      * <p>
 262      * PopupFactory uses this as a way to know when the Popup shouldn't
 263      * be cached based on the Window.
 264      */
 265     static class DefaultFrame extends Frame {
 266     }
 267 }