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