KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > Frame


1 /*
2  * @(#)Frame.java 1.147 04/05/18
3  *
4  * Copyright 2004 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.awt.peer.FramePeer;
10 import java.awt.event.*;
11 import java.util.Vector JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.io.ObjectOutputStream JavaDoc;
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import sun.awt.AppContext;
17 import sun.awt.SunToolkit;
18 import java.lang.ref.WeakReference JavaDoc;
19 import javax.accessibility.*;
20
21 /**
22  * A <code>Frame</code> is a top-level window with a title and a border.
23  * <p>
24  * The size of the frame includes any area designated for the
25  * border. The dimensions of the border area may be obtained
26  * using the <code>getInsets</code> method, however, since
27  * these dimensions are platform-dependent, a valid insets
28  * value cannot be obtained until the frame is made displayable
29  * by either calling <code>pack</code> or <code>show</code>.
30  * Since the border area is included in the overall size of the
31  * frame, the border effectively obscures a portion of the frame,
32  * constraining the area available for rendering and/or displaying
33  * subcomponents to the rectangle which has an upper-left corner
34  * location of <code>(insets.left, insets.top)</code>, and has a size of
35  * <code>width - (insets.left + insets.right)</code> by
36  * <code>height - (insets.top + insets.bottom)</code>.
37  * <p>
38  * The default layout for a frame is <code>BorderLayout</code>.
39  * <p>
40  * A frame may have its native decorations (i.e. <code>Frame</code>
41  * and <code>Titlebar</code>) turned off
42  * with <code>setUndecorated</code>. This can only be done while the frame
43  * is not {@link Component#isDisplayable() displayable}.
44  * <p>
45  * In a multi-screen environment, you can create a <code>Frame</code>
46  * on a different screen device by constructing the <code>Frame</code>
47  * with {@link #Frame(GraphicsConfiguration)} or
48  * {@link #Frame(String title, GraphicsConfiguration)}. The
49  * <code>GraphicsConfiguration</code> object is one of the
50  * <code>GraphicsConfiguration</code> objects of the target screen
51  * device.
52  * <p>
53  * In a virtual device multi-screen environment in which the desktop
54  * area could span multiple physical screen devices, the bounds of all
55  * configurations are relative to the virtual-coordinate system. The
56  * origin of the virtual-coordinate system is at the upper left-hand
57  * corner of the primary physical screen. Depending on the location
58  * of the primary screen in the virtual device, negative coordinates
59  * are possible, as shown in the following figure.
60  * <p>
61  * <img SRC="doc-files/MultiScreen.gif"
62  * alt="Diagram of virtual device encompassing three physical screens and one primary physical screen. The primary physical screen
63  * shows (0,0) coords while a different physical screen shows (-80,-100) coords."
64  * ALIGN=center HSPACE=10 VSPACE=7>
65  * <p>
66  * In such an environment, when calling <code>setLocation</code>,
67  * you must pass a virtual coordinate to this method. Similarly,
68  * calling <code>getLocationOnScreen</code> on a <code>Frame</code>
69  * returns virtual device coordinates. Call the <code>getBounds</code>
70  * method of a <code>GraphicsConfiguration</code> to find its origin in
71  * the virtual coordinate system.
72  * <p>
73  * The following code sets the
74  * location of the <code>Frame</code> at (10, 10) relative
75  * to the origin of the physical screen of the corresponding
76  * <code>GraphicsConfiguration</code>. If the bounds of the
77  * <code>GraphicsConfiguration</code> is not taken into account, the
78  * <code>Frame</code> location would be set at (10, 10) relative to the
79  * virtual-coordinate system and would appear on the primary physical
80  * screen, which might be different from the physical screen of the
81  * specified <code>GraphicsConfiguration</code>.
82  *
83  * <pre>
84  * Frame f = new Frame(GraphicsConfiguration gc);
85  * Rectangle bounds = gc.getBounds();
86  * f.setLocation(10 + bounds.x, 10 + bounds.y);
87  * </pre>
88  *
89  * <p>
90  * Frames are capable of generating the following types of
91  * <code>WindowEvent</code>s:
92  * <ul>
93  * <li><code>WINDOW_OPENED</code>
94  * <li><code>WINDOW_CLOSING</code>:
95  * <br>If the program doesn't
96  * explicitly hide or dispose the window while processing
97  * this event, the window close operation is canceled.
98  * <li><code>WINDOW_CLOSED</code>
99  * <li><code>WINDOW_ICONIFIED</code>
100  * <li><code>WINDOW_DEICONIFIED</code>
101  * <li><code>WINDOW_ACTIVATED</code>
102  * <li><code>WINDOW_DEACTIVATED</code>
103  * <li><code>WINDOW_GAINED_FOCUS</code>
104  * <li><code>WINDOW_LOST_FOCUS</code>
105  * <li><code>WINDOW_STATE_CHANGED</code>
106  * </ul>
107  *
108  * @version 1.147, 05/18/04
109  * @author Sami Shaio
110  * @see WindowEvent
111  * @see Window#addWindowListener
112  * @since JDK1.0
113  */

114 public class Frame extends Window JavaDoc implements MenuContainer JavaDoc {
115
116     /* Note: These are being obsoleted; programs should use the Cursor class
117      * variables going forward. See Cursor and Component.setCursor.
118      */

119
120    /**
121     * @deprecated replaced by <code>Cursor.DEFAULT_CURSOR</code>.
122     */

123     @Deprecated JavaDoc
124     public static final int DEFAULT_CURSOR = Cursor.DEFAULT_CURSOR;
125
126
127    /**
128     * @deprecated replaced by <code>Cursor.CROSSHAIR_CURSOR</code>.
129     */

130     @Deprecated JavaDoc
131     public static final int CROSSHAIR_CURSOR = Cursor.CROSSHAIR_CURSOR;
132
133    /**
134     * @deprecated replaced by <code>Cursor.TEXT_CURSOR</code>.
135     */

136     @Deprecated JavaDoc
137     public static final int TEXT_CURSOR = Cursor.TEXT_CURSOR;
138
139    /**
140     * @deprecated replaced by <code>Cursor.WAIT_CURSOR</code>.
141     */

142     @Deprecated JavaDoc
143     public static final int WAIT_CURSOR = Cursor.WAIT_CURSOR;
144
145    /**
146     * @deprecated replaced by <code>Cursor.SW_RESIZE_CURSOR</code>.
147     */

148     @Deprecated JavaDoc
149     public static final int SW_RESIZE_CURSOR = Cursor.SW_RESIZE_CURSOR;
150
151    /**
152     * @deprecated replaced by <code>Cursor.SE_RESIZE_CURSOR</code>.
153     */

154     @Deprecated JavaDoc
155     public static final int SE_RESIZE_CURSOR = Cursor.SE_RESIZE_CURSOR;
156
157    /**
158     * @deprecated replaced by <code>Cursor.NW_RESIZE_CURSOR</code>.
159     */

160     @Deprecated JavaDoc
161     public static final int NW_RESIZE_CURSOR = Cursor.NW_RESIZE_CURSOR;
162
163    /**
164     * @deprecated replaced by <code>Cursor.NE_RESIZE_CURSOR</code>.
165     */

166     @Deprecated JavaDoc
167     public static final int NE_RESIZE_CURSOR = Cursor.NE_RESIZE_CURSOR;
168
169    /**
170     * @deprecated replaced by <code>Cursor.N_RESIZE_CURSOR</code>.
171     */

172     @Deprecated JavaDoc
173     public static final int N_RESIZE_CURSOR = Cursor.N_RESIZE_CURSOR;
174
175    /**
176     * @deprecated replaced by <code>Cursor.S_RESIZE_CURSOR</code>.
177     */

178     @Deprecated JavaDoc
179     public static final int S_RESIZE_CURSOR = Cursor.S_RESIZE_CURSOR;
180
181    /**
182     * @deprecated replaced by <code>Cursor.W_RESIZE_CURSOR</code>.
183     */

184     @Deprecated JavaDoc
185     public static final int W_RESIZE_CURSOR = Cursor.W_RESIZE_CURSOR;
186
187    /**
188     * @deprecated replaced by <code>Cursor.E_RESIZE_CURSOR</code>.
189     */

190     @Deprecated JavaDoc
191     public static final int E_RESIZE_CURSOR = Cursor.E_RESIZE_CURSOR;
192
193    /**
194     * @deprecated replaced by <code>Cursor.HAND_CURSOR</code>.
195     */

196     @Deprecated JavaDoc
197     public static final int HAND_CURSOR = Cursor.HAND_CURSOR;
198
199    /**
200     * @deprecated replaced by <code>Cursor.MOVE_CURSOR</code>.
201     */

202     @Deprecated JavaDoc
203     public static final int MOVE_CURSOR = Cursor.MOVE_CURSOR;
204
205
206     /**
207      * Frame is in the "normal" state. This symbolic constant names a
208      * frame state with all state bits cleared.
209      * @see #setExtendedState(int)
210      * @see #getExtendedState
211      */

212     public static final int NORMAL = 0;
213
214     /**
215      * This state bit indicates that frame is iconified.
216      * @see #setExtendedState(int)
217      * @see #getExtendedState
218      */

219     public static final int ICONIFIED = 1;
220
221     /**
222      * This state bit indicates that frame is maximized in the
223      * horizontal direction.
224      * @see #setExtendedState(int)
225      * @see #getExtendedState
226      * @since 1.4
227      */

228     public static final int MAXIMIZED_HORIZ = 2;
229
230     /**
231      * This state bit indicates that frame is maximized in the
232      * vertical direction.
233      * @see #setExtendedState(int)
234      * @see #getExtendedState
235      * @since 1.4
236      */

237     public static final int MAXIMIZED_VERT = 4;
238
239     /**
240      * This state bit mask indicates that frame is fully maximized
241      * (that is both horizontally and vertically). It is just a
242      * convenience alias for
243      * <code>MAXIMIZED_VERT&nbsp;|&nbsp;MAXIMIZED_HORIZ</code>.
244      *
245      * <p>Note that the correct test for frame being fully maximized is
246      * <pre>
247      * (state & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH
248      * </pre>
249      *
250      * <p>To test is frame is maximized in <em>some</em> direction use
251      * <pre>
252      * (state & Frame.MAXIMIZED_BOTH) != 0
253      * </pre>
254      *
255      * @see #setExtendedState(int)
256      * @see #getExtendedState
257      * @since 1.4
258      */

259     public static final int MAXIMIZED_BOTH = MAXIMIZED_VERT | MAXIMIZED_HORIZ;
260
261     /**
262      * Maximized bounds for this frame.
263      * @see #setMaximizedBounds(Rectangle)
264      * @see #getMaximizedBounds
265      * @serial
266      * @since 1.4
267      */

268     Rectangle JavaDoc maximizedBounds;
269
270
271     /**
272      * This is the title of the frame. It can be changed
273      * at any time. <code>title</code> can be null and if
274      * this is the case the <code>title</code> = "".
275      *
276      * @serial
277      * @see #getTitle
278      * @see #setTitle(String)
279      */

280     String JavaDoc title = "Untitled";
281
282     /**
283      * <code>icon</code> is the graphical way we can
284      * represent the frame.
285      * <code>icon</code> can be null, but obviously if
286      * you try to set the icon image <code>icon</code>
287      * cannot be null.
288      *
289      * @serial
290      * @see #getIconImage
291      * @see #setIconImage(Image)
292      */

293     transient Image JavaDoc icon;
294
295     /**
296      * The frames menubar. If <code>menuBar</code> = null
297      * the frame will not have a menubar.
298      *
299      * @serial
300      * @see #getMenuBar
301      * @see #setMenuBar(MenuBar)
302      */

303     MenuBar JavaDoc menuBar;
304
305     /**
306      * This field indicates whether the frame is resizable.
307      * This property can be changed at any time.
308      * <code>resizable</code> will be true if the frame is
309      * resizable, otherwise it will be false.
310      *
311      * @serial
312      * @see #isResizable()
313      */

314     boolean resizable = true;
315
316     /**
317      * This field indicates whether the frame is undecorated.
318      * This property can only be changed while the frame is not displayable.
319      * <code>undecorated</code> will be true if the frame is
320      * undecorated, otherwise it will be false.
321      *
322      * @serial
323      * @see #setUndecorated(boolean)
324      * @see #isUndecorated()
325      * @see Component#isDisplayable()
326      * @since 1.4
327      */

328     boolean undecorated = false;
329
330     /**
331      * <code>mbManagement</code> is only used by the Motif implementation.
332      *
333      * @serial
334      */

335     boolean mbManagement = false; /* used only by the Motif impl. */
336
337     // XXX: uwe: abuse old field for now
338
// will need to take care of serialization
339
private int state = NORMAL;
340
341     /*
342      * The Windows owned by the Frame.
343      * Note: in 1.2 this has been superceded by Window.ownedWindowList
344      *
345      * @serial
346      * @see java.awt.Window#ownedWindowList
347      */

348     Vector JavaDoc ownedWindows;
349
350     /*
351      * We insert a weak reference into the Vector of all Frames
352      * instead of 'this' so that garbage collection can still take
353      * place correctly.
354      */

355     transient private WeakReference JavaDoc weakThis;
356
357     private static final String JavaDoc base = "frame";
358     private static int nameCounter = 0;
359
360     /*
361      * JDK 1.1 serialVersionUID
362      */

363      private static final long serialVersionUID = 2673458971256075116L;
364
365     static {
366         /* ensure that the necessary native libraries are loaded */
367     Toolkit.loadLibraries();
368         if (!GraphicsEnvironment.isHeadless()) {
369             initIDs();
370         }
371     }
372
373     /**
374      * Constructs a new instance of <code>Frame</code> that is
375      * initially invisible. The title of the <code>Frame</code>
376      * is empty.
377      * @exception HeadlessException when GraphicsEnvironment.isHeadless()
378      * returns true
379      * @see java.awt.GraphicsEnvironment#isHeadless()
380      * @see Component#setSize
381      * @see Component#setVisible(boolean)
382      */

383     public Frame() throws HeadlessException JavaDoc {
384         this("");
385     }
386
387     /**
388      * Create a <code>Frame</code> with the specified
389      * <code>GraphicsConfiguration</code> of
390      * a screen device.
391      * @param gc the <code>GraphicsConfiguration</code>
392      * of the target screen device. If <code>gc</code>
393      * is <code>null</code>, the system default
394      * <code>GraphicsConfiguration</code> is assumed.
395      * @exception IllegalArgumentException if
396      * <code>gc</code> is not from a screen device.
397      * This exception is always thrown
398      * when GraphicsEnvironment.isHeadless() returns true
399      * @see java.awt.GraphicsEnvironment#isHeadless()
400      * @since 1.3
401      */

402     public Frame(GraphicsConfiguration JavaDoc gc) {
403         this("", gc);
404     }
405
406     /**
407      * Constructs a new, initially invisible <code>Frame</code> object
408      * with the specified title.
409      * @param title the title to be displayed in the frame's border.
410      * A <code>null</code> value
411      * is treated as an empty string, "".
412      * @exception HeadlessException when GraphicsEnvironment.isHeadless()
413      * returns true
414      * @see java.awt.GraphicsEnvironment#isHeadless()
415      * @see java.awt.Component#setSize
416      * @see java.awt.Component#setVisible(boolean)
417      * @see java.awt.GraphicsConfiguration#getBounds
418      */

419     public Frame(String JavaDoc title) throws HeadlessException JavaDoc {
420         init(title, null);
421     }
422     
423     /**
424      * Constructs a new, initially invisible <code>Frame</code> object
425      * with the specified title and a
426      * <code>GraphicsConfiguration</code>.
427      * @param title the title to be displayed in the frame's border.
428      * A <code>null</code> value
429      * is treated as an empty string, "".
430      * @param gc the <code>GraphicsConfiguration</code>
431      * of the target screen device. If <code>gc</code> is
432      * <code>null</code>, the system default
433      * <code>GraphicsConfiguration</code> is assumed.
434      * @exception IllegalArgumentException if <code>gc</code>
435      * is not from a screen device.
436      * This exception is always thrown
437      * when GraphicsEnvironment.isHeadless() returns true
438      * @see java.awt.GraphicsEnvironment#isHeadless()
439      * @see java.awt.Component#setSize
440      * @see java.awt.Component#setVisible(boolean)
441      * @see java.awt.GraphicsConfiguration#getBounds
442      */

443     public Frame(String JavaDoc title, GraphicsConfiguration JavaDoc gc) {
444         super(gc);
445         init(title, gc);
446     }
447
448     private void init(String JavaDoc title, GraphicsConfiguration JavaDoc gc) {
449         this.title = title;
450         weakThis = new WeakReference JavaDoc(this);
451         addToFrameList();
452         SunToolkit.checkAndSetPolicy(this, false);
453     }
454     
455     /**
456      * Disposes of the input methods and context, and removes
457      * this Frame from the AppContext. Subclasses that override
458      * this method should call super.finalize().
459      */

460     protected void finalize() throws Throwable JavaDoc {
461         // We have to remove the (hard) reference to weakThis in the
462
// AppContext's Frame list Vector, otherwise the WeakReference
463
// instance that points to this Frame will never get garbage
464
// collected.
465
removeFromFrameList();
466         super.finalize();
467     }
468
469     /**
470      * Construct a name for this component. Called by getName() when the
471      * name is null.
472      */

473     String JavaDoc constructComponentName() {
474         synchronized (getClass()) {
475         return base + nameCounter++;
476     }
477     }
478
479     /**
480      * Makes this Frame displayable by connecting it to
481      * a native screen resource. Making a frame displayable will
482      * cause any of its children to be made displayable.
483      * This method is called internally by the toolkit and should
484      * not be called directly by programs.
485      * @see Component#isDisplayable
486      * @see #removeNotify
487      */

488     public void addNotify() {
489     synchronized (getTreeLock()) {
490         if (peer == null) {
491         peer = getToolkit().createFrame(this);
492         }
493         FramePeer p = (FramePeer)peer;
494         MenuBar JavaDoc menuBar = this.menuBar;
495         if (menuBar != null) {
496             mbManagement = true;
497         menuBar.addNotify();
498         p.setMenuBar(menuBar);
499         }
500         p.setMaximizedBounds(maximizedBounds);
501         super.addNotify();
502     }
503     }
504
505     /**
506      * Gets the title of the frame. The title is displayed in the
507      * frame's border.
508      * @return the title of this frame, or an empty string ("")
509      * if this frame doesn't have a title.
510      * @see #setTitle(String)
511      */

512     public String JavaDoc getTitle() {
513     return title;
514     }
515
516     /**
517      * Sets the title for this frame to the specified string.
518      * @param title the title to be displayed in the frame's border.
519      * A <code>null</code> value
520      * is treated as an empty string, "".
521      * @see #getTitle
522      */

523     public void setTitle(String JavaDoc title) {
524         String JavaDoc oldTitle = this.title;
525         if (title == null) {
526             title = "";
527         }
528
529
530         synchronized(this) {
531             this.title = title;
532             FramePeer peer = (FramePeer)this.peer;
533             if (peer != null) {
534                 peer.setTitle(title);
535             }
536         }
537         firePropertyChange("title", oldTitle, title);
538     }
539
540     /**
541      * Gets the image to be displayed in the minimized icon
542      * for this frame.
543      * @return the icon image for this frame, or <code>null</code>
544      * if this frame doesn't have an icon image.
545      * @see #setIconImage(Image)
546      */

547     public Image JavaDoc getIconImage() {
548     return icon;
549     }
550
551     /**
552      * Sets the image to be displayed in the minimized icon for this frame.
553      * Not all platforms support the concept of minimizing a window.
554      * @param image the icon image to be displayed.
555      * If this parameter is <code>null</code> then the
556      * icon image is set to the default image, which may vary
557      * with platform.
558      * @see #getIconImage
559      */

560     public synchronized void setIconImage(Image JavaDoc image) {
561     this.icon = image;
562         FramePeer peer = (FramePeer)this.peer;
563     if (peer != null) {
564         peer.setIconImage(image);
565     }
566     }
567
568     /**
569      * Gets the menu bar for this frame.
570      * @return the menu bar for this frame, or <code>null</code>
571      * if this frame doesn't have a menu bar.
572      * @see #setMenuBar(MenuBar)
573      */

574     public MenuBar JavaDoc getMenuBar() {
575     return menuBar;
576     }
577
578     /**
579      * Sets the menu bar for this frame to the specified menu bar.
580      * @param mb the menu bar being set.
581      * If this parameter is <code>null</code> then any
582      * existing menu bar on this frame is removed.
583      * @see #getMenuBar
584      */

585     public void setMenuBar(MenuBar JavaDoc mb) {
586         synchronized (getTreeLock()) {
587         if (menuBar == mb) {
588             return;
589         }
590         if ((mb != null) && (mb.parent != null)) {
591             mb.parent.remove(mb);
592         }
593         if (menuBar != null) {
594             remove(menuBar);
595         }
596         menuBar = mb;
597         if (menuBar != null) {
598             menuBar.parent = this;
599
600         FramePeer peer = (FramePeer)this.peer;
601         if (peer != null) {
602             mbManagement = true;
603             menuBar.addNotify();
604             if (valid) {
605                 invalidate();
606             }
607             peer.setMenuBar(menuBar);
608         }
609         }
610     }
611     }
612
613     /**
614      * Indicates whether this frame is resizable by the user.
615      * By default, all frames are initially resizable.
616      * @return <code>true</code> if the user can resize this frame;
617      * <code>false</code> otherwise.
618      * @see java.awt.Frame#setResizable(boolean)
619      */

620     public boolean isResizable() {
621     return resizable;
622     }
623
624     /**
625      * Sets whether this frame is resizable by the user.
626      * @param resizable <code>true</code> if this frame is resizable;
627      * <code>false</code> otherwise.
628      * @see java.awt.Frame#isResizable
629      */

630     public void setResizable(boolean resizable) {
631         boolean oldResizable = this.resizable;
632         boolean testvalid = false;
633
634         synchronized (this) {
635         this.resizable = resizable;
636         FramePeer peer = (FramePeer)this.peer;
637         if (peer != null) {
638             peer.setResizable(resizable);
639         testvalid = true;
640         }
641     }
642
643     // On some platforms, changing the resizable state affects
644
// the insets of the Frame. If we could, we'd call invalidate()
645
// from the peer, but we need to guarantee that we're not holding
646
// the Frame lock when we call invalidate().
647
if (testvalid && valid) {
648         invalidate();
649     }
650         firePropertyChange("resizable", oldResizable, resizable);
651     }
652
653
654     /**
655      * Sets the state of this frame (obsolete).
656      * <p>
657      * In older versions of JDK a frame state could only be NORMAL or
658      * ICONIFIED. Since JDK 1.4 set of supported frame states is
659      * expanded and frame state is represented as a bitwise mask.
660      * <p>
661      * For compatibility with old programs this method still accepts
662      * <code>Frame.NORMAL</code> and <code>Frame.ICONIFIED</code> but
663      * it only changes the iconic state of the frame, other aspects of
664      * frame state are not affected by this method.
665      *
666      * @param state either <code>Frame.NORMAL</code> or
667      * <code>Frame.ICONIFIED</code>.
668      * @see #getState
669      * @see #setExtendedState(int)
670      */

671     public synchronized void setState(int state) {
672     int current = getExtendedState();
673     if (state == ICONIFIED && (current & ICONIFIED) == 0) {
674         setExtendedState(current | ICONIFIED);
675     }
676     else if (state == NORMAL && (current & ICONIFIED) != 0) {
677         setExtendedState(current & ~ICONIFIED);
678     }
679     }
680
681     /**
682      * Sets the state of this frame. The state is
683      * represented as a bitwise mask.
684      * <ul>
685      * <li><code>NORMAL</code>
686      * <br>Indicates that no state bits are set.
687      * <li><code>ICONIFIED</code>
688      * <li><code>MAXIMIZED_HORIZ</code>
689      * <li><code>MAXIMIZED_VERT</code>
690      * <li><code>MAXIMIZED_BOTH</code>
691      * <br>Concatenates <code>MAXIMIZED_HORIZ</code>
692      * and <code>MAXIMIZED_VERT</code>.
693      * </ul>
694      * <p>Note that if the state is not supported on a
695      * given platform, nothing will happen. The application
696      * may determine if a specific state is available via
697      * the <code>java.awt.Toolkit#isFrameStateSupported(int state)</code>
698      * method.
699      *
700      * @param state a bitwise mask of frame state constants
701      * @see #getExtendedState
702      * @see java.awt.Toolkit#isFrameStateSupported(int)
703      * @since 1.4
704      */

705     public synchronized void setExtendedState(int state) {
706         if ( !isFrameStateSupported( state ) ) {
707             return;
708         }
709         this.state = state;
710         FramePeer peer = (FramePeer)this.peer;
711         if (peer != null) {
712             peer.setState(state);
713         }
714     }
715     private boolean isFrameStateSupported(int state) {
716         if( !getToolkit().isFrameStateSupported( state ) ) {
717             // * Toolkit.isFrameStateSupported returns always false
718
// on compound state even if all parts are supported;
719
// * if part of state is not supported, state is not supported;
720
// * MAXIMIZED_BOTH is not a compound state.
721
if( ((state & ICONIFIED) != 0) &&
722                 !getToolkit().isFrameStateSupported( ICONIFIED )) {
723                 return false;
724             }else {
725                 state &= ~ICONIFIED;
726             }
727             return getToolkit().isFrameStateSupported( state );
728         }
729         return true;
730     }
731
732     /**
733      * Gets the state of this frame (obsolete).
734      * <p>
735      * In older versions of JDK a frame state could only be NORMAL or
736      * ICONIFIED. Since JDK 1.4 set of supported frame states is
737      * expanded and frame state is represented as a bitwise mask.
738      * <p>
739      * For compatibility with old programs this method still returns
740      * <code>Frame.NORMAL</code> and <code>Frame.ICONIFIED</code> but
741      * it only reports the iconic state of the frame, other aspects of
742      * frame state are not reported by this method.
743      *
744      * @return <code>Frame.NORMAL</code> or <code>Frame.ICONIFIED</code>.
745      * @see #setState(int)
746      * @see #getExtendedState
747      */

748     public synchronized int getState() {
749         return (getExtendedState() & ICONIFIED) != 0 ? ICONIFIED : NORMAL;
750     }
751
752
753     /**
754      * Gets the state of this frame. The state is
755      * represented as a bitwise mask.
756      * <ul>
757      * <li><code>NORMAL</code>
758      * <br>Indicates that no state bits are set.
759      * <li><code>ICONIFIED</code>
760      * <li><code>MAXIMIZED_HORIZ</code>
761      * <li><code>MAXIMIZED_VERT</code>
762      * <li><code>MAXIMIZED_BOTH</code>
763      * <br>Concatenates <code>MAXIMIZED_HORIZ</code>
764      * and <code>MAXIMIZED_VERT</code>.
765      * </ul>
766      *
767      * @return a bitwise mask of frame state constants
768      * @see #setExtendedState(int)
769      * @since 1.4
770      */

771     public synchronized int getExtendedState() {
772         FramePeer peer = (FramePeer)this.peer;
773         if (peer != null) {
774             state = peer.getState();
775         }
776         return state;
777     }
778
779     /**
780      * Sets the maximized bounds for this frame.
781      * <p>
782      * When a frame is in maximized state the system supplies some
783      * defaults bounds. This method allows some or all of those
784      * system supplied values to be overridden.
785      * <p>
786      * If <code>bounds</code> is <code>null</code>, accept bounds
787      * supplied by the system. If non-<code>null</code> you can
788      * override some of the system supplied values while accepting
789      * others by setting those fields you want to accept from system
790      * to <code>Integer.MAX_VALUE</code>.
791      * <p>
792      * On some systems only the size portion of the bounds is taken
793      * into account.
794      *
795      * @param bounds bounds for the maximized state
796      * @see #getMaximizedBounds()
797      * @since 1.4
798      */

799     public synchronized void setMaximizedBounds(Rectangle JavaDoc bounds) {
800     this.maximizedBounds = bounds;
801         FramePeer peer = (FramePeer)this.peer;
802     if (peer != null) {
803         peer.setMaximizedBounds(bounds);
804     }
805     }
806
807     /**
808      * Gets maximized bounds for this frame.
809      * Some fields may contain <code>Integer.MAX_VALUE</code> to indicate
810      * that system supplied values for this field must be used.
811      *
812      * @return maximized bounds for this frame; may be <code>null</code>
813      * @see #setMaximizedBounds(Rectangle)
814      * @since 1.4
815      */

816     public Rectangle JavaDoc getMaximizedBounds() {
817     return maximizedBounds;
818     }
819
820
821     /**
822      * Disables or enables decorations for this frame.
823      * This method can only be called while the frame is not displayable.
824      * @param undecorated <code>true</code> if no frame decorations are
825      * to be enabled;
826      * <code>false</code> if frame decorations are to be enabled.
827      * @throws <code>IllegalComponentStateException</code> if the frame
828      * is displayable.
829      * @see #isUndecorated
830      * @see Component#isDisplayable
831      * @see javax.swing.JFrame#setDefaultLookAndFeelDecorated(boolean)
832      * @since 1.4
833      */

834     public void setUndecorated(boolean undecorated) {
835         /* Make sure we don't run in the middle of peer creation.*/
836         synchronized (getTreeLock()) {
837             if (isDisplayable()) {
838                 throw new IllegalComponentStateException JavaDoc("The frame is displayable.");
839             }
840             this.undecorated = undecorated;
841         }
842     }
843
844     /**
845      * Indicates whether this frame is undecorated.
846      * By default, all frames are initially decorated.
847      * @return <code>true</code> if frame is undecorated;
848      * <code>false</code> otherwise.
849      * @see java.awt.Frame#setUndecorated(boolean)
850      * @since 1.4
851      */

852     public boolean isUndecorated() {
853         return undecorated;
854     }
855
856     /**
857      * Removes the specified menu bar from this frame.
858      * @param m the menu component to remove.
859      * If <code>m</code> is <code>null</code>, then
860      * no action is taken
861      */

862     public void remove(MenuComponent JavaDoc m) {
863     synchronized (getTreeLock()) {
864         if (m == menuBar) {
865         menuBar = null;
866         FramePeer peer = (FramePeer)this.peer;
867         if (peer != null) {
868             mbManagement = true;
869             if (valid) {
870                 invalidate();
871             }
872             peer.setMenuBar(null);
873             m.removeNotify();
874         }
875         m.parent = null;
876         } else {
877         super.remove(m);
878         }
879     }
880     }
881
882     /**
883      * Makes this Frame undisplayable by removing its connection
884      * to its native screen resource. Making a Frame undisplayable
885      * will cause any of its children to be made undisplayable.
886      * This method is called by the toolkit internally and should
887      * not be called directly by programs.
888      * @see Component#isDisplayable
889      * @see #addNotify
890      */

891     public void removeNotify() {
892         synchronized (getTreeLock()) {
893         FramePeer peer = (FramePeer)this.peer;
894         if (peer != null) {
895                 // get the latest Frame state before disposing
896
getState();
897
898                 if (menuBar != null) {
899                 mbManagement = true;
900             peer.setMenuBar(null);
901             menuBar.removeNotify();
902                 }
903         }
904         super.removeNotify();
905     }
906     }
907
908     void postProcessKeyEvent(KeyEvent e) {
909         if (menuBar != null && menuBar.handleShortcut(e)) {
910             e.consume();
911             return;
912         }
913         super.postProcessKeyEvent(e);
914     }
915
916     /**
917      * Returns a string representing the state of this <code>Frame</code>.
918      * This method is intended to be used only for debugging purposes, and the
919      * content and format of the returned string may vary between
920      * implementations. The returned string may be empty but may not be
921      * <code>null</code>.
922      *
923      * @return the parameter string of this frame
924      */

925     protected String JavaDoc paramString() {
926     String JavaDoc str = super.paramString();
927     if (title != null) {
928         str += ",title=" + title;
929     }
930     if (resizable) {
931         str += ",resizable";
932     }
933     getExtendedState(); // sync with peer
934
if (state == NORMAL) {
935         str += ",normal";
936     }
937     else {
938         if ((state & ICONIFIED) != 0) {
939         str += ",iconified";
940         }
941         if ((state & MAXIMIZED_BOTH) == MAXIMIZED_BOTH) {
942         str += ",maximized";
943         }
944         else if ((state & MAXIMIZED_HORIZ) != 0) {
945         str += ",maximized_horiz";
946         }
947         else if ((state & MAXIMIZED_VERT) != 0) {
948         str += ",maximized_vert";
949         }
950     }
951     return str;
952     }
953
954     /**
955      * @deprecated As of JDK version 1.1,
956      * replaced by <code>Component.setCursor(Cursor)</code>.
957      */

958     @Deprecated JavaDoc
959     public void setCursor(int cursorType) {
960     if (cursorType < DEFAULT_CURSOR || cursorType > MOVE_CURSOR) {
961         throw new IllegalArgumentException JavaDoc("illegal cursor type");
962     }
963     setCursor(Cursor.getPredefinedCursor(cursorType));
964     }
965
966     /**
967      * @deprecated As of JDK version 1.1,
968      * replaced by <code>Component.getCursor()</code>.
969      */

970     @Deprecated JavaDoc
971     public int getCursorType() {
972     return (getCursor().getType());
973     }
974
975     /**
976      * Returns an array containing all Frames created by the application.
977      * If called from an applet, the array will only include the Frames
978      * accessible by that applet.
979      * @since 1.2
980      */

981     public static Frame JavaDoc[] getFrames() {
982         synchronized (Frame JavaDoc.class) {
983             Frame JavaDoc realCopy[];
984             Vector JavaDoc frameList =
985         (Vector JavaDoc)AppContext.getAppContext().get(Frame JavaDoc.class);
986             if (frameList != null) {
987             // Recall that frameList is actually a Vector of WeakReferences
988
// and calling get() on one of these references may return
989
// null. Make two arrays-- one the size of the Vector
990
// (fullCopy with size fullSize), and one the size of all
991
// non-null get()s (realCopy with size realSize).
992
int fullSize = frameList.size();
993         int realSize = 0;
994         Frame JavaDoc fullCopy[] = new Frame JavaDoc[fullSize];
995
996         for (int i = 0; i < fullSize; i++) {
997             fullCopy[realSize] = (Frame JavaDoc)
998                 (((WeakReference JavaDoc) (frameList.elementAt(i))).get());
999
1000            if (fullCopy[realSize] != null) {
1001                realSize++;
1002            }
1003        }
1004
1005        if (fullSize != realSize) {
1006            realCopy = new Frame JavaDoc[realSize];
1007            System.arraycopy(fullCopy, 0, realCopy, 0, realSize);
1008        } else {
1009            realCopy = fullCopy;
1010        }
1011            } else {
1012                realCopy = new Frame JavaDoc[0];
1013            }
1014            return realCopy;
1015        }
1016    }
1017
1018    void addToFrameList() {
1019        synchronized (Frame JavaDoc.class) {
1020            Vector JavaDoc frameList = (Vector JavaDoc)appContext.get(Frame JavaDoc.class);
1021            if (frameList == null) {
1022                frameList = new Vector JavaDoc();
1023                appContext.put(Frame JavaDoc.class, frameList);
1024            }
1025            frameList.addElement(weakThis);
1026        }
1027    }
1028
1029    void removeFromFrameList() {
1030        synchronized (Frame JavaDoc.class) {
1031            Vector JavaDoc frameList = (Vector JavaDoc)appContext.get(Frame JavaDoc.class);
1032            if (frameList != null) {
1033                frameList.removeElement(weakThis);
1034            }
1035        }
1036    }
1037
1038    /* Serialization support. If there's a MenuBar we restore
1039     * its (transient) parent field here. Likewise for top level
1040     * windows that are "owned" by this frame.
1041     */

1042
1043    /**
1044     * <code>Frame</code>'s Serialized Data Version.
1045     *
1046     * @serial
1047     */

1048    private int frameSerializedDataVersion = 1;
1049
1050    /**
1051     * Writes default serializable fields to stream. Writes
1052     * an optional serializable <code>Icon</code>, which is
1053     * available as of 1.4.
1054     *
1055     * @param s the <code>ObjectOutputStream</code> to write
1056     * @serialData an optional <code>Icon</code>
1057     * @see javax.swing.Icon
1058     * @see #readObject(ObjectInputStream)
1059     */

1060    private void writeObject(ObjectOutputStream JavaDoc s)
1061      throws IOException JavaDoc
1062    {
1063      s.defaultWriteObject();
1064      if (icon instanceof Serializable JavaDoc) {
1065          s.writeObject(icon);
1066      }
1067      else
1068      {
1069          s.writeObject(null);
1070      }
1071    }
1072
1073    /**
1074     * Reads the <code>ObjectInputStream</code>. Tries
1075     * to read an <code>Icon</code>, which is optional
1076     * data available as of 1.4. If an <code>Icon</code>
1077     * is not available, but anything other than an EOF
1078     * is detected, an <code>OptionalDataException</code>
1079     * will be thrown..
1080     * Unrecognized keys or values will be ignored.
1081     *
1082     * @param s the <code>ObjectInputStream</code> to read
1083     * @exception OptionalDataException if an <code>Icon</code>
1084     * is not available, but anything other than an EOF
1085     * is detected
1086     * @exception HeadlessException if
1087     * <code>GraphicsEnvironment.isHeadless</code> returns
1088     * <code>true</code>
1089     * @see java.awt.GraphicsEnvironment#isHeadless()
1090     * @see javax.swing.Icon
1091     * @see #writeObject(ObjectOutputStream)
1092     */

1093    private void readObject(ObjectInputStream JavaDoc s)
1094      throws ClassNotFoundException JavaDoc, IOException JavaDoc, HeadlessException JavaDoc
1095    {
1096      // HeadlessException is thrown by Window's readObject
1097
s.defaultReadObject();
1098      
1099      try {
1100      icon = (Image JavaDoc) s.readObject();
1101      } catch (java.io.OptionalDataException JavaDoc e) {
1102      // pre-1.4 instances will not have this optional data.
1103
// e.eof will be true to indicate that there is no more
1104
// data available for this object.
1105

1106     // If e.eof is not true, throw the exception as it
1107
// might have been caused by unrelated reasons.
1108
if (!e.eof) {
1109             throw (e);
1110          }
1111      }
1112
1113      if (menuBar != null)
1114    menuBar.parent = this;
1115
1116      // Ensure 1.1 serialized Frames can read & hook-up
1117
// owned windows properly
1118
//
1119
if (ownedWindows != null) {
1120          for (int i = 0; i < ownedWindows.size(); i++) {
1121          connectOwnedWindow((Window JavaDoc) ownedWindows.elementAt(i));
1122      }
1123          ownedWindows = null;
1124      }
1125
1126      weakThis = new WeakReference JavaDoc(this);
1127      addToFrameList();
1128    }
1129    /**
1130     * Initialize JNI field and method IDs
1131     */

1132    private static native void initIDs();
1133
1134    /*
1135     * --- Accessibility Support ---
1136     *
1137     */

1138
1139    /**
1140     * Gets the AccessibleContext associated with this Frame.
1141     * For frames, the AccessibleContext takes the form of an
1142     * AccessibleAWTFrame.
1143     * A new AccessibleAWTFrame instance is created if necessary.
1144     *
1145     * @return an AccessibleAWTFrame that serves as the
1146     * AccessibleContext of this Frame
1147     */

1148    public AccessibleContext getAccessibleContext() {
1149        if (accessibleContext == null) {
1150            accessibleContext = new AccessibleAWTFrame();
1151        }
1152        return accessibleContext;
1153    }
1154
1155    /**
1156     * This class implements accessibility support for the
1157     * <code>Frame</code> class. It provides an implementation of the
1158     * Java Accessibility API appropriate to frame user-interface elements.
1159     */

1160    protected class AccessibleAWTFrame extends AccessibleAWTWindow
1161    {
1162        /*
1163         * JDK 1.3 serialVersionUID
1164         */

1165        private static final long serialVersionUID = -6172960752956030250L;
1166
1167        /**
1168         * Get the role of this object.
1169         *
1170         * @return an instance of AccessibleRole describing the role of the
1171         * object
1172         * @see AccessibleRole
1173         */

1174        public AccessibleRole getAccessibleRole() {
1175            return AccessibleRole.FRAME;
1176        }
1177
1178        /**
1179         * Get the state of this object.
1180         *
1181         * @return an instance of AccessibleStateSet containing the current
1182         * state set of the object
1183         * @see AccessibleState
1184         */

1185        public AccessibleStateSet getAccessibleStateSet() {
1186            AccessibleStateSet states = super.getAccessibleStateSet();
1187            if (getFocusOwner() != null) {
1188                states.add(AccessibleState.ACTIVE);
1189            }
1190            if (isResizable()) {
1191                states.add(AccessibleState.RESIZABLE);
1192            }
1193            return states;
1194        }
1195
1196
1197    } // inner class AccessibleAWTFrame
1198

1199}
1200
Popular Tags