1 /* 2 * Copyright (c) 1995, 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 package java.awt; 26 27 import java.awt.event.*; 28 import java.awt.geom.Path2D; 29 import java.awt.geom.Point2D; 30 import java.awt.im.InputContext; 31 import java.awt.image.BufferStrategy; 32 import java.awt.image.BufferedImage; 33 import java.awt.peer.ComponentPeer; 34 import java.awt.peer.WindowPeer; 35 import java.beans.PropertyChangeListener; 36 import java.io.IOException; 37 import java.io.ObjectInputStream; 38 import java.io.ObjectOutputStream; 39 import java.io.OptionalDataException; 40 import java.io.Serializable; 41 import java.lang.ref.WeakReference; 42 import java.lang.reflect.InvocationTargetException; 43 import java.security.AccessController; 44 import java.util.ArrayList; 45 import java.util.Arrays; 46 import java.util.EventListener; 47 import java.util.Locale; 48 import java.util.ResourceBundle; 49 import java.util.Set; 50 import java.util.Vector; 51 import java.util.concurrent.atomic.AtomicBoolean; 52 import javax.accessibility.*; 53 import sun.awt.AWTAccessor; 54 import sun.awt.AWTPermissions; 55 import sun.awt.AppContext; 56 import sun.awt.CausedFocusEvent; 57 import sun.awt.SunToolkit; 58 import sun.awt.util.IdentityArrayList; 59 import sun.java2d.Disposer; 60 import sun.java2d.pipe.Region; 61 import sun.security.action.GetPropertyAction; 62 import sun.util.logging.PlatformLogger; 63 64 /** 65 * A {@code Window} object is a top-level window with no borders and no 66 * menubar. 67 * The default layout for a window is {@code BorderLayout}. 68 * <p> 69 * A window must have either a frame, dialog, or another window defined as its 70 * owner when it's constructed. 71 * <p> 72 * In a multi-screen environment, you can create a {@code Window} 73 * on a different screen device by constructing the {@code Window} 74 * with {@link #Window(Window, GraphicsConfiguration)}. The 75 * {@code GraphicsConfiguration} object is one of the 76 * {@code GraphicsConfiguration} objects of the target screen device. 77 * <p> 78 * In a virtual device multi-screen environment in which the desktop 79 * area could span multiple physical screen devices, the bounds of all 80 * configurations are relative to the virtual device coordinate system. 81 * The origin of the virtual-coordinate system is at the upper left-hand 82 * corner of the primary physical screen. Depending on the location of 83 * the primary screen in the virtual device, negative coordinates are 84 * possible, as shown in the following figure. 85 * <p> 86 * <img src="doc-files/MultiScreen.gif" 87 * alt="Diagram shows virtual device containing 4 physical screens. Primary physical screen shows coords (0,0), other screen shows (-80,-100)." 88 * style="float:center; margin: 7px 10px;"> 89 * <p> 90 * In such an environment, when calling {@code setLocation}, 91 * you must pass a virtual coordinate to this method. Similarly, 92 * calling {@code getLocationOnScreen} on a {@code Window} returns 93 * virtual device coordinates. Call the {@code getBounds} method 94 * of a {@code GraphicsConfiguration} to find its origin in the virtual 95 * coordinate system. 96 * <p> 97 * The following code sets the location of a {@code Window} 98 * at (10, 10) relative to the origin of the physical screen 99 * of the corresponding {@code GraphicsConfiguration}. If the 100 * bounds of the {@code GraphicsConfiguration} is not taken 101 * into account, the {@code Window} location would be set 102 * at (10, 10) relative to the virtual-coordinate system and would appear 103 * on the primary physical screen, which might be different from the 104 * physical screen of the specified {@code GraphicsConfiguration}. 105 * 106 * <pre> 107 * Window w = new Window(Window owner, GraphicsConfiguration gc); 108 * Rectangle bounds = gc.getBounds(); 109 * w.setLocation(10 + bounds.x, 10 + bounds.y); 110 * </pre> 111 * 112 * <p> 113 * Note: the location and size of top-level windows (including 114 * {@code Window}s, {@code Frame}s, and {@code Dialog}s) 115 * are under the control of the desktop's window management system. 116 * Calls to {@code setLocation}, {@code setSize}, and 117 * {@code setBounds} are requests (not directives) which are 118 * forwarded to the window management system. Every effort will be 119 * made to honor such requests. However, in some cases the window 120 * management system may ignore such requests, or modify the requested 121 * geometry in order to place and size the {@code Window} in a way 122 * that more closely matches the desktop settings. 123 * <p> 124 * Due to the asynchronous nature of native event handling, the results 125 * returned by {@code getBounds}, {@code getLocation}, 126 * {@code getLocationOnScreen}, and {@code getSize} might not 127 * reflect the actual geometry of the Window on screen until the last 128 * request has been processed. During the processing of subsequent 129 * requests these values might change accordingly while the window 130 * management system fulfills the requests. 131 * <p> 132 * An application may set the size and location of an invisible 133 * {@code Window} arbitrarily, but the window management system may 134 * subsequently change its size and/or location when the 135 * {@code Window} is made visible. One or more {@code ComponentEvent}s 136 * will be generated to indicate the new geometry. 137 * <p> 138 * Windows are capable of generating the following WindowEvents: 139 * WindowOpened, WindowClosed, WindowGainedFocus, WindowLostFocus. 140 * 141 * @author Sami Shaio 142 * @author Arthur van Hoff 143 * @see WindowEvent 144 * @see #addWindowListener 145 * @see java.awt.BorderLayout 146 * @since 1.0 147 */ 148 public class Window extends Container implements Accessible { 149 150 /** 151 * Enumeration of available <i>window types</i>. 152 * 153 * A window type defines the generic visual appearance and behavior of a 154 * top-level window. For example, the type may affect the kind of 155 * decorations of a decorated {@code Frame} or {@code Dialog} instance. 156 * <p> 157 * Some platforms may not fully support a certain window type. Depending on 158 * the level of support, some properties of the window type may be 159 * disobeyed. 160 * 161 * @see #getType 162 * @see #setType 163 * @since 1.7 164 */ 165 public static enum Type { 166 /** 167 * Represents a <i>normal</i> window. 168 * 169 * This is the default type for objects of the {@code Window} class or 170 * its descendants. Use this type for regular top-level windows. 171 */ 172 NORMAL, 173 174 /** 175 * Represents a <i>utility</i> window. 176 * 177 * A utility window is usually a small window such as a toolbar or a 178 * palette. The native system may render the window with smaller 179 * title-bar if the window is either a {@code Frame} or a {@code 180 * Dialog} object, and if it has its decorations enabled. 181 */ 182 UTILITY, 183 184 /** 185 * Represents a <i>popup</i> window. 186 * 187 * A popup window is a temporary window such as a drop-down menu or a 188 * tooltip. On some platforms, windows of that type may be forcibly 189 * made undecorated even if they are instances of the {@code Frame} or 190 * {@code Dialog} class, and have decorations enabled. 191 */ 192 POPUP 193 } 194 195 /** 196 * This represents the warning message that is 197 * to be displayed in a non secure window. ie : 198 * a window that has a security manager installed that denies 199 * {@code AWTPermission("showWindowWithoutWarningBanner")}. 200 * This message can be displayed anywhere in the window. 201 * 202 * @serial 203 * @see #getWarningString 204 */ 205 String warningString; 206 207 /** 208 * {@code icons} is the graphical way we can 209 * represent the frames and dialogs. 210 * {@code Window} can't display icon but it's 211 * being inherited by owned {@code Dialog}s. 212 * 213 * @serial 214 * @see #getIconImages 215 * @see #setIconImages 216 */ 217 transient java.util.List<Image> icons; 218 219 /** 220 * Holds the reference to the component which last had focus in this window 221 * before it lost focus. 222 */ 223 private transient Component temporaryLostComponent; 224 225 static boolean systemSyncLWRequests = false; 226 boolean syncLWRequests = false; 227 transient boolean beforeFirstShow = true; 228 private transient boolean disposing = false; 229 transient WindowDisposerRecord disposerRecord = null; 230 231 static final int OPENED = 0x01; 232 233 /** 234 * An Integer value representing the Window State. 235 * 236 * @serial 237 * @since 1.2 238 * @see #show 239 */ 240 int state; 241 242 /** 243 * A boolean value representing Window always-on-top state 244 * @since 1.5 245 * @serial 246 * @see #setAlwaysOnTop 247 * @see #isAlwaysOnTop 248 */ 249 private boolean alwaysOnTop; 250 251 /** 252 * Contains all the windows that have a peer object associated, 253 * i. e. between addNotify() and removeNotify() calls. The list 254 * of all Window instances can be obtained from AppContext object. 255 * 256 * @since 1.6 257 */ 258 private static final IdentityArrayList<Window> allWindows = new IdentityArrayList<Window>(); 259 260 /** 261 * A vector containing all the windows this 262 * window currently owns. 263 * @since 1.2 264 * @see #getOwnedWindows 265 */ 266 transient Vector<WeakReference<Window>> ownedWindowList = 267 new Vector<WeakReference<Window>>(); 268 269 /* 270 * We insert a weak reference into the Vector of all Windows in AppContext 271 * instead of 'this' so that garbage collection can still take place 272 * correctly. 273 */ 274 private transient WeakReference<Window> weakThis; 275 276 transient boolean showWithParent; 277 278 /** 279 * Contains the modal dialog that blocks this window, or null 280 * if the window is unblocked. 281 * 282 * @since 1.6 283 */ 284 transient Dialog modalBlocker; 285 286 /** 287 * @serial 288 * 289 * @see java.awt.Dialog.ModalExclusionType 290 * @see #getModalExclusionType 291 * @see #setModalExclusionType 292 * 293 * @since 1.6 294 */ 295 Dialog.ModalExclusionType modalExclusionType; 296 297 transient WindowListener windowListener; 298 transient WindowStateListener windowStateListener; 299 transient WindowFocusListener windowFocusListener; 300 301 transient InputContext inputContext; 302 private transient Object inputContextLock = new Object(); 303 304 /** 305 * Unused. Maintained for serialization backward-compatibility. 306 * 307 * @serial 308 * @since 1.2 309 */ 310 private FocusManager focusMgr; 311 312 /** 313 * Indicates whether this Window can become the focused Window. 314 * 315 * @serial 316 * @see #getFocusableWindowState 317 * @see #setFocusableWindowState 318 * @since 1.4 319 */ 320 private boolean focusableWindowState = true; 321 322 /** 323 * Indicates whether this window should receive focus on 324 * subsequently being shown (with a call to {@code setVisible(true)}), or 325 * being moved to the front (with a call to {@code toFront()}). 326 * 327 * @serial 328 * @see #setAutoRequestFocus 329 * @see #isAutoRequestFocus 330 * @since 1.7 331 */ 332 private volatile boolean autoRequestFocus = true; 333 334 /* 335 * Indicates that this window is being shown. This flag is set to true at 336 * the beginning of show() and to false at the end of show(). 337 * 338 * @see #show() 339 * @see Dialog#shouldBlock 340 */ 341 transient boolean isInShow = false; 342 343 /** 344 * The opacity level of the window 345 * 346 * @serial 347 * @see #setOpacity(float) 348 * @see #getOpacity() 349 * @since 1.7 350 */ 351 private float opacity = 1.0f; 352 353 /** 354 * The shape assigned to this window. This field is set to {@code null} if 355 * no shape is set (rectangular window). 356 * 357 * @serial 358 * @see #getShape() 359 * @see #setShape(Shape) 360 * @since 1.7 361 */ 362 private Shape shape = null; 363 364 private static final String base = "win"; 365 private static int nameCounter = 0; 366 367 /* 368 * JDK 1.1 serialVersionUID 369 */ 370 private static final long serialVersionUID = 4497834738069338734L; 371 372 private static final PlatformLogger log = PlatformLogger.getLogger("java.awt.Window"); 373 374 private static final boolean locationByPlatformProp; 375 376 transient boolean isTrayIconWindow = false; 377 378 /** 379 * These fields are initialized in the native peer code 380 * or via AWTAccessor's WindowAccessor. 381 */ 382 private transient volatile int securityWarningWidth = 0; 383 private transient volatile int securityWarningHeight = 0; 384 385 /** 386 * These fields represent the desired location for the security 387 * warning if this window is untrusted. 388 * See com.sun.awt.SecurityWarning for more details. 389 */ 390 private transient double securityWarningPointX = 2.0; 391 private transient double securityWarningPointY = 0.0; 392 private transient float securityWarningAlignmentX = RIGHT_ALIGNMENT; 393 private transient float securityWarningAlignmentY = TOP_ALIGNMENT; 394 395 static { 396 /* ensure that the necessary native libraries are loaded */ 397 Toolkit.loadLibraries(); 398 if (!GraphicsEnvironment.isHeadless()) { 399 initIDs(); 400 } 401 402 String s = java.security.AccessController.doPrivileged( 403 new GetPropertyAction("java.awt.syncLWRequests")); 404 systemSyncLWRequests = (s != null && s.equals("true")); 405 s = java.security.AccessController.doPrivileged( 406 new GetPropertyAction("java.awt.Window.locationByPlatform")); 407 locationByPlatformProp = (s != null && s.equals("true")); 408 } 409 410 /** 411 * Initialize JNI field and method IDs for fields that may be 412 accessed from C. 413 */ 414 private static native void initIDs(); 415 416 /** 417 * Constructs a new, initially invisible window in default size with the 418 * specified {@code GraphicsConfiguration}. 419 * <p> 420 * If there is a security manager, then it is invoked to check 421 * {@code AWTPermission("showWindowWithoutWarningBanner")} 422 * to determine whether or not the window must be displayed with 423 * a warning banner. 424 * 425 * @param gc the {@code GraphicsConfiguration} of the target screen 426 * device. If {@code gc} is {@code null}, the system default 427 * {@code GraphicsConfiguration} is assumed 428 * @exception IllegalArgumentException if {@code gc} 429 * is not from a screen device 430 * @exception HeadlessException when 431 * {@code GraphicsEnvironment.isHeadless()} returns {@code true} 432 * 433 * @see java.awt.GraphicsEnvironment#isHeadless 434 */ 435 Window(GraphicsConfiguration gc) { 436 init(gc); 437 } 438 439 transient Object anchor = new Object(); 440 static class WindowDisposerRecord implements sun.java2d.DisposerRecord { 441 WeakReference<Window> owner; 442 final WeakReference<Window> weakThis; 443 final WeakReference<AppContext> context; 444 445 WindowDisposerRecord(AppContext context, Window victim) { 446 weakThis = victim.weakThis; 447 this.context = new WeakReference<AppContext>(context); 448 } 449 450 public void updateOwner() { 451 Window victim = weakThis.get(); 452 owner = (victim == null) 453 ? null 454 : new WeakReference<Window>(victim.getOwner()); 455 } 456 457 public void dispose() { 458 if (owner != null) { 459 Window parent = owner.get(); 460 if (parent != null) { 461 parent.removeOwnedWindow(weakThis); 462 } 463 } 464 AppContext ac = context.get(); 465 if (null != ac) { 466 Window.removeFromWindowList(ac, weakThis); 467 } 468 } 469 } 470 471 private GraphicsConfiguration initGC(GraphicsConfiguration gc) { 472 GraphicsEnvironment.checkHeadless(); 473 474 if (gc == null) { 475 gc = GraphicsEnvironment.getLocalGraphicsEnvironment(). 476 getDefaultScreenDevice().getDefaultConfiguration(); 477 } 478 setGraphicsConfiguration(gc); 479 480 return gc; 481 } 482 483 private void init(GraphicsConfiguration gc) { 484 GraphicsEnvironment.checkHeadless(); 485 486 syncLWRequests = systemSyncLWRequests; 487 488 weakThis = new WeakReference<Window>(this); 489 addToWindowList(); 490 491 setWarningString(); 492 this.cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); 493 this.visible = false; 494 495 gc = initGC(gc); 496 497 if (gc.getDevice().getType() != 498 GraphicsDevice.TYPE_RASTER_SCREEN) { 499 throw new IllegalArgumentException("not a screen device"); 500 } 501 setLayout(new BorderLayout()); 502 503 /* offset the initial location with the original of the screen */ 504 /* and any insets */ 505 Rectangle screenBounds = gc.getBounds(); 506 Insets screenInsets = getToolkit().getScreenInsets(gc); 507 int x = getX() + screenBounds.x + screenInsets.left; 508 int y = getY() + screenBounds.y + screenInsets.top; 509 if (x != this.x || y != this.y) { 510 setLocation(x, y); 511 /* reset after setLocation */ 512 setLocationByPlatform(locationByPlatformProp); 513 } 514 515 modalExclusionType = Dialog.ModalExclusionType.NO_EXCLUDE; 516 disposerRecord = new WindowDisposerRecord(appContext, this); 517 sun.java2d.Disposer.addRecord(anchor, disposerRecord); 518 519 SunToolkit.checkAndSetPolicy(this); 520 } 521 522 /** 523 * Constructs a new, initially invisible window in the default size. 524 * <p> 525 * If there is a security manager set, it is invoked to check 526 * {@code AWTPermission("showWindowWithoutWarningBanner")}. 527 * If that check fails with a {@code SecurityException} then a warning 528 * banner is created. 529 * 530 * @exception HeadlessException when 531 * {@code GraphicsEnvironment.isHeadless()} returns {@code true} 532 * 533 * @see java.awt.GraphicsEnvironment#isHeadless 534 */ 535 Window() throws HeadlessException { 536 GraphicsEnvironment.checkHeadless(); 537 init((GraphicsConfiguration)null); 538 } 539 540 /** 541 * Constructs a new, initially invisible window with the specified 542 * {@code Frame} as its owner. The window will not be focusable 543 * unless its owner is showing on the screen. 544 * <p> 545 * If there is a security manager set, it is invoked to check 546 * {@code AWTPermission("showWindowWithoutWarningBanner")}. 547 * If that check fails with a {@code SecurityException} then a warning 548 * banner is created. 549 * 550 * @param owner the {@code Frame} to act as owner or {@code null} 551 * if this window has no owner 552 * @exception IllegalArgumentException if the {@code owner}'s 553 * {@code GraphicsConfiguration} is not from a screen device 554 * @exception HeadlessException when 555 * {@code GraphicsEnvironment.isHeadless} returns {@code true} 556 * 557 * @see java.awt.GraphicsEnvironment#isHeadless 558 * @see #isShowing 559 */ 560 public Window(Frame owner) { 561 this(owner == null ? (GraphicsConfiguration)null : 562 owner.getGraphicsConfiguration()); 563 ownedInit(owner); 564 } 565 566 /** 567 * Constructs a new, initially invisible window with the specified 568 * {@code Window} as its owner. This window will not be focusable 569 * unless its nearest owning {@code Frame} or {@code Dialog} 570 * is showing on the screen. 571 * <p> 572 * If there is a security manager set, it is invoked to check 573 * {@code AWTPermission("showWindowWithoutWarningBanner")}. 574 * If that check fails with a {@code SecurityException} then a 575 * warning banner is created. 576 * 577 * @param owner the {@code Window} to act as owner or 578 * {@code null} if this window has no owner 579 * @exception IllegalArgumentException if the {@code owner}'s 580 * {@code GraphicsConfiguration} is not from a screen device 581 * @exception HeadlessException when 582 * {@code GraphicsEnvironment.isHeadless()} returns 583 * {@code true} 584 * 585 * @see java.awt.GraphicsEnvironment#isHeadless 586 * @see #isShowing 587 * 588 * @since 1.2 589 */ 590 public Window(Window owner) { 591 this(owner == null ? (GraphicsConfiguration)null : 592 owner.getGraphicsConfiguration()); 593 ownedInit(owner); 594 } 595 596 /** 597 * Constructs a new, initially invisible window with the specified owner 598 * {@code Window} and a {@code GraphicsConfiguration} 599 * of a screen device. The Window will not be focusable unless 600 * its nearest owning {@code Frame} or {@code Dialog} 601 * is showing on the screen. 602 * <p> 603 * If there is a security manager set, it is invoked to check 604 * {@code AWTPermission("showWindowWithoutWarningBanner")}. If that 605 * check fails with a {@code SecurityException} then a warning banner 606 * is created. 607 * 608 * @param owner the window to act as owner or {@code null} 609 * if this window has no owner 610 * @param gc the {@code GraphicsConfiguration} of the target 611 * screen device; if {@code gc} is {@code null}, 612 * the system default {@code GraphicsConfiguration} is assumed 613 * @exception IllegalArgumentException if {@code gc} 614 * is not from a screen device 615 * @exception HeadlessException when 616 * {@code GraphicsEnvironment.isHeadless()} returns 617 * {@code true} 618 * 619 * @see java.awt.GraphicsEnvironment#isHeadless 620 * @see GraphicsConfiguration#getBounds 621 * @see #isShowing 622 * @since 1.3 623 */ 624 public Window(Window owner, GraphicsConfiguration gc) { 625 this(gc); 626 ownedInit(owner); 627 } 628 629 private void ownedInit(Window owner) { 630 this.parent = owner; 631 if (owner != null) { 632 owner.addOwnedWindow(weakThis); 633 if (owner.isAlwaysOnTop()) { 634 try { 635 setAlwaysOnTop(true); 636 } catch (SecurityException ignore) { 637 } 638 } 639 } 640 641 // WindowDisposerRecord requires a proper value of parent field. 642 disposerRecord.updateOwner(); 643 } 644 645 /** 646 * Construct a name for this component. Called by getName() when the 647 * name is null. 648 */ 649 String constructComponentName() { 650 synchronized (Window.class) { 651 return base + nameCounter++; 652 } 653 } 654 655 /** 656 * Returns the sequence of images to be displayed as the icon for this window. 657 * <p> 658 * This method returns a copy of the internally stored list, so all operations 659 * on the returned object will not affect the window's behavior. 660 * 661 * @return the copy of icon images' list for this window, or 662 * empty list if this window doesn't have icon images. 663 * @see #setIconImages 664 * @see #setIconImage(Image) 665 * @since 1.6 666 */ 667 public java.util.List<Image> getIconImages() { 668 java.util.List<Image> icons = this.icons; 669 if (icons == null || icons.size() == 0) { 670 return new ArrayList<Image>(); 671 } 672 return new ArrayList<Image>(icons); 673 } 674 675 /** 676 * Sets the sequence of images to be displayed as the icon 677 * for this window. Subsequent calls to {@code getIconImages} will 678 * always return a copy of the {@code icons} list. 679 * <p> 680 * Depending on the platform capabilities one or several images 681 * of different dimensions will be used as the window's icon. 682 * <p> 683 * The {@code icons} list is scanned for the images of most 684 * appropriate dimensions from the beginning. If the list contains 685 * several images of the same size, the first will be used. 686 * <p> 687 * Ownerless windows with no icon specified use platform-default icon. 688 * The icon of an owned window may be inherited from the owner 689 * unless explicitly overridden. 690 * Setting the icon to {@code null} or empty list restores 691 * the default behavior. 692 * <p> 693 * Note : Native windowing systems may use different images of differing 694 * dimensions to represent a window, depending on the context (e.g. 695 * window decoration, window list, taskbar, etc.). They could also use 696 * just a single image for all contexts or no image at all. 697 * 698 * @param icons the list of icon images to be displayed. 699 * @see #getIconImages() 700 * @see #setIconImage(Image) 701 * @since 1.6 702 */ 703 public synchronized void setIconImages(java.util.List<? extends Image> icons) { 704 this.icons = (icons == null) ? new ArrayList<Image>() : 705 new ArrayList<Image>(icons); 706 WindowPeer peer = (WindowPeer)this.peer; 707 if (peer != null) { 708 peer.updateIconImages(); 709 } 710 // Always send a property change event 711 firePropertyChange("iconImage", null, null); 712 } 713 714 /** 715 * Sets the image to be displayed as the icon for this window. 716 * <p> 717 * This method can be used instead of {@link #setIconImages setIconImages()} 718 * to specify a single image as a window's icon. 719 * <p> 720 * The following statement: 721 * <pre> 722 * setIconImage(image); 723 * </pre> 724 * is equivalent to: 725 * <pre> 726 * ArrayList<Image> imageList = new ArrayList<Image>(); 727 * imageList.add(image); 728 * setIconImages(imageList); 729 * </pre> 730 * <p> 731 * Note : Native windowing systems may use different images of differing 732 * dimensions to represent a window, depending on the context (e.g. 733 * window decoration, window list, taskbar, etc.). They could also use 734 * just a single image for all contexts or no image at all. 735 * 736 * @param image the icon image to be displayed. 737 * @see #setIconImages 738 * @see #getIconImages() 739 * @since 1.6 740 */ 741 public void setIconImage(Image image) { 742 ArrayList<Image> imageList = new ArrayList<Image>(); 743 if (image != null) { 744 imageList.add(image); 745 } 746 setIconImages(imageList); 747 } 748 749 /** 750 * Makes this Window displayable by creating the connection to its 751 * native screen resource. 752 * This method is called internally by the toolkit and should 753 * not be called directly by programs. 754 * @see Component#isDisplayable 755 * @see Container#removeNotify 756 * @since 1.0 757 */ 758 @SuppressWarnings("deprecation") 759 public void addNotify() { 760 synchronized (getTreeLock()) { 761 Container parent = this.parent; 762 if (parent != null && parent.getPeer() == null) { 763 parent.addNotify(); 764 } 765 if (peer == null) { 766 peer = getToolkit().createWindow(this); 767 } 768 synchronized (allWindows) { 769 allWindows.add(this); 770 } 771 super.addNotify(); 772 } 773 } 774 775 /** 776 * {@inheritDoc} 777 */ 778 public void removeNotify() { 779 synchronized (getTreeLock()) { 780 synchronized (allWindows) { 781 allWindows.remove(this); 782 } 783 super.removeNotify(); 784 } 785 } 786 787 /** 788 * Causes this Window to be sized to fit the preferred size 789 * and layouts of its subcomponents. The resulting width and 790 * height of the window are automatically enlarged if either 791 * of dimensions is less than the minimum size as specified 792 * by the previous call to the {@code setMinimumSize} method. 793 * <p> 794 * If the window and/or its owner are not displayable yet, 795 * both of them are made displayable before calculating 796 * the preferred size. The Window is validated after its 797 * size is being calculated. 798 * 799 * @see Component#isDisplayable 800 * @see #setMinimumSize 801 */ 802 @SuppressWarnings("deprecation") 803 public void pack() { 804 Container parent = this.parent; 805 if (parent != null && parent.getPeer() == null) { 806 parent.addNotify(); 807 } 808 if (peer == null) { 809 addNotify(); 810 } 811 Dimension newSize = getPreferredSize(); 812 if (peer != null) { 813 setClientSize(newSize.width, newSize.height); 814 } 815 816 if(beforeFirstShow) { 817 isPacked = true; 818 } 819 820 validateUnconditionally(); 821 } 822 823 /** 824 * Sets the minimum size of this window to a constant 825 * value. Subsequent calls to {@code getMinimumSize} 826 * will always return this value. If current window's 827 * size is less than {@code minimumSize} the size of the 828 * window is automatically enlarged to honor the minimum size. 829 * <p> 830 * If the {@code setSize} or {@code setBounds} methods 831 * are called afterwards with a width or height less than 832 * that was specified by the {@code setMinimumSize} method 833 * the window is automatically enlarged to meet 834 * the {@code minimumSize} value. The {@code minimumSize} 835 * value also affects the behaviour of the {@code pack} method. 836 * <p> 837 * The default behavior is restored by setting the minimum size 838 * parameter to the {@code null} value. 839 * <p> 840 * Resizing operation may be restricted if the user tries 841 * to resize window below the {@code minimumSize} value. 842 * This behaviour is platform-dependent. 843 * 844 * @param minimumSize the new minimum size of this window 845 * @see Component#setMinimumSize 846 * @see #getMinimumSize 847 * @see #isMinimumSizeSet 848 * @see #setSize(Dimension) 849 * @see #pack 850 * @since 1.6 851 */ 852 public void setMinimumSize(Dimension minimumSize) { 853 synchronized (getTreeLock()) { 854 super.setMinimumSize(minimumSize); 855 Dimension size = getSize(); 856 if (isMinimumSizeSet()) { 857 if (size.width < minimumSize.width || size.height < minimumSize.height) { 858 int nw = Math.max(width, minimumSize.width); 859 int nh = Math.max(height, minimumSize.height); 860 setSize(nw, nh); 861 } 862 } 863 if (peer != null) { 864 ((WindowPeer)peer).updateMinimumSize(); 865 } 866 } 867 } 868 869 /** 870 * {@inheritDoc} 871 * <p> 872 * The {@code d.width} and {@code d.height} values 873 * are automatically enlarged if either is less than 874 * the minimum size as specified by previous call to 875 * {@code setMinimumSize}. 876 * <p> 877 * The method changes the geometry-related data. Therefore, 878 * the native windowing system may ignore such requests, or it may modify 879 * the requested data, so that the {@code Window} object is placed and sized 880 * in a way that corresponds closely to the desktop settings. 881 * 882 * @see #getSize 883 * @see #setBounds 884 * @see #setMinimumSize 885 * @since 1.6 886 */ 887 public void setSize(Dimension d) { 888 super.setSize(d); 889 } 890 891 /** 892 * {@inheritDoc} 893 * <p> 894 * The {@code width} and {@code height} values 895 * are automatically enlarged if either is less than 896 * the minimum size as specified by previous call to 897 * {@code setMinimumSize}. 898 * <p> 899 * The method changes the geometry-related data. Therefore, 900 * the native windowing system may ignore such requests, or it may modify 901 * the requested data, so that the {@code Window} object is placed and sized 902 * in a way that corresponds closely to the desktop settings. 903 * 904 * @see #getSize 905 * @see #setBounds 906 * @see #setMinimumSize 907 * @since 1.6 908 */ 909 public void setSize(int width, int height) { 910 super.setSize(width, height); 911 } 912 913 /** 914 * {@inheritDoc} 915 * <p> 916 * The method changes the geometry-related data. Therefore, 917 * the native windowing system may ignore such requests, or it may modify 918 * the requested data, so that the {@code Window} object is placed and sized 919 * in a way that corresponds closely to the desktop settings. 920 */ 921 @Override 922 public void setLocation(int x, int y) { 923 super.setLocation(x, y); 924 } 925 926 /** 927 * {@inheritDoc} 928 * <p> 929 * The method changes the geometry-related data. Therefore, 930 * the native windowing system may ignore such requests, or it may modify 931 * the requested data, so that the {@code Window} object is placed and sized 932 * in a way that corresponds closely to the desktop settings. 933 */ 934 @Override 935 public void setLocation(Point p) { 936 super.setLocation(p); 937 } 938 939 /** 940 * @deprecated As of JDK version 1.1, 941 * replaced by {@code setBounds(int, int, int, int)}. 942 */ 943 @Deprecated 944 public void reshape(int x, int y, int width, int height) { 945 if (isMinimumSizeSet()) { 946 Dimension minSize = getMinimumSize(); 947 if (width < minSize.width) { 948 width = minSize.width; 949 } 950 if (height < minSize.height) { 951 height = minSize.height; 952 } 953 } 954 super.reshape(x, y, width, height); 955 } 956 957 void setClientSize(int w, int h) { 958 synchronized (getTreeLock()) { 959 setBoundsOp(ComponentPeer.SET_CLIENT_SIZE); 960 setBounds(x, y, w, h); 961 } 962 } 963 964 static private final AtomicBoolean 965 beforeFirstWindowShown = new AtomicBoolean(true); 966 967 final void closeSplashScreen() { 968 if (isTrayIconWindow) { 969 return; 970 } 971 if (beforeFirstWindowShown.getAndSet(false)) { 972 // We don't use SplashScreen.getSplashScreen() to avoid instantiating 973 // the object if it hasn't been requested by user code explicitly 974 SunToolkit.closeSplashScreen(); 975 SplashScreen.markClosed(); 976 } 977 } 978 979 /** 980 * Shows or hides this {@code Window} depending on the value of parameter 981 * {@code b}. 982 * <p> 983 * If the method shows the window then the window is also made 984 * focused under the following conditions: 985 * <ul> 986 * <li> The {@code Window} meets the requirements outlined in the 987 * {@link #isFocusableWindow} method. 988 * <li> The {@code Window}'s {@code autoRequestFocus} property is of the {@code true} value. 989 * <li> Native windowing system allows the {@code Window} to get focused. 990 * </ul> 991 * There is an exception for the second condition (the value of the 992 * {@code autoRequestFocus} property). The property is not taken into account if the 993 * window is a modal dialog, which blocks the currently focused window. 994 * <p> 995 * Developers must never assume that the window is the focused or active window 996 * until it receives a WINDOW_GAINED_FOCUS or WINDOW_ACTIVATED event. 997 * @param b if {@code true}, makes the {@code Window} visible, 998 * otherwise hides the {@code Window}. 999 * If the {@code Window} and/or its owner 1000 * are not yet displayable, both are made displayable. The 1001 * {@code Window} will be validated prior to being made visible. 1002 * If the {@code Window} is already visible, this will bring the 1003 * {@code Window} to the front.<p> 1004 * If {@code false}, hides this {@code Window}, its subcomponents, and all 1005 * of its owned children. 1006 * The {@code Window} and its subcomponents can be made visible again 1007 * with a call to {@code #setVisible(true)}. 1008 * @see java.awt.Component#isDisplayable 1009 * @see java.awt.Component#setVisible 1010 * @see java.awt.Window#toFront 1011 * @see java.awt.Window#dispose 1012 * @see java.awt.Window#setAutoRequestFocus 1013 * @see java.awt.Window#isFocusableWindow 1014 */ 1015 public void setVisible(boolean b) { 1016 super.setVisible(b); 1017 } 1018 1019 /** 1020 * Makes the Window visible. If the Window and/or its owner 1021 * are not yet displayable, both are made displayable. The 1022 * Window will be validated prior to being made visible. 1023 * If the Window is already visible, this will bring the Window 1024 * to the front. 1025 * @see Component#isDisplayable 1026 * @see #toFront 1027 * @deprecated As of JDK version 1.5, replaced by 1028 * {@link #setVisible(boolean)}. 1029 */ 1030 @Deprecated 1031 public void show() { 1032 if (peer == null) { 1033 addNotify(); 1034 } 1035 validateUnconditionally(); 1036 1037 isInShow = true; 1038 if (visible) { 1039 toFront(); 1040 } else { 1041 beforeFirstShow = false; 1042 closeSplashScreen(); 1043 Dialog.checkShouldBeBlocked(this); 1044 super.show(); 1045 synchronized (getTreeLock()) { 1046 this.locationByPlatform = false; 1047 } 1048 for (int i = 0; i < ownedWindowList.size(); i++) { 1049 Window child = ownedWindowList.elementAt(i).get(); 1050 if ((child != null) && child.showWithParent) { 1051 child.show(); 1052 child.showWithParent = false; 1053 } // endif 1054 } // endfor 1055 if (!isModalBlocked()) { 1056 updateChildrenBlocking(); 1057 } else { 1058 // fix for 6532736: after this window is shown, its blocker 1059 // should be raised to front 1060 modalBlocker.toFront_NoClientCode(); 1061 } 1062 if (this instanceof Frame || this instanceof Dialog) { 1063 updateChildFocusableWindowState(this); 1064 } 1065 } 1066 isInShow = false; 1067 1068 // If first time shown, generate WindowOpened event 1069 if ((state & OPENED) == 0) { 1070 postWindowEvent(WindowEvent.WINDOW_OPENED); 1071 state |= OPENED; 1072 } 1073 } 1074 1075 @SuppressWarnings("deprecation") 1076 static void updateChildFocusableWindowState(Window w) { 1077 if (w.getPeer() != null && w.isShowing()) { 1078 ((WindowPeer)w.getPeer()).updateFocusableWindowState(); 1079 } 1080 for (int i = 0; i < w.ownedWindowList.size(); i++) { 1081 Window child = w.ownedWindowList.elementAt(i).get(); 1082 if (child != null) { 1083 updateChildFocusableWindowState(child); 1084 } 1085 } 1086 } 1087 1088 synchronized void postWindowEvent(int id) { 1089 if (windowListener != null 1090 || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0 1091 || Toolkit.enabledOnToolkit(AWTEvent.WINDOW_EVENT_MASK)) { 1092 WindowEvent e = new WindowEvent(this, id); 1093 Toolkit.getEventQueue().postEvent(e); 1094 } 1095 } 1096 1097 /** 1098 * Hide this Window, its subcomponents, and all of its owned children. 1099 * The Window and its subcomponents can be made visible again 1100 * with a call to {@code show}. 1101 * @see #show 1102 * @see #dispose 1103 * @deprecated As of JDK version 1.5, replaced by 1104 * {@link #setVisible(boolean)}. 1105 */ 1106 @Deprecated 1107 public void hide() { 1108 synchronized(ownedWindowList) { 1109 for (int i = 0; i < ownedWindowList.size(); i++) { 1110 Window child = ownedWindowList.elementAt(i).get(); 1111 if ((child != null) && child.visible) { 1112 child.hide(); 1113 child.showWithParent = true; 1114 } 1115 } 1116 } 1117 if (isModalBlocked()) { 1118 modalBlocker.unblockWindow(this); 1119 } 1120 super.hide(); 1121 synchronized (getTreeLock()) { 1122 this.locationByPlatform = false; 1123 } 1124 } 1125 1126 final void clearMostRecentFocusOwnerOnHide() { 1127 /* do nothing */ 1128 } 1129 1130 /** 1131 * Releases all of the native screen resources used by this 1132 * {@code Window}, its subcomponents, and all of its owned 1133 * children. That is, the resources for these {@code Component}s 1134 * will be destroyed, any memory they consume will be returned to the 1135 * OS, and they will be marked as undisplayable. 1136 * <p> 1137 * The {@code Window} and its subcomponents can be made displayable 1138 * again by rebuilding the native resources with a subsequent call to 1139 * {@code pack} or {@code show}. The states of the recreated 1140 * {@code Window} and its subcomponents will be identical to the 1141 * states of these objects at the point where the {@code Window} 1142 * was disposed (not accounting for additional modifications between 1143 * those actions). 1144 * <p> 1145 * <b>Note</b>: When the last displayable window 1146 * within the Java virtual machine (VM) is disposed of, the VM may 1147 * terminate. See <a href="doc-files/AWTThreadIssues.html#Autoshutdown"> 1148 * AWT Threading Issues</a> for more information. 1149 * @see Component#isDisplayable 1150 * @see #pack 1151 * @see #show 1152 */ 1153 public void dispose() { 1154 doDispose(); 1155 } 1156 1157 /* 1158 * Fix for 4872170. 1159 * If dispose() is called on parent then its children have to be disposed as well 1160 * as reported in javadoc. So we need to implement this functionality even if a 1161 * child overrides dispose() in a wrong way without calling super.dispose(). 1162 */ 1163 @SuppressWarnings("deprecation") 1164 void disposeImpl() { 1165 dispose(); 1166 if (getPeer() != null) { 1167 doDispose(); 1168 } 1169 } 1170 1171 void doDispose() { 1172 class DisposeAction implements Runnable { 1173 public void run() { 1174 disposing = true; 1175 try { 1176 // Check if this window is the fullscreen window for the 1177 // device. Exit the fullscreen mode prior to disposing 1178 // of the window if that's the case. 1179 GraphicsDevice gd = getGraphicsConfiguration().getDevice(); 1180 if (gd.getFullScreenWindow() == Window.this) { 1181 gd.setFullScreenWindow(null); 1182 } 1183 1184 Object[] ownedWindowArray; 1185 synchronized(ownedWindowList) { 1186 ownedWindowArray = new Object[ownedWindowList.size()]; 1187 ownedWindowList.copyInto(ownedWindowArray); 1188 } 1189 for (int i = 0; i < ownedWindowArray.length; i++) { 1190 Window child = (Window) (((WeakReference) 1191 (ownedWindowArray[i])).get()); 1192 if (child != null) { 1193 child.disposeImpl(); 1194 } 1195 } 1196 hide(); 1197 beforeFirstShow = true; 1198 removeNotify(); 1199 synchronized (inputContextLock) { 1200 if (inputContext != null) { 1201 inputContext.dispose(); 1202 inputContext = null; 1203 } 1204 } 1205 clearCurrentFocusCycleRootOnHide(); 1206 } finally { 1207 disposing = false; 1208 } 1209 } 1210 } 1211 boolean fireWindowClosedEvent = isDisplayable(); 1212 DisposeAction action = new DisposeAction(); 1213 if (EventQueue.isDispatchThread()) { 1214 action.run(); 1215 } 1216 else { 1217 try { 1218 EventQueue.invokeAndWait(this, action); 1219 } 1220 catch (InterruptedException e) { 1221 System.err.println("Disposal was interrupted:"); 1222 e.printStackTrace(); 1223 } 1224 catch (InvocationTargetException e) { 1225 System.err.println("Exception during disposal:"); 1226 e.printStackTrace(); 1227 } 1228 } 1229 // Execute outside the Runnable because postWindowEvent is 1230 // synchronized on (this). We don't need to synchronize the call 1231 // on the EventQueue anyways. 1232 if (fireWindowClosedEvent) { 1233 postWindowEvent(WindowEvent.WINDOW_CLOSED); 1234 } 1235 } 1236 1237 /* 1238 * Should only be called while holding the tree lock. 1239 * It's overridden here because parent == owner in Window, 1240 * and we shouldn't adjust counter on owner 1241 */ 1242 void adjustListeningChildrenOnParent(long mask, int num) { 1243 } 1244 1245 // Should only be called while holding tree lock 1246 void adjustDescendantsOnParent(int num) { 1247 // do nothing since parent == owner and we shouldn't 1248 // adjust counter on owner 1249 } 1250 1251 /** 1252 * If this Window is visible, brings this Window to the front and may make 1253 * it the focused Window. 1254 * <p> 1255 * Places this Window at the top of the stacking order and shows it in 1256 * front of any other Windows in this VM. No action will take place if this 1257 * Window is not visible. Some platforms do not allow Windows which own 1258 * other Windows to appear on top of those owned Windows. Some platforms 1259 * may not permit this VM to place its Windows above windows of native 1260 * applications, or Windows of other VMs. This permission may depend on 1261 * whether a Window in this VM is already focused. Every attempt will be 1262 * made to move this Window as high as possible in the stacking order; 1263 * however, developers should not assume that this method will move this 1264 * Window above all other windows in every situation. 1265 * <p> 1266 * Developers must never assume that this Window is the focused or active 1267 * Window until this Window receives a WINDOW_GAINED_FOCUS or WINDOW_ACTIVATED 1268 * event. On platforms where the top-most window is the focused window, this 1269 * method will <b>probably</b> focus this Window (if it is not already focused) 1270 * under the following conditions: 1271 * <ul> 1272 * <li> The window meets the requirements outlined in the 1273 * {@link #isFocusableWindow} method. 1274 * <li> The window's property {@code autoRequestFocus} is of the 1275 * {@code true} value. 1276 * <li> Native windowing system allows the window to get focused. 1277 * </ul> 1278 * On platforms where the stacking order does not typically affect the focused 1279 * window, this method will <b>probably</b> leave the focused and active 1280 * Windows unchanged. 1281 * <p> 1282 * If this method causes this Window to be focused, and this Window is a 1283 * Frame or a Dialog, it will also become activated. If this Window is 1284 * focused, but it is not a Frame or a Dialog, then the first Frame or 1285 * Dialog that is an owner of this Window will be activated. 1286 * <p> 1287 * If this window is blocked by modal dialog, then the blocking dialog 1288 * is brought to the front and remains above the blocked window. 1289 * 1290 * @see #toBack 1291 * @see #setAutoRequestFocus 1292 * @see #isFocusableWindow 1293 */ 1294 public void toFront() { 1295 toFront_NoClientCode(); 1296 } 1297 1298 // This functionality is implemented in a final package-private method 1299 // to insure that it cannot be overridden by client subclasses. 1300 final void toFront_NoClientCode() { 1301 if (visible) { 1302 WindowPeer peer = (WindowPeer)this.peer; 1303 if (peer != null) { 1304 peer.toFront(); 1305 } 1306 if (isModalBlocked()) { 1307 modalBlocker.toFront_NoClientCode(); 1308 } 1309 } 1310 } 1311 1312 /** 1313 * If this Window is visible, sends this Window to the back and may cause 1314 * it to lose focus or activation if it is the focused or active Window. 1315 * <p> 1316 * Places this Window at the bottom of the stacking order and shows it 1317 * behind any other Windows in this VM. No action will take place is this 1318 * Window is not visible. Some platforms do not allow Windows which are 1319 * owned by other Windows to appear below their owners. Every attempt will 1320 * be made to move this Window as low as possible in the stacking order; 1321 * however, developers should not assume that this method will move this 1322 * Window below all other windows in every situation. 1323 * <p> 1324 * Because of variations in native windowing systems, no guarantees about 1325 * changes to the focused and active Windows can be made. Developers must 1326 * never assume that this Window is no longer the focused or active Window 1327 * until this Window receives a WINDOW_LOST_FOCUS or WINDOW_DEACTIVATED 1328 * event. On platforms where the top-most window is the focused window, 1329 * this method will <b>probably</b> cause this Window to lose focus. In 1330 * that case, the next highest, focusable Window in this VM will receive 1331 * focus. On platforms where the stacking order does not typically affect 1332 * the focused window, this method will <b>probably</b> leave the focused 1333 * and active Windows unchanged. 1334 * 1335 * @see #toFront 1336 */ 1337 public void toBack() { 1338 toBack_NoClientCode(); 1339 } 1340 1341 // This functionality is implemented in a final package-private method 1342 // to insure that it cannot be overridden by client subclasses. 1343 final void toBack_NoClientCode() { 1344 if(isAlwaysOnTop()) { 1345 try { 1346 setAlwaysOnTop(false); 1347 }catch(SecurityException e) { 1348 } 1349 } 1350 if (visible) { 1351 WindowPeer peer = (WindowPeer)this.peer; 1352 if (peer != null) { 1353 peer.toBack(); 1354 } 1355 } 1356 } 1357 1358 /** 1359 * Returns the toolkit of this frame. 1360 * @return the toolkit of this window. 1361 * @see Toolkit 1362 * @see Toolkit#getDefaultToolkit 1363 * @see Component#getToolkit 1364 */ 1365 public Toolkit getToolkit() { 1366 return Toolkit.getDefaultToolkit(); 1367 } 1368 1369 /** 1370 * Gets the warning string that is displayed with this window. 1371 * If this window is insecure, the warning string is displayed 1372 * somewhere in the visible area of the window. A window is 1373 * insecure if there is a security manager and the security 1374 * manager denies 1375 * {@code AWTPermission("showWindowWithoutWarningBanner")}. 1376 * <p> 1377 * If the window is secure, then {@code getWarningString} 1378 * returns {@code null}. If the window is insecure, this 1379 * method checks for the system property 1380 * {@code awt.appletWarning} 1381 * and returns the string value of that property. 1382 * @return the warning string for this window. 1383 */ 1384 public final String getWarningString() { 1385 return warningString; 1386 } 1387 1388 private void setWarningString() { 1389 warningString = null; 1390 SecurityManager sm = System.getSecurityManager(); 1391 if (sm != null) { 1392 try { 1393 sm.checkPermission(AWTPermissions.TOPLEVEL_WINDOW_PERMISSION); 1394 } catch (SecurityException se) { 1395 // make sure the privileged action is only 1396 // for getting the property! We don't want the 1397 // above checkPermission call to always succeed! 1398 warningString = AccessController.doPrivileged( 1399 new GetPropertyAction("awt.appletWarning", 1400 "Java Applet Window")); 1401 } 1402 } 1403 } 1404 1405 /** 1406 * Gets the {@code Locale} object that is associated 1407 * with this window, if the locale has been set. 1408 * If no locale has been set, then the default locale 1409 * is returned. 1410 * @return the locale that is set for this window. 1411 * @see java.util.Locale 1412 * @since 1.1 1413 */ 1414 public Locale getLocale() { 1415 if (this.locale == null) { 1416 return Locale.getDefault(); 1417 } 1418 return this.locale; 1419 } 1420 1421 /** 1422 * Gets the input context for this window. A window always has an input context, 1423 * which is shared by subcomponents unless they create and set their own. 1424 * @see Component#getInputContext 1425 * @since 1.2 1426 */ 1427 public InputContext getInputContext() { 1428 synchronized (inputContextLock) { 1429 if (inputContext == null) { 1430 inputContext = InputContext.getInstance(); 1431 } 1432 } 1433 return inputContext; 1434 } 1435 1436 /** 1437 * Set the cursor image to a specified cursor. 1438 * <p> 1439 * The method may have no visual effect if the Java platform 1440 * implementation and/or the native system do not support 1441 * changing the mouse cursor shape. 1442 * @param cursor One of the constants defined 1443 * by the {@code Cursor} class. If this parameter is null 1444 * then the cursor for this window will be set to the type 1445 * Cursor.DEFAULT_CURSOR. 1446 * @see Component#getCursor 1447 * @see Cursor 1448 * @since 1.1 1449 */ 1450 public void setCursor(Cursor cursor) { 1451 if (cursor == null) { 1452 cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); 1453 } 1454 super.setCursor(cursor); 1455 } 1456 1457 /** 1458 * Returns the owner of this window. 1459 * 1460 * @return the owner of this window 1461 * @since 1.2 1462 */ 1463 public Window getOwner() { 1464 return getOwner_NoClientCode(); 1465 } 1466 final Window getOwner_NoClientCode() { 1467 return (Window)parent; 1468 } 1469 1470 /** 1471 * Return an array containing all the windows this 1472 * window currently owns. 1473 * 1474 * @return the array of all the owned windows 1475 * @since 1.2 1476 */ 1477 public Window[] getOwnedWindows() { 1478 return getOwnedWindows_NoClientCode(); 1479 } 1480 final Window[] getOwnedWindows_NoClientCode() { 1481 Window realCopy[]; 1482 1483 synchronized(ownedWindowList) { 1484 // Recall that ownedWindowList is actually a Vector of 1485 // WeakReferences and calling get() on one of these references 1486 // may return null. Make two arrays-- one the size of the 1487 // Vector (fullCopy with size fullSize), and one the size of 1488 // all non-null get()s (realCopy with size realSize). 1489 int fullSize = ownedWindowList.size(); 1490 int realSize = 0; 1491 Window fullCopy[] = new Window[fullSize]; 1492 1493 for (int i = 0; i < fullSize; i++) { 1494 fullCopy[realSize] = ownedWindowList.elementAt(i).get(); 1495 1496 if (fullCopy[realSize] != null) { 1497 realSize++; 1498 } 1499 } 1500 1501 if (fullSize != realSize) { 1502 realCopy = Arrays.copyOf(fullCopy, realSize); 1503 } else { 1504 realCopy = fullCopy; 1505 } 1506 } 1507 1508 return realCopy; 1509 } 1510 1511 boolean isModalBlocked() { 1512 return modalBlocker != null; 1513 } 1514 1515 void setModalBlocked(Dialog blocker, boolean blocked, boolean peerCall) { 1516 this.modalBlocker = blocked ? blocker : null; 1517 if (peerCall) { 1518 WindowPeer peer = (WindowPeer)this.peer; 1519 if (peer != null) { 1520 peer.setModalBlocked(blocker, blocked); 1521 } 1522 } 1523 } 1524 1525 Dialog getModalBlocker() { 1526 return modalBlocker; 1527 } 1528 1529 /* 1530 * Returns a list of all displayable Windows, i. e. all the 1531 * Windows which peer is not null. 1532 * 1533 * @see #addNotify 1534 * @see #removeNotify 1535 */ 1536 static IdentityArrayList<Window> getAllWindows() { 1537 synchronized (allWindows) { 1538 IdentityArrayList<Window> v = new IdentityArrayList<Window>(); 1539 v.addAll(allWindows); 1540 return v; 1541 } 1542 } 1543 1544 static IdentityArrayList<Window> getAllUnblockedWindows() { 1545 synchronized (allWindows) { 1546 IdentityArrayList<Window> unblocked = new IdentityArrayList<Window>(); 1547 for (int i = 0; i < allWindows.size(); i++) { 1548 Window w = allWindows.get(i); 1549 if (!w.isModalBlocked()) { 1550 unblocked.add(w); 1551 } 1552 } 1553 return unblocked; 1554 } 1555 } 1556 1557 private static Window[] getWindows(AppContext appContext) { 1558 synchronized (Window.class) { 1559 Window realCopy[]; 1560 @SuppressWarnings("unchecked") 1561 Vector<WeakReference<Window>> windowList = 1562 (Vector<WeakReference<Window>>)appContext.get(Window.class); 1563 if (windowList != null) { 1564 int fullSize = windowList.size(); 1565 int realSize = 0; 1566 Window fullCopy[] = new Window[fullSize]; 1567 for (int i = 0; i < fullSize; i++) { 1568 Window w = windowList.get(i).get(); 1569 if (w != null) { 1570 fullCopy[realSize++] = w; 1571 } 1572 } 1573 if (fullSize != realSize) { 1574 realCopy = Arrays.copyOf(fullCopy, realSize); 1575 } else { 1576 realCopy = fullCopy; 1577 } 1578 } else { 1579 realCopy = new Window[0]; 1580 } 1581 return realCopy; 1582 } 1583 } 1584 1585 /** 1586 * Returns an array of all {@code Window}s, both owned and ownerless, 1587 * created by this application. 1588 * If called from an applet, the array includes only the {@code Window}s 1589 * accessible by that applet. 1590 * <p> 1591 * <b>Warning:</b> this method may return system created windows, such 1592 * as a print dialog. Applications should not assume the existence of 1593 * these dialogs, nor should an application assume anything about these 1594 * dialogs such as component positions, {@code LayoutManager}s 1595 * or serialization. 1596 * 1597 * @return the array of all the {@code Window}s created by the application 1598 * @see Frame#getFrames 1599 * @see Window#getOwnerlessWindows 1600 * 1601 * @since 1.6 1602 */ 1603 public static Window[] getWindows() { 1604 return getWindows(AppContext.getAppContext()); 1605 } 1606 1607 /** 1608 * Returns an array of all {@code Window}s created by this application 1609 * that have no owner. They include {@code Frame}s and ownerless 1610 * {@code Dialog}s and {@code Window}s. 1611 * If called from an applet, the array includes only the {@code Window}s 1612 * accessible by that applet. 1613 * <p> 1614 * <b>Warning:</b> this method may return system created windows, such 1615 * as a print dialog. Applications should not assume the existence of 1616 * these dialogs, nor should an application assume anything about these 1617 * dialogs such as component positions, {@code LayoutManager}s 1618 * or serialization. 1619 * 1620 * @return the array of all the ownerless {@code Window}s 1621 * created by this application 1622 * @see Frame#getFrames 1623 * @see Window#getWindows() 1624 * 1625 * @since 1.6 1626 */ 1627 public static Window[] getOwnerlessWindows() { 1628 Window[] allWindows = Window.getWindows(); 1629 1630 int ownerlessCount = 0; 1631 for (Window w : allWindows) { 1632 if (w.getOwner() == null) { 1633 ownerlessCount++; 1634 } 1635 } 1636 1637 Window[] ownerless = new Window[ownerlessCount]; 1638 int c = 0; 1639 for (Window w : allWindows) { 1640 if (w.getOwner() == null) { 1641 ownerless[c++] = w; 1642 } 1643 } 1644 1645 return ownerless; 1646 } 1647 1648 Window getDocumentRoot() { 1649 synchronized (getTreeLock()) { 1650 Window w = this; 1651 while (w.getOwner() != null) { 1652 w = w.getOwner(); 1653 } 1654 return w; 1655 } 1656 } 1657 1658 /** 1659 * Specifies the modal exclusion type for this window. If a window is modal 1660 * excluded, it is not blocked by some modal dialogs. See {@link 1661 * java.awt.Dialog.ModalExclusionType Dialog.ModalExclusionType} for 1662 * possible modal exclusion types. 1663 * <p> 1664 * If the given type is not supported, {@code NO_EXCLUDE} is used. 1665 * <p> 1666 * Note: changing the modal exclusion type for a visible window may have no 1667 * effect until it is hidden and then shown again. 1668 * 1669 * @param exclusionType the modal exclusion type for this window; a {@code null} 1670 * value is equivalent to {@link Dialog.ModalExclusionType#NO_EXCLUDE 1671 * NO_EXCLUDE} 1672 * @throws SecurityException if the calling thread does not have permission 1673 * to set the modal exclusion property to the window with the given 1674 * {@code exclusionType} 1675 * @see java.awt.Dialog.ModalExclusionType 1676 * @see java.awt.Window#getModalExclusionType 1677 * @see java.awt.Toolkit#isModalExclusionTypeSupported 1678 * 1679 * @since 1.6 1680 */ 1681 public void setModalExclusionType(Dialog.ModalExclusionType exclusionType) { 1682 if (exclusionType == null) { 1683 exclusionType = Dialog.ModalExclusionType.NO_EXCLUDE; 1684 } 1685 if (!Toolkit.getDefaultToolkit().isModalExclusionTypeSupported(exclusionType)) { 1686 exclusionType = Dialog.ModalExclusionType.NO_EXCLUDE; 1687 } 1688 if (modalExclusionType == exclusionType) { 1689 return; 1690 } 1691 if (exclusionType == Dialog.ModalExclusionType.TOOLKIT_EXCLUDE) { 1692 SecurityManager sm = System.getSecurityManager(); 1693 if (sm != null) { 1694 sm.checkPermission(AWTPermissions.TOOLKIT_MODALITY_PERMISSION); 1695 } 1696 } 1697 modalExclusionType = exclusionType; 1698 1699 // if we want on-fly changes, we need to uncomment the lines below 1700 // and override the method in Dialog to use modalShow() instead 1701 // of updateChildrenBlocking() 1702 /* 1703 if (isModalBlocked()) { 1704 modalBlocker.unblockWindow(this); 1705 } 1706 Dialog.checkShouldBeBlocked(this); 1707 updateChildrenBlocking(); 1708 */ 1709 } 1710 1711 /** 1712 * Returns the modal exclusion type of this window. 1713 * 1714 * @return the modal exclusion type of this window 1715 * 1716 * @see java.awt.Dialog.ModalExclusionType 1717 * @see java.awt.Window#setModalExclusionType 1718 * 1719 * @since 1.6 1720 */ 1721 public Dialog.ModalExclusionType getModalExclusionType() { 1722 return modalExclusionType; 1723 } 1724 1725 boolean isModalExcluded(Dialog.ModalExclusionType exclusionType) { 1726 if ((modalExclusionType != null) && 1727 modalExclusionType.compareTo(exclusionType) >= 0) 1728 { 1729 return true; 1730 } 1731 Window owner = getOwner_NoClientCode(); 1732 return (owner != null) && owner.isModalExcluded(exclusionType); 1733 } 1734 1735 void updateChildrenBlocking() { 1736 Vector<Window> childHierarchy = new Vector<Window>(); 1737 Window[] ownedWindows = getOwnedWindows(); 1738 for (int i = 0; i < ownedWindows.length; i++) { 1739 childHierarchy.add(ownedWindows[i]); 1740 } 1741 int k = 0; 1742 while (k < childHierarchy.size()) { 1743 Window w = childHierarchy.get(k); 1744 if (w.isVisible()) { 1745 if (w.isModalBlocked()) { 1746 Dialog blocker = w.getModalBlocker(); 1747 blocker.unblockWindow(w); 1748 } 1749 Dialog.checkShouldBeBlocked(w); 1750 Window[] wOwned = w.getOwnedWindows(); 1751 for (int j = 0; j < wOwned.length; j++) { 1752 childHierarchy.add(wOwned[j]); 1753 } 1754 } 1755 k++; 1756 } 1757 } 1758 1759 /** 1760 * Adds the specified window listener to receive window events from 1761 * this window. 1762 * If l is null, no exception is thrown and no action is performed. 1763 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" 1764 * >AWT Threading Issues</a> for details on AWT's threading model. 1765 * 1766 * @param l the window listener 1767 * @see #removeWindowListener 1768 * @see #getWindowListeners 1769 */ 1770 public synchronized void addWindowListener(WindowListener l) { 1771 if (l == null) { 1772 return; 1773 } 1774 newEventsOnly = true; 1775 windowListener = AWTEventMulticaster.add(windowListener, l); 1776 } 1777 1778 /** 1779 * Adds the specified window state listener to receive window 1780 * events from this window. If {@code l} is {@code null}, 1781 * no exception is thrown and no action is performed. 1782 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" 1783 * >AWT Threading Issues</a> for details on AWT's threading model. 1784 * 1785 * @param l the window state listener 1786 * @see #removeWindowStateListener 1787 * @see #getWindowStateListeners 1788 * @since 1.4 1789 */ 1790 public synchronized void addWindowStateListener(WindowStateListener l) { 1791 if (l == null) { 1792 return; 1793 } 1794 windowStateListener = AWTEventMulticaster.add(windowStateListener, l); 1795 newEventsOnly = true; 1796 } 1797 1798 /** 1799 * Adds the specified window focus listener to receive window events 1800 * from this window. 1801 * If l is null, no exception is thrown and no action is performed. 1802 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" 1803 * >AWT Threading Issues</a> for details on AWT's threading model. 1804 * 1805 * @param l the window focus listener 1806 * @see #removeWindowFocusListener 1807 * @see #getWindowFocusListeners 1808 * @since 1.4 1809 */ 1810 public synchronized void addWindowFocusListener(WindowFocusListener l) { 1811 if (l == null) { 1812 return; 1813 } 1814 windowFocusListener = AWTEventMulticaster.add(windowFocusListener, l); 1815 newEventsOnly = true; 1816 } 1817 1818 /** 1819 * Removes the specified window listener so that it no longer 1820 * receives window events from this window. 1821 * If l is null, no exception is thrown and no action is performed. 1822 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" 1823 * >AWT Threading Issues</a> for details on AWT's threading model. 1824 * 1825 * @param l the window listener 1826 * @see #addWindowListener 1827 * @see #getWindowListeners 1828 */ 1829 public synchronized void removeWindowListener(WindowListener l) { 1830 if (l == null) { 1831 return; 1832 } 1833 windowListener = AWTEventMulticaster.remove(windowListener, l); 1834 } 1835 1836 /** 1837 * Removes the specified window state listener so that it no 1838 * longer receives window events from this window. If 1839 * {@code l} is {@code null}, no exception is thrown and 1840 * no action is performed. 1841 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" 1842 * >AWT Threading Issues</a> for details on AWT's threading model. 1843 * 1844 * @param l the window state listener 1845 * @see #addWindowStateListener 1846 * @see #getWindowStateListeners 1847 * @since 1.4 1848 */ 1849 public synchronized void removeWindowStateListener(WindowStateListener l) { 1850 if (l == null) { 1851 return; 1852 } 1853 windowStateListener = AWTEventMulticaster.remove(windowStateListener, l); 1854 } 1855 1856 /** 1857 * Removes the specified window focus listener so that it no longer 1858 * receives window events from this window. 1859 * If l is null, no exception is thrown and no action is performed. 1860 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads" 1861 * >AWT Threading Issues</a> for details on AWT's threading model. 1862 * 1863 * @param l the window focus listener 1864 * @see #addWindowFocusListener 1865 * @see #getWindowFocusListeners 1866 * @since 1.4 1867 */ 1868 public synchronized void removeWindowFocusListener(WindowFocusListener l) { 1869 if (l == null) { 1870 return; 1871 } 1872 windowFocusListener = AWTEventMulticaster.remove(windowFocusListener, l); 1873 } 1874 1875 /** 1876 * Returns an array of all the window listeners 1877 * registered on this window. 1878 * 1879 * @return all of this window's {@code WindowListener}s 1880 * or an empty array if no window 1881 * listeners are currently registered 1882 * 1883 * @see #addWindowListener 1884 * @see #removeWindowListener 1885 * @since 1.4 1886 */ 1887 public synchronized WindowListener[] getWindowListeners() { 1888 return getListeners(WindowListener.class); 1889 } 1890 1891 /** 1892 * Returns an array of all the window focus listeners 1893 * registered on this window. 1894 * 1895 * @return all of this window's {@code WindowFocusListener}s 1896 * or an empty array if no window focus 1897 * listeners are currently registered 1898 * 1899 * @see #addWindowFocusListener 1900 * @see #removeWindowFocusListener 1901 * @since 1.4 1902 */ 1903 public synchronized WindowFocusListener[] getWindowFocusListeners() { 1904 return getListeners(WindowFocusListener.class); 1905 } 1906 1907 /** 1908 * Returns an array of all the window state listeners 1909 * registered on this window. 1910 * 1911 * @return all of this window's {@code WindowStateListener}s 1912 * or an empty array if no window state 1913 * listeners are currently registered 1914 * 1915 * @see #addWindowStateListener 1916 * @see #removeWindowStateListener 1917 * @since 1.4 1918 */ 1919 public synchronized WindowStateListener[] getWindowStateListeners() { 1920 return getListeners(WindowStateListener.class); 1921 } 1922 1923 1924 /** 1925 * Returns an array of all the objects currently registered 1926 * as <code><em>Foo</em>Listener</code>s 1927 * upon this {@code Window}. 1928 * <code><em>Foo</em>Listener</code>s are registered using the 1929 * <code>add<em>Foo</em>Listener</code> method. 1930 * 1931 * <p> 1932 * 1933 * You can specify the {@code listenerType} argument 1934 * with a class literal, such as 1935 * <code><em>Foo</em>Listener.class</code>. 1936 * For example, you can query a 1937 * {@code Window} {@code w} 1938 * for its window listeners with the following code: 1939 * 1940 * <pre>WindowListener[] wls = (WindowListener[])(w.getListeners(WindowListener.class));</pre> 1941 * 1942 * If no such listeners exist, this method returns an empty array. 1943 * 1944 * @param listenerType the type of listeners requested; this parameter 1945 * should specify an interface that descends from 1946 * {@code java.util.EventListener} 1947 * @return an array of all objects registered as 1948 * <code><em>Foo</em>Listener</code>s on this window, 1949 * or an empty array if no such 1950 * listeners have been added 1951 * @exception ClassCastException if {@code listenerType} 1952 * doesn't specify a class or interface that implements 1953 * {@code java.util.EventListener} 1954 * @exception NullPointerException if {@code listenerType} is {@code null} 1955 * 1956 * @see #getWindowListeners 1957 * @since 1.3 1958 */ 1959 public <T extends EventListener> T[] getListeners(Class<T> listenerType) { 1960 EventListener l = null; 1961 if (listenerType == WindowFocusListener.class) { 1962 l = windowFocusListener; 1963 } else if (listenerType == WindowStateListener.class) { 1964 l = windowStateListener; 1965 } else if (listenerType == WindowListener.class) { 1966 l = windowListener; 1967 } else { 1968 return super.getListeners(listenerType); 1969 } 1970 return AWTEventMulticaster.getListeners(l, listenerType); 1971 } 1972 1973 // REMIND: remove when filtering is handled at lower level 1974 boolean eventEnabled(AWTEvent e) { 1975 switch(e.id) { 1976 case WindowEvent.WINDOW_OPENED: 1977 case WindowEvent.WINDOW_CLOSING: 1978 case WindowEvent.WINDOW_CLOSED: 1979 case WindowEvent.WINDOW_ICONIFIED: 1980 case WindowEvent.WINDOW_DEICONIFIED: 1981 case WindowEvent.WINDOW_ACTIVATED: 1982 case WindowEvent.WINDOW_DEACTIVATED: 1983 if ((eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0 || 1984 windowListener != null) { 1985 return true; 1986 } 1987 return false; 1988 case WindowEvent.WINDOW_GAINED_FOCUS: 1989 case WindowEvent.WINDOW_LOST_FOCUS: 1990 if ((eventMask & AWTEvent.WINDOW_FOCUS_EVENT_MASK) != 0 || 1991 windowFocusListener != null) { 1992 return true; 1993 } 1994 return false; 1995 case WindowEvent.WINDOW_STATE_CHANGED: 1996 if ((eventMask & AWTEvent.WINDOW_STATE_EVENT_MASK) != 0 || 1997 windowStateListener != null) { 1998 return true; 1999 } 2000 return false; 2001 default: 2002 break; 2003 } 2004 return super.eventEnabled(e); 2005 } 2006 2007 /** 2008 * Processes events on this window. If the event is an 2009 * {@code WindowEvent}, it invokes the 2010 * {@code processWindowEvent} method, else it invokes its 2011 * superclass's {@code processEvent}. 2012 * <p>Note that if the event parameter is {@code null} 2013 * the behavior is unspecified and may result in an 2014 * exception. 2015 * 2016 * @param e the event 2017 */ 2018 protected void processEvent(AWTEvent e) { 2019 if (e instanceof WindowEvent) { 2020 switch (e.getID()) { 2021 case WindowEvent.WINDOW_OPENED: 2022 case WindowEvent.WINDOW_CLOSING: 2023 case WindowEvent.WINDOW_CLOSED: 2024 case WindowEvent.WINDOW_ICONIFIED: 2025 case WindowEvent.WINDOW_DEICONIFIED: 2026 case WindowEvent.WINDOW_ACTIVATED: 2027 case WindowEvent.WINDOW_DEACTIVATED: 2028 processWindowEvent((WindowEvent)e); 2029 break; 2030 case WindowEvent.WINDOW_GAINED_FOCUS: 2031 case WindowEvent.WINDOW_LOST_FOCUS: 2032 processWindowFocusEvent((WindowEvent)e); 2033 break; 2034 case WindowEvent.WINDOW_STATE_CHANGED: 2035 processWindowStateEvent((WindowEvent)e); 2036 break; 2037 } 2038 return; 2039 } 2040 super.processEvent(e); 2041 } 2042 2043 /** 2044 * Processes window events occurring on this window by 2045 * dispatching them to any registered WindowListener objects. 2046 * NOTE: This method will not be called unless window events 2047 * are enabled for this component; this happens when one of the 2048 * following occurs: 2049 * <ul> 2050 * <li>A WindowListener object is registered via 2051 * {@code addWindowListener} 2052 * <li>Window events are enabled via {@code enableEvents} 2053 * </ul> 2054 * <p>Note that if the event parameter is {@code null} 2055 * the behavior is unspecified and may result in an 2056 * exception. 2057 * 2058 * @param e the window event 2059 * @see Component#enableEvents 2060 */ 2061 protected void processWindowEvent(WindowEvent e) { 2062 WindowListener listener = windowListener; 2063 if (listener != null) { 2064 switch(e.getID()) { 2065 case WindowEvent.WINDOW_OPENED: 2066 listener.windowOpened(e); 2067 break; 2068 case WindowEvent.WINDOW_CLOSING: 2069 listener.windowClosing(e); 2070 break; 2071 case WindowEvent.WINDOW_CLOSED: 2072 listener.windowClosed(e); 2073 break; 2074 case WindowEvent.WINDOW_ICONIFIED: 2075 listener.windowIconified(e); 2076 break; 2077 case WindowEvent.WINDOW_DEICONIFIED: 2078 listener.windowDeiconified(e); 2079 break; 2080 case WindowEvent.WINDOW_ACTIVATED: 2081 listener.windowActivated(e); 2082 break; 2083 case WindowEvent.WINDOW_DEACTIVATED: 2084 listener.windowDeactivated(e); 2085 break; 2086 default: 2087 break; 2088 } 2089 } 2090 } 2091 2092 /** 2093 * Processes window focus event occurring on this window by 2094 * dispatching them to any registered WindowFocusListener objects. 2095 * NOTE: this method will not be called unless window focus events 2096 * are enabled for this window. This happens when one of the 2097 * following occurs: 2098 * <ul> 2099 * <li>a WindowFocusListener is registered via 2100 * {@code addWindowFocusListener} 2101 * <li>Window focus events are enabled via {@code enableEvents} 2102 * </ul> 2103 * <p>Note that if the event parameter is {@code null} 2104 * the behavior is unspecified and may result in an 2105 * exception. 2106 * 2107 * @param e the window focus event 2108 * @see Component#enableEvents 2109 * @since 1.4 2110 */ 2111 protected void processWindowFocusEvent(WindowEvent e) { 2112 WindowFocusListener listener = windowFocusListener; 2113 if (listener != null) { 2114 switch (e.getID()) { 2115 case WindowEvent.WINDOW_GAINED_FOCUS: 2116 listener.windowGainedFocus(e); 2117 break; 2118 case WindowEvent.WINDOW_LOST_FOCUS: 2119 listener.windowLostFocus(e); 2120 break; 2121 default: 2122 break; 2123 } 2124 } 2125 } 2126 2127 /** 2128 * Processes window state event occurring on this window by 2129 * dispatching them to any registered {@code WindowStateListener} 2130 * objects. 2131 * NOTE: this method will not be called unless window state events 2132 * are enabled for this window. This happens when one of the 2133 * following occurs: 2134 * <ul> 2135 * <li>a {@code WindowStateListener} is registered via 2136 * {@code addWindowStateListener} 2137 * <li>window state events are enabled via {@code enableEvents} 2138 * </ul> 2139 * <p>Note that if the event parameter is {@code null} 2140 * the behavior is unspecified and may result in an 2141 * exception. 2142 * 2143 * @param e the window state event 2144 * @see java.awt.Component#enableEvents 2145 * @since 1.4 2146 */ 2147 protected void processWindowStateEvent(WindowEvent e) { 2148 WindowStateListener listener = windowStateListener; 2149 if (listener != null) { 2150 switch (e.getID()) { 2151 case WindowEvent.WINDOW_STATE_CHANGED: 2152 listener.windowStateChanged(e); 2153 break; 2154 default: 2155 break; 2156 } 2157 } 2158 } 2159 2160 /** 2161 * Implements a debugging hook -- checks to see if 2162 * the user has typed <i>control-shift-F1</i>. If so, 2163 * the list of child windows is dumped to {@code System.out}. 2164 * @param e the keyboard event 2165 */ 2166 void preProcessKeyEvent(KeyEvent e) { 2167 // Dump the list of child windows to System.out. 2168 if (e.isActionKey() && e.getKeyCode() == KeyEvent.VK_F1 && 2169 e.isControlDown() && e.isShiftDown() && 2170 e.getID() == KeyEvent.KEY_PRESSED) { 2171 list(System.out, 0); 2172 } 2173 } 2174 2175 void postProcessKeyEvent(KeyEvent e) { 2176 // Do nothing 2177 } 2178 2179 2180 /** 2181 * Sets whether this window should always be above other windows. If 2182 * there are multiple always-on-top windows, their relative order is 2183 * unspecified and platform dependent. 2184 * <p> 2185 * If some other window is already always-on-top then the 2186 * relative order between these windows is unspecified (depends on 2187 * platform). No window can be brought to be over the always-on-top 2188 * window except maybe another always-on-top window. 2189 * <p> 2190 * All windows owned by an always-on-top window inherit this state and 2191 * automatically become always-on-top. If a window ceases to be 2192 * always-on-top, the windows that it owns will no longer be 2193 * always-on-top. When an always-on-top window is sent {@link #toBack 2194 * toBack}, its always-on-top state is set to {@code false}. 2195 * 2196 * <p> When this method is called on a window with a value of 2197 * {@code true}, and the window is visible and the platform 2198 * supports always-on-top for this window, the window is immediately 2199 * brought forward, "sticking" it in the top-most position. If the 2200 * window isn`t currently visible, this method sets the always-on-top 2201 * state to {@code true} but does not bring the window forward. 2202 * When the window is later shown, it will be always-on-top. 2203 * 2204 * <p> When this method is called on a window with a value of 2205 * {@code false} the always-on-top state is set to normal. It may also 2206 * cause an unspecified, platform-dependent change in the z-order of 2207 * top-level windows, but other always-on-top windows will remain in 2208 * top-most position. Calling this method with a value of {@code false} 2209 * on a window that has a normal state has no effect. 2210 * 2211 * <p><b>Note</b>: some platforms might not support always-on-top 2212 * windows. To detect if always-on-top windows are supported by the 2213 * current platform, use {@link Toolkit#isAlwaysOnTopSupported()} and 2214 * {@link Window#isAlwaysOnTopSupported()}. If always-on-top mode 2215 * isn't supported for this window or this window's toolkit does not 2216 * support always-on-top windows, calling this method has no effect. 2217 * <p> 2218 * If a SecurityManager is installed, the calling thread must be 2219 * granted the AWTPermission "setWindowAlwaysOnTop" in 2220 * order to set the value of this property. If this 2221 * permission is not granted, this method will throw a 2222 * SecurityException, and the current value of the property will 2223 * be left unchanged. 2224 * 2225 * @param alwaysOnTop true if the window should always be above other 2226 * windows 2227 * @throws SecurityException if the calling thread does not have 2228 * permission to set the value of always-on-top property 2229 * 2230 * @see #isAlwaysOnTop 2231 * @see #toFront 2232 * @see #toBack 2233 * @see AWTPermission 2234 * @see #isAlwaysOnTopSupported 2235 * @see #getToolkit 2236 * @see Toolkit#isAlwaysOnTopSupported 2237 * @since 1.5 2238 */ 2239 public final void setAlwaysOnTop(boolean alwaysOnTop) throws SecurityException { 2240 SecurityManager security = System.getSecurityManager(); 2241 if (security != null) { 2242 security.checkPermission(AWTPermissions.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION); 2243 } 2244 2245 boolean oldAlwaysOnTop; 2246 synchronized(this) { 2247 oldAlwaysOnTop = this.alwaysOnTop; 2248 this.alwaysOnTop = alwaysOnTop; 2249 } 2250 if (oldAlwaysOnTop != alwaysOnTop ) { 2251 if (isAlwaysOnTopSupported()) { 2252 WindowPeer peer = (WindowPeer)this.peer; 2253 synchronized(getTreeLock()) { 2254 if (peer != null) { 2255 peer.updateAlwaysOnTopState(); 2256 } 2257 } 2258 } 2259 firePropertyChange("alwaysOnTop", oldAlwaysOnTop, alwaysOnTop); 2260 } 2261 setOwnedWindowsAlwaysOnTop(alwaysOnTop); 2262 } 2263 2264 @SuppressWarnings({"rawtypes", "unchecked"}) 2265 private void setOwnedWindowsAlwaysOnTop(boolean alwaysOnTop) { 2266 WeakReference<Window>[] ownedWindowArray; 2267 synchronized (ownedWindowList) { 2268 ownedWindowArray = new WeakReference[ownedWindowList.size()]; 2269 ownedWindowList.copyInto(ownedWindowArray); 2270 } 2271 2272 for (WeakReference<Window> ref : ownedWindowArray) { 2273 Window window = ref.get(); 2274 if (window != null) { 2275 try { 2276 window.setAlwaysOnTop(alwaysOnTop); 2277 } catch (SecurityException ignore) { 2278 } 2279 } 2280 } 2281 } 2282 2283 /** 2284 * Returns whether the always-on-top mode is supported for this 2285 * window. Some platforms may not support always-on-top windows, some 2286 * may support only some kinds of top-level windows; for example, 2287 * a platform may not support always-on-top modal dialogs. 2288 * 2289 * @return {@code true}, if the always-on-top mode is supported for 2290 * this window and this window's toolkit supports always-on-top windows, 2291 * {@code false} otherwise 2292 * 2293 * @see #setAlwaysOnTop(boolean) 2294 * @see #getToolkit 2295 * @see Toolkit#isAlwaysOnTopSupported 2296 * @since 1.6 2297 */ 2298 public boolean isAlwaysOnTopSupported() { 2299 return Toolkit.getDefaultToolkit().isAlwaysOnTopSupported(); 2300 } 2301 2302 2303 /** 2304 * Returns whether this window is an always-on-top window. 2305 * @return {@code true}, if the window is in always-on-top state, 2306 * {@code false} otherwise 2307 * @see #setAlwaysOnTop 2308 * @since 1.5 2309 */ 2310 public final boolean isAlwaysOnTop() { 2311 return alwaysOnTop; 2312 } 2313 2314 2315 /** 2316 * Returns the child Component of this Window that has focus if this Window 2317 * is focused; returns null otherwise. 2318 * 2319 * @return the child Component with focus, or null if this Window is not 2320 * focused 2321 * @see #getMostRecentFocusOwner 2322 * @see #isFocused 2323 */ 2324 public Component getFocusOwner() { 2325 return (isFocused()) 2326 ? KeyboardFocusManager.getCurrentKeyboardFocusManager(). 2327 getFocusOwner() 2328 : null; 2329 } 2330 2331 /** 2332 * Returns the child Component of this Window that will receive the focus 2333 * when this Window is focused. If this Window is currently focused, this 2334 * method returns the same Component as {@code getFocusOwner()}. If 2335 * this Window is not focused, then the child Component that most recently 2336 * requested focus will be returned. If no child Component has ever 2337 * requested focus, and this is a focusable Window, then this Window's 2338 * initial focusable Component is returned. If no child Component has ever 2339 * requested focus, and this is a non-focusable Window, null is returned. 2340 * 2341 * @return the child Component that will receive focus when this Window is 2342 * focused 2343 * @see #getFocusOwner 2344 * @see #isFocused 2345 * @see #isFocusableWindow 2346 * @since 1.4 2347 */ 2348 public Component getMostRecentFocusOwner() { 2349 if (isFocused()) { 2350 return getFocusOwner(); 2351 } else { 2352 Component mostRecent = 2353 KeyboardFocusManager.getMostRecentFocusOwner(this); 2354 if (mostRecent != null) { 2355 return mostRecent; 2356 } else { 2357 return (isFocusableWindow()) 2358 ? getFocusTraversalPolicy().getInitialComponent(this) 2359 : null; 2360 } 2361 } 2362 } 2363 2364 /** 2365 * Returns whether this Window is active. Only a Frame or a Dialog may be 2366 * active. The native windowing system may denote the active Window or its 2367 * children with special decorations, such as a highlighted title bar. The 2368 * active Window is always either the focused Window, or the first Frame or 2369 * Dialog that is an owner of the focused Window. 2370 * 2371 * @return whether this is the active Window. 2372 * @see #isFocused 2373 * @since 1.4 2374 */ 2375 public boolean isActive() { 2376 return (KeyboardFocusManager.getCurrentKeyboardFocusManager(). 2377 getActiveWindow() == this); 2378 } 2379 2380 /** 2381 * Returns whether this Window is focused. If there exists a focus owner, 2382 * the focused Window is the Window that is, or contains, that focus owner. 2383 * If there is no focus owner, then no Window is focused. 2384 * <p> 2385 * If the focused Window is a Frame or a Dialog it is also the active 2386 * Window. Otherwise, the active Window is the first Frame or Dialog that 2387 * is an owner of the focused Window. 2388 * 2389 * @return whether this is the focused Window. 2390 * @see #isActive 2391 * @since 1.4 2392 */ 2393 public boolean isFocused() { 2394 return (KeyboardFocusManager.getCurrentKeyboardFocusManager(). 2395 getGlobalFocusedWindow() == this); 2396 } 2397 2398 /** 2399 * Gets a focus traversal key for this Window. (See {@code 2400 * setFocusTraversalKeys} for a full description of each key.) 2401 * <p> 2402 * If the traversal key has not been explicitly set for this Window, 2403 * then this Window's parent's traversal key is returned. If the 2404 * traversal key has not been explicitly set for any of this Window's 2405 * ancestors, then the current KeyboardFocusManager's default traversal key 2406 * is returned. 2407 * 2408 * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, 2409 * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, 2410 * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or 2411 * KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS 2412 * @return the AWTKeyStroke for the specified key 2413 * @see Container#setFocusTraversalKeys 2414 * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS 2415 * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS 2416 * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS 2417 * @see KeyboardFocusManager#DOWN_CYCLE_TRAVERSAL_KEYS 2418 * @throws IllegalArgumentException if id is not one of 2419 * KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, 2420 * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, 2421 * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or 2422 * KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS 2423 * @since 1.4 2424 */ 2425 @SuppressWarnings("unchecked") 2426 public Set<AWTKeyStroke> getFocusTraversalKeys(int id) { 2427 if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) { 2428 throw new IllegalArgumentException("invalid focus traversal key identifier"); 2429 } 2430 2431 // Okay to return Set directly because it is an unmodifiable view 2432 @SuppressWarnings("rawtypes") 2433 Set keystrokes = (focusTraversalKeys != null) 2434 ? focusTraversalKeys[id] 2435 : null; 2436 2437 if (keystrokes != null) { 2438 return keystrokes; 2439 } else { 2440 return KeyboardFocusManager.getCurrentKeyboardFocusManager(). 2441 getDefaultFocusTraversalKeys(id); 2442 } 2443 } 2444 2445 /** 2446 * Does nothing because Windows must always be roots of a focus traversal 2447 * cycle. The passed-in value is ignored. 2448 * 2449 * @param focusCycleRoot this value is ignored 2450 * @see #isFocusCycleRoot 2451 * @see Container#setFocusTraversalPolicy 2452 * @see Container#getFocusTraversalPolicy 2453 * @since 1.4 2454 */ 2455 public final void setFocusCycleRoot(boolean focusCycleRoot) { 2456 } 2457 2458 /** 2459 * Always returns {@code true} because all Windows must be roots of a 2460 * focus traversal cycle. 2461 * 2462 * @return {@code true} 2463 * @see #setFocusCycleRoot 2464 * @see Container#setFocusTraversalPolicy 2465 * @see Container#getFocusTraversalPolicy 2466 * @since 1.4 2467 */ 2468 public final boolean isFocusCycleRoot() { 2469 return true; 2470 } 2471 2472 /** 2473 * Always returns {@code null} because Windows have no ancestors; they 2474 * represent the top of the Component hierarchy. 2475 * 2476 * @return {@code null} 2477 * @see Container#isFocusCycleRoot() 2478 * @since 1.4 2479 */ 2480 public final Container getFocusCycleRootAncestor() { 2481 return null; 2482 } 2483 2484 /** 2485 * Returns whether this Window can become the focused Window, that is, 2486 * whether this Window or any of its subcomponents can become the focus 2487 * owner. For a Frame or Dialog to be focusable, its focusable Window state 2488 * must be set to {@code true}. For a Window which is not a Frame or 2489 * Dialog to be focusable, its focusable Window state must be set to 2490 * {@code true}, its nearest owning Frame or Dialog must be 2491 * showing on the screen, and it must contain at least one Component in 2492 * its focus traversal cycle. If any of these conditions is not met, then 2493 * neither this Window nor any of its subcomponents can become the focus 2494 * owner. 2495 * 2496 * @return {@code true} if this Window can be the focused Window; 2497 * {@code false} otherwise 2498 * @see #getFocusableWindowState 2499 * @see #setFocusableWindowState 2500 * @see #isShowing 2501 * @see Component#isFocusable 2502 * @since 1.4 2503 */ 2504 public final boolean isFocusableWindow() { 2505 // If a Window/Frame/Dialog was made non-focusable, then it is always 2506 // non-focusable. 2507 if (!getFocusableWindowState()) { 2508 return false; 2509 } 2510 2511 // All other tests apply only to Windows. 2512 if (this instanceof Frame || this instanceof Dialog) { 2513 return true; 2514 } 2515 2516 // A Window must have at least one Component in its root focus 2517 // traversal cycle to be focusable. 2518 if (getFocusTraversalPolicy().getDefaultComponent(this) == null) { 2519 return false; 2520 } 2521 2522 // A Window's nearest owning Frame or Dialog must be showing on the 2523 // screen. 2524 for (Window owner = getOwner(); owner != null; 2525 owner = owner.getOwner()) 2526 { 2527 if (owner instanceof Frame || owner instanceof Dialog) { 2528 return owner.isShowing(); 2529 } 2530 } 2531 2532 return false; 2533 } 2534 2535 /** 2536 * Returns whether this Window can become the focused Window if it meets 2537 * the other requirements outlined in {@code isFocusableWindow}. If 2538 * this method returns {@code false}, then 2539 * {@code isFocusableWindow} will return {@code false} as well. 2540 * If this method returns {@code true}, then 2541 * {@code isFocusableWindow} may return {@code true} or 2542 * {@code false} depending upon the other requirements which must be 2543 * met in order for a Window to be focusable. 2544 * <p> 2545 * By default, all Windows have a focusable Window state of 2546 * {@code true}. 2547 * 2548 * @return whether this Window can be the focused Window 2549 * @see #isFocusableWindow 2550 * @see #setFocusableWindowState 2551 * @see #isShowing 2552 * @see Component#setFocusable 2553 * @since 1.4 2554 */ 2555 public boolean getFocusableWindowState() { 2556 return focusableWindowState; 2557 } 2558 2559 /** 2560 * Sets whether this Window can become the focused Window if it meets 2561 * the other requirements outlined in {@code isFocusableWindow}. If 2562 * this Window's focusable Window state is set to {@code false}, then 2563 * {@code isFocusableWindow} will return {@code false}. If this 2564 * Window's focusable Window state is set to {@code true}, then 2565 * {@code isFocusableWindow} may return {@code true} or 2566 * {@code false} depending upon the other requirements which must be 2567 * met in order for a Window to be focusable. 2568 * <p> 2569 * Setting a Window's focusability state to {@code false} is the 2570 * standard mechanism for an application to identify to the AWT a Window 2571 * which will be used as a floating palette or toolbar, and thus should be 2572 * a non-focusable Window. 2573 * 2574 * Setting the focusability state on a visible {@code Window} 2575 * can have a delayed effect on some platforms — the actual 2576 * change may happen only when the {@code Window} becomes 2577 * hidden and then visible again. To ensure consistent behavior 2578 * across platforms, set the {@code Window}'s focusable state 2579 * when the {@code Window} is invisible and then show it. 2580 * 2581 * @param focusableWindowState whether this Window can be the focused 2582 * Window 2583 * @see #isFocusableWindow 2584 * @see #getFocusableWindowState 2585 * @see #isShowing 2586 * @see Component#setFocusable 2587 * @since 1.4 2588 */ 2589 public void setFocusableWindowState(boolean focusableWindowState) { 2590 boolean oldFocusableWindowState; 2591 synchronized (this) { 2592 oldFocusableWindowState = this.focusableWindowState; 2593 this.focusableWindowState = focusableWindowState; 2594 } 2595 WindowPeer peer = (WindowPeer)this.peer; 2596 if (peer != null) { 2597 peer.updateFocusableWindowState(); 2598 } 2599 firePropertyChange("focusableWindowState", oldFocusableWindowState, 2600 focusableWindowState); 2601 if (oldFocusableWindowState && !focusableWindowState && isFocused()) { 2602 for (Window owner = getOwner(); 2603 owner != null; 2604 owner = owner.getOwner()) 2605 { 2606 Component toFocus = 2607 KeyboardFocusManager.getMostRecentFocusOwner(owner); 2608 if (toFocus != null && toFocus.requestFocus(false, CausedFocusEvent.Cause.ACTIVATION)) { 2609 return; 2610 } 2611 } 2612 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 2613 clearGlobalFocusOwnerPriv(); 2614 } 2615 } 2616 2617 /** 2618 * Sets whether this window should receive focus on 2619 * subsequently being shown (with a call to {@link #setVisible setVisible(true)}), 2620 * or being moved to the front (with a call to {@link #toFront}). 2621 * <p> 2622 * Note that {@link #setVisible setVisible(true)} may be called indirectly 2623 * (e.g. when showing an owner of the window makes the window to be shown). 2624 * {@link #toFront} may also be called indirectly (e.g. when 2625 * {@link #setVisible setVisible(true)} is called on already visible window). 2626 * In all such cases this property takes effect as well. 2627 * <p> 2628 * The value of the property is not inherited by owned windows. 2629 * 2630 * @param autoRequestFocus whether this window should be focused on 2631 * subsequently being shown or being moved to the front 2632 * @see #isAutoRequestFocus 2633 * @see #isFocusableWindow 2634 * @see #setVisible 2635 * @see #toFront 2636 * @since 1.7 2637 */ 2638 public void setAutoRequestFocus(boolean autoRequestFocus) { 2639 this.autoRequestFocus = autoRequestFocus; 2640 } 2641 2642 /** 2643 * Returns whether this window should receive focus on subsequently being shown 2644 * (with a call to {@link #setVisible setVisible(true)}), or being moved to the front 2645 * (with a call to {@link #toFront}). 2646 * <p> 2647 * By default, the window has {@code autoRequestFocus} value of {@code true}. 2648 * 2649 * @return {@code autoRequestFocus} value 2650 * @see #setAutoRequestFocus 2651 * @since 1.7 2652 */ 2653 public boolean isAutoRequestFocus() { 2654 return autoRequestFocus; 2655 } 2656 2657 /** 2658 * Adds a PropertyChangeListener to the listener list. The listener is 2659 * registered for all bound properties of this class, including the 2660 * following: 2661 * <ul> 2662 * <li>this Window's font ("font")</li> 2663 * <li>this Window's background color ("background")</li> 2664 * <li>this Window's foreground color ("foreground")</li> 2665 * <li>this Window's focusability ("focusable")</li> 2666 * <li>this Window's focus traversal keys enabled state 2667 * ("focusTraversalKeysEnabled")</li> 2668 * <li>this Window's Set of FORWARD_TRAVERSAL_KEYS 2669 * ("forwardFocusTraversalKeys")</li> 2670 * <li>this Window's Set of BACKWARD_TRAVERSAL_KEYS 2671 * ("backwardFocusTraversalKeys")</li> 2672 * <li>this Window's Set of UP_CYCLE_TRAVERSAL_KEYS 2673 * ("upCycleFocusTraversalKeys")</li> 2674 * <li>this Window's Set of DOWN_CYCLE_TRAVERSAL_KEYS 2675 * ("downCycleFocusTraversalKeys")</li> 2676 * <li>this Window's focus traversal policy ("focusTraversalPolicy") 2677 * </li> 2678 * <li>this Window's focusable Window state ("focusableWindowState") 2679 * </li> 2680 * <li>this Window's always-on-top state("alwaysOnTop")</li> 2681 * </ul> 2682 * Note that if this Window is inheriting a bound property, then no 2683 * event will be fired in response to a change in the inherited property. 2684 * <p> 2685 * If listener is null, no exception is thrown and no action is performed. 2686 * 2687 * @param listener the PropertyChangeListener to be added 2688 * 2689 * @see Component#removePropertyChangeListener 2690 * @see #addPropertyChangeListener(java.lang.String,java.beans.PropertyChangeListener) 2691 */ 2692 public void addPropertyChangeListener(PropertyChangeListener listener) { 2693 super.addPropertyChangeListener(listener); 2694 } 2695 2696 /** 2697 * Adds a PropertyChangeListener to the listener list for a specific 2698 * property. The specified property may be user-defined, or one of the 2699 * following: 2700 * <ul> 2701 * <li>this Window's font ("font")</li> 2702 * <li>this Window's background color ("background")</li> 2703 * <li>this Window's foreground color ("foreground")</li> 2704 * <li>this Window's focusability ("focusable")</li> 2705 * <li>this Window's focus traversal keys enabled state 2706 * ("focusTraversalKeysEnabled")</li> 2707 * <li>this Window's Set of FORWARD_TRAVERSAL_KEYS 2708 * ("forwardFocusTraversalKeys")</li> 2709 * <li>this Window's Set of BACKWARD_TRAVERSAL_KEYS 2710 * ("backwardFocusTraversalKeys")</li> 2711 * <li>this Window's Set of UP_CYCLE_TRAVERSAL_KEYS 2712 * ("upCycleFocusTraversalKeys")</li> 2713 * <li>this Window's Set of DOWN_CYCLE_TRAVERSAL_KEYS 2714 * ("downCycleFocusTraversalKeys")</li> 2715 * <li>this Window's focus traversal policy ("focusTraversalPolicy") 2716 * </li> 2717 * <li>this Window's focusable Window state ("focusableWindowState") 2718 * </li> 2719 * <li>this Window's always-on-top state("alwaysOnTop")</li> 2720 * </ul> 2721 * Note that if this Window is inheriting a bound property, then no 2722 * event will be fired in response to a change in the inherited property. 2723 * <p> 2724 * If listener is null, no exception is thrown and no action is performed. 2725 * 2726 * @param propertyName one of the property names listed above 2727 * @param listener the PropertyChangeListener to be added 2728 * 2729 * @see #addPropertyChangeListener(java.beans.PropertyChangeListener) 2730 * @see Component#removePropertyChangeListener 2731 */ 2732 public void addPropertyChangeListener(String propertyName, 2733 PropertyChangeListener listener) { 2734 super.addPropertyChangeListener(propertyName, listener); 2735 } 2736 2737 /** 2738 * Indicates if this container is a validate root. 2739 * <p> 2740 * {@code Window} objects are the validate roots, and, therefore, they 2741 * override this method to return {@code true}. 2742 * 2743 * @return {@code true} 2744 * @since 1.7 2745 * @see java.awt.Container#isValidateRoot 2746 */ 2747 @Override 2748 public boolean isValidateRoot() { 2749 return true; 2750 } 2751 2752 /** 2753 * Dispatches an event to this window or one of its sub components. 2754 * @param e the event 2755 */ 2756 void dispatchEventImpl(AWTEvent e) { 2757 if (e.getID() == ComponentEvent.COMPONENT_RESIZED) { 2758 invalidate(); 2759 validate(); 2760 } 2761 super.dispatchEventImpl(e); 2762 } 2763 2764 /** 2765 * @deprecated As of JDK version 1.1 2766 * replaced by {@code dispatchEvent(AWTEvent)}. 2767 */ 2768 @Deprecated 2769 public boolean postEvent(Event e) { 2770 if (handleEvent(e)) { 2771 e.consume(); 2772 return true; 2773 } 2774 return false; 2775 } 2776 2777 /** 2778 * Checks if this Window is showing on screen. 2779 * @see Component#setVisible 2780 */ 2781 public boolean isShowing() { 2782 return visible; 2783 } 2784 2785 boolean isDisposing() { 2786 return disposing; 2787 } 2788 2789 /** 2790 * @deprecated As of J2SE 1.4, replaced by 2791 * {@link Component#applyComponentOrientation Component.applyComponentOrientation}. 2792 */ 2793 @Deprecated 2794 public void applyResourceBundle(ResourceBundle rb) { 2795 applyComponentOrientation(ComponentOrientation.getOrientation(rb)); 2796 } 2797 2798 /** 2799 * @deprecated As of J2SE 1.4, replaced by 2800 * {@link Component#applyComponentOrientation Component.applyComponentOrientation}. 2801 */ 2802 @Deprecated 2803 public void applyResourceBundle(String rbName) { 2804 applyResourceBundle(ResourceBundle.getBundle(rbName)); 2805 } 2806 2807 /* 2808 * Support for tracking all windows owned by this window 2809 */ 2810 void addOwnedWindow(WeakReference<Window> weakWindow) { 2811 if (weakWindow != null) { 2812 synchronized(ownedWindowList) { 2813 // this if statement should really be an assert, but we don't 2814 // have asserts... 2815 if (!ownedWindowList.contains(weakWindow)) { 2816 ownedWindowList.addElement(weakWindow); 2817 } 2818 } 2819 } 2820 } 2821 2822 void removeOwnedWindow(WeakReference<Window> weakWindow) { 2823 if (weakWindow != null) { 2824 // synchronized block not required since removeElement is 2825 // already synchronized 2826 ownedWindowList.removeElement(weakWindow); 2827 } 2828 } 2829 2830 void connectOwnedWindow(Window child) { 2831 child.parent = this; 2832 addOwnedWindow(child.weakThis); 2833 child.disposerRecord.updateOwner(); 2834 } 2835 2836 private void addToWindowList() { 2837 synchronized (Window.class) { 2838 @SuppressWarnings("unchecked") 2839 Vector<WeakReference<Window>> windowList = (Vector<WeakReference<Window>>)appContext.get(Window.class); 2840 if (windowList == null) { 2841 windowList = new Vector<WeakReference<Window>>(); 2842 appContext.put(Window.class, windowList); 2843 } 2844 windowList.add(weakThis); 2845 } 2846 } 2847 2848 private static void removeFromWindowList(AppContext context, WeakReference<Window> weakThis) { 2849 synchronized (Window.class) { 2850 @SuppressWarnings("unchecked") 2851 Vector<WeakReference<Window>> windowList = (Vector<WeakReference<Window>>)context.get(Window.class); 2852 if (windowList != null) { 2853 windowList.remove(weakThis); 2854 } 2855 } 2856 } 2857 2858 private void removeFromWindowList() { 2859 removeFromWindowList(appContext, weakThis); 2860 } 2861 2862 /** 2863 * Window type. 2864 * 2865 * Synchronization: ObjectLock 2866 */ 2867 private Type type = Type.NORMAL; 2868 2869 /** 2870 * Sets the type of the window. 2871 * 2872 * This method can only be called while the window is not displayable. 2873 * 2874 * @param type the window type 2875 * @throws IllegalComponentStateException if the window 2876 * is displayable. 2877 * @throws IllegalArgumentException if the type is {@code null} 2878 * @see Component#isDisplayable 2879 * @see #getType 2880 * @since 1.7 2881 */ 2882 public void setType(Type type) { 2883 if (type == null) { 2884 throw new IllegalArgumentException("type should not be null."); 2885 } 2886 synchronized (getTreeLock()) { 2887 if (isDisplayable()) { 2888 throw new IllegalComponentStateException( 2889 "The window is displayable."); 2890 } 2891 synchronized (getObjectLock()) { 2892 this.type = type; 2893 } 2894 } 2895 } 2896 2897 /** 2898 * Returns the type of the window. 2899 * 2900 * @return the type of the window 2901 * @see #setType 2902 * @since 1.7 2903 */ 2904 public Type getType() { 2905 synchronized (getObjectLock()) { 2906 return type; 2907 } 2908 } 2909 2910 /** 2911 * The window serialized data version. 2912 * 2913 * @serial 2914 */ 2915 private int windowSerializedDataVersion = 2; 2916 2917 /** 2918 * Writes default serializable fields to stream. Writes 2919 * a list of serializable {@code WindowListener}s and 2920 * {@code WindowFocusListener}s as optional data. 2921 * Writes a list of child windows as optional data. 2922 * Writes a list of icon images as optional data 2923 * 2924 * @param s the {@code ObjectOutputStream} to write 2925 * @serialData {@code null} terminated sequence of 2926 * 0 or more pairs; the pair consists of a {@code String} 2927 * and {@code Object}; the {@code String} 2928 * indicates the type of object and is one of the following: 2929 * {@code windowListenerK} indicating a 2930 * {@code WindowListener} object; 2931 * {@code windowFocusWindowK} indicating a 2932 * {@code WindowFocusListener} object; 2933 * {@code ownedWindowK} indicating a child 2934 * {@code Window} object 2935 * 2936 * @see AWTEventMulticaster#save(java.io.ObjectOutputStream, java.lang.String, java.util.EventListener) 2937 * @see Component#windowListenerK 2938 * @see Component#windowFocusListenerK 2939 * @see Component#ownedWindowK 2940 * @see #readObject(ObjectInputStream) 2941 */ 2942 private void writeObject(ObjectOutputStream s) throws IOException { 2943 synchronized (this) { 2944 // Update old focusMgr fields so that our object stream can be read 2945 // by previous releases 2946 focusMgr = new FocusManager(); 2947 focusMgr.focusRoot = this; 2948 focusMgr.focusOwner = getMostRecentFocusOwner(); 2949 2950 s.defaultWriteObject(); 2951 2952 // Clear fields so that we don't keep extra references around 2953 focusMgr = null; 2954 2955 AWTEventMulticaster.save(s, windowListenerK, windowListener); 2956 AWTEventMulticaster.save(s, windowFocusListenerK, windowFocusListener); 2957 AWTEventMulticaster.save(s, windowStateListenerK, windowStateListener); 2958 } 2959 2960 s.writeObject(null); 2961 2962 synchronized (ownedWindowList) { 2963 for (int i = 0; i < ownedWindowList.size(); i++) { 2964 Window child = ownedWindowList.elementAt(i).get(); 2965 if (child != null) { 2966 s.writeObject(ownedWindowK); 2967 s.writeObject(child); 2968 } 2969 } 2970 } 2971 s.writeObject(null); 2972 2973 //write icon array 2974 if (icons != null) { 2975 for (Image i : icons) { 2976 if (i instanceof Serializable) { 2977 s.writeObject(i); 2978 } 2979 } 2980 } 2981 s.writeObject(null); 2982 } 2983 2984 // 2985 // Part of deserialization procedure to be called before 2986 // user's code. 2987 // 2988 private void initDeserializedWindow() { 2989 setWarningString(); 2990 inputContextLock = new Object(); 2991 2992 // Deserialized Windows are not yet visible. 2993 visible = false; 2994 2995 weakThis = new WeakReference<>(this); 2996 2997 anchor = new Object(); 2998 disposerRecord = new WindowDisposerRecord(appContext, this); 2999 sun.java2d.Disposer.addRecord(anchor, disposerRecord); 3000 3001 addToWindowList(); 3002 initGC(null); 3003 ownedWindowList = new Vector<>(); 3004 } 3005 3006 private void deserializeResources(ObjectInputStream s) 3007 throws ClassNotFoundException, IOException, HeadlessException { 3008 3009 if (windowSerializedDataVersion < 2) { 3010 // Translate old-style focus tracking to new model. For 1.4 and 3011 // later releases, we'll rely on the Window's initial focusable 3012 // Component. 3013 if (focusMgr != null) { 3014 if (focusMgr.focusOwner != null) { 3015 KeyboardFocusManager. 3016 setMostRecentFocusOwner(this, focusMgr.focusOwner); 3017 } 3018 } 3019 3020 // This field is non-transient and relies on default serialization. 3021 // However, the default value is insufficient, so we need to set 3022 // it explicitly for object data streams prior to 1.4. 3023 focusableWindowState = true; 3024 3025 3026 } 3027 3028 Object keyOrNull; 3029 while(null != (keyOrNull = s.readObject())) { 3030 String key = ((String)keyOrNull).intern(); 3031 3032 if (windowListenerK == key) { 3033 addWindowListener((WindowListener)(s.readObject())); 3034 } else if (windowFocusListenerK == key) { 3035 addWindowFocusListener((WindowFocusListener)(s.readObject())); 3036 } else if (windowStateListenerK == key) { 3037 addWindowStateListener((WindowStateListener)(s.readObject())); 3038 } else // skip value for unrecognized key 3039 s.readObject(); 3040 } 3041 3042 try { 3043 while (null != (keyOrNull = s.readObject())) { 3044 String key = ((String)keyOrNull).intern(); 3045 3046 if (ownedWindowK == key) 3047 connectOwnedWindow((Window) s.readObject()); 3048 3049 else // skip value for unrecognized key 3050 s.readObject(); 3051 } 3052 3053 //read icons 3054 Object obj = s.readObject(); //Throws OptionalDataException 3055 //for pre1.6 objects. 3056 icons = new ArrayList<Image>(); //Frame.readObject() assumes 3057 //pre1.6 version if icons is null. 3058 while (obj != null) { 3059 if (obj instanceof Image) { 3060 icons.add((Image)obj); 3061 } 3062 obj = s.readObject(); 3063 } 3064 } 3065 catch (OptionalDataException e) { 3066 // 1.1 serialized form 3067 // ownedWindowList will be updated by Frame.readObject 3068 } 3069 3070 } 3071 3072 /** 3073 * Reads the {@code ObjectInputStream} and an optional 3074 * list of listeners to receive various events fired by 3075 * the component; also reads a list of 3076 * (possibly {@code null}) child windows. 3077 * Unrecognized keys or values will be ignored. 3078 * 3079 * @param s the {@code ObjectInputStream} to read 3080 * @exception HeadlessException if 3081 * {@code GraphicsEnvironment.isHeadless} returns 3082 * {@code true} 3083 * @see java.awt.GraphicsEnvironment#isHeadless 3084 * @see #writeObject 3085 */ 3086 private void readObject(ObjectInputStream s) 3087 throws ClassNotFoundException, IOException, HeadlessException 3088 { 3089 GraphicsEnvironment.checkHeadless(); 3090 initDeserializedWindow(); 3091 ObjectInputStream.GetField f = s.readFields(); 3092 3093 syncLWRequests = f.get("syncLWRequests", systemSyncLWRequests); 3094 state = f.get("state", 0); 3095 focusableWindowState = f.get("focusableWindowState", true); 3096 windowSerializedDataVersion = f.get("windowSerializedDataVersion", 1); 3097 locationByPlatform = f.get("locationByPlatform", locationByPlatformProp); 3098 // Note: 1.4 (or later) doesn't use focusMgr 3099 focusMgr = (FocusManager)f.get("focusMgr", null); 3100 Dialog.ModalExclusionType et = (Dialog.ModalExclusionType) 3101 f.get("modalExclusionType", Dialog.ModalExclusionType.NO_EXCLUDE); 3102 setModalExclusionType(et); // since 6.0 3103 boolean aot = f.get("alwaysOnTop", false); 3104 if(aot) { 3105 setAlwaysOnTop(aot); // since 1.5; subject to permission check 3106 } 3107 shape = (Shape)f.get("shape", null); 3108 opacity = (Float)f.get("opacity", 1.0f); 3109 3110 this.securityWarningWidth = 0; 3111 this.securityWarningHeight = 0; 3112 this.securityWarningPointX = 2.0; 3113 this.securityWarningPointY = 0.0; 3114 this.securityWarningAlignmentX = RIGHT_ALIGNMENT; 3115 this.securityWarningAlignmentY = TOP_ALIGNMENT; 3116 3117 deserializeResources(s); 3118 } 3119 3120 /* 3121 * --- Accessibility Support --- 3122 * 3123 */ 3124 3125 /** 3126 * Gets the AccessibleContext associated with this Window. 3127 * For windows, the AccessibleContext takes the form of an 3128 * AccessibleAWTWindow. 3129 * A new AccessibleAWTWindow instance is created if necessary. 3130 * 3131 * @return an AccessibleAWTWindow that serves as the 3132 * AccessibleContext of this Window 3133 * @since 1.3 3134 */ 3135 public AccessibleContext getAccessibleContext() { 3136 if (accessibleContext == null) { 3137 accessibleContext = new AccessibleAWTWindow(); 3138 } 3139 return accessibleContext; 3140 } 3141 3142 /** 3143 * This class implements accessibility support for the 3144 * {@code Window} class. It provides an implementation of the 3145 * Java Accessibility API appropriate to window user-interface elements. 3146 * @since 1.3 3147 */ 3148 protected class AccessibleAWTWindow extends AccessibleAWTContainer 3149 { 3150 /* 3151 * JDK 1.3 serialVersionUID 3152 */ 3153 private static final long serialVersionUID = 4215068635060671780L; 3154 3155 /** 3156 * Get the role of this object. 3157 * 3158 * @return an instance of AccessibleRole describing the role of the 3159 * object 3160 * @see javax.accessibility.AccessibleRole 3161 */ 3162 public AccessibleRole getAccessibleRole() { 3163 return AccessibleRole.WINDOW; 3164 } 3165 3166 /** 3167 * Get the state of this object. 3168 * 3169 * @return an instance of AccessibleStateSet containing the current 3170 * state set of the object 3171 * @see javax.accessibility.AccessibleState 3172 */ 3173 public AccessibleStateSet getAccessibleStateSet() { 3174 AccessibleStateSet states = super.getAccessibleStateSet(); 3175 if (getFocusOwner() != null) { 3176 states.add(AccessibleState.ACTIVE); 3177 } 3178 return states; 3179 } 3180 3181 } // inner class AccessibleAWTWindow 3182 3183 @Override 3184 void setGraphicsConfiguration(GraphicsConfiguration gc) { 3185 if (gc == null) { 3186 gc = GraphicsEnvironment. 3187 getLocalGraphicsEnvironment(). 3188 getDefaultScreenDevice(). 3189 getDefaultConfiguration(); 3190 } 3191 synchronized (getTreeLock()) { 3192 super.setGraphicsConfiguration(gc); 3193 if (log.isLoggable(PlatformLogger.Level.FINER)) { 3194 log.finer("+ Window.setGraphicsConfiguration(): new GC is \n+ " + getGraphicsConfiguration_NoClientCode() + "\n+ this is " + this); 3195 } 3196 } 3197 } 3198 3199 /** 3200 * Sets the location of the window relative to the specified 3201 * component according to the following scenarios. 3202 * <p> 3203 * The target screen mentioned below is a screen to which 3204 * the window should be placed after the setLocationRelativeTo 3205 * method is called. 3206 * <ul> 3207 * <li>If the component is {@code null}, or the {@code 3208 * GraphicsConfiguration} associated with this component is 3209 * {@code null}, the window is placed in the center of the 3210 * screen. The center point can be obtained with the {@link 3211 * GraphicsEnvironment#getCenterPoint 3212 * GraphicsEnvironment.getCenterPoint} method. 3213 * <li>If the component is not {@code null}, but it is not 3214 * currently showing, the window is placed in the center of 3215 * the target screen defined by the {@code 3216 * GraphicsConfiguration} associated with this component. 3217 * <li>If the component is not {@code null} and is shown on 3218 * the screen, then the window is located in such a way that 3219 * the center of the window coincides with the center of the 3220 * component. 3221 * </ul> 3222 * <p> 3223 * If the screens configuration does not allow the window to 3224 * be moved from one screen to another, then the window is 3225 * only placed at the location determined according to the 3226 * above conditions and its {@code GraphicsConfiguration} is 3227 * not changed. 3228 * <p> 3229 * <b>Note</b>: If the lower edge of the window is out of the screen, 3230 * then the window is placed to the side of the {@code Component} 3231 * that is closest to the center of the screen. So if the 3232 * component is on the right part of the screen, the window 3233 * is placed to its left, and vice versa. 3234 * <p> 3235 * If after the window location has been calculated, the upper, 3236 * left, or right edge of the window is out of the screen, 3237 * then the window is located in such a way that the upper, 3238 * left, or right edge of the window coincides with the 3239 * corresponding edge of the screen. If both left and right 3240 * edges of the window are out of the screen, the window is 3241 * placed at the left side of the screen. The similar placement 3242 * will occur if both top and bottom edges are out of the screen. 3243 * In that case, the window is placed at the top side of the screen. 3244 * <p> 3245 * The method changes the geometry-related data. Therefore, 3246 * the native windowing system may ignore such requests, or it may modify 3247 * the requested data, so that the {@code Window} object is placed and sized 3248 * in a way that corresponds closely to the desktop settings. 3249 * 3250 * @param c the component in relation to which the window's location 3251 * is determined 3252 * @see java.awt.GraphicsEnvironment#getCenterPoint 3253 * @since 1.4 3254 */ 3255 public void setLocationRelativeTo(Component c) { 3256 // target location 3257 int dx = 0, dy = 0; 3258 // target GC 3259 GraphicsConfiguration gc = getGraphicsConfiguration_NoClientCode(); 3260 Rectangle gcBounds = gc.getBounds(); 3261 3262 Dimension windowSize = getSize(); 3263 3264 // search a top-level of c 3265 Window componentWindow = SunToolkit.getContainingWindow(c); 3266 if ((c == null) || (componentWindow == null)) { 3267 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 3268 gc = ge.getDefaultScreenDevice().getDefaultConfiguration(); 3269 gcBounds = gc.getBounds(); 3270 Point centerPoint = ge.getCenterPoint(); 3271 dx = centerPoint.x - windowSize.width / 2; 3272 dy = centerPoint.y - windowSize.height / 2; 3273 } else if (!c.isShowing()) { 3274 gc = componentWindow.getGraphicsConfiguration(); 3275 gcBounds = gc.getBounds(); 3276 dx = gcBounds.x + (gcBounds.width - windowSize.width) / 2; 3277 dy = gcBounds.y + (gcBounds.height - windowSize.height) / 2; 3278 } else { 3279 gc = componentWindow.getGraphicsConfiguration(); 3280 gcBounds = gc.getBounds(); 3281 Dimension compSize = c.getSize(); 3282 Point compLocation = c.getLocationOnScreen(); 3283 dx = compLocation.x + ((compSize.width - windowSize.width) / 2); 3284 dy = compLocation.y + ((compSize.height - windowSize.height) / 2); 3285 3286 // Adjust for bottom edge being offscreen 3287 if (dy + windowSize.height > gcBounds.y + gcBounds.height) { 3288 dy = gcBounds.y + gcBounds.height - windowSize.height; 3289 if (compLocation.x - gcBounds.x + compSize.width / 2 < gcBounds.width / 2) { 3290 dx = compLocation.x + compSize.width; 3291 } else { 3292 dx = compLocation.x - windowSize.width; 3293 } 3294 } 3295 } 3296 3297 // Avoid being placed off the edge of the screen: 3298 // bottom 3299 if (dy + windowSize.height > gcBounds.y + gcBounds.height) { 3300 dy = gcBounds.y + gcBounds.height - windowSize.height; 3301 } 3302 // top 3303 if (dy < gcBounds.y) { 3304 dy = gcBounds.y; 3305 } 3306 // right 3307 if (dx + windowSize.width > gcBounds.x + gcBounds.width) { 3308 dx = gcBounds.x + gcBounds.width - windowSize.width; 3309 } 3310 // left 3311 if (dx < gcBounds.x) { 3312 dx = gcBounds.x; 3313 } 3314 3315 setLocation(dx, dy); 3316 } 3317 3318 /** 3319 * Overridden from Component. Top-level Windows should not propagate a 3320 * MouseWheelEvent beyond themselves into their owning Windows. 3321 */ 3322 void deliverMouseWheelToAncestor(MouseWheelEvent e) {} 3323 3324 /** 3325 * Overridden from Component. Top-level Windows don't dispatch to ancestors 3326 */ 3327 boolean dispatchMouseWheelToAncestor(MouseWheelEvent e) {return false;} 3328 3329 /** 3330 * Creates a new strategy for multi-buffering on this component. 3331 * Multi-buffering is useful for rendering performance. This method 3332 * attempts to create the best strategy available with the number of 3333 * buffers supplied. It will always create a {@code BufferStrategy} 3334 * with that number of buffers. 3335 * A page-flipping strategy is attempted first, then a blitting strategy 3336 * using accelerated buffers. Finally, an unaccelerated blitting 3337 * strategy is used. 3338 * <p> 3339 * Each time this method is called, 3340 * the existing buffer strategy for this component is discarded. 3341 * @param numBuffers number of buffers to create 3342 * @exception IllegalArgumentException if numBuffers is less than 1. 3343 * @exception IllegalStateException if the component is not displayable 3344 * @see #isDisplayable 3345 * @see #getBufferStrategy 3346 * @since 1.4 3347 */ 3348 public void createBufferStrategy(int numBuffers) { 3349 super.createBufferStrategy(numBuffers); 3350 } 3351 3352 /** 3353 * Creates a new strategy for multi-buffering on this component with the 3354 * required buffer capabilities. This is useful, for example, if only 3355 * accelerated memory or page flipping is desired (as specified by the 3356 * buffer capabilities). 3357 * <p> 3358 * Each time this method 3359 * is called, the existing buffer strategy for this component is discarded. 3360 * @param numBuffers number of buffers to create, including the front buffer 3361 * @param caps the required capabilities for creating the buffer strategy; 3362 * cannot be {@code null} 3363 * @exception AWTException if the capabilities supplied could not be 3364 * supported or met; this may happen, for example, if there is not enough 3365 * accelerated memory currently available, or if page flipping is specified 3366 * but not possible. 3367 * @exception IllegalArgumentException if numBuffers is less than 1, or if 3368 * caps is {@code null} 3369 * @see #getBufferStrategy 3370 * @since 1.4 3371 */ 3372 public void createBufferStrategy(int numBuffers, 3373 BufferCapabilities caps) throws AWTException { 3374 super.createBufferStrategy(numBuffers, caps); 3375 } 3376 3377 /** 3378 * Returns the {@code BufferStrategy} used by this component. This 3379 * method will return null if a {@code BufferStrategy} has not yet 3380 * been created or has been disposed. 3381 * 3382 * @return the buffer strategy used by this component 3383 * @see #createBufferStrategy 3384 * @since 1.4 3385 */ 3386 public BufferStrategy getBufferStrategy() { 3387 return super.getBufferStrategy(); 3388 } 3389 3390 Component getTemporaryLostComponent() { 3391 return temporaryLostComponent; 3392 } 3393 Component setTemporaryLostComponent(Component component) { 3394 Component previousComp = temporaryLostComponent; 3395 // Check that "component" is an acceptable focus owner and don't store it otherwise 3396 // - or later we will have problems with opposite while handling WINDOW_GAINED_FOCUS 3397 if (component == null || component.canBeFocusOwner()) { 3398 temporaryLostComponent = component; 3399 } else { 3400 temporaryLostComponent = null; 3401 } 3402 return previousComp; 3403 } 3404 3405 /** 3406 * Checks whether this window can contain focus owner. 3407 * Verifies that it is focusable and as container it can container focus owner. 3408 * @since 1.5 3409 */ 3410 boolean canContainFocusOwner(Component focusOwnerCandidate) { 3411 return super.canContainFocusOwner(focusOwnerCandidate) && isFocusableWindow(); 3412 } 3413 3414 private boolean locationByPlatform = locationByPlatformProp; 3415 3416 3417 /** 3418 * Sets whether this Window should appear at the default location for the 3419 * native windowing system or at the current location (returned by 3420 * {@code getLocation}) the next time the Window is made visible. 3421 * This behavior resembles a native window shown without programmatically 3422 * setting its location. Most windowing systems cascade windows if their 3423 * locations are not explicitly set. The actual location is determined once the 3424 * window is shown on the screen. 3425 * <p> 3426 * This behavior can also be enabled by setting the System Property 3427 * "java.awt.Window.locationByPlatform" to "true", though calls to this method 3428 * take precedence. 3429 * <p> 3430 * Calls to {@code setVisible}, {@code setLocation} and 3431 * {@code setBounds} after calling {@code setLocationByPlatform} clear 3432 * this property of the Window. 3433 * <p> 3434 * For example, after the following code is executed: 3435 * <pre> 3436 * setLocationByPlatform(true); 3437 * setVisible(true); 3438 * boolean flag = isLocationByPlatform(); 3439 * </pre> 3440 * The window will be shown at platform's default location and 3441 * {@code flag} will be {@code false}. 3442 * <p> 3443 * In the following sample: 3444 * <pre> 3445 * setLocationByPlatform(true); 3446 * setLocation(10, 10); 3447 * boolean flag = isLocationByPlatform(); 3448 * setVisible(true); 3449 * </pre> 3450 * The window will be shown at (10, 10) and {@code flag} will be 3451 * {@code false}. 3452 * 3453 * @param locationByPlatform {@code true} if this Window should appear 3454 * at the default location, {@code false} if at the current location 3455 * @throws IllegalComponentStateException if the window 3456 * is showing on screen and locationByPlatform is {@code true}. 3457 * @see #setLocation 3458 * @see #isShowing 3459 * @see #setVisible 3460 * @see #isLocationByPlatform 3461 * @see java.lang.System#getProperty(String) 3462 * @since 1.5 3463 */ 3464 public void setLocationByPlatform(boolean locationByPlatform) { 3465 synchronized (getTreeLock()) { 3466 if (locationByPlatform && isShowing()) { 3467 throw new IllegalComponentStateException("The window is showing on screen."); 3468 } 3469 this.locationByPlatform = locationByPlatform; 3470 } 3471 } 3472 3473 /** 3474 * Returns {@code true} if this Window will appear at the default location 3475 * for the native windowing system the next time this Window is made visible. 3476 * This method always returns {@code false} if the Window is showing on the 3477 * screen. 3478 * 3479 * @return whether this Window will appear at the default location 3480 * @see #setLocationByPlatform 3481 * @see #isShowing 3482 * @since 1.5 3483 */ 3484 public boolean isLocationByPlatform() { 3485 synchronized (getTreeLock()) { 3486 return locationByPlatform; 3487 } 3488 } 3489 3490 /** 3491 * {@inheritDoc} 3492 * <p> 3493 * The {@code width} or {@code height} values 3494 * are automatically enlarged if either is less than 3495 * the minimum size as specified by previous call to 3496 * {@code setMinimumSize}. 3497 * <p> 3498 * The method changes the geometry-related data. Therefore, 3499 * the native windowing system may ignore such requests, or it may modify 3500 * the requested data, so that the {@code Window} object is placed and sized 3501 * in a way that corresponds closely to the desktop settings. 3502 * 3503 * @see #getBounds 3504 * @see #setLocation(int, int) 3505 * @see #setLocation(Point) 3506 * @see #setSize(int, int) 3507 * @see #setSize(Dimension) 3508 * @see #setMinimumSize 3509 * @see #setLocationByPlatform 3510 * @see #isLocationByPlatform 3511 * @since 1.6 3512 */ 3513 public void setBounds(int x, int y, int width, int height) { 3514 synchronized (getTreeLock()) { 3515 if (getBoundsOp() == ComponentPeer.SET_LOCATION || 3516 getBoundsOp() == ComponentPeer.SET_BOUNDS) 3517 { 3518 locationByPlatform = false; 3519 } 3520 super.setBounds(x, y, width, height); 3521 } 3522 } 3523 3524 /** 3525 * {@inheritDoc} 3526 * <p> 3527 * The {@code r.width} or {@code r.height} values 3528 * will be automatically enlarged if either is less than 3529 * the minimum size as specified by previous call to 3530 * {@code setMinimumSize}. 3531 * <p> 3532 * The method changes the geometry-related data. Therefore, 3533 * the native windowing system may ignore such requests, or it may modify 3534 * the requested data, so that the {@code Window} object is placed and sized 3535 * in a way that corresponds closely to the desktop settings. 3536 * 3537 * @see #getBounds 3538 * @see #setLocation(int, int) 3539 * @see #setLocation(Point) 3540 * @see #setSize(int, int) 3541 * @see #setSize(Dimension) 3542 * @see #setMinimumSize 3543 * @see #setLocationByPlatform 3544 * @see #isLocationByPlatform 3545 * @since 1.6 3546 */ 3547 public void setBounds(Rectangle r) { 3548 setBounds(r.x, r.y, r.width, r.height); 3549 } 3550 3551 /** 3552 * Determines whether this component will be displayed on the screen. 3553 * @return {@code true} if the component and all of its ancestors 3554 * until a toplevel window are visible, {@code false} otherwise 3555 */ 3556 boolean isRecursivelyVisible() { 3557 // 5079694 fix: for a toplevel to be displayed, its parent doesn't have to be visible. 3558 // We're overriding isRecursivelyVisible to implement this policy. 3559 return visible; 3560 } 3561 3562 3563 // ******************** SHAPES & TRANSPARENCY CODE ******************** 3564 3565 /** 3566 * Returns the opacity of the window. 3567 * 3568 * @return the opacity of the window 3569 * 3570 * @see Window#setOpacity(float) 3571 * @see GraphicsDevice.WindowTranslucency 3572 * 3573 * @since 1.7 3574 */ 3575 public float getOpacity() { 3576 synchronized (getTreeLock()) { 3577 return opacity; 3578 } 3579 } 3580 3581 /** 3582 * Sets the opacity of the window. 3583 * <p> 3584 * The opacity value is in the range [0..1]. Note that setting the opacity 3585 * level of 0 may or may not disable the mouse event handling on this 3586 * window. This is a platform-dependent behavior. 3587 * <p> 3588 * The following conditions must be met in order to set the opacity value 3589 * less than {@code 1.0f}: 3590 * <ul> 3591 * <li>The {@link GraphicsDevice.WindowTranslucency#TRANSLUCENT TRANSLUCENT} 3592 * translucency must be supported by the underlying system 3593 * <li>The window must be undecorated (see {@link Frame#setUndecorated} 3594 * and {@link Dialog#setUndecorated}) 3595 * <li>The window must not be in full-screen mode (see {@link 3596 * GraphicsDevice#setFullScreenWindow(Window)}) 3597 * </ul> 3598 * <p> 3599 * If the requested opacity value is less than {@code 1.0f}, and any of the 3600 * above conditions are not met, the window opacity will not change, 3601 * and the {@code IllegalComponentStateException} will be thrown. 3602 * <p> 3603 * The translucency levels of individual pixels may also be effected by the 3604 * alpha component of their color (see {@link Window#setBackground(Color)}) and the 3605 * current shape of this window (see {@link #setShape(Shape)}). 3606 * 3607 * @param opacity the opacity level to set to the window 3608 * 3609 * @throws IllegalArgumentException if the opacity is out of the range 3610 * [0..1] 3611 * @throws IllegalComponentStateException if the window is decorated and 3612 * the opacity is less than {@code 1.0f} 3613 * @throws IllegalComponentStateException if the window is in full screen 3614 * mode, and the opacity is less than {@code 1.0f} 3615 * @throws UnsupportedOperationException if the {@code 3616 * GraphicsDevice.WindowTranslucency#TRANSLUCENT TRANSLUCENT} 3617 * translucency is not supported and the opacity is less than 3618 * {@code 1.0f} 3619 * 3620 * @see Window#getOpacity 3621 * @see Window#setBackground(Color) 3622 * @see Window#setShape(Shape) 3623 * @see Frame#isUndecorated 3624 * @see Dialog#isUndecorated 3625 * @see GraphicsDevice.WindowTranslucency 3626 * @see GraphicsDevice#isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency) 3627 * 3628 * @since 1.7 3629 */ 3630 @SuppressWarnings("deprecation") 3631 public void setOpacity(float opacity) { 3632 synchronized (getTreeLock()) { 3633 if (opacity < 0.0f || opacity > 1.0f) { 3634 throw new IllegalArgumentException( 3635 "The value of opacity should be in the range [0.0f .. 1.0f]."); 3636 } 3637 if (opacity < 1.0f) { 3638 GraphicsConfiguration gc = getGraphicsConfiguration(); 3639 GraphicsDevice gd = gc.getDevice(); 3640 if (gc.getDevice().getFullScreenWindow() == this) { 3641 throw new IllegalComponentStateException( 3642 "Setting opacity for full-screen window is not supported."); 3643 } 3644 if (!gd.isWindowTranslucencySupported( 3645 GraphicsDevice.WindowTranslucency.TRANSLUCENT)) 3646 { 3647 throw new UnsupportedOperationException( 3648 "TRANSLUCENT translucency is not supported."); 3649 } 3650 } 3651 this.opacity = opacity; 3652 WindowPeer peer = (WindowPeer)getPeer(); 3653 if (peer != null) { 3654 peer.setOpacity(opacity); 3655 } 3656 } 3657 } 3658 3659 /** 3660 * Returns the shape of the window. 3661 * 3662 * The value returned by this method may not be the same as 3663 * previously set with {@code setShape(shape)}, but it is guaranteed 3664 * to represent the same shape. 3665 * 3666 * @return the shape of the window or {@code null} if no 3667 * shape is specified for the window 3668 * 3669 * @see Window#setShape(Shape) 3670 * @see GraphicsDevice.WindowTranslucency 3671 * 3672 * @since 1.7 3673 */ 3674 public Shape getShape() { 3675 synchronized (getTreeLock()) { 3676 return shape == null ? null : new Path2D.Float(shape); 3677 } 3678 } 3679 3680 /** 3681 * Sets the shape of the window. 3682 * <p> 3683 * Setting a shape cuts off some parts of the window. Only the parts that 3684 * belong to the given {@link Shape} remain visible and clickable. If 3685 * the shape argument is {@code null}, this method restores the default 3686 * shape, making the window rectangular on most platforms. 3687 * <p> 3688 * The following conditions must be met to set a non-null shape: 3689 * <ul> 3690 * <li>The {@link GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSPARENT 3691 * PERPIXEL_TRANSPARENT} translucency must be supported by the 3692 * underlying system 3693 * <li>The window must be undecorated (see {@link Frame#setUndecorated} 3694 * and {@link Dialog#setUndecorated}) 3695 * <li>The window must not be in full-screen mode (see {@link 3696 * GraphicsDevice#setFullScreenWindow(Window)}) 3697 * </ul> 3698 * <p> 3699 * If the requested shape is not {@code null}, and any of the above 3700 * conditions are not met, the shape of this window will not change, 3701 * and either the {@code UnsupportedOperationException} or {@code 3702 * IllegalComponentStateException} will be thrown. 3703 * <p> 3704 * The translucency levels of individual pixels may also be effected by the 3705 * alpha component of their color (see {@link Window#setBackground(Color)}) and the 3706 * opacity value (see {@link #setOpacity(float)}). See {@link 3707 * GraphicsDevice.WindowTranslucency} for more details. 3708 * 3709 * @param shape the shape to set to the window 3710 * 3711 * @throws IllegalComponentStateException if the shape is not {@code 3712 * null} and the window is decorated 3713 * @throws IllegalComponentStateException if the shape is not {@code 3714 * null} and the window is in full-screen mode 3715 * @throws UnsupportedOperationException if the shape is not {@code 3716 * null} and {@link GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSPARENT 3717 * PERPIXEL_TRANSPARENT} translucency is not supported 3718 * 3719 * @see Window#getShape() 3720 * @see Window#setBackground(Color) 3721 * @see Window#setOpacity(float) 3722 * @see Frame#isUndecorated 3723 * @see Dialog#isUndecorated 3724 * @see GraphicsDevice.WindowTranslucency 3725 * @see GraphicsDevice#isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency) 3726 * 3727 * @since 1.7 3728 */ 3729 @SuppressWarnings("deprecation") 3730 public void setShape(Shape shape) { 3731 synchronized (getTreeLock()) { 3732 if (shape != null) { 3733 GraphicsConfiguration gc = getGraphicsConfiguration(); 3734 GraphicsDevice gd = gc.getDevice(); 3735 if (gc.getDevice().getFullScreenWindow() == this) { 3736 throw new IllegalComponentStateException( 3737 "Setting shape for full-screen window is not supported."); 3738 } 3739 if (!gd.isWindowTranslucencySupported( 3740 GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)) 3741 { 3742 throw new UnsupportedOperationException( 3743 "PERPIXEL_TRANSPARENT translucency is not supported."); 3744 } 3745 } 3746 this.shape = (shape == null) ? null : new Path2D.Float(shape); 3747 WindowPeer peer = (WindowPeer)getPeer(); 3748 if (peer != null) { 3749 peer.applyShape(shape == null ? null : Region.getInstance(shape, null)); 3750 } 3751 } 3752 } 3753 3754 /** 3755 * Gets the background color of this window. 3756 * <p> 3757 * Note that the alpha component of the returned color indicates whether 3758 * the window is in the non-opaque (per-pixel translucent) mode. 3759 * 3760 * @return this component's background color 3761 * 3762 * @see Window#setBackground(Color) 3763 * @see Window#isOpaque 3764 * @see GraphicsDevice.WindowTranslucency 3765 */ 3766 @Override 3767 public Color getBackground() { 3768 return super.getBackground(); 3769 } 3770 3771 /** 3772 * Sets the background color of this window. 3773 * <p> 3774 * If the windowing system supports the {@link 3775 * GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT PERPIXEL_TRANSLUCENT} 3776 * translucency, the alpha component of the given background color 3777 * may effect the mode of operation for this window: it indicates whether 3778 * this window must be opaque (alpha equals {@code 1.0f}) or per-pixel translucent 3779 * (alpha is less than {@code 1.0f}). If the given background color is 3780 * {@code null}, the window is considered completely opaque. 3781 * <p> 3782 * All the following conditions must be met to enable the per-pixel 3783 * transparency mode for this window: 3784 * <ul> 3785 * <li>The {@link GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT 3786 * PERPIXEL_TRANSLUCENT} translucency must be supported by the graphics 3787 * device where this window is located 3788 * <li>The window must be undecorated (see {@link Frame#setUndecorated} 3789 * and {@link Dialog#setUndecorated}) 3790 * <li>The window must not be in full-screen mode (see {@link 3791 * GraphicsDevice#setFullScreenWindow(Window)}) 3792 * </ul> 3793 * <p> 3794 * If the alpha component of the requested background color is less than 3795 * {@code 1.0f}, and any of the above conditions are not met, the background 3796 * color of this window will not change, the alpha component of the given 3797 * background color will not affect the mode of operation for this window, 3798 * and either the {@code UnsupportedOperationException} or {@code 3799 * IllegalComponentStateException} will be thrown. 3800 * <p> 3801 * When the window is per-pixel translucent, the drawing sub-system 3802 * respects the alpha value of each individual pixel. If a pixel gets 3803 * painted with the alpha color component equal to zero, it becomes 3804 * visually transparent. If the alpha of the pixel is equal to 1.0f, the 3805 * pixel is fully opaque. Interim values of the alpha color component make 3806 * the pixel semi-transparent. In this mode, the background of the window 3807 * gets painted with the alpha value of the given background color. If the 3808 * alpha value of the argument of this method is equal to {@code 0}, the 3809 * background is not painted at all. 3810 * <p> 3811 * The actual level of translucency of a given pixel also depends on window 3812 * opacity (see {@link #setOpacity(float)}), as well as the current shape of 3813 * this window (see {@link #setShape(Shape)}). 3814 * <p> 3815 * Note that painting a pixel with the alpha value of {@code 0} may or may 3816 * not disable the mouse event handling on this pixel. This is a 3817 * platform-dependent behavior. To make sure the mouse events do not get 3818 * dispatched to a particular pixel, the pixel must be excluded from the 3819 * shape of the window. 3820 * <p> 3821 * Enabling the per-pixel translucency mode may change the graphics 3822 * configuration of this window due to the native platform requirements. 3823 * 3824 * @param bgColor the color to become this window's background color. 3825 * 3826 * @throws IllegalComponentStateException if the alpha value of the given 3827 * background color is less than {@code 1.0f} and the window is decorated 3828 * @throws IllegalComponentStateException if the alpha value of the given 3829 * background color is less than {@code 1.0f} and the window is in 3830 * full-screen mode 3831 * @throws UnsupportedOperationException if the alpha value of the given 3832 * background color is less than {@code 1.0f} and {@link 3833 * GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT 3834 * PERPIXEL_TRANSLUCENT} translucency is not supported 3835 * 3836 * @see Window#getBackground 3837 * @see Window#isOpaque 3838 * @see Window#setOpacity(float) 3839 * @see Window#setShape(Shape) 3840 * @see Frame#isUndecorated 3841 * @see Dialog#isUndecorated 3842 * @see GraphicsDevice.WindowTranslucency 3843 * @see GraphicsDevice#isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency) 3844 * @see GraphicsConfiguration#isTranslucencyCapable() 3845 */ 3846 @Override 3847 @SuppressWarnings("deprecation") 3848 public void setBackground(Color bgColor) { 3849 Color oldBg = getBackground(); 3850 super.setBackground(bgColor); 3851 if (oldBg != null && oldBg.equals(bgColor)) { 3852 return; 3853 } 3854 int oldAlpha = oldBg != null ? oldBg.getAlpha() : 255; 3855 int alpha = bgColor != null ? bgColor.getAlpha() : 255; 3856 if ((oldAlpha == 255) && (alpha < 255)) { // non-opaque window 3857 GraphicsConfiguration gc = getGraphicsConfiguration(); 3858 GraphicsDevice gd = gc.getDevice(); 3859 if (gc.getDevice().getFullScreenWindow() == this) { 3860 throw new IllegalComponentStateException( 3861 "Making full-screen window non opaque is not supported."); 3862 } 3863 if (!gc.isTranslucencyCapable()) { 3864 GraphicsConfiguration capableGC = gd.getTranslucencyCapableGC(); 3865 if (capableGC == null) { 3866 throw new UnsupportedOperationException( 3867 "PERPIXEL_TRANSLUCENT translucency is not supported"); 3868 } 3869 setGraphicsConfiguration(capableGC); 3870 } 3871 setLayersOpaque(this, false); 3872 } else if ((oldAlpha < 255) && (alpha == 255)) { 3873 setLayersOpaque(this, true); 3874 } 3875 WindowPeer peer = (WindowPeer)getPeer(); 3876 if (peer != null) { 3877 peer.setOpaque(alpha == 255); 3878 } 3879 } 3880 3881 /** 3882 * Indicates if the window is currently opaque. 3883 * <p> 3884 * The method returns {@code false} if the background color of the window 3885 * is not {@code null} and the alpha component of the color is less than 3886 * {@code 1.0f}. The method returns {@code true} otherwise. 3887 * 3888 * @return {@code true} if the window is opaque, {@code false} otherwise 3889 * 3890 * @see Window#getBackground 3891 * @see Window#setBackground(Color) 3892 * @since 1.7 3893 */ 3894 @Override 3895 public boolean isOpaque() { 3896 Color bg = getBackground(); 3897 return bg != null ? bg.getAlpha() == 255 : true; 3898 } 3899 3900 @SuppressWarnings("deprecation") 3901 private void updateWindow() { 3902 synchronized (getTreeLock()) { 3903 WindowPeer peer = (WindowPeer)getPeer(); 3904 if (peer != null) { 3905 peer.updateWindow(); 3906 } 3907 } 3908 } 3909 3910 /** 3911 * {@inheritDoc} 3912 * 3913 * @since 1.7 3914 */ 3915 @Override 3916 public void paint(Graphics g) { 3917 if (!isOpaque()) { 3918 Graphics gg = g.create(); 3919 try { 3920 if (gg instanceof Graphics2D) { 3921 gg.setColor(getBackground()); 3922 ((Graphics2D)gg).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC)); 3923 gg.fillRect(0, 0, getWidth(), getHeight()); 3924 } 3925 } finally { 3926 gg.dispose(); 3927 } 3928 } 3929 super.paint(g); 3930 } 3931 3932 private static void setLayersOpaque(Component component, boolean isOpaque) { 3933 // Shouldn't use instanceof to avoid loading Swing classes 3934 // if it's a pure AWT application. 3935 if (SunToolkit.isInstanceOf(component, "javax.swing.RootPaneContainer")) { 3936 javax.swing.RootPaneContainer rpc = (javax.swing.RootPaneContainer)component; 3937 javax.swing.JRootPane root = rpc.getRootPane(); 3938 javax.swing.JLayeredPane lp = root.getLayeredPane(); 3939 Container c = root.getContentPane(); 3940 javax.swing.JComponent content = 3941 (c instanceof javax.swing.JComponent) ? (javax.swing.JComponent)c : null; 3942 lp.setOpaque(isOpaque); 3943 root.setOpaque(isOpaque); 3944 if (content != null) { 3945 content.setOpaque(isOpaque); 3946 3947 // Iterate down one level to see whether we have a JApplet 3948 // (which is also a RootPaneContainer) which requires processing 3949 int numChildren = content.getComponentCount(); 3950 if (numChildren > 0) { 3951 Component child = content.getComponent(0); 3952 // It's OK to use instanceof here because we've 3953 // already loaded the RootPaneContainer class by now 3954 if (child instanceof javax.swing.RootPaneContainer) { 3955 setLayersOpaque(child, isOpaque); 3956 } 3957 } 3958 } 3959 } 3960 } 3961 3962 3963 // ************************** MIXING CODE ******************************* 3964 3965 // A window has an owner, but it does NOT have a container 3966 @Override 3967 final Container getContainer() { 3968 return null; 3969 } 3970 3971 /** 3972 * Applies the shape to the component 3973 * @param shape Shape to be applied to the component 3974 */ 3975 @Override 3976 final void applyCompoundShape(Region shape) { 3977 // The shape calculated by mixing code is not intended to be applied 3978 // to windows or frames 3979 } 3980 3981 @Override 3982 final void applyCurrentShape() { 3983 // The shape calculated by mixing code is not intended to be applied 3984 // to windows or frames 3985 } 3986 3987 @Override 3988 final void mixOnReshaping() { 3989 // The shape calculated by mixing code is not intended to be applied 3990 // to windows or frames 3991 } 3992 3993 @Override 3994 final Point getLocationOnWindow() { 3995 return new Point(0, 0); 3996 } 3997 3998 // ****************** END OF MIXING CODE ******************************** 3999 4000 /** 4001 * Limit the given double value with the given range. 4002 */ 4003 private static double limit(double value, double min, double max) { 4004 value = Math.max(value, min); 4005 value = Math.min(value, max); 4006 return value; 4007 } 4008 4009 /** 4010 * Calculate the position of the security warning. 4011 * 4012 * This method gets the window location/size as reported by the native 4013 * system since the locally cached values may represent outdated data. 4014 * 4015 * The method is used from the native code, or via AWTAccessor. 4016 * 4017 * NOTE: this method is invoked on the toolkit thread, and therefore is not 4018 * supposed to become public/user-overridable. 4019 */ 4020 private Point2D calculateSecurityWarningPosition(double x, double y, 4021 double w, double h) 4022 { 4023 // The position according to the spec of SecurityWarning.setPosition() 4024 double wx = x + w * securityWarningAlignmentX + securityWarningPointX; 4025 double wy = y + h * securityWarningAlignmentY + securityWarningPointY; 4026 4027 // First, make sure the warning is not too far from the window bounds 4028 wx = Window.limit(wx, 4029 x - securityWarningWidth - 2, 4030 x + w + 2); 4031 wy = Window.limit(wy, 4032 y - securityWarningHeight - 2, 4033 y + h + 2); 4034 4035 // Now make sure the warning window is visible on the screen 4036 GraphicsConfiguration graphicsConfig = 4037 getGraphicsConfiguration_NoClientCode(); 4038 Rectangle screenBounds = graphicsConfig.getBounds(); 4039 Insets screenInsets = 4040 Toolkit.getDefaultToolkit().getScreenInsets(graphicsConfig); 4041 4042 wx = Window.limit(wx, 4043 screenBounds.x + screenInsets.left, 4044 screenBounds.x + screenBounds.width - screenInsets.right 4045 - securityWarningWidth); 4046 wy = Window.limit(wy, 4047 screenBounds.y + screenInsets.top, 4048 screenBounds.y + screenBounds.height - screenInsets.bottom 4049 - securityWarningHeight); 4050 4051 return new Point2D.Double(wx, wy); 4052 } 4053 4054 static { 4055 AWTAccessor.setWindowAccessor(new AWTAccessor.WindowAccessor() { 4056 public float getOpacity(Window window) { 4057 return window.opacity; 4058 } 4059 public void setOpacity(Window window, float opacity) { 4060 window.setOpacity(opacity); 4061 } 4062 public Shape getShape(Window window) { 4063 return window.getShape(); 4064 } 4065 public void setShape(Window window, Shape shape) { 4066 window.setShape(shape); 4067 } 4068 public void setOpaque(Window window, boolean opaque) { 4069 Color bg = window.getBackground(); 4070 if (bg == null) { 4071 bg = new Color(0, 0, 0, 0); 4072 } 4073 window.setBackground(new Color(bg.getRed(), bg.getGreen(), bg.getBlue(), 4074 opaque ? 255 : 0)); 4075 } 4076 public void updateWindow(Window window) { 4077 window.updateWindow(); 4078 } 4079 4080 public Dimension getSecurityWarningSize(Window window) { 4081 return new Dimension(window.securityWarningWidth, 4082 window.securityWarningHeight); 4083 } 4084 4085 public void setSecurityWarningSize(Window window, int width, int height) 4086 { 4087 window.securityWarningWidth = width; 4088 window.securityWarningHeight = height; 4089 } 4090 4091 @SuppressWarnings("deprecation") 4092 public void setSecurityWarningPosition(Window window, 4093 Point2D point, float alignmentX, float alignmentY) 4094 { 4095 window.securityWarningPointX = point.getX(); 4096 window.securityWarningPointY = point.getY(); 4097 window.securityWarningAlignmentX = alignmentX; 4098 window.securityWarningAlignmentY = alignmentY; 4099 4100 synchronized (window.getTreeLock()) { 4101 WindowPeer peer = (WindowPeer)window.getPeer(); 4102 if (peer != null) { 4103 peer.repositionSecurityWarning(); 4104 } 4105 } 4106 } 4107 4108 public Point2D calculateSecurityWarningPosition(Window window, 4109 double x, double y, double w, double h) 4110 { 4111 return window.calculateSecurityWarningPosition(x, y, w, h); 4112 } 4113 4114 public void setLWRequestStatus(Window changed, boolean status) { 4115 changed.syncLWRequests = status; 4116 } 4117 4118 public boolean isAutoRequestFocus(Window w) { 4119 return w.autoRequestFocus; 4120 } 4121 4122 public boolean isTrayIconWindow(Window w) { 4123 return w.isTrayIconWindow; 4124 } 4125 4126 public void setTrayIconWindow(Window w, boolean isTrayIconWindow) { 4127 w.isTrayIconWindow = isTrayIconWindow; 4128 } 4129 }); // WindowAccessor 4130 } // static 4131 4132 // a window doesn't need to be updated in the Z-order. 4133 @Override 4134 void updateZOrder() {} 4135 4136 } // class Window 4137 4138 4139 /** 4140 * This class is no longer used, but is maintained for Serialization 4141 * backward-compatibility. 4142 */ 4143 class FocusManager implements java.io.Serializable { 4144 Container focusRoot; 4145 Component focusOwner; 4146 4147 /* 4148 * JDK 1.1 serialVersionUID 4149 */ 4150 static final long serialVersionUID = 2491878825643557906L; 4151 }