1 /* 2 * Copyright (c) 1999, 2020, 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.BorderLayout; 29 import java.awt.Component; 30 import java.awt.Frame; 31 import java.awt.Graphics; 32 import java.awt.GraphicsEnvironment; 33 import java.awt.Window; 34 35 import sun.awt.ModalExclude; 36 37 /** 38 * Popups are used to display a <code>Component</code> to the user, typically 39 * on top of all the other <code>Component</code>s in a particular containment 40 * hierarchy. <code>Popup</code>s have a very small life cycle. Once you 41 * have obtained a <code>Popup</code>, and hidden it (invoked the 42 * <code>hide</code> method), you should no longer 43 * invoke any methods on it. This allows the <code>PopupFactory</code> to cache 44 * <code>Popup</code>s for later use. 45 * <p> 46 * The general contract is that if you need to change the size of the 47 * <code>Component</code>, or location of the <code>Popup</code>, you should 48 * obtain a new <code>Popup</code>. 49 * <p> 50 * <code>Popup</code> does not descend from <code>Component</code>, rather 51 * implementations of <code>Popup</code> are responsible for creating 52 * and maintaining their own <code>Component</code>s to render the 53 * requested <code>Component</code> to the user. 54 * <p> 55 * You typically do not explicitly create an instance of <code>Popup</code>, 56 * instead obtain one from a <code>PopupFactory</code>. 57 * 58 * @see PopupFactory 59 * 60 * @since 1.4 61 */ 62 public class Popup { 63 /** 64 * The Component representing the Popup. 65 */ 66 private Component component; 67 68 /** 69 * Creates a <code>Popup</code> for the Component <code>owner</code> 70 * containing the Component <code>contents</code>. <code>owner</code> 71 * is used to determine which <code>Window</code> the new 72 * <code>Popup</code> will parent the <code>Component</code> the 73 * <code>Popup</code> creates to. 74 * A null <code>owner</code> implies there is no valid parent. 75 * <code>x</code> and 76 * <code>y</code> specify the preferred initial location to place 77 * the <code>Popup</code> at. Based on screen size, or other paramaters, 78 * the <code>Popup</code> may not display at <code>x</code> and 79 * <code>y</code>. 80 * 81 * @param owner Component mouse coordinates are relative to, may be null 82 * @param contents Contents of the Popup 83 * @param x Initial x screen coordinate 84 * @param y Initial y screen coordinate 85 * @exception IllegalArgumentException if contents is null 86 */ 87 protected Popup(Component owner, Component contents, int x, int y) { 88 this(); 89 if (contents == null) { 90 throw new IllegalArgumentException("Contents must be non-null"); 91 } 92 reset(owner, contents, x, y); 93 } 94 95 /** 96 * Creates a <code>Popup</code>. This is provided for subclasses. 97 */ 98 protected Popup() { 99 } 100 101 /** 102 * Makes the <code>Popup</code> visible. If the <code>Popup</code> is 103 * currently visible, this has no effect. 104 */ 105 106 @SuppressWarnings("deprecation") 107 public void show() { 108 Component component = getComponent(); 109 110 if (component != null) { 111 component.show(); 112 } 113 } 114 115 /** 116 * Hides and disposes of the <code>Popup</code>. Once a <code>Popup</code> 117 * has been disposed you should no longer invoke methods on it. A 118 * <code>dispose</code>d <code>Popup</code> may be reclaimed and later used 119 * based on the <code>PopupFactory</code>. As such, if you invoke methods 120 * on a <code>disposed</code> <code>Popup</code>, indeterminate 121 * behavior will result. 122 */ 123 124 @SuppressWarnings("deprecation") 125 public void hide() { 126 Component component = getComponent(); 127 128 if (component instanceof JWindow) { 129 component.hide(); 130 ((JWindow)component).getContentPane().removeAll(); 131 } 132 dispose(); 133 } 134 135 /** 136 * Frees any resources the <code>Popup</code> may be holding onto. 137 */ 138 void dispose() { 139 Component component = getComponent(); 140 Window window = SwingUtilities.getWindowAncestor(component); 141 142 if (component instanceof JWindow) { 143 ((Window)component).dispose(); 144 component = null; 145 } 146 // If our parent is a DefaultFrame, we need to dispose it, too. 147 if (window instanceof DefaultFrame) { 148 window.dispose(); 149 } 150 } 151 152 /** 153 * Resets the <code>Popup</code> to an initial state. 154 */ 155 void reset(Component owner, Component contents, int ownerX, int ownerY) { 156 if (getComponent() == null) { 157 component = createComponent(owner); 158 } 159 160 Component c = getComponent(); 161 162 if (c instanceof JWindow) { 163 JWindow component = (JWindow)getComponent(); 164 // Sets the proper location, and resets internal state of the window 165 component.setBounds(ownerX, ownerY, 1, 1); 166 component.getContentPane().add(contents, BorderLayout.CENTER); 167 component.invalidate(); 168 component.validate(); 169 if(component.isVisible()) { 170 // Do not call pack() if window is not visible to 171 // avoid early native peer creation 172 pack(); 173 } 174 } 175 } 176 177 178 /** 179 * Causes the <code>Popup</code> to be sized to fit the preferred size 180 * of the <code>Component</code> it contains. 181 */ 182 void pack() { 183 Component component = getComponent(); 184 185 if (component instanceof Window) { 186 ((Window)component).pack(); 187 } 188 } 189 190 /** 191 * Returns the <code>Window</code> to use as the parent of the 192 * <code>Window</code> created for the <code>Popup</code>. This creates 193 * a new <code>DefaultFrame</code>, if necessary. 194 */ 195 private Window getParentWindow(Component owner) { 196 Window window = null; 197 198 if (owner instanceof Window) { 199 window = (Window)owner; 200 } 201 else if (owner != null) { 202 window = SwingUtilities.getWindowAncestor(owner); 203 } 204 if (window == null) { 205 window = new DefaultFrame(); 206 } 207 return window; 208 } 209 210 /** 211 * Creates the Component to use as the parent of the <code>Popup</code>. 212 * The default implementation creates a <code>Window</code>, subclasses 213 * should override. 214 */ 215 Component createComponent(Component owner) { 216 if (GraphicsEnvironment.isHeadless()) { 217 // Generally not useful, bail. 218 return null; 219 } 220 return new HeavyWeightWindow(getParentWindow(owner)); 221 } 222 223 /** 224 * Returns the <code>Component</code> returned from 225 * <code>createComponent</code> that will hold the <code>Popup</code>. 226 */ 227 Component getComponent() { 228 return component; 229 } 230 231 232 /** 233 * Component used to house window. 234 */ 235 @SuppressWarnings("serial") // Superclass is not serializable across versions 236 static class HeavyWeightWindow extends JWindow implements ModalExclude { 237 HeavyWeightWindow(Window parent) { 238 super(parent); 239 setFocusableWindowState(false); 240 setType(Window.Type.POPUP); 241 242 // Popups are typically transient and most likely won't benefit 243 // from true double buffering. Turn it off here. 244 getRootPane().setUseTrueDoubleBuffering(false); 245 // Try to set "always-on-top" for the popup window. 246 // Applets usually don't have sufficient permissions to do it. 247 // In this case simply ignore the exception. 248 try { 249 setAlwaysOnTop(true); 250 } catch (SecurityException se) { 251 // setAlwaysOnTop is restricted, 252 // the exception is ignored 253 } 254 } 255 256 public void update(Graphics g) { 257 paint(g); 258 } 259 260 @SuppressWarnings("deprecation") 261 public void show() { 262 this.pack(); 263 if (getWidth() > 0 && getHeight() > 0) { 264 super.show(); 265 } 266 } 267 } 268 269 270 /** 271 * Used if no valid Window ancestor of the supplied owner is found. 272 * <p> 273 * PopupFactory uses this as a way to know when the Popup shouldn't 274 * be cached based on the Window. 275 */ 276 @SuppressWarnings("serial") // JDK-implementation class 277 static class DefaultFrame extends Frame { 278 } 279 }