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