1 /* 2 * Copyright (c) 1999, 2008, 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 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 component.invalidate(); 160 component.validate(); 161 if(component.isVisible()) { 162 // Do not call pack() if window is not visible to 163 // avoid early native peer creation 164 pack(); 165 } 166 } 167 } 168 169 170 /** 171 * Causes the <code>Popup</code> to be sized to fit the preferred size 172 * of the <code>Component</code> it contains. 173 */ 174 void pack() { 175 Component component = getComponent(); 176 177 if (component instanceof Window) { 178 ((Window)component).pack(); 179 } 180 } 181 182 /** 183 * Returns the <code>Window</code> to use as the parent of the 184 * <code>Window</code> created for the <code>Popup</code>. This creates 185 * a new <code>DefaultFrame</code>, if necessary. 186 */ 187 private Window getParentWindow(Component owner) { 188 Window window = null; 189 190 if (owner instanceof Window) { 191 window = (Window)owner; 192 } 193 else if (owner != null) { 194 window = SwingUtilities.getWindowAncestor(owner); 195 } 196 if (window == null) { 197 window = new DefaultFrame(); 198 } 199 return window; 200 } 201 202 /** 203 * Creates the Component to use as the parent of the <code>Popup</code>. 204 * The default implementation creates a <code>Window</code>, subclasses 205 * should override. 206 */ 207 Component createComponent(Component owner) { 208 if (GraphicsEnvironment.isHeadless()) { 209 // Generally not useful, bail. 210 return null; 211 } 212 return new HeavyWeightWindow(getParentWindow(owner)); 213 } 214 215 /** 216 * Returns the <code>Component</code> returned from 217 * <code>createComponent</code> that will hold the <code>Popup</code>. 218 */ 219 Component getComponent() { 220 return component; 221 } 222 223 224 /** 225 * Component used to house window. 226 */ 227 static class HeavyWeightWindow extends JWindow implements ModalExclude { 228 HeavyWeightWindow(Window parent) { 229 super(parent); 230 setFocusableWindowState(false); 231 setType(Window.Type.POPUP); 232 233 // Popups are typically transient and most likely won't benefit 234 // from true double buffering. Turn it off here. 235 getRootPane().setUseTrueDoubleBuffering(false); 236 // Try to set "always-on-top" for the popup window. 237 // Applets usually don't have sufficient permissions to do it. 238 // In this case simply ignore the exception. 239 try { 240 setAlwaysOnTop(true); 241 } catch (SecurityException se) { 242 // setAlwaysOnTop is restricted, 243 // the exception is ignored 244 } 245 } 246 247 public void update(Graphics g) { 248 paint(g); 249 } 250 251 public void show() { 252 this.pack(); 253 if (getWidth() > 0 && getHeight() > 0) { 254 super.show(); 255 } 256 } 257 } 258 259 260 /** 261 * Used if no valid Window ancestor of the supplied owner is found. 262 * <p> 263 * PopupFactory uses this as a way to know when the Popup shouldn't 264 * be cached based on the Window. 265 */ 266 static class DefaultFrame extends Frame { 267 } 268 }