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