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  &n