KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > Popup


1 /*
2  * @(#)Popup.java 1.17 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing;
9
10 import java.awt.*;
11 import sun.awt.ModalExclude;
12
13 /**
14  * Popups are used to display a <code>Component</code> to the user, typically
15  * on top of all the other <code>Component</code>s in a particular containment
16  * hierarchy. <code>Popup</code>s have a very small life cycle. Once you
17  * have obtained a <code>Popup</code>, and hidden it (invoked the
18  * <code>hide</code> method), you should no longer
19  * invoke any methods on it. This allows the <code>PopupFactory</code> to cache
20  * <code>Popup</code>s for later use.
21  * <p>
22  * The general contract is that if you need to change the size of the
23  * <code>Component</code>, or location of the <code>Popup</code>, you should
24  * obtain a new <code>Popup</code>.
25  * <p>
26  * <code>Popup</code> does not descend from <code>Component</code>, rather
27  * implementations of <code>Popup</code> are responsible for creating
28  * and maintaining their own <code>Component</code>s to render the
29  * requested <code>Component</code> to the user.
30  * <p>
31  * You typically do not explicitly create an instance of <code>Popup</code>,
32  * instead obtain one from a <code>PopupFactory</code>.
33  *
34  * @see PopupFactory
35  *
36  * @version 1.17 12/19/03
37  * @since 1.4
38  */

39 public class Popup {
40     /**
41      * The Component representing the Popup.
42      */

43     private Component component;
44
45     /**
46      * Creates a <code>Popup</code> for the Component <code>owner</code>
47      * containing the Component <code>contents</code>. <code>owner</code>
48      * is used to determine which <code>Window</code> the new
49      * <code>Popup</code> will parent the <code>Component</code> the
50      * <code>Popup</code> creates to.
51      * A null <code>owner</code> implies there is no valid parent.
52      * <code>x</code> and
53      * <code>y</code> specify the preferred initial location to place
54      * the <code>Popup</code> at. Based on screen size, or other paramaters,
55      * the <code>Popup</code> may not display at <code>x</code> and
56      * <code>y</code>.
57      *
58      * @param owner Component mouse coordinates are relative to, may be null
59      * @param contents Contents of the Popup
60      * @param x Initial x screen coordinate
61      * @param y Initial y screen coordinate
62      * @exception IllegalArgumentException if contents is null
63      */

64     protected Popup(Component owner, Component contents, int x, int y) {
65         this();
66         if (contents == null) {
67             throw new IllegalArgumentException JavaDoc("Contents must be non-null");
68         }
69         reset(owner, contents, x, y);
70     }
71
72     /**
73      * Creates a <code>Popup</code>. This is provided for subclasses.
74      */

75     protected Popup() {
76     }
77
78     /**
79      * Makes the <code>Popup</code> visible. If the <code>Popup</code> is
80      * currently visible, this has no effect.
81      */

82     public void show() {
83         Component component = getComponent();
84
85         if (component != null) {
86             component.show();
87         }
88     }
89
90     /**
91      * Hides and disposes of the <code>Popup</code>. Once a <code>Popup</code>
92      * has been disposed you should no longer invoke methods on it. A
93      * <code>dispose</code>d <code>Popup</code> may be reclaimed and later used
94      * based on the <code>PopupFactory</code>. As such, if you invoke methods
95      * on a <code>disposed</code> <code>Popup</code>, indeterminate
96      * behavior will result.
97      */

98     public void hide() {
99         Component component = getComponent();
100
101         if (component instanceof JWindow JavaDoc) {
102             component.hide();
103             ((JWindow JavaDoc)component).getContentPane().removeAll();
104         }
105         dispose();
106     }
107
108     /**
109      * Frees any resources the <code>Popup</code> may be holding onto.
110      */

111     void dispose() {
112         Component component = getComponent();
113
114         if (component instanceof JWindow JavaDoc) {
115             ((Window)component).dispose();
116             component = null;
117         }
118     }
119
120     /**
121      * Resets the <code>Popup</code> to an initial state.
122      */

123     void reset(Component owner, Component contents, int ownerX, int ownerY) {
124         if (getComponent() == null) {
125             component = createComponent(owner);
126         }
127
128         Component c = getComponent();
129
130         if (c instanceof JWindow JavaDoc) {
131             JWindow JavaDoc component = (JWindow JavaDoc)getComponent();
132
133             component.setLocation(ownerX, ownerY);
134             component.getContentPane().add(contents, BorderLayout.CENTER);
135             contents.invalidate();
136             if(component.isVisible()) {
137                 // Do not call pack() if window is not visible to
138
// avoid early native peer creation
139
pack();
140             }
141         }
142     }
143
144
145     /**
146      * Causes the <code>Popup</code> to be sized to fit the preferred size
147      * of the <code>Component</code> it contains.
148      */

149     void pack() {
150         Component component = getComponent();
151
152         if (component instanceof Window) {
153             ((Window)component).pack();
154         }
155     }
156
157     /**
158      * Returns the <code>Window</code> to use as the parent of the
159      * <code>Window</code> created for the <code>Popup</code>. This creates
160      * a new <code>Frame</code> each time it is invoked. Subclasses that wish
161      * to support a different <code>Window</code> parent should override
162      * this.
163      */

164     private Window getParentWindow(Component owner) {
165         Window window = null;
166
167         if (owner instanceof Window) {
168             window = (Window)owner;
169         }
170         else if (owner != null) {
171             window = SwingUtilities.getWindowAncestor(owner);
172         }
173         if (window == null) {
174             window = new DefaultFrame();
175         }
176         return window;
177     }
178
179     /**
180      * Creates the Component to use as the parent of the <code>Popup</code>.
181      * The default implementation creates a <code>Window</code>, subclasses
182      * should override.
183      */

184     Component createComponent(Component owner) {
185         if (GraphicsEnvironment.isHeadless()) {
186             // Generally not useful, bail.
187
return null;
188         }
189         return new HeavyWeightWindow(getParentWindow(owner));
190     }
191
192     /**
193      * Returns the <code>Component</code> returned from
194      * <code>createComponent</code> that will hold the <code>Popup</code>.
195      */

196     Component getComponent() {
197         return component;
198     }
199
200
201     /**
202      * Component used to house window.
203      */

204     static class HeavyWeightWindow extends JWindow JavaDoc implements ModalExclude {
205         HeavyWeightWindow(Window parent) {
206             super(parent);
207             setFocusableWindowState(false);
208             setName("###overrideRedirect###");
209         }
210
211         public void update(Graphics g) {
212             paint(g);
213         }
214
215     public void show() {
216         this.pack();
217         super.show();
218     }
219     }
220
221
222     /**
223      * Used if no valid Window ancestor of the supplied owner is found.
224      * <p>
225      * PopupFactory uses this as a way to know when the Popup shouldn't
226      * be cached based on the Window.
227      */

228     static class DefaultFrame extends Frame {
229     }
230 }
231
Popular Tags