1/*
2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package javax.swing;
27
28import java.awt.*;
29
30import sun.awt.ModalExclude;
31import 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 */
58public 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
102    @SuppressWarnings("deprecation")
103    public void show() {
104        Component component = getComponent();
105
106        if (component != null) {
107            component.show();
108        }
109    }
110
111    /**
112     * Hides and disposes of the <code>Popup</code>. Once a <code>Popup</code>
113     * has been disposed you should no longer invoke methods on it. A
114     * <code>dispose</code>d <code>Popup</code> may be reclaimed and later used
115     * based on the <code>PopupFactory</code>. As such, if you invoke methods
116     * on a <code>disposed</code> <code>Popup</code>, indeterminate
117     * behavior will result.
118     */
119
120    @SuppressWarnings("deprecation")
121    public void hide() {
122        Component component = getComponent();
123
124        if (component instanceof JWindow) {
125            component.hide();
126            ((JWindow)component).getContentPane().removeAll();
127        }
128        dispose();
129    }
130
131    /**
132     * Frees any resources the <code>Popup</code> may be holding onto.
133     */
134    void dispose() {
135        Component component = getComponent();
136        Window window = SwingUtilities.getWindowAncestor(component);
137
138        if (component instanceof JWindow) {
139            ((Window)component).dispose();
140            component = null;
141        }
142        // If our parent is a DefaultFrame, we need to dispose it, too.
143        if (window instanceof DefaultFrame) {
144            window.dispose();
145        }
146    }
147
148    /**
149     * Resets the <code>Popup</code> to an initial state.
150     */
151    void reset(Component owner, Component contents, int ownerX, int ownerY) {
152        if (getComponent() == null) {
153            component = createComponent(owner);
154        }
155
156        Component c = getComponent();
157
158        if (c instanceof JWindow) {
159            JWindow component = (JWindow)getComponent();
160
161            component.setLocation(ownerX, ownerY);
162            component.getContentPane().add(contents, BorderLayout.CENTER);
163            component.invalidate();
164            component.validate();
165            if(component.isVisible()) {
166                // Do not call pack() if window is not visible to
167                // avoid early native peer creation
168                pack();
169            }
170        }
171    }
172
173
174    /**
175     * Causes the <code>Popup</code> to be sized to fit the preferred size
176     * of the <code>Component</code> it contains.
177     */
178    void pack() {
179        Component component = getComponent();
180
181        if (component instanceof Window) {
182            ((Window)component).pack();
183        }
184    }
185
186    /**
187     * Returns the <code>Window</code> to use as the parent of the
188     * <code>Window</code> created for the <code>Popup</code>. This creates
189     * a new <code>DefaultFrame</code>, if necessary.
190     */
191    private Window getParentWindow(Component owner) {
192        Window window = null;
193
194        if (owner instanceof Window) {
195            window = (Window)owner;
196        }
197        else if (owner != null) {
198            window = SwingUtilities.getWindowAncestor(owner);
199        }
200        if (window == null) {
201            window = new DefaultFrame();
202        }
203        return window;
204    }
205
206    /**
207     * Creates the Component to use as the parent of the <code>Popup</code>.
208     * The default implementation creates a <code>Window</code>, subclasses
209     * should override.
210     */
211    Component createComponent(Component owner) {
212        if (GraphicsEnvironment.isHeadless()) {
213            // Generally not useful, bail.
214            return null;
215        }
216        return new HeavyWeightWindow(getParentWindow(owner));
217    }
218
219    /**
220     * Returns the <code>Component</code> returned from
221     * <code>createComponent</code> that will hold the <code>Popup</code>.
222     */
223    Component getComponent() {
224        return component;
225    }
226
227
228    /**
229     * Component used to house window.
230     */
231    @SuppressWarnings("serial") // Superclass is not serializable across versions
232    static class HeavyWeightWindow extends JWindow implements ModalExclude {
233        HeavyWeightWindow(Window parent) {
234            super(parent);
235            setFocusableWindowState(false);
236            setType(Window.Type.POPUP);
237
238            // Popups are typically transient and most likely won't benefit
239            // from true double buffering.  Turn it off here.
240            getRootPane().setUseTrueDoubleBuffering(false);
241            // Try to set "always-on-top" for the popup window.
242            // Applets usually don't have sufficient permissions to do it.
243            // In this case simply ignore the exception.
244            try {
245                setAlwaysOnTop(true);
246            } catch (SecurityException se) {
247                // setAlwaysOnTop is restricted,
248                // the exception is ignored
249            }
250        }
251
252        public void update(Graphics g) {
253            paint(g);
254        }
255
256        @SuppressWarnings("deprecation")
257        public void show() {
258            this.pack();
259            if (getWidth() > 0 && getHeight() > 0) {
260                super.show();
261            }
262        }
263    }
264
265
266    /**
267     * Used if no valid Window ancestor of the supplied owner is found.
268     * <p>
269     * PopupFactory uses this as a way to know when the Popup shouldn't
270     * be cached based on the Window.
271     */
272    @SuppressWarnings("serial") // JDK-implementation class
273    static class DefaultFrame extends Frame {
274    }
275}
276