KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > Window


1 /*
2  * @(#)Window.java 1.213 07/01/23
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.awt;
8
9 import java.applet.Applet JavaDoc;
10 import java.awt.peer.WindowPeer;
11 import java.awt.peer.ComponentPeer;
12 import java.awt.event.*;
13 import java.awt.image.BufferStrategy JavaDoc;
14 import java.util.Vector JavaDoc;
15 import java.util.Locale JavaDoc;
16 import java.util.EventListener JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.io.Serializable JavaDoc;
19 import java.io.ObjectOutputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OptionalDataException JavaDoc;
23 import java.awt.im.InputContext JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.security.AccessController JavaDoc;
28 import java.security.PrivilegedAction JavaDoc;
29 import javax.accessibility.*;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import sun.security.action.GetPropertyAction;
32 import sun.security.util.SecurityConstants;
33 import sun.awt.DebugHelper;
34
35 /**
36  * A <code>Window</code> object is a top-level window with no borders and no
37  * menubar.
38  * The default layout for a window is <code>BorderLayout</code>.
39  * <p>
40  * A window must have either a frame, dialog, or another window defined as its
41  * owner when it's constructed.
42  * <p>
43  * In a multi-screen environment, you can create a <code>Window</code>
44  * on a different screen device by constructing the <code>Window</code>
45  * with {@link #Window(Window, GraphicsConfiguration)}. The
46  * <code>GraphicsConfiguration</code> object is one of the
47  * <code>GraphicsConfiguration</code> objects of the target screen device.
48  * <p>
49  * In a virtual device multi-screen environment in which the desktop
50  * area could span multiple physical screen devices, the bounds of all
51  * configurations are relative to the virtual device coordinate system.
52  * The origin of the virtual-coordinate system is at the upper left-hand
53  * corner of the primary physical screen. Depending on the location of
54  * the primary screen in the virtual device, negative coordinates are
55  * possible, as shown in the following figure.
56  * <p>
57  * <img SRC="doc-files/MultiScreen.gif"
58  * alt="Diagram shows virtual device containing 4 physical screens. Primary physical screen shows coords (0,0), other screen shows (-80,-100)."
59  * ALIGN=center HSPACE=10 VSPACE=7>
60  * <p>
61  * In such an environment, when calling <code>setLocation</code>,
62  * you must pass a virtual coordinate to this method. Similarly,
63  * calling <code>getLocationOnScreen</code> on a <code>Window</code> returns
64  * virtual device coordinates. Call the <code>getBounds</code> method
65  * of a <code>GraphicsConfiguration</code> to find its origin in the virtual
66  * coordinate system.
67  * <p>
68  * The following code sets the location of a <code>Window</code>
69  * at (10, 10) relative to the origin of the physical screen
70  * of the corresponding <code>GraphicsConfiguration</code>. If the
71  * bounds of the <code>GraphicsConfiguration</code> is not taken
72  * into account, the <code>Window</code> location would be set
73  * at (10, 10) relative to the virtual-coordinate system and would appear
74  * on the primary physical screen, which might be different from the
75  * physical screen of the specified <code>GraphicsConfiguration</code>.
76  *
77  * <pre>
78  * Window w = new Window(Window owner, GraphicsConfiguration gc);
79  * Rectangle bounds = gc.getBounds();
80  * w.setLocation(10 + bounds.x, 10 + bounds.y);
81  * </pre>
82  *
83  * <p>
84  * Note: the location and size of top-level windows (including
85  * <code>Window</code>s, <code>Frame</code>s, and <code>Dialog</code>s)
86  * are under the control of the desktop's window management system.
87  * Calls to <code>setLocation</code>, <code>setSize</code>, and
88  * <code>setBounds</code> are requests (not directives) which are
89  * forwarded to the window management system. Every effort will be
90  * made to honor such requests. However, in some cases the window
91  * management system may ignore such requests, or modify the requested
92  * geometry in order to place and size the <code>Window</code> in a way
93  * that more closely matches the desktop settings.
94  *
95  * Due to the asynchronous nature of native event handling, the results
96  * returned by <code>getBounds</code>, <code>getLocation</code>,
97  * <code>getLocationOnScreen</code>, and <code>getSize</code> might not
98  * reflect the actual geometry of the Window on screen until the last
99  * request has been processed. During the processing of subsequent
100  * requests these values might change accordingly while the window
101  * management system fulfills the requests.
102  *
103  * <p>
104  * Windows are capable of generating the following WindowEvents:
105  * WindowOpened, WindowClosed, WindowGainedFocus, WindowLostFocus.
106  *
107  * @version 1.213, 01/23/07
108  * @author Sami Shaio
109  * @author Arthur van Hoff
110  * @see WindowEvent
111  * @see #addWindowListener
112  * @see java.awt.BorderLayout
113  * @since JDK1.0
114  */

115 public class Window extends Container JavaDoc implements Accessible {
116
117     /**
118      * This represents the warning message that is
119      * to be displayed in a non secure window. ie :
120      * a window that has a security manager installed for
121      * which calling SecurityManager.checkTopLevelWindow()
122      * is false. This message can be displayed anywhere in
123      * the window.
124      *
125      * @serial
126      * @see #getWarningString
127      */

128     String JavaDoc warningString;
129
130     /**
131      * Holds the reference to the component which last had focus in this window
132      * before it lost focus.
133      */

134     private transient Component JavaDoc temporaryLostComponent;
135
136     static boolean systemSyncLWRequests = false;
137     boolean syncLWRequests = false;
138     transient boolean beforeFirstShow = true;
139
140     static final int OPENED = 0x01;
141
142     /**
143      * An Integer value representing the Window State.
144      *
145      * @serial
146      * @since 1.2
147      * @see #show
148      */

149     int state;
150
151     /**
152      * A boolean value representing Window always-on-top state
153      * @since 1.5
154      * @serial
155      * @see #setAlwaysOnTop
156      * @see #isAlwaysOnTop
157      */

158     private boolean alwaysOnTop;
159
160     /**
161      * A vector containing all the windows this
162      * window currently owns.
163      * @since 1.2
164      * @see #getOwnedWindows
165      */

166     transient Vector JavaDoc<WeakReference JavaDoc<Window JavaDoc>> ownedWindowList =
167                                             new Vector JavaDoc<WeakReference JavaDoc<Window JavaDoc>>();
168     private transient WeakReference JavaDoc<Window JavaDoc> weakThis;
169
170     transient boolean showWithParent;
171     
172     transient WindowListener windowListener;
173     transient WindowStateListener windowStateListener;
174     transient WindowFocusListener windowFocusListener;
175
176     transient InputContext JavaDoc inputContext;
177     private transient Object JavaDoc inputContextLock = new Object JavaDoc();
178
179     /**
180      * Unused. Maintained for serialization backward-compatibility.
181      *
182      * @serial
183      * @since 1.2
184      */

185     private FocusManager focusMgr;
186
187     /**
188      * Indicates whether this Window can become the focused Window.
189      *
190      * @serial
191      * @see #getFocusableWindowState
192      * @see #setFocusableWindowState
193      * @since 1.4
194      */

195     private boolean focusableWindowState = true;
196
197     private static final String JavaDoc base = "win";
198     private static int nameCounter = 0;
199
200     /*
201      * JDK 1.1 serialVersionUID
202      */

203     private static final long serialVersionUID = 4497834738069338734L;
204
205     private static final DebugHelper dbg = DebugHelper.create(Window JavaDoc.class);
206
207     private static final boolean locationByPlatformProp;
208
209     /**
210      * Indicates whether this Window is modal excluded, i. e. is not
211      * blocked by modal dialogs
212      * @since 1.5
213      */

214     private transient boolean modalExcluded = false;
215
216     static {
217         /* ensure that the necessary native libraries are loaded */
218     Toolkit.loadLibraries();
219         if (!GraphicsEnvironment.isHeadless()) {
220             initIDs();
221         }
222         
223         String JavaDoc s = (String JavaDoc) java.security.AccessController.doPrivileged(
224             new GetPropertyAction("java.awt.syncLWRequests"));
225         systemSyncLWRequests = (s != null && s.equals("true"));
226         s = (String JavaDoc) java.security.AccessController.doPrivileged(
227             new GetPropertyAction("java.awt.Window.locationByPlatform"));
228         locationByPlatformProp = (s != null && s.equals("true"));
229     }
230
231     /**
232      * Initialize JNI field and method IDs for fields that may be
233        accessed from C.
234      */

235     private static native void initIDs();
236
237     /**
238      * Constructs a new window in default size with the
239      * specified <code>GraphicsConfiguration</code>.
240      * <p>
241      * If there is a security manager, this method first calls
242      * the security manager's <code>checkTopLevelWindow</code>
243      * method with <code>this</code>
244      * as its argument to determine whether or not the window
245      * must be displayed with a warning banner.
246      * @param gc the <code>GraphicsConfiguration</code>
247      * of the target screen device. If <code>gc</code> is
248      * <code>null</code>, the system default
249      * <code>GraphicsConfiguration</code> is assumed.
250      * @exception IllegalArgumentException if <code>gc</code>
251      * is not from a screen device. This exception is always
252      * thrown when GraphicsEnvironment.isHeadless() returns true.
253      * @see java.awt.GraphicsEnvironment#isHeadless
254      * @see java.lang.SecurityManager#checkTopLevelWindow
255      */

256     Window(GraphicsConfiguration JavaDoc gc) {
257         init(gc);
258     }
259
260     private void init(GraphicsConfiguration JavaDoc gc) {
261         if (GraphicsEnvironment.isHeadless()) {
262             throw new IllegalArgumentException JavaDoc("headless environment");
263         }
264
265         syncLWRequests = systemSyncLWRequests;
266
267     setWarningString();
268     this.cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
269     this.visible = false;
270         if (gc == null) {
271             this.graphicsConfig =
272                 GraphicsEnvironment.getLocalGraphicsEnvironment().
273              getDefaultScreenDevice().getDefaultConfiguration();
274         } else {
275             this.graphicsConfig = gc;
276         }
277         if (graphicsConfig.getDevice().getType() !=
278             GraphicsDevice.TYPE_RASTER_SCREEN) {
279             throw new IllegalArgumentException JavaDoc("not a screen device");
280         }
281     setLayout(new BorderLayout JavaDoc());
282
283         /* offset the initial location with the original of the screen */
284         /* and any insets */
285         Rectangle JavaDoc screenBounds = graphicsConfig.getBounds();
286         Insets JavaDoc screenInsets = getToolkit().getScreenInsets(graphicsConfig);
287         int x = getX() + screenBounds.x + screenInsets.left;
288         int y = getY() + screenBounds.y + screenInsets.top;
289         if (x != this.x || y != this.y) {
290             setLocation(x, y);
291             /* reset after setLocation */
292             setLocationByPlatform(locationByPlatformProp);
293         }
294     }
295     
296     /**
297      * Constructs a new window in the default size.
298      *
299      * <p>First, if there is a security manager, its
300      * <code>checkTopLevelWindow</code>
301      * method is called with <code>this</code>
302      * as its argument
303      * to see if it's ok to display the window without a warning banner.
304      * If the default implementation of <code>checkTopLevelWindow</code>
305      * is used (that is, that method is not overriden), then this results in
306      * a call to the security manager's <code>checkPermission</code> method
307      * with an <code>AWTPermission("showWindowWithoutWarningBanner")</code>
308      * permission. It that method raises a SecurityException,
309      * <code>checkTopLevelWindow</code> returns false, otherwise it
310      * returns true. If it returns false, a warning banner is created.
311      *
312      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
313      * returns true
314      * @see java.awt.GraphicsEnvironment#isHeadless
315      * @see java.lang.SecurityManager#checkTopLevelWindow
316      */

317     Window() throws HeadlessException JavaDoc {
318         GraphicsEnvironment.checkHeadless();
319         init((GraphicsConfiguration JavaDoc)null);
320     }
321     
322     /**
323      * Constructs a new invisible window with the specified
324      * <code>Frame</code> as its owner. The Window will not be focusable
325      * unless its owner is showing on the screen.
326      * <p>
327      * If there is a security manager, this method first calls
328      * the security manager's <code>checkTopLevelWindow</code>
329      * method with <code>this</code>
330      * as its argument to determine whether or not the window
331      * must be displayed with a warning banner.
332      *
333      * @param owner the <code>Frame</code> to act as owner
334      * @exception IllegalArgumentException if the <code>owner</code>'s
335      * <code>GraphicsConfiguration</code> is not from a screen device
336      * @exception java.lang.IllegalArgumentException if
337      * <code>owner</code> is <code>null</code>; this exception
338      * is always thrown when <code>GraphicsEnvironment.isHeadless</code>
339      * returns true
340      * @see java.awt.GraphicsEnvironment#isHeadless
341      * @see java.lang.SecurityManager#checkTopLevelWindow
342      * @see #isShowing
343      */

344     public Window(Frame JavaDoc owner) {
345         this(owner == null ? (GraphicsConfiguration JavaDoc)null :
346             owner.getGraphicsConfiguration());
347     ownedInit(owner);
348     }
349
350     /**
351      * Constructs a new invisible window with the specified
352      * <code>Window</code> as its owner. The Window will not be focusable
353      * unless its nearest owning Frame or Dialog is showing on the screen.
354      * <p>
355      * If there is a security manager, this method first calls
356      * the security manager's <code>checkTopLevelWindow</code>
357      * method with <code>this</code>
358      * as its argument to determine whether or not the window
359      * must be displayed with a warning banner.
360      *
361      * @param owner the <code>Window</code> to act as owner
362      * @exception IllegalArgumentException if the <code>owner</code>'s
363      * <code>GraphicsConfiguration</code> is not from a screen device
364      * @exception java.lang.IllegalArgumentException if <code>owner</code>
365      * is <code>null</code>. This exception is always thrown
366      * when GraphicsEnvironment.isHeadless() returns true.
367      * @see java.awt.GraphicsEnvironment#isHeadless
368      * @see java.lang.SecurityManager#checkTopLevelWindow
369      * @see #isShowing
370      * @since 1.2
371      */

372     public Window(Window JavaDoc owner) {
373         this(owner == null ? (GraphicsConfiguration JavaDoc)null :
374             owner.getGraphicsConfiguration());
375     ownedInit(owner);
376     }
377
378     /**
379      * Constructs a new invisible window with the specified
380      * window as its owner and a
381      * <code>GraphicsConfiguration</code> of a screen device. The Window will
382      * not be focusable unless its nearest owning Frame or Dialog is showing on
383      * the screen.
384      * <p>
385      * If there is a security manager, this method first calls
386      * the security manager's <code>checkTopLevelWindow</code>
387      * method with <code>this</code>
388      * as its argument to determine whether or not the window
389      * must be displayed with a warning banner.
390      *
391      * @param owner the window to act as owner
392      * @param gc the <code>GraphicsConfiguration</code>
393      * of the target screen device; if <code>gc</code> is
394      * <code>null</code>, the system default
395      * <code>GraphicsConfiguration</code> is assumed
396      * @throws IllegalArgumentException if
397      * <code>owner</code> is <code>null</code>
398      * @throws IllegalArgumentException if <code>gc</code> is not from
399      * a screen device; this exception is always thrown when
400      * <code>GraphicsEnvironment.isHeadless</code> returns
401      * <code>true</code>
402      * @see java.awt.GraphicsEnvironment#isHeadless
403      * @see java.lang.SecurityManager#checkTopLevelWindow
404      * @see GraphicsConfiguration#getBounds
405      * @see #isShowing
406      * @since 1.3
407      */

408     public Window(Window JavaDoc owner, GraphicsConfiguration JavaDoc gc) {
409         this(gc);
410     ownedInit(owner);
411     }
412
413     private void ownedInit(Window JavaDoc owner) {
414     if (owner == null) {
415         throw new IllegalArgumentException JavaDoc("null owner window");
416     }
417     this.parent = owner;
418     this.weakThis = new WeakReference JavaDoc<Window JavaDoc>(this);
419     owner.addOwnedWindow(weakThis);
420         modalExcluded = owner.modalExcluded;
421     }
422
423     /**
424      * Disposes of the input methods and context, and removes
425      * this Window from the GUI hierarchy. Subclasses that override
426      * this method should call super.finalize().
427      */

428     protected void finalize() throws Throwable JavaDoc {
429         // We have to remove the (hard) reference to weakThis in the
430
// parent's ownedWindowList, otherwise the WeakReference
431
// instance that points to this Window will never get garbage
432
// collected.
433
if (parent != null) {
434         ((Window JavaDoc)parent).removeOwnedWindow(weakThis);
435     }
436     super.finalize();
437     }
438
439     /**
440      * Construct a name for this component. Called by getName() when the
441      * name is null.
442      */

443     String JavaDoc constructComponentName() {
444         synchronized (getClass()) {
445         return base + nameCounter++;
446     }
447     }
448
449     /**
450      * Makes this Window displayable by creating the connection to its
451      * native screen resource.
452      * This method is called internally by the toolkit and should
453      * not be called directly by programs.
454      * @see Component#isDisplayable
455      * @see Container#removeNotify
456      * @since JDK1.0
457      */

458     public void addNotify() {
459     synchronized (getTreeLock()) {
460         Container JavaDoc parent = this.parent;
461         if (parent != null && parent.getPeer() == null) {
462             parent.addNotify();
463         }
464
465         if (peer == null)
466         peer = getToolkit().createWindow(this);
467         super.addNotify();
468     }
469     }
470
471     /**
472      * Causes this Window to be sized to fit the preferred size
473      * and layouts of its subcomponents. If the window and/or its owner
474      * are not yet displayable, both are made displayable before
475      * calculating the preferred size. The Window will be validated
476      * after the preferredSize is calculated.
477      * @see Component#isDisplayable
478      */

479     public void pack() {
480     Container JavaDoc parent = this.parent;
481     if (parent != null && parent.getPeer() == null) {
482         parent.addNotify();
483     }
484     if (peer == null) {
485         addNotify();
486     }
487         Dimension JavaDoc newSize = getPreferredSize();
488         if (peer != null) {
489             setClientSize(newSize.width, newSize.height);
490         }
491
492         if(beforeFirstShow) {
493             isPacked = true;
494         }
495
496     validate();
497     }
498     
499     
500     void setClientSize(int w, int h) {
501         synchronized (getTreeLock()) {
502             setBoundsOp(ComponentPeer.SET_CLIENT_SIZE);
503             setBounds(x, y, w, h);
504         }
505     }
506
507     /**
508      * @deprecated As of JDK 5, replaced by
509      * {@link Component#setVisible(boolean) Component.setVisible(boolean)}.
510      */

511     @Deprecated JavaDoc
512     public void show() {
513     if (peer == null) {
514         addNotify();
515     }
516     validate();
517
518     if (visible) {
519         toFront();
520     } else {
521             beforeFirstShow = false;
522         super.show();
523             locationByPlatform = false;
524         for (int i = 0; i < ownedWindowList.size(); i++) {
525                 Window JavaDoc child = ownedWindowList.elementAt(i).get();
526                 if ((child != null) && child.showWithParent) {
527                     child.show();
528                     child.showWithParent = false;
529                 } // endif
530
} // endfor
531
if (this instanceof Frame JavaDoc || this instanceof Dialog JavaDoc) {
532                 updateChildFocusableWindowState(this);
533             }
534     }
535         
536         // If first time shown, generate WindowOpened event
537
if ((state & OPENED) == 0) {
538             postWindowEvent(WindowEvent.WINDOW_OPENED);
539             state |= OPENED;
540         }
541     }
542
543     static void updateChildFocusableWindowState(Window JavaDoc w) {
544         if (w.getPeer() != null && w.isShowing()) {
545             ((WindowPeer)w.getPeer()).updateFocusableWindowState();
546         }
547         for (int i = 0; i < w.ownedWindowList.size(); i++) {
548             Window JavaDoc child = w.ownedWindowList.elementAt(i).get();
549             if (child != null) {
550                 updateChildFocusableWindowState(child);
551             }
552         }
553     }
554
555     synchronized void postWindowEvent(int id) {
556         if (windowListener != null
557             || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0
558             || Toolkit.enabledOnToolkit(AWTEvent.WINDOW_EVENT_MASK)) {
559             WindowEvent e = new WindowEvent(this, id);
560             Toolkit.getEventQueue().postEvent(e);
561         }
562     }
563
564     /**
565      * @deprecated As of JDK 5, replaced by
566      * {@link Component#setVisible(boolean) Component.setVisible(boolean)}.
567      */

568     @Deprecated JavaDoc
569     public void hide() {
570         synchronized(ownedWindowList) {
571         for (int i = 0; i < ownedWindowList.size(); i++) {
572             Window JavaDoc child = ownedWindowList.elementAt(i).get();
573         if ((child != null) && child.visible) {
574                     child.hide();
575                     child.showWithParent = true;
576                 }
577         }
578     }
579     super.hide();
580     }
581
582     final void clearMostRecentFocusOwnerOnHide() {
583         /* do nothing */
584     }
585
586     /**
587      * Releases all of the native screen resources used by this
588      * <code>Window</code>, its subcomponents, and all of its owned
589      * children. That is, the resources for these <code>Component</code>s
590      * will be destroyed, any memory they consume will be returned to the
591      * OS, and they will be marked as undisplayable.
592      * <p>
593      * The <code>Window</code> and its subcomponents can be made displayable
594      * again by rebuilding the native resources with a subsequent call to
595      * <code>pack</code> or <code>show</code>. The states of the recreated
596      * <code>Window</code> and its subcomponents will be identical to the
597      * states of these objects at the point where the <code>Window</code>
598      * was disposed (not accounting for additional modifications between
599      * those actions).
600      * <p>
601      * <b>Note</b>: When the last displayable window
602      * within the Java virtual machine (VM) is disposed of, the VM may
603      * terminate. See <a HREF="doc-files/AWTThreadIssues.html">
604      * AWT Threading Issues</a> for more information.
605      * @see Component#isDisplayable
606      * @see #pack
607      * @see #show
608      */

609     public void dispose() {
610         doDispose();
611     }
612
613     /*
614      * Fix for 4872170.
615      * If dispose() is called on parent then its children have to be disposed as well
616      * as reported in javadoc. So we need to implement this functionality even if a
617      * child overrides dispose() in a wrong way without calling super.dispose().
618      */

619     void disposeImpl() {
620         dispose();
621         if (getPeer() != null) {
622             doDispose();
623         }
624     }
625
626     void doDispose() {
627     class DisposeAction implements Runnable JavaDoc {
628         public void run() {
629         Object JavaDoc[] ownedWindowArray;
630             synchronized(ownedWindowList) {
631         ownedWindowArray = new Object JavaDoc[ownedWindowList.size()];
632         ownedWindowList.copyInto(ownedWindowArray);
633         }
634         for (int i = 0; i < ownedWindowArray.length; i++) {
635         Window JavaDoc child = (Window JavaDoc) (((WeakReference JavaDoc)
636                    (ownedWindowArray[i])).get());
637         if (child != null) {
638             child.disposeImpl();
639         }
640         }
641             hide();
642             beforeFirstShow = true;
643             removeNotify();
644             synchronized (inputContextLock) {
645                 if (inputContext != null) {
646                     inputContext.dispose();
647                     inputContext = null;
648                 }
649             }
650             clearCurrentFocusCycleRootOnHide();
651         }
652     }
653         DisposeAction action = new DisposeAction();
654     if (EventQueue.isDispatchThread()) {
655         action.run();
656     }
657     else {
658         try {
659             EventQueue.invokeAndWait(action);
660         }
661         catch (InterruptedException JavaDoc e) {
662             System.err.println("Disposal was interrupted:");
663         e.printStackTrace();
664         }
665         catch (InvocationTargetException JavaDoc e) {
666             System.err.println("Exception during disposal:");
667         e.printStackTrace();
668         }
669     }
670     // Execute outside the Runnable because postWindowEvent is
671
// synchronized on (this). We don't need to synchronize the call
672
// on the EventQueue anyways.
673
postWindowEvent(WindowEvent.WINDOW_CLOSED);
674     }
675
676     /*
677      * Should only be called while holding the tree lock.
678      * It's overridden here because parent == owner in Window,
679      * and we shouldn't adjust counter on owner
680      */

681     void adjustListeningChildrenOnParent(long mask, int num) {
682     }
683
684     // Should only be called while holding tree lock
685
void adjustDecendantsOnParent(int num) {
686         // do nothing since parent == owner and we shouldn't
687
// ajust counter on owner
688
}
689
690     /**
691      * If this Window is visible, brings this Window to the front and may make
692      * it the focused Window.
693      * <p>
694      * Places this Window at the top of the stacking order and shows it in
695      * front of any other Windows in this VM. No action will take place if this
696      * Window is not visible. Some platforms do not allow Windows which own
697      * other Windows to appear on top of those owned Windows. Some platforms
698      * may not permit this VM to place its Windows above windows of native
699      * applications, or Windows of other VMs. This permission may depend on
700      * whether a Window in this VM is already focused. Every attempt will be
701      * made to move this Window as high as possible in the stacking order;
702      * however, developers should not assume that this method will move this
703      * Window above all other windows in every situation.
704      * <p>
705      * Because of variations in native windowing systems, no guarantees about
706      * changes to the focused and active Windows can be made. Developers must
707      * never assume that this Window is the focused or active Window until this
708      * Window receives a WINDOW_GAINED_FOCUS or WINDOW_ACTIVATED event. On
709      * platforms where the top-most window is the focused window, this method
710      * will <b>probably</b> focus this Window, if it is not already focused. On
711      * platforms where the stacking order does not typically affect the focused
712      * window, this method will <b>probably</b> leave the focused and active
713      * Windows unchanged.
714      * <p>
715      * If this method causes this Window to be focused, and this Window is a
716      * Frame or a Dialog, it will also become activated. If this Window is
717      * focused, but it is not a Frame or a Dialog, then the first Frame or
718      * Dialog that is an owner of this Window will be activated.
719      *
720      * @see #toBack
721      */

722     public void toFront() {
723         if (visible) {
724         WindowPeer peer = (WindowPeer)this.peer;
725         if (peer != null) {
726             peer.toFront();
727         }
728     }
729     }
730
731     /**
732      * If this Window is visible, sends this Window to the back and may cause
733      * it to lose focus or activation if it is the focused or active Window.
734      * <p>
735      * Places this Window at the bottom of the stacking order and shows it
736      * behind any other Windows in this VM. No action will take place is this
737      * Window is not visible. Some platforms do not allow Windows which are
738      * owned by other Windows to appear below their owners. Every attempt will
739      * be made to move this Window as low as possible in the stacking order;
740      * however, developers should not assume that this method will move this
741      * Window below all other windows in every situation.
742      * <p>
743      * Because of variations in native windowing systems, no guarantees about
744      * changes to the focused and active Windows can be made. Developers must
745      * never assume that this Window is no longer the focused or active Window
746      * until this Window receives a WINDOW_LOST_FOCUS or WINDOW_DEACTIVATED
747      * event. On platforms where the top-most window is the focused window,
748      * this method will <b>probably</b> cause this Window to lose focus. In
749      * that case, the next highest, focusable Window in this VM will receive
750      * focus. On platforms where the stacking order does not typically affect
751      * the focused window, this method will <b>probably</b> leave the focused
752      * and active Windows unchanged.
753      *
754      * @see #toFront
755      */

756     public void toBack() {
757         if (visible) {
758         WindowPeer peer = (WindowPeer)this.peer;
759         if (peer != null) {
760             peer.toBack();
761         }
762     }
763     }
764
765     /**
766      * Returns the toolkit of this frame.
767      * @return the toolkit of this window.
768      * @see Toolkit
769      * @see Toolkit#getDefaultToolkit
770      * @see Component#getToolkit
771      */

772     public Toolkit JavaDoc getToolkit() {
773     return Toolkit.getDefaultToolkit();
774     }
775
776     /**
777      * Gets the warning string that is displayed with this window.
778      * If this window is insecure, the warning string is displayed
779      * somewhere in the visible area of the window. A window is
780      * insecure if there is a security manager, and the security
781      * manager's <code>checkTopLevelWindow</code> method returns
782      * <code>false</code> when this window is passed to it as an
783      * argument.
784      * <p>
785      * If the window is secure, then <code>getWarningString</code>
786      * returns <code>null</code>. If the window is insecure, this
787      * method checks for the system property
788      * <code>awt.appletWarning</code>
789      * and returns the string value of that property.
790      * @return the warning string for this window.
791      * @see java.lang.SecurityManager#checkTopLevelWindow(java.lang.Object)
792      */

793     public final String JavaDoc getWarningString() {
794     return warningString;
795     }
796
797     private void setWarningString() {
798     warningString = null;
799     SecurityManager JavaDoc sm = System.getSecurityManager();
800     if (sm != null) {
801         if (!sm.checkTopLevelWindow(this)) {
802         // make sure the privileged action is only
803
// for getting the property! We don't want the
804
// above checkTopLevelWindow call to always succeed!
805
warningString = (String JavaDoc) AccessController.doPrivileged(
806               new GetPropertyAction("awt.appletWarning",
807                         "Java Applet Window"));
808         }
809     }
810     }
811
812     /**
813      * Gets the <code>Locale</code> object that is associated
814      * with this window, if the locale has been set.
815      * If no locale has been set, then the default locale
816      * is returned.
817      * @return the locale that is set for this window.
818      * @see java.util.Locale
819      * @since JDK1.1
820      */

821     public Locale JavaDoc getLocale() {
822       if (this.locale == null) {
823     return Locale.getDefault();
824       }
825       return this.locale;
826     }
827
828     /**
829      * Gets the input context for this window. A window always has an input context,
830      * which is shared by subcomponents unless they create and set their own.
831      * @see Component#getInputContext
832      * @since 1.2
833      */

834     public InputContext JavaDoc getInputContext() {
835         if (inputContext == null) {
836             synchronized (inputContextLock) {
837                 if (inputContext == null) {
838                     inputContext = InputContext.getInstance();
839                 }
840             }
841         }
842         return inputContext;
843     }
844
845     /**
846      * Set the cursor image to a specified cursor.
847      * @param cursor One of the constants defined
848      * by the <code>Cursor</code> class. If this parameter is null
849      * then the cursor for this window will be set to the type
850      * Cursor.DEFAULT_CURSOR.
851      * @see Component#getCursor
852      * @see Cursor
853      * @since JDK1.1
854      */

855     public void setCursor(Cursor JavaDoc cursor) {
856         if (cursor == null) {
857             cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
858         }
859         super.setCursor(cursor);
860     }
861
862     /**
863      * Returns the owner of this window.
864      * @since 1.2
865      */

866     public Window JavaDoc getOwner() {
867         return (Window JavaDoc)parent;
868     }
869
870     /**
871      * Return an array containing all the windows this
872      * window currently owns.
873      * @since 1.2
874      */

875     public Window JavaDoc[] getOwnedWindows() {
876         Window JavaDoc realCopy[];
877
878     synchronized(ownedWindowList) {
879         // Recall that ownedWindowList is actually a Vector of
880
// WeakReferences and calling get() on one of these references
881
// may return null. Make two arrays-- one the size of the
882
// Vector (fullCopy with size fullSize), and one the size of
883
// all non-null get()s (realCopy with size realSize).
884
int fullSize = ownedWindowList.size();
885         int realSize = 0;
886         Window JavaDoc fullCopy[] = new Window JavaDoc[fullSize];
887
888         for (int i = 0; i < fullSize; i++) {
889             fullCopy[realSize] = ownedWindowList.elementAt(i).get();
890
891         if (fullCopy[realSize] != null) {
892             realSize++;
893         }
894         }
895
896         if (fullSize != realSize) {
897             realCopy = new Window JavaDoc[realSize];
898         System.arraycopy(fullCopy, 0, realCopy, 0, realSize);
899         } else {
900             realCopy = fullCopy;
901         }
902     }
903
904         return realCopy;
905     }
906
907     /**
908      * Adds the specified window listener to receive window events from
909      * this window.
910      * If l is null, no exception is thrown and no action is performed.
911      *
912      * @param l the window listener
913      * @see #removeWindowListener
914      * @see #getWindowListeners
915      */

916     public synchronized void addWindowListener(WindowListener l) {
917     if (l == null) {
918         return;
919     }
920         newEventsOnly = true;
921         windowListener = AWTEventMulticaster.add(windowListener, l);
922     }
923
924     /**
925      * Adds the specified window state listener to receive window
926      * events from this window. If <code>l</code> is </code>null</code>,
927      * no exception is thrown and no action is performed.
928      *
929      * @param l the window state listener
930      * @see #removeWindowStateListener
931      * @see #getWindowStateListeners
932      * @since 1.4
933      */

934     public synchronized void addWindowStateListener(WindowStateListener l) {
935         if (l == null) {
936             return;
937         }
938         windowStateListener = AWTEventMulticaster.add(windowStateListener, l);
939         newEventsOnly = true;
940     }
941
942     /**
943      * Adds the specified window focus listener to receive window events
944      * from this window.
945      * If l is null, no exception is thrown and no action is performed.
946      *
947      * @param l the window focus listener
948      * @see #removeWindowFocusListener
949      * @see #getWindowFocusListeners
950      */

951     public synchronized void addWindowFocusListener(WindowFocusListener l) {
952         if (l == null) {
953             return;
954         }
955         windowFocusListener = AWTEventMulticaster.add(windowFocusListener, l);
956         newEventsOnly = true;
957     }
958
959     /**
960      * Removes the specified window listener so that it no longer
961      * receives window events from this window.
962      * If l is null, no exception is thrown and no action is performed.
963      *
964      * @param l the window listener
965      * @see #addWindowListener
966      * @see #getWindowListeners
967      */

968     public synchronized void removeWindowListener(WindowListener l) {
969     if (l == null) {
970         return;
971     }
972         windowListener = AWTEventMulticaster.remove(windowListener, l);
973     }
974
975     /**
976      * Removes the specified window state listener so that it no
977      * longer receives window events from this window. If
978      * <code>l</code> is <code>null</code>, no exception is thrown and
979      * no action is performed.
980      *
981      * @param l the window state listener
982      * @see #addWindowStateListener
983      * @see #getWindowStateListeners
984      * @since 1.4
985      */

986     public synchronized void removeWindowStateListener(WindowStateListener l) {
987         if (l == null) {
988             return;
989         }
990         windowStateListener = AWTEventMulticaster.remove(windowStateListener, l);
991     }
992
993     /**
994      * Removes the specified window focus listener so that it no longer
995      * receives window events from this window.
996      * If l is null, no exception is thrown and no action is performed.
997      *
998      * @param l the window focus listener
999      * @see #addWindowFocusListener
1000     * @see #getWindowFocusListeners
1001     */

1002    public synchronized void removeWindowFocusListener(WindowFocusListener l) {
1003        if (l == null) {
1004            return;
1005        }
1006        windowFocusListener = AWTEventMulticaster.remove(windowFocusListener, l);
1007    }
1008
1009    /**
1010     * Returns an array of all the window listeners
1011     * registered on this window.
1012     *
1013     * @return all of this window's <code>WindowListener</code>s
1014     * or an empty array if no window
1015     * listeners are currently registered
1016     *
1017     * @see #addWindowListener
1018     * @see #removeWindowListener
1019     * @since 1.4
1020     */

1021    public synchronized WindowListener[] getWindowListeners() {
1022        return (WindowListener[])(getListeners(WindowListener.class));
1023    }
1024
1025    /**
1026     * Returns an array of all the window focus listeners
1027     * registered on this window.
1028     *
1029     * @return all of this window's <code>WindowFocusListener</code>s
1030     * or an empty array if no window focus
1031     * listeners are currently registered
1032     *
1033     * @see #addWindowFocusListener
1034     * @see #removeWindowFocusListener
1035     * @since 1.4
1036     */

1037    public synchronized WindowFocusListener[] getWindowFocusListeners() {
1038        return (WindowFocusListener[])(getListeners(WindowFocusListener.class));
1039    }
1040
1041    /**
1042     * Returns an array of all the window state listeners
1043     * registered on this window.
1044     *
1045     * @return all of this window's <code>WindowStateListener</code>s
1046     * or an empty array if no window state
1047     * listeners are currently registered
1048     *
1049     * @see #addWindowStateListener
1050     * @see #removeWindowStateListener
1051     * @since 1.4
1052     */

1053    public synchronized WindowStateListener[] getWindowStateListeners() {
1054        return (WindowStateListener[])(getListeners(WindowStateListener.class));
1055    }
1056
1057
1058    /**
1059     * Returns an array of all the objects currently registered
1060     * as <code><em>Foo</em>Listener</code>s
1061     * upon this <code>Window</code>.
1062     * <code><em>Foo</em>Listener</code>s are registered using the
1063     * <code>add<em>Foo</em>Listener</code> method.
1064     *
1065     * <p>
1066     *
1067     * You can specify the <code>listenerType</code> argument
1068     * with a class literal, such as
1069     * <code><em>Foo</em>Listener.class</code>.
1070     * For example, you can query a
1071     * <code>Window</code> <code>w</code>
1072     * for its window listeners with the following code:
1073     *
1074     * <pre>WindowListener[] wls = (WindowListener[])(w.getListeners(WindowListener.class));</pre>
1075     *
1076     * If no such listeners exist, this method returns an empty array.
1077     *
1078     * @param listenerType the type of listeners requested; this parameter
1079     * should specify an interface that descends from
1080     * <code>java.util.EventListener</code>
1081     * @return an array of all objects registered as
1082     * <code><em>Foo</em>Listener</code>s on this window,
1083     * or an empty array if no such
1084     * listeners have been added
1085     * @exception ClassCastException if <code>listenerType</code>
1086     * doesn't specify a class or interface that implements
1087     * <code>java.util.EventListener</code>
1088     *
1089     * @see #getWindowListeners
1090     * @since 1.3
1091     */

1092    public <T extends EventListener JavaDoc> T[] getListeners(Class JavaDoc<T> listenerType) {
1093    EventListener JavaDoc l = null;
1094        if (listenerType == WindowFocusListener.class) {
1095            l = windowFocusListener;
1096    } else if (listenerType == WindowStateListener.class) {
1097            l = windowStateListener;
1098        } else if (listenerType == WindowListener.class) {
1099            l = windowListener;
1100        } else {
1101            return super.getListeners(listenerType);
1102        }
1103        return AWTEventMulticaster.getListeners(l, listenerType);
1104    }
1105
1106    // REMIND: remove when filtering is handled at lower level
1107
boolean eventEnabled(AWTEvent JavaDoc e) {
1108        switch(e.id) {
1109          case WindowEvent.WINDOW_OPENED:
1110          case WindowEvent.WINDOW_CLOSING:
1111          case WindowEvent.WINDOW_CLOSED:
1112          case WindowEvent.WINDOW_ICONIFIED:
1113          case WindowEvent.WINDOW_DEICONIFIED:
1114          case WindowEvent.WINDOW_ACTIVATED:
1115          case WindowEvent.WINDOW_DEACTIVATED:
1116            if ((eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0 ||
1117                windowListener != null) {
1118                return true;
1119            }
1120            return false;
1121          case WindowEvent.WINDOW_GAINED_FOCUS:
1122          case WindowEvent.WINDOW_LOST_FOCUS:
1123            if ((eventMask & AWTEvent.WINDOW_FOCUS_EVENT_MASK) != 0 ||
1124                windowFocusListener != null) {
1125                return true;
1126            }
1127            return false;
1128          case WindowEvent.WINDOW_STATE_CHANGED:
1129            if ((eventMask & AWTEvent.WINDOW_STATE_EVENT_MASK) != 0 ||
1130                windowStateListener != null) {
1131                return true;
1132            }
1133            return false;
1134          default:
1135            break;
1136        }
1137        return super.eventEnabled(e);
1138    }
1139
1140    /**
1141     * Processes events on this window. If the event is an
1142     * <code>WindowEvent</code>, it invokes the
1143     * <code>processWindowEvent</code> method, else it invokes its
1144     * superclass's <code>processEvent</code>.
1145     * <p>Note that if the event parameter is <code>null</code>
1146     * the behavior is unspecified and may result in an
1147     * exception.
1148     *
1149     * @param e the event
1150     */

1151    protected void processEvent(AWTEvent JavaDoc e) {
1152        if (e instanceof WindowEvent) {
1153            switch (e.getID()) {
1154                case WindowEvent.WINDOW_OPENED:
1155                case WindowEvent.WINDOW_CLOSING:
1156                case WindowEvent.WINDOW_CLOSED:
1157                case WindowEvent.WINDOW_ICONIFIED:
1158                case WindowEvent.WINDOW_DEICONIFIED:
1159                case WindowEvent.WINDOW_ACTIVATED:
1160                case WindowEvent.WINDOW_DEACTIVATED:
1161                    processWindowEvent((WindowEvent)e);
1162                    break;
1163                case WindowEvent.WINDOW_GAINED_FOCUS:
1164                case WindowEvent.WINDOW_LOST_FOCUS:
1165                    processWindowFocusEvent((WindowEvent)e);
1166                    break;
1167                case WindowEvent.WINDOW_STATE_CHANGED:
1168            processWindowStateEvent((WindowEvent)e);
1169                default:
1170                    break;
1171            }
1172            return;
1173        }
1174    super.processEvent(e);
1175    }
1176
1177    /**
1178     * Processes window events occurring on this window by
1179     * dispatching them to any registered WindowListener objects.
1180     * NOTE: This method will not be called unless window events
1181     * are enabled for this component; this happens when one of the
1182     * following occurs:
1183     * <ul>
1184     * <li>A WindowListener object is registered via
1185     * <code>addWindowListener</code>
1186     * <li>Window events are enabled via <code>enableEvents</code>
1187     * </ul>
1188     * <p>Note that if the event parameter is <code>null</code>
1189     * the behavior is unspecified and may result in an
1190     * exception.
1191     *
1192     * @param e the window event
1193     * @see Component#enableEvents
1194     */

1195    protected void processWindowEvent(WindowEvent e) {
1196        WindowListener listener = windowListener;
1197        if (listener != null) {
1198            switch(e.getID()) {
1199                case WindowEvent.WINDOW_OPENED:
1200                    listener.windowOpened(e);
1201                    break;
1202                case WindowEvent.WINDOW_CLOSING:
1203                    listener.windowClosing(e);
1204                    break;
1205                case WindowEvent.WINDOW_CLOSED:
1206                    listener.windowClosed(e);
1207                    break;
1208                case WindowEvent.WINDOW_ICONIFIED:
1209                    listener.windowIconified(e);
1210                    break;
1211                case WindowEvent.WINDOW_DEICONIFIED:
1212                    listener.windowDeiconified(e);
1213                    break;
1214                case WindowEvent.WINDOW_ACTIVATED:
1215                    listener.windowActivated(e);
1216                    break;
1217                case WindowEvent.WINDOW_DEACTIVATED:
1218                    listener.windowDeactivated(e);
1219                    break;
1220                default:
1221                    break;
1222            }
1223        }
1224    }
1225
1226    /**
1227     * Processes window focus event occuring on this window by
1228     * dispatching them to any registered WindowFocusListener objects.
1229     * NOTE: this method will not be called unless window focus events
1230     * are enabled for this window. This happens when one of the
1231     * following occurs:
1232     * <ul>
1233     * <li>a WindowFocusListener is registered via
1234     * <code>addWindowFocusListener</code>
1235     * <li>Window focus events are enabled via <code>enableEvents</code>
1236     * </ul>
1237     * <p>Note that if the event parameter is <code>null</code>
1238     * the behavior is unspecified and may result in an
1239     * exception.
1240     *
1241     * @param e the window focus event
1242     * @see Component#enableEvents
1243     */

1244    protected void processWindowFocusEvent(WindowEvent e) {
1245        WindowFocusListener listener = windowFocusListener;
1246        if (listener != null) {
1247            switch (e.getID()) {
1248                case WindowEvent.WINDOW_GAINED_FOCUS:
1249                    listener.windowGainedFocus(e);
1250                    break;
1251                case WindowEvent.WINDOW_LOST_FOCUS:
1252                    listener.windowLostFocus(e);
1253                    break;
1254                default:
1255                    break;
1256            }
1257        }
1258    }
1259
1260    /**
1261     * Processes window state event occuring on this window by
1262     * dispatching them to any registered <code>WindowStateListener</code>
1263     * objects.
1264     * NOTE: this method will not be called unless window state events
1265     * are enabled for this window. This happens when one of the
1266     * following occurs:
1267     * <ul>
1268     * <li>a <code>WindowStateListener</code> is registered via
1269     * <code>addWindowStateListener</code>
1270     * <li>window state events are enabled via <code>enableEvents</code>
1271     * </ul>
1272     * <p>Note that if the event parameter is <code>null</code>
1273     * the behavior is unspecified and may result in an
1274     * exception.
1275     *
1276     * @param e the window state event
1277     * @see java.awt.Component#enableEvents
1278     * @since 1.4
1279     */

1280    protected void processWindowStateEvent(WindowEvent e) {
1281        WindowStateListener listener = windowStateListener;
1282    if (listener != null) {
1283        switch (e.getID()) {
1284        case WindowEvent.WINDOW_STATE_CHANGED:
1285            listener.windowStateChanged(e);
1286            break;
1287        default:
1288            break;
1289        }
1290    }
1291    }
1292
1293    /**
1294     * Implements a debugging hook -- checks to see if
1295     * the user has typed <i>control-shift-F1</i>. If so,
1296     * the list of child windows is dumped to <code>System.out</code>.
1297     * @param e the keyboard event
1298     */

1299    void preProcessKeyEvent(KeyEvent e) {
1300        // Dump the list of child windows to System.out.
1301
if (e.isActionKey() && e.getKeyCode() == KeyEvent.VK_F1 &&
1302            e.isControlDown() && e.isShiftDown() &&
1303            e.getID() == KeyEvent.KEY_PRESSED) {
1304            list(System.out, 0);
1305        }
1306    }
1307
1308    void postProcessKeyEvent(KeyEvent e) {
1309    // Do nothing
1310
}
1311  
1312    /**
1313     * Changes the always-on-top window state. An always-on-top window
1314     * is a window that stays above all other windows except maybe
1315     * other always-on-top windows. If there are several always-on-top
1316     * windows the order in which they stay relative to each other is
1317     * not specified and is platform dependent.
1318     * <p>
1319     * If some other window already is always-on-top then the
1320     * relative order between these windows is unspecified (depends on
1321     * platform). No window can be brought to be over always-on-top
1322     * window except maybe another always-on-top window.
1323     * <p>
1324     * All owned windows of an always-on-top window automatically
1325     * become always-on-top windows. If a window ceases to be
1326     * always-on-top its owned windows cease to be always-on-top.
1327     * <p> When an always-on-top window is sent {@link #toBack toBack}
1328     * its always-on-top state is set to <code>false</code>.
1329     * <p>
1330     * This method makes the window always-on-top if
1331     * <code>alwaysOnTop</code> is <code>true</code>. If the window is
1332     * visible, this includes bringing window <code>toFront</code>, then
1333     * "sticking" it to the top-most position. If the window is not
1334     * visible it does nothing other than setting the always-on-top
1335     * property. If later the window is shown, it will be always-on-top.
1336     * If the Window is already always-on-top, this call does nothing.
1337     * <p>
1338     * If <code>alwaysOnTop</code> is <code>false</code> this
1339     * method changes the state from always-on-top to normal. The window
1340     * remains top-most but its z-order can be changed in the normal way
1341     * as for any other window. Does nothing if this Window is not
1342     * always-on-top. Has no effect on relative z-order of windows if
1343     * there are no other always-on-top windows.
1344     * <p>
1345     * <b>Note</b>: some platforms might not support always-on-top
1346     * windows. There is no public API to detect if the platform
1347     * supports always-on-top at runtime.
1348     * <p>
1349     * If a SecurityManager is installed, the calling thread must be
1350     * granted the AWTPermission "setWindowAlwaysOnTop" in
1351     * order to set the value of this property. If this
1352     * permission is not granted, this method will throw a
1353     * SecurityException, and the current value of the property will
1354     * be left unchanged.
1355     *
1356     * @param alwaysOnTop new value of always-on-top state of the
1357     * window
1358     * @throws SecurityException if the calling thread does not have permission
1359     * to set the value of always-on-top property
1360     * @see #isAlwaysOnTop
1361     * @see #toFront
1362     * @see #toBack
1363     * @see AWTPermission
1364     * @since 1.5
1365     */

1366    public final void setAlwaysOnTop(boolean alwaysOnTop)
1367      throws SecurityException JavaDoc
1368    {
1369        SecurityManager JavaDoc security = System.getSecurityManager();
1370        if (security != null) {
1371            security.checkPermission(SecurityConstants.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION);
1372        }
1373
1374        boolean oldAlwaysOnTop;
1375        synchronized(this) {
1376            oldAlwaysOnTop = this.alwaysOnTop;
1377            this.alwaysOnTop = alwaysOnTop;
1378        }
1379        if (oldAlwaysOnTop != alwaysOnTop ) {
1380            WindowPeer peer = (WindowPeer)this.peer;
1381            synchronized(getTreeLock()) {
1382                if (peer != null) {
1383                    peer.updateAlwaysOnTop();
1384                }
1385            }
1386            firePropertyChange("alwaysOnTop", oldAlwaysOnTop, alwaysOnTop);
1387        }
1388    }
1389
1390    /**
1391     * Returns whether this window is an always-on-top window.
1392     * @return <code>true</code>, if the window is in always-on-top state,
1393     * <code>false</code> otherwise
1394     * @see #setAlwaysOnTop
1395     * @since 1.5
1396     */

1397    public final boolean isAlwaysOnTop() {
1398        return alwaysOnTop;
1399    }
1400
1401
1402    /**
1403     * Returns the child Component of this Window that has focus if this Window
1404     * is focused; returns null otherwise.
1405     *
1406     * @return the child Component with focus, or null if this Window is not
1407     * focused
1408     * @see #getMostRecentFocusOwner
1409     * @see #isFocused
1410     */

1411    public Component JavaDoc getFocusOwner() {
1412    return (isFocused())
1413        ? KeyboardFocusManager.getCurrentKeyboardFocusManager().
1414              getFocusOwner()
1415        : null;
1416    }
1417 
1418    /**
1419     * Returns the child Component of this Window that will receive the focus
1420     * when this Window is focused. If this Window is currently focused, this
1421     * method returns the same Component as <code>getFocusOwner()</code>. If
1422     * this Window is not focused, then the child Component that most recently
1423     * requested focus will be returned. If no child Component has ever
1424     * requested focus, and this is a focusable Window, then this Window's
1425     * initial focusable Component is returned. If no child Component has ever
1426     * requested focus, and this is a non-focusable Window, null is returned.
1427     *
1428     * @return the child Component that will receive focus when this Window is
1429     * focused
1430     * @see #getFocusOwner
1431     * @see #isFocused
1432     * @see #isFocusableWindow
1433     * @since 1.4
1434     */

1435    public Component JavaDoc getMostRecentFocusOwner() {
1436    if (isFocused()) {
1437        return getFocusOwner();
1438    } else {
1439        Component JavaDoc mostRecent =
1440        KeyboardFocusManager.getMostRecentFocusOwner(this);
1441        if (mostRecent != null) {
1442        return mostRecent;
1443        } else {
1444        return (isFocusableWindow())
1445            ? getFocusTraversalPolicy().getInitialComponent(this)
1446            : null;
1447        }
1448        }
1449    }
1450  
1451    /**
1452     * Returns whether this Window is active. Only a Frame or a Dialog may be
1453     * active. The native windowing system may denote the active Window or its
1454     * children with special decorations, such as a highlighted title bar. The
1455     * active Window is always either the focused Window, or the first Frame or
1456     * Dialog that is an owner of the focused Window.
1457     *
1458     * @return whether this is the active Window.
1459     * @see #isFocused
1460     * @since 1.4
1461     */

1462    public boolean isActive() {
1463    return (KeyboardFocusManager.getCurrentKeyboardFocusManager().
1464        getActiveWindow() == this);
1465    }
1466  
1467    /**
1468     * Returns whether this Window is focused. If there exists a focus owner,
1469     * the focused Window is the Window that is, or contains, that focus owner.
1470     * If there is no focus owner, then no Window is focused.
1471     * <p>
1472     * If the focused Window is a Frame or a Dialog it is also the active
1473     * Window. Otherwise, the active Window is the first Frame or Dialog that
1474     * is an owner of the focused Window.
1475     *
1476     * @return whether this is the focused Window.
1477     * @see #isActive
1478     * @since 1.4
1479     */

1480    public boolean isFocused() {
1481    return (KeyboardFocusManager.getCurrentKeyboardFocusManager().
1482        getGlobalFocusedWindow() == this);
1483    }
1484  
1485    /**
1486     * Gets a focus traversal key for this Window. (See <code>
1487     * setFocusTraversalKeys</code> for a full description of each key.)
1488     * <p>
1489     * If the traversal key has not been explicitly set for this Window,
1490     * then this Window's parent's traversal key is returned. If the
1491     * traversal key has not been explicitly set for any of this Window's
1492     * ancestors, then the current KeyboardFocusManager's default traversal key
1493     * is returned.
1494     *
1495     * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
1496     * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
1497     * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
1498     * KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
1499     * @return the AWTKeyStroke for the specified key
1500     * @see Container#setFocusTraversalKeys
1501     * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
1502     * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
1503     * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
1504     * @see KeyboardFocusManager#DOWN_CYCLE_TRAVERSAL_KEYS
1505     * @throws IllegalArgumentException if id is not one of
1506     * KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
1507     * KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
1508     * KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
1509     * KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
1510     * @since 1.4
1511     */

1512    public Set JavaDoc<AWTKeyStroke JavaDoc> getFocusTraversalKeys(int id) {
1513    if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) {
1514        throw new IllegalArgumentException JavaDoc("invalid focus traversal key identifier");
1515    }
1516  
1517        // Okay to return Set directly because it is an unmodifiable view
1518
Set JavaDoc keystrokes = (focusTraversalKeys != null)
1519            ? focusTraversalKeys[id]
1520            : null;
1521
1522    if (keystrokes != null) {
1523        return keystrokes;
1524    } else {
1525        return KeyboardFocusManager.getCurrentKeyboardFocusManager().
1526        getDefaultFocusTraversalKeys(id);
1527    }
1528    }
1529  
1530    /**
1531     * Does nothing because Windows must always be roots of a focus traversal
1532     * cycle. The passed-in value is ignored.
1533     *
1534     * @param focusCycleRoot this value is ignored
1535     * @see #isFocusCycleRoot
1536     * @see Container#setFocusTraversalPolicy
1537     * @see Container#getFocusTraversalPolicy
1538     * @since 1.4
1539     */

1540    public final void setFocusCycleRoot(boolean focusCycleRoot) {
1541    }
1542  
1543    /**
1544     * Always returns <code>true</code> because all Windows must be roots of a
1545     * focus traversal cycle.
1546     *
1547     * @return <code>true</code>
1548     * @see #setFocusCycleRoot
1549     * @see Container#setFocusTraversalPolicy
1550     * @see Container#getFocusTraversalPolicy
1551     * @since 1.4
1552     */

1553    public final boolean isFocusCycleRoot() {
1554    return true;
1555    }
1556  
1557    /**
1558     * Always returns <code>null</code> because Windows have no ancestors; they
1559     * represent the top of the Component hierarchy.
1560     *
1561     * @return <code>null</code>
1562     * @see Container#isFocusCycleRoot()
1563     * @since 1.4
1564     */

1565    public final Container JavaDoc getFocusCycleRootAncestor() {
1566    return null;
1567    }
1568 
1569    /**
1570     * Returns whether this Window can become the focused Window, that is,
1571     * whether this Window or any of its subcomponents can become the focus
1572     * owner. For a Frame or Dialog to be focusable, its focusable Window state
1573     * must be set to <code>true</code>. For a Window which is not a Frame or
1574     * Dialog to be focusable, its focusable Window state must be set to
1575     * <code>true</code>, its nearest owning Frame or Dialog must be
1576     * showing on the screen, and it must contain at least one Component in
1577     * its focus traversal cycle. If any of these conditions is not met, then
1578     * neither this Window nor any of its subcomponents can become the focus
1579     * owner.
1580     *
1581     * @return <code>true</code> if this Window can be the focused Window;
1582     * <code>false</code> otherwise
1583     * @see #getFocusableWindowState
1584     * @see #setFocusableWindowState
1585     * @see #isShowing
1586     * @see Component#isFocusable
1587     * @since 1.4
1588     */

1589    public final boolean isFocusableWindow() {
1590        // If a Window/Frame/Dialog was made non-focusable, then it is always
1591
// non-focusable.
1592
if (!getFocusableWindowState()) {
1593            return false;
1594        }
1595
1596        // All other tests apply only to Windows.
1597
if (this instanceof Frame JavaDoc || this instanceof Dialog JavaDoc) {
1598            return true;
1599        }
1600
1601        // A Window must have at least one Component in its root focus
1602
// traversal cycle to be focusable.
1603
if (getFocusTraversalPolicy().getDefaultComponent(this) == null) {
1604            return false;
1605        }
1606
1607        // A Window's nearest owning Frame or Dialog must be showing on the
1608
// screen.
1609
for (Window JavaDoc owner = getOwner(); owner != null;
1610             owner = owner.getOwner())
1611        {
1612            if (owner instanceof Frame JavaDoc || owner instanceof Dialog JavaDoc) {
1613                return owner.isShowing();
1614            }
1615        }
1616
1617        return false;
1618    }
1619
1620    /**
1621     * Returns whether this Window can become the focused Window if it meets
1622     * the other requirements outlined in <code>isFocusableWindow</code>. If
1623     * this method returns <code>false</code>, then
1624     * <code>isFocusableWindow</code> will return <code>false</code> as well.
1625     * If this method returns <code>true</code>, then
1626     * <code>isFocusableWindow</code> may return <code>true</code> or
1627     * <code>false</code> depending upon the other requirements which must be
1628     * met in order for a Window to be focusable.
1629     * <p>
1630     * By default, all Windows have a focusable Window state of
1631     * <code>true</code>.
1632     *
1633     * @return whether this Window can be the focused Window
1634     * @see #isFocusableWindow
1635     * @see #setFocusableWindowState
1636     * @see #isShowing
1637     * @see Component#setFocusable
1638     * @since 1.4
1639     */

1640    public boolean getFocusableWindowState() {
1641        return focusableWindowState;
1642    }
1643
1644    /**
1645     * Sets whether this Window can become the focused Window if it meets
1646     * the other requirements outlined in <code>isFocusableWindow</code>. If
1647     * this Window's focusable Window state is set to <code>false</code>, then
1648     * <code>isFocusableWindow</code> will return <code>false</code>. If this
1649     * Window's focusable Window state is set to <code>true</code>, then
1650     * <code>isFocusableWindow</code> may return <code>true</code> or
1651     * <code>false</code> depending upon the other requirements which must be
1652     * met in order for a Window to be focusable.
1653     * <p>
1654     * Setting a Window's focusability state to <code>false</code> is the
1655     * standard mechanism for an application to identify to the AWT a Window
1656     * which will be used as a floating palette or toolbar, and thus should be
1657     * a non-focusable Window.
1658     *
1659     * Setting the focusability state on a visible <code>Window</code>
1660     * can have a delayed effect on some platforms &#151; the actual
1661     * change may happen only when the <code>Window</code> becomes
1662     * hidden and then visible again. To ensure consistent behavior
1663     * across platforms, set the <code>Window</code>'s focusable state
1664     * when the <code>WIndow</code> is invisible and then show it.
1665     *
1666     * @param focusableWindowState whether this Window can be the focused
1667     * Window
1668     * @see #isFocusableWindow
1669     * @see #getFocusableWindowState
1670     * @see #isShowing
1671     * @see Component#setFocusable
1672     * @since 1.4
1673     */

1674    public void setFocusableWindowState(boolean focusableWindowState) {
1675    boolean oldFocusableWindowState;
1676    synchronized (this) {
1677        oldFocusableWindowState = this.focusableWindowState;
1678        this.focusableWindowState = focusableWindowState;
1679    }
1680        WindowPeer peer = (WindowPeer)this.peer;
1681        if (peer != null) {
1682            peer.updateFocusableWindowState();
1683        }
1684    firePropertyChange("focusableWindowState", oldFocusableWindowState,
1685               focusableWindowState);
1686    if (oldFocusableWindowState && !focusableWindowState && isFocused()) {
1687        for (Window JavaDoc owner = (Window JavaDoc)getParent();
1688         owner != null;
1689         owner = (Window JavaDoc)owner.getParent())
1690        {
1691            Component JavaDoc toFocus =
1692            KeyboardFocusManager.getMostRecentFocusOwner(owner);
1693            if (toFocus != null && toFocus.requestFocus(false)) {
1694            return;
1695            }
1696        }
1697        KeyboardFocusManager.getCurrentKeyboardFocusManager().
1698        clearGlobalFocusOwner();
1699    }
1700    }
1701 
1702    /**
1703     * Adds a PropertyChangeListener to the listener list. The listener is
1704     * registered for all bound properties of this class, including the
1705     * following:
1706     * <ul>
1707     * <li>this Window's font ("font")</li>
1708     * <li>this Window's background color ("background")</li>
1709     * <li>this Window's foreground color ("foreground")</li>
1710     * <li>this Window's focusability ("focusable")</li>
1711     * <li>this Window's focus traversal keys enabled state
1712     * ("focusTraversalKeysEnabled")</li>
1713     * <li>this Window's Set of FORWARD_TRAVERSAL_KEYS
1714     * ("forwardFocusTraversalKeys")</li>
1715     * <li>this Window's Set of BACKWARD_TRAVERSAL_KEYS
1716     * ("backwardFocusTraversalKeys")</li>
1717     * <li>this Window's Set of UP_CYCLE_TRAVERSAL_KEYS
1718     * ("upCycleFocusTraversalKeys")</li>
1719     * <li>this Window's Set of DOWN_CYCLE_TRAVERSAL_KEYS
1720     * ("downCycleFocusTraversalKeys")</li>
1721     * <li>this Window's focus traversal policy ("focusTraversalPolicy")
1722     * </li>
1723     * <li>this Window's focusable Window state ("focusableWindowState")
1724     * </li>
1725     * <li>this Window's always-on-top state("alwaysOnTop")</li>
1726     * </ul>
1727     * Note that if this Window is inheriting a bound property, then no
1728     * event will be fired in response to a change in the inherited property.
1729     * <p>
1730     * If listener is null, no exception is thrown and no action is performed.
1731     *
1732     * @param listener the PropertyChangeListener to be added
1733     *
1734     * @see Component#removePropertyChangeListener
1735     * @see #addPropertyChangeListener(java.lang.String,java.beans.PropertyChangeListener)
1736     */

1737    public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
1738    super.addPropertyChangeListener(listener);
1739    }
1740 
1741    /**
1742     * Adds a PropertyChangeListener to the listener list for a specific
1743     * property. The specified property may be user-defined, or one of the
1744     * following:
1745     * <ul>
1746     * <li>this Window's font ("font")</li>
1747     * <li>this Window's background color ("background")</li>
1748     * <li>this Window's foreground color ("foreground")</li>
1749     * <li>this Window's focusability ("focusable")</li>
1750     * <li>this Window's focus traversal keys enabled state
1751     * ("focusTraversalKeysEnabled")</li>
1752     * <li>this Window's Set of FORWARD_TRAVERSAL_KEYS
1753     * ("forwardFocusTraversalKeys")</li>
1754     * <li>this Window's Set of BACKWARD_TRAVERSAL_KEYS
1755     * ("backwardFocusTraversalKeys")</li>
1756     * <li>this Window's Set of UP_CYCLE_TRAVERSAL_KEYS
1757     * ("upCycleFocusTraversalKeys")</li>
1758     * <li>this Window's Set of DOWN_CYCLE_TRAVERSAL_KEYS
1759     * ("downCycleFocusTraversalKeys")</li>
1760     * <li>this Window's focus traversal policy ("focusTraversalPolicy")
1761     * </li>
1762     * <li>this Window's focusable Window state ("focusableWindowState")
1763     * </li>
1764     * <li>this Window's always-on-top state("alwaysOnTop")</li>
1765     * </ul>
1766     * Note that if this Window is inheriting a bound property, then no
1767     * event will be fired in response to a change in the inherited property.
1768     * <p>
1769     * If listener is null, no exception is thrown and no action is performed.
1770     *
1771     * @param propertyName one of the property names listed above
1772     * @param listener the PropertyChangeListener to be added
1773     *
1774     * @see #addPropertyChangeListener(java.beans.PropertyChangeListener)
1775     * @see Component#removePropertyChangeListener
1776     */

1777    public void addPropertyChangeListener(String JavaDoc propertyName,
1778                      PropertyChangeListener JavaDoc listener) {
1779    super.addPropertyChangeListener(propertyName, listener);
1780    }
1781 
1782    /**
1783     * Dispatches an event to this window or one of its sub components.
1784     * @param e the event
1785     */

1786    void dispatchEventImpl(AWTEvent JavaDoc e) {
1787    if (e.getID() == ComponentEvent.COMPONENT_RESIZED) {
1788        invalidate();
1789        validate();
1790    }
1791    super.dispatchEventImpl(e);
1792    }
1793  
1794    /**
1795     * @deprecated As of JDK version 1.1
1796     * replaced by <code>dispatchEvent(AWTEvent)</code>.
1797     */

1798    @Deprecated JavaDoc
1799    public boolean postEvent(Event JavaDoc e) {
1800        if (handleEvent(e)) {
1801            e.consume();
1802            return true;
1803        }
1804        return false;
1805    }
1806
1807    /**
1808     * Checks if this Window is showing on screen.
1809     * @see Component#setVisible
1810    */

1811    public boolean isShowing() {
1812    return visible;
1813    }
1814
1815    /**
1816     * @deprecated As of J2SE 1.4, replaced by
1817     * {@link Component#applyComponentOrientation Component.applyComponentOrientation}.
1818     */

1819    @Deprecated JavaDoc
1820    public void applyResourceBundle(ResourceBundle JavaDoc rb) {
1821        applyComponentOrientation(ComponentOrientation.getOrientation(rb));
1822    }
1823    
1824    /**
1825     * @deprecated As of J2SE 1.4, replaced by
1826     * {@link Component#applyComponentOrientation Component.applyComponentOrientation}.
1827     */

1828    @Deprecated JavaDoc
1829    public void applyResourceBundle(String JavaDoc rbName) {
1830        applyResourceBundle(ResourceBundle.getBundle(rbName));
1831    }
1832
1833   /*
1834    * Support for tracking all windows owned by this window
1835    */

1836    void addOwnedWindow(WeakReference JavaDoc weakWindow) {
1837        if (weakWindow != null) {
1838        synchronized(ownedWindowList) {
1839            // this if statement should really be an assert, but we don't
1840
// have asserts...
1841
if (!ownedWindowList.contains(weakWindow)) {
1842            ownedWindowList.addElement(weakWindow);
1843        }
1844        }
1845    }
1846    }
1847
1848    void removeOwnedWindow(WeakReference JavaDoc weakWindow) {
1849        if (weakWindow != null) {
1850        // synchronized block not required since removeElement is
1851
// already synchronized
1852
ownedWindowList.removeElement(weakWindow);
1853    }
1854    }
1855
1856    void connectOwnedWindow(Window JavaDoc child) {
1857        WeakReference JavaDoc weakChild = new WeakReference JavaDoc(child);
1858    child.weakThis = weakChild;
1859    child.parent = this;
1860    addOwnedWindow(weakChild);
1861    }
1862    
1863    /**
1864     * The window serialized data version.
1865     *
1866     * @serial
1867     */

1868    private int windowSerializedDataVersion = 2;
1869
1870    /**
1871     * Writes default serializable fields to stream. Writes
1872     * a list of serializable <code>WindowListener</code>s and
1873     * <code>WindowFocusListener</code>s as optional data.
1874     * Writes a list of child windows as optional data.
1875     *
1876     * @param s the <code>ObjectOutputStream</code> to write
1877     * @serialData <code>null</code> terminated sequence of
1878     * 0 or more pairs; the pair consists of a <code>String</code>
1879     * and and <code>Object</code>; the <code>String</code>
1880     * indicates the type of object and is one of the following:
1881     * <code>windowListenerK</code> indicating a
1882     * <code>WindowListener</code> object;
1883     * <code>windowFocusWindowK</code> indicating a
1884     * <code>WindowFocusListener</code> object;
1885     * <code>ownedWindowK</code> indicating a child
1886     * <code>Window</code> object
1887     *
1888     * @see AWTEventMulticaster#save(java.io.ObjectOutputStream, java.lang.String, java.util.EventListener)
1889     * @see Component#windowListenerK
1890     * @see Component#windowFocusListenerK
1891     * @see Component#ownedWindowK
1892     * @see #readObject(ObjectInputStream)
1893     */

1894    private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
1895        synchronized (this) {
1896        // Update old focusMgr fields so that our object stream can be read
1897
// by previous releases
1898
focusMgr = new FocusManager();
1899        focusMgr.focusRoot = this;
1900        focusMgr.focusOwner = getMostRecentFocusOwner();
1901
1902        s.defaultWriteObject();
1903
1904        // Clear fields so that we don't keep extra references around
1905
focusMgr = null;
1906
1907        AWTEventMulticaster.save(s, windowListenerK, windowListener);
1908            AWTEventMulticaster.save(s, windowFocusListenerK, windowFocusListener);
1909            AWTEventMulticaster.save(s, windowStateListenerK, windowStateListener);
1910    }
1911
1912    s.writeObject(null);
1913
1914    synchronized (ownedWindowList) {
1915        for (int i = 0; i < ownedWindowList.size(); i++) {
1916            Window JavaDoc child = ownedWindowList.elementAt(i).get();
1917        if (child != null) {
1918            s.writeObject(ownedWindowK);
1919            s.writeObject(child);
1920        }
1921        }
1922    }
1923    s.writeObject(null);
1924    }
1925
1926    /**
1927     * Reads the <code>ObjectInputStream</code> and an optional
1928     * list of listeners to receive various events fired by
1929     * the component; also reads a list of
1930     * (possibly <code>null</code>) child windows.
1931     * Unrecognized keys or values will be ignored.
1932     *
1933     * @param s the <code>ObjectInputStream</code> to read
1934     * @exception HeadlessException if
1935     * <code>GraphicsEnvironment.isHeadless</code> returns
1936     * <code>true</code>
1937     * @see java.awt.GraphicsEnvironment#isHeadless
1938     * @see #writeObject
1939     */

1940    private void readObject(ObjectInputStream JavaDoc s)
1941      throws ClassNotFoundException JavaDoc, IOException JavaDoc, HeadlessException JavaDoc
1942    {
1943        GraphicsEnvironment.checkHeadless();
1944        setWarningString();
1945        inputContextLock = new Object JavaDoc();
1946        
1947        // Deserialized Windows are not yet visible.
1948
visible = false;
1949        weakThis = new WeakReference JavaDoc(this);
1950        ObjectInputStream.GetField JavaDoc f = s.readFields();
1951        
1952        syncLWRequests = f.get("syncLWRequests", systemSyncLWRequests);
1953        state = f.get("state", 0);
1954        focusableWindowState = f.get("focusableWindowState", true);
1955        windowSerializedDataVersion = f.get("windowSerializedDataVersion", 1);
1956        locationByPlatform = f.get("locationByPlatform", locationByPlatformProp);
1957        // Note: 1.4 (or later) doesn't use focusMgr
1958
focusMgr = (FocusManager)f.get("focusMgr", null);
1959        boolean aot = f.get("alwaysOnTop", false);
1960        if(aot) {
1961            setAlwaysOnTop(aot); // since 1.5; subject to permission check
1962
}
1963
1964        ownedWindowList = new Vector JavaDoc();
1965      if (windowSerializedDataVersion < 2) {
1966          // Translate old-style focus tracking to new model. For 1.4 and
1967
// later releases, we'll rely on the Window's initial focusable
1968
// Component.
1969
if (focusMgr != null) {
1970              if (focusMgr.focusOwner != null) {
1971                  KeyboardFocusManager.
1972                      setMostRecentFocusOwner(this, focusMgr.focusOwner);
1973              }
1974          }
1975  
1976          // This field is non-transient and relies on default serialization.
1977
// However, the default value is insufficient, so we need to set
1978
// it explicitly for object data streams prior to 1.4.
1979
focusableWindowState = true;
1980      }
1981  
1982      Object JavaDoc keyOrNull;
1983      while(null != (keyOrNull = s.readObject())) {
1984          String JavaDoc key = ((String JavaDoc)keyOrNull).intern();
1985
1986          if (windowListenerK == key) {
1987              addWindowListener((WindowListener)(s.readObject()));
1988          } else if (windowFocusListenerK == key) {
1989              addWindowFocusListener((WindowFocusListener)(s.readObject()));
1990          } else if (windowStateListenerK == key) {
1991              addWindowStateListener((WindowStateListener)(s.readObject()));
1992          } else // skip value for unrecognized key
1993
s.readObject();
1994      }
1995
1996      try {
1997          while (null != (keyOrNull = s.readObject())) {
1998              String JavaDoc key = ((String JavaDoc)keyOrNull).intern();
1999
2000              if (ownedWindowK == key)
2001                  connectOwnedWindow((Window JavaDoc) s.readObject());
2002
2003              else // skip value for unrecognized key
2004
s.readObject();
2005          }
2006      }
2007      catch (OptionalDataException JavaDoc e) {
2008          // 1.1 serialized form
2009
// ownedWindowList will be updated by Frame.readObject
2010
}
2011
2012    }
2013
2014    /*
2015     * --- Accessibility Support ---
2016     *
2017     */

2018
2019    /**
2020     * Gets the AccessibleContext associated with this Window.
2021     * For windows, the AccessibleContext takes the form of an
2022     * AccessibleAWTWindow.
2023     * A new AccessibleAWTWindow instance is created if necessary.
2024     *
2025     * @return an AccessibleAWTWindow that serves as the
2026     * AccessibleContext of this Window
2027     */

2028    public AccessibleContext getAccessibleContext() {
2029        if (accessibleContext == null) {
2030            accessibleContext = new AccessibleAWTWindow();
2031        }
2032        return accessibleContext;
2033    }
2034
2035    /**
2036     * This class implements accessibility support for the
2037     * <code>Window</code> class. It provides an implementation of the
2038     * Java Accessibility API appropriate to window user-interface elements.
2039     */

2040    protected class AccessibleAWTWindow extends AccessibleAWTContainer
2041    {
2042        /*
2043         * JDK 1.3 serialVersionUID
2044         */

2045        private static final long serialVersionUID = 4215068635060671780L;
2046
2047        /**
2048         * Get the role of this object.
2049         *
2050         * @return an instance of AccessibleRole describing the role of the
2051         * object
2052         * @see javax.accessibility.AccessibleRole
2053         */

2054        public AccessibleRole getAccessibleRole() {
2055            return AccessibleRole.WINDOW;
2056        }
2057
2058        /**
2059         * Get the state of this object.
2060         *
2061         * @return an instance of AccessibleStateSet containing the current
2062         * state set of the object
2063         * @see javax.accessibility.AccessibleState
2064         */

2065        public AccessibleStateSet getAccessibleStateSet() {
2066            AccessibleStateSet states = super.getAccessibleStateSet();
2067            if (getFocusOwner() != null) {
2068                states.add(AccessibleState.ACTIVE);
2069            }
2070            return states;
2071        }
2072
2073    } // inner class AccessibleAWTWindow
2074

2075    /**
2076     * This method returns the GraphicsConfiguration used by this Window.
2077     */

2078    public GraphicsConfiguration JavaDoc getGraphicsConfiguration() {
2079        //NOTE: for multiscreen, this will need to take into account
2080
//which screen the window is on/mostly on instead of returning the
2081
//default or constructor argument config.
2082
synchronized(getTreeLock()) {
2083            if (graphicsConfig == null && !GraphicsEnvironment.isHeadless()) {
2084                graphicsConfig =
2085                    GraphicsEnvironment. getLocalGraphicsEnvironment().
2086                    getDefaultScreenDevice().
2087                    getDefaultConfiguration();
2088            }
2089            return graphicsConfig;
2090        }
2091    }
2092
2093    /**
2094     * Reset this Window's GraphicsConfiguration to match its peer.
2095     */

2096    void resetGC() {
2097        if (!GraphicsEnvironment.isHeadless()) {
2098            // use the peer's GC
2099
setGCFromPeer();
2100            // if it's still null, use the default
2101
if (graphicsConfig == null) {
2102                graphicsConfig = GraphicsEnvironment.
2103                    getLocalGraphicsEnvironment().
2104                    getDefaultScreenDevice().
2105                    getDefaultConfiguration();
2106            }
2107            if (dbg.on) {
2108                dbg.println("+ Window.resetGC(): new GC is \n+ " + graphicsConfig + "\n+ this is " + this);
2109            }
2110        }
2111    }
2112
2113    /**
2114     * Sets the location of the window relative to the specified
2115     * component. If the component is not currently showing,
2116     * or <code>c</code> is <code>null</code>, the
2117     * window is centered on the screen. If the bottom of the
2118     * component is offscreen, the window is placed to the
2119     * side of the <code>Component</code> that is closest
2120     * to the center of the screen. So if the
2121     * <code>Component</code> is on the right part of the
2122     * screen, the <code>Window</code> is placed to its left,
2123     * and visa versa.
2124     *
2125     * @param c the component in relation to which the window's location
2126     * is determined
2127     * @since 1.4
2128     */

2129    public void setLocationRelativeTo(Component JavaDoc c) {
2130        Container JavaDoc root=null;
2131
2132        if (c != null) {
2133            if (c instanceof Window JavaDoc || c instanceof Applet JavaDoc) {
2134               root = (Container JavaDoc)c;
2135            } else {
2136                Container JavaDoc parent;
2137                for(parent = c.getParent() ; parent != null ; parent = parent.getParent()) {
2138                    if (parent instanceof Window JavaDoc || parent instanceof Applet JavaDoc) {
2139                        root = parent;
2140                        break;
2141                    }
2142                }
2143            }
2144        }
2145
2146        if((c != null && !c.isShowing()) || root == null ||
2147           !root.isShowing()) {
2148            Dimension JavaDoc paneSize = getSize();
2149            Dimension JavaDoc screenSize = getToolkit().getScreenSize();
2150
2151            setLocation((screenSize.width - paneSize.width) / 2,
2152                        (screenSize.height - paneSize.height) / 2);
2153        } else {
2154            Dimension JavaDoc invokerSize = c.getSize();
2155            Point JavaDoc invokerScreenLocation = c.getLocationOnScreen();
2156
2157            Rectangle JavaDoc windowBounds = getBounds();
2158            int dx = invokerScreenLocation.x+((invokerSize.width-windowBounds.width)>>1);
2159            int dy = invokerScreenLocation.y+((invokerSize.height - windowBounds.height)>>1);
2160            Rectangle JavaDoc ss = root.getGraphicsConfiguration().getBounds();
2161
2162            // Adjust for bottom edge being offscreen
2163
if (dy+windowBounds.height>ss.height) {
2164                dy = ss.height-windowBounds.height;
2165                if (invokerScreenLocation.x - ss.x + invokerSize.width / 2 <
2166                    ss.width / 2) {
2167                    dx = invokerScreenLocation.x+invokerSize.width;
2168                }
2169                else {
2170                    dx = invokerScreenLocation.x-windowBounds.width;
2171                }
2172            }
2173
2174            // Avoid being placed off the edge of the screen
2175
if (dx+windowBounds.width > ss.x + ss.width) {
2176                dx = ss.x + ss.width - windowBounds.width;
2177            }
2178            if (dx < ss.x) dx = 0;
2179            if (dy < ss.y) dy = 0;
2180
2181            setLocation(dx, dy);
2182        }
2183    }
2184    
2185    /**
2186     * Overridden from Component. Top-level Windows should not propagate a
2187     * MouseWheelEvent beyond themselves into their owning Windows.
2188     */

2189    void deliverMouseWheelToAncestor(MouseWheelEvent e) {}
2190
2191    /**
2192     * Overridden from Component. Top-level Windows don't dispatch to ancestors
2193     */

2194    boolean dispatchMouseWheelToAncestor(MouseWheelEvent e) {return false;}
2195
2196    /**
2197     * Creates a new strategy for multi-buffering on this component.
2198     * Multi-buffering is useful for rendering performance. This method
2199     * attempts to create the best strategy available with the number of
2200     * buffers supplied. It will always create a <code>BufferStrategy</code>
2201     * with that number of buffers.
2202     * A page-flipping strategy is attempted first, then a blitting strategy
2203     * using accelerated buffers. Finally, an unaccelerated blitting
2204     * strategy is used.
2205     * <p>
2206     * Each time this method is called,
2207     * the existing buffer strategy for this component is discarded.
2208     * @param numBuffers number of buffers to create
2209     * @exception IllegalArgumentException if numBuffers is less than 1.
2210     * @exception IllegalStateException if the component is not displayable
2211     * @see #isDisplayable
2212     * @see #getBufferStrategy
2213     * @since 1.4
2214     */

2215    public void createBufferStrategy(int numBuffers) {
2216        super.createBufferStrategy(numBuffers);
2217    }
2218    
2219    /**
2220     * Creates a new strategy for multi-buffering on this component with the
2221     * required buffer capabilities. This is useful, for example, if only
2222     * accelerated memory or page flipping is desired (as specified by the
2223     * buffer capabilities).
2224     * <p>
2225     * Each time this method
2226     * is called, the existing buffer strategy for this component is discarded.
2227     * @param numBuffers number of buffers to create, including the front buffer
2228     * @param caps the required capabilities for creating the buffer strategy;
2229     * cannot be <code>null</code>
2230     * @exception AWTException if the capabilities supplied could not be
2231     * supported or met; this may happen, for example, if there is not enough
2232     * accelerated memory currently available, or if page flipping is specified
2233     * but not possible.
2234     * @exception IllegalArgumentException if numBuffers is less than 1, or if
2235     * caps is <code>null</code>
2236     * @see #getBufferStrategy
2237     * @since 1.4
2238     */

2239    public void createBufferStrategy(int numBuffers,
2240        BufferCapabilities JavaDoc caps) throws AWTException JavaDoc {
2241        super.createBufferStrategy(numBuffers, caps);
2242    }
2243    
2244    /**
2245     * @return the buffer strategy used by this component
2246     * @see #createBufferStrategy
2247     * @since 1.4
2248     */

2249    public BufferStrategy JavaDoc getBufferStrategy() {
2250        return super.getBufferStrategy();
2251    }
2252
2253    Component JavaDoc getTemporaryLostComponent() {
2254        return temporaryLostComponent;
2255    }
2256    Component JavaDoc setTemporaryLostComponent(Component JavaDoc component) {
2257        Component JavaDoc previousComp = temporaryLostComponent;
2258        // Check that "component" is an acceptable focus owner and don't store it otherwise
2259
// - or later we will have problems with opposite while handling WINDOW_GAINED_FOCUS
2260
if (component == null
2261            || (component.isDisplayable() && component.isVisible() && component.isEnabled() && component.isFocusable()))
2262        {
2263            temporaryLostComponent = component;
2264        } else {
2265            temporaryLostComponent = null;
2266        }
2267        return previousComp;
2268    }
2269
2270    /**
2271     * Checks whether this window can contain focus owner.
2272     * Verifies that it is focusable and as container it can container focus owner.
2273     * @since 1.5
2274     */

2275    boolean canContainFocusOwner(Component JavaDoc focusOwnerCandidate) {
2276        return super.canContainFocusOwner(focusOwnerCandidate) && isFocusableWindow();
2277    }
2278
2279    private boolean locationByPlatform = locationByPlatformProp;
2280
2281    
2282    /**
2283     * Sets whether this Window should appear at the default location for the
2284     * native windowing system or at the current location (returned by
2285     * <code>getLocation</code>) the next time the Window is made visible.
2286     * This behavior resembles a native window shown without programmatically
2287     * setting its location. Most windowing systems cascade windows if their
2288     * locations are not explicitly set. The actual location is determined once the
2289     * window is shown on the screen.
2290     * <p>
2291     * This behavior can also be enabled by setting the System Property
2292     * "java.awt.Window.locationByPlatform" to "true", though calls to this method
2293     * take precedence.
2294     * <p>
2295     * Calls to <code>setVisible</code>, <code>setLocation</code> and
2296     * <code>setBounds</code> after calling <code>setLocationByPlatform</code> clear
2297     * this property of the Window.
2298     * <p>
2299     * For example, after the following code is executed:
2300     * <pre><blockquote>
2301     * setLocationByPlatform(true);
2302     * setVisible(true);
2303     * boolean flag = isLocationByPlatform();
2304     * </blockquote></pre>
2305     * The window will be shown at platform's default location and
2306     * <code>flag</code> will be <code>false</code>.
2307     * <p>
2308     * In the following sample:
2309     * <pre><blockquote>
2310     * setLocationByPlatform(true);
2311     * setLocation(10, 10);
2312     * boolean flag = isLocationByPlatform();
2313     * setVisible(true);
2314     * </blockquote></pre>
2315     * The window will be shown at (10, 10) and <code>flag</code> will be
2316     * <code>false</code>.
2317     *
2318     * @param locationByPlatform <code>true</code> if this Window should appear
2319     * at the default location, <code>false</code> if at the current location
2320     * @throws <code>IllegalComponentStateException</code> if the window
2321     * is showing on screen and locationByPlatform is <code>true</code>.
2322     * @see #setLocation
2323     * @see #isShowing
2324     * @see #setVisible
2325     * @see #isLocationByPlatform
2326     * @see java.lang.System#getProperty(String)
2327     * @since 1.5
2328     */

2329    public void setLocationByPlatform(boolean locationByPlatform) {
2330        synchronized (getTreeLock()) {
2331            if (locationByPlatform && isShowing()) {
2332                throw new IllegalComponentStateException JavaDoc("The window is showing on screen.");
2333            }
2334            this.locationByPlatform = locationByPlatform;
2335        }
2336    }
2337    
2338    /**
2339     * Returns <code>true</code> if this Window will appear at the default location
2340     * for the native windowing system the next time this Window is made visible.
2341     * This method always returns <code>false</code> if the Window is showing on the
2342     * screen.
2343     *
2344     * @return whether this Window will appear at the default location
2345     * @see #setLocationByPlatform
2346     * @see #isShowing
2347     * @since 1.5
2348     */

2349    public boolean isLocationByPlatform() {
2350        synchronized (getTreeLock()) {
2351            return locationByPlatform;
2352        }
2353    }
2354    
2355    /**
2356     * @since 1.5
2357     * @see #setLocationByPlatform
2358     * @see #isLocationByPlatform
2359     */

2360    public void setBounds(int x, int y, int width, int height) {
2361        synchronized (getTreeLock()) {
2362            if (getBoundsOp() == ComponentPeer.SET_LOCATION ||
2363                getBoundsOp() == ComponentPeer.SET_BOUNDS)
2364            {
2365                locationByPlatform = false;
2366            }
2367            super.setBounds(x, y, width, height);
2368        }
2369    }
2370} // class Window
2371

2372
2373/**
2374 * This class is no longer used, but is maintained for Serialization
2375 * backward-compatibility.
2376 */

2377class FocusManager implements java.io.Serializable JavaDoc {
2378    Container JavaDoc focusRoot;
2379    Component JavaDoc focusOwner;
2380
2381    /*
2382     * JDK 1.1 serialVersionUID
2383     */

2384    static final long serialVersionUID = 2491878825643557906L;
2385}
2386
Popular Tags