blob: 0e897d433097d3191d7d560468e8f010ddb12205 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1995-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package java.awt;
26
27import java.awt.peer.FramePeer;
28import java.awt.event.*;
29import java.util.ArrayList;
30import java.util.Arrays;
31import java.util.List;
32import java.util.Vector;
33import java.io.Serializable;
34import java.io.ObjectOutputStream;
35import java.io.ObjectInputStream;
36import java.io.IOException;
37import sun.awt.AppContext;
38import sun.awt.SunToolkit;
39import java.lang.ref.WeakReference;
40import javax.accessibility.*;
41
42/**
43 * A <code>Frame</code> is a top-level window with a title and a border.
44 * <p>
45 * The size of the frame includes any area designated for the
46 * border. The dimensions of the border area may be obtained
47 * using the <code>getInsets</code> method, however, since
48 * these dimensions are platform-dependent, a valid insets
49 * value cannot be obtained until the frame is made displayable
50 * by either calling <code>pack</code> or <code>show</code>.
51 * Since the border area is included in the overall size of the
52 * frame, the border effectively obscures a portion of the frame,
53 * constraining the area available for rendering and/or displaying
54 * subcomponents to the rectangle which has an upper-left corner
55 * location of <code>(insets.left, insets.top)</code>, and has a size of
56 * <code>width - (insets.left + insets.right)</code> by
57 * <code>height - (insets.top + insets.bottom)</code>.
58 * <p>
59 * The default layout for a frame is <code>BorderLayout</code>.
60 * <p>
61 * A frame may have its native decorations (i.e. <code>Frame</code>
62 * and <code>Titlebar</code>) turned off
63 * with <code>setUndecorated</code>. This can only be done while the frame
64 * is not {@link Component#isDisplayable() displayable}.
65 * <p>
66 * In a multi-screen environment, you can create a <code>Frame</code>
67 * on a different screen device by constructing the <code>Frame</code>
68 * with {@link #Frame(GraphicsConfiguration)} or
69 * {@link #Frame(String title, GraphicsConfiguration)}. The
70 * <code>GraphicsConfiguration</code> object is one of the
71 * <code>GraphicsConfiguration</code> objects of the target screen
72 * device.
73 * <p>
74 * In a virtual device multi-screen environment in which the desktop
75 * area could span multiple physical screen devices, the bounds of all
76 * configurations are relative to the virtual-coordinate system. The
77 * origin of the virtual-coordinate system is at the upper left-hand
78 * corner of the primary physical screen. Depending on the location
79 * of the primary screen in the virtual device, negative coordinates
80 * are possible, as shown in the following figure.
81 * <p>
82 * <img src="doc-files/MultiScreen.gif"
83 * alt="Diagram of virtual device encompassing three physical screens and one primary physical screen. The primary physical screen
84 * shows (0,0) coords while a different physical screen shows (-80,-100) coords."
85 * ALIGN=center HSPACE=10 VSPACE=7>
86 * <p>
87 * In such an environment, when calling <code>setLocation</code>,
88 * you must pass a virtual coordinate to this method. Similarly,
89 * calling <code>getLocationOnScreen</code> on a <code>Frame</code>
90 * returns virtual device coordinates. Call the <code>getBounds</code>
91 * method of a <code>GraphicsConfiguration</code> to find its origin in
92 * the virtual coordinate system.
93 * <p>
94 * The following code sets the
95 * location of the <code>Frame</code> at (10, 10) relative
96 * to the origin of the physical screen of the corresponding
97 * <code>GraphicsConfiguration</code>. If the bounds of the
98 * <code>GraphicsConfiguration</code> is not taken into account, the
99 * <code>Frame</code> location would be set at (10, 10) relative to the
100 * virtual-coordinate system and would appear on the primary physical
101 * screen, which might be different from the physical screen of the
102 * specified <code>GraphicsConfiguration</code>.
103 *
104 * <pre>
105 * Frame f = new Frame(GraphicsConfiguration gc);
106 * Rectangle bounds = gc.getBounds();
107 * f.setLocation(10 + bounds.x, 10 + bounds.y);
108 * </pre>
109 *
110 * <p>
111 * Frames are capable of generating the following types of
112 * <code>WindowEvent</code>s:
113 * <ul>
114 * <li><code>WINDOW_OPENED</code>
115 * <li><code>WINDOW_CLOSING</code>:
116 * <br>If the program doesn't
117 * explicitly hide or dispose the window while processing
118 * this event, the window close operation is canceled.
119 * <li><code>WINDOW_CLOSED</code>
120 * <li><code>WINDOW_ICONIFIED</code>
121 * <li><code>WINDOW_DEICONIFIED</code>
122 * <li><code>WINDOW_ACTIVATED</code>
123 * <li><code>WINDOW_DEACTIVATED</code>
124 * <li><code>WINDOW_GAINED_FOCUS</code>
125 * <li><code>WINDOW_LOST_FOCUS</code>
126 * <li><code>WINDOW_STATE_CHANGED</code>
127 * </ul>
128 *
129 * @author Sami Shaio
130 * @see WindowEvent
131 * @see Window#addWindowListener
132 * @since JDK1.0
133 */
134public class Frame extends Window implements MenuContainer {
135
136 /* Note: These are being obsoleted; programs should use the Cursor class
137 * variables going forward. See Cursor and Component.setCursor.
138 */
139
140 /**
141 * @deprecated replaced by <code>Cursor.DEFAULT_CURSOR</code>.
142 */
143 @Deprecated
144 public static final int DEFAULT_CURSOR = Cursor.DEFAULT_CURSOR;
145
146
147 /**
148 * @deprecated replaced by <code>Cursor.CROSSHAIR_CURSOR</code>.
149 */
150 @Deprecated
151 public static final int CROSSHAIR_CURSOR = Cursor.CROSSHAIR_CURSOR;
152
153 /**
154 * @deprecated replaced by <code>Cursor.TEXT_CURSOR</code>.
155 */
156 @Deprecated
157 public static final int TEXT_CURSOR = Cursor.TEXT_CURSOR;
158
159 /**
160 * @deprecated replaced by <code>Cursor.WAIT_CURSOR</code>.
161 */
162 @Deprecated
163 public static final int WAIT_CURSOR = Cursor.WAIT_CURSOR;
164
165 /**
166 * @deprecated replaced by <code>Cursor.SW_RESIZE_CURSOR</code>.
167 */
168 @Deprecated
169 public static final int SW_RESIZE_CURSOR = Cursor.SW_RESIZE_CURSOR;
170
171 /**
172 * @deprecated replaced by <code>Cursor.SE_RESIZE_CURSOR</code>.
173 */
174 @Deprecated
175 public static final int SE_RESIZE_CURSOR = Cursor.SE_RESIZE_CURSOR;
176
177 /**
178 * @deprecated replaced by <code>Cursor.NW_RESIZE_CURSOR</code>.
179 */
180 @Deprecated
181 public static final int NW_RESIZE_CURSOR = Cursor.NW_RESIZE_CURSOR;
182
183 /**
184 * @deprecated replaced by <code>Cursor.NE_RESIZE_CURSOR</code>.
185 */
186 @Deprecated
187 public static final int NE_RESIZE_CURSOR = Cursor.NE_RESIZE_CURSOR;
188
189 /**
190 * @deprecated replaced by <code>Cursor.N_RESIZE_CURSOR</code>.
191 */
192 @Deprecated
193 public static final int N_RESIZE_CURSOR = Cursor.N_RESIZE_CURSOR;
194
195 /**
196 * @deprecated replaced by <code>Cursor.S_RESIZE_CURSOR</code>.
197 */
198 @Deprecated
199 public static final int S_RESIZE_CURSOR = Cursor.S_RESIZE_CURSOR;
200
201 /**
202 * @deprecated replaced by <code>Cursor.W_RESIZE_CURSOR</code>.
203 */
204 @Deprecated
205 public static final int W_RESIZE_CURSOR = Cursor.W_RESIZE_CURSOR;
206
207 /**
208 * @deprecated replaced by <code>Cursor.E_RESIZE_CURSOR</code>.
209 */
210 @Deprecated
211 public static final int E_RESIZE_CURSOR = Cursor.E_RESIZE_CURSOR;
212
213 /**
214 * @deprecated replaced by <code>Cursor.HAND_CURSOR</code>.
215 */
216 @Deprecated
217 public static final int HAND_CURSOR = Cursor.HAND_CURSOR;
218
219 /**
220 * @deprecated replaced by <code>Cursor.MOVE_CURSOR</code>.
221 */
222 @Deprecated
223 public static final int MOVE_CURSOR = Cursor.MOVE_CURSOR;
224
225
226 /**
227 * Frame is in the "normal" state. This symbolic constant names a
228 * frame state with all state bits cleared.
229 * @see #setExtendedState(int)
230 * @see #getExtendedState
231 */
232 public static final int NORMAL = 0;
233
234 /**
235 * This state bit indicates that frame is iconified.
236 * @see #setExtendedState(int)
237 * @see #getExtendedState
238 */
239 public static final int ICONIFIED = 1;
240
241 /**
242 * This state bit indicates that frame is maximized in the
243 * horizontal direction.
244 * @see #setExtendedState(int)
245 * @see #getExtendedState
246 * @since 1.4
247 */
248 public static final int MAXIMIZED_HORIZ = 2;
249
250 /**
251 * This state bit indicates that frame is maximized in the
252 * vertical direction.
253 * @see #setExtendedState(int)
254 * @see #getExtendedState
255 * @since 1.4
256 */
257 public static final int MAXIMIZED_VERT = 4;
258
259 /**
260 * This state bit mask indicates that frame is fully maximized
261 * (that is both horizontally and vertically). It is just a
262 * convenience alias for
263 * <code>MAXIMIZED_VERT&nbsp;|&nbsp;MAXIMIZED_HORIZ</code>.
264 *
265 * <p>Note that the correct test for frame being fully maximized is
266 * <pre>
267 * (state & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH
268 * </pre>
269 *
270 * <p>To test is frame is maximized in <em>some</em> direction use
271 * <pre>
272 * (state & Frame.MAXIMIZED_BOTH) != 0
273 * </pre>
274 *
275 * @see #setExtendedState(int)
276 * @see #getExtendedState
277 * @since 1.4
278 */
279 public static final int MAXIMIZED_BOTH = MAXIMIZED_VERT | MAXIMIZED_HORIZ;
280
281 /**
282 * Maximized bounds for this frame.
283 * @see #setMaximizedBounds(Rectangle)
284 * @see #getMaximizedBounds
285 * @serial
286 * @since 1.4
287 */
288 Rectangle maximizedBounds;
289
290
291 /**
292 * This is the title of the frame. It can be changed
293 * at any time. <code>title</code> can be null and if
294 * this is the case the <code>title</code> = "".
295 *
296 * @serial
297 * @see #getTitle
298 * @see #setTitle(String)
299 */
300 String title = "Untitled";
301
302 /**
303 * The frames menubar. If <code>menuBar</code> = null
304 * the frame will not have a menubar.
305 *
306 * @serial
307 * @see #getMenuBar
308 * @see #setMenuBar(MenuBar)
309 */
310 MenuBar menuBar;
311
312 /**
313 * This field indicates whether the frame is resizable.
314 * This property can be changed at any time.
315 * <code>resizable</code> will be true if the frame is
316 * resizable, otherwise it will be false.
317 *
318 * @serial
319 * @see #isResizable()
320 */
321 boolean resizable = true;
322
323 /**
324 * This field indicates whether the frame is undecorated.
325 * This property can only be changed while the frame is not displayable.
326 * <code>undecorated</code> will be true if the frame is
327 * undecorated, otherwise it will be false.
328 *
329 * @serial
330 * @see #setUndecorated(boolean)
331 * @see #isUndecorated()
332 * @see Component#isDisplayable()
333 * @since 1.4
334 */
335 boolean undecorated = false;
336
337 /**
338 * <code>mbManagement</code> is only used by the Motif implementation.
339 *
340 * @serial
341 */
342 boolean mbManagement = false; /* used only by the Motif impl. */
343
344 // XXX: uwe: abuse old field for now
345 // will need to take care of serialization
346 private int state = NORMAL;
347
348 /*
349 * The Windows owned by the Frame.
350 * Note: in 1.2 this has been superceded by Window.ownedWindowList
351 *
352 * @serial
353 * @see java.awt.Window#ownedWindowList
354 */
355 Vector ownedWindows;
356
357 private static final String 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
378 * <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>
379 * @see java.awt.GraphicsEnvironment#isHeadless()
380 * @see Component#setSize
381 * @see Component#setVisible(boolean)
382 */
383 public Frame() throws HeadlessException {
384 this("");
385 }
386
387 /**
388 * Constructs a new, initially invisible {@code Frame} with the
389 * specified {@code GraphicsConfiguration}.
390 *
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 * @exception HeadlessException when
398 * <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>
399 * @see java.awt.GraphicsEnvironment#isHeadless()
400 * @since 1.3
401 */
402 public Frame(GraphicsConfiguration 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
413 * <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>
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 title) throws HeadlessException {
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 * @exception HeadlessException when
437 * <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>
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 * @since 1.3
443 */
444 public Frame(String title, GraphicsConfiguration gc) {
445 super(gc);
446 init(title, gc);
447 }
448
449 private void init(String title, GraphicsConfiguration gc) {
450 this.title = title;
451 SunToolkit.checkAndSetPolicy(this, false);
452 }
453
454 /**
455 * Construct a name for this component. Called by getName() when the
456 * name is null.
457 */
458 String constructComponentName() {
459 synchronized (Frame.class) {
460 return base + nameCounter++;
461 }
462 }
463
464 /**
465 * Makes this Frame displayable by connecting it to
466 * a native screen resource. Making a frame displayable will
467 * cause any of its children to be made displayable.
468 * This method is called internally by the toolkit and should
469 * not be called directly by programs.
470 * @see Component#isDisplayable
471 * @see #removeNotify
472 */
473 public void addNotify() {
474 synchronized (getTreeLock()) {
475 if (peer == null) {
476 peer = getToolkit().createFrame(this);
477 }
478 FramePeer p = (FramePeer)peer;
479 MenuBar menuBar = this.menuBar;
480 if (menuBar != null) {
481 mbManagement = true;
482 menuBar.addNotify();
483 p.setMenuBar(menuBar);
484 }
485 p.setMaximizedBounds(maximizedBounds);
486 super.addNotify();
487 }
488 }
489
490 /**
491 * Gets the title of the frame. The title is displayed in the
492 * frame's border.
493 * @return the title of this frame, or an empty string ("")
494 * if this frame doesn't have a title.
495 * @see #setTitle(String)
496 */
497 public String getTitle() {
498 return title;
499 }
500
501 /**
502 * Sets the title for this frame to the specified string.
503 * @param title the title to be displayed in the frame's border.
504 * A <code>null</code> value
505 * is treated as an empty string, "".
506 * @see #getTitle
507 */
508 public void setTitle(String title) {
509 String oldTitle = this.title;
510 if (title == null) {
511 title = "";
512 }
513
514
515 synchronized(this) {
516 this.title = title;
517 FramePeer peer = (FramePeer)this.peer;
518 if (peer != null) {
519 peer.setTitle(title);
520 }
521 }
522 firePropertyChange("title", oldTitle, title);
523 }
524
525 /**
526 * Returns the image to be displayed as the icon for this frame.
527 * <p>
528 * This method is obsolete and kept for backward compatibility
529 * only. Use {@link Window#getIconImages Window.getIconImages()} instead.
530 * <p>
531 * If a list of several images was specified as a Window's icon,
532 * this method will return the first item of the list.
533 *
534 * @return the icon image for this frame, or <code>null</code>
535 * if this frame doesn't have an icon image.
536 * @see #setIconImage(Image)
537 * @see Window#getIconImages()
538 * @see Window#setIconImages
539 */
540 public Image getIconImage() {
541 java.util.List<Image> icons = this.icons;
542 if (icons != null) {
543 if (icons.size() > 0) {
544 return icons.get(0);
545 }
546 }
547 return null;
548 }
549
550 /**
551 * {@inheritDoc}
552 */
553 public void setIconImage(Image image) {
554 super.setIconImage(image);
555 }
556
557 /**
558 * Gets the menu bar for this frame.
559 * @return the menu bar for this frame, or <code>null</code>
560 * if this frame doesn't have a menu bar.
561 * @see #setMenuBar(MenuBar)
562 */
563 public MenuBar getMenuBar() {
564 return menuBar;
565 }
566
567 /**
568 * Sets the menu bar for this frame to the specified menu bar.
569 * @param mb the menu bar being set.
570 * If this parameter is <code>null</code> then any
571 * existing menu bar on this frame is removed.
572 * @see #getMenuBar
573 */
574 public void setMenuBar(MenuBar mb) {
575 synchronized (getTreeLock()) {
576 if (menuBar == mb) {
577 return;
578 }
579 if ((mb != null) && (mb.parent != null)) {
580 mb.parent.remove(mb);
581 }
582 if (menuBar != null) {
583 remove(menuBar);
584 }
585 menuBar = mb;
586 if (menuBar != null) {
587 menuBar.parent = this;
588
589 FramePeer peer = (FramePeer)this.peer;
590 if (peer != null) {
591 mbManagement = true;
592 menuBar.addNotify();
593 if (valid) {
594 invalidate();
595 }
596 peer.setMenuBar(menuBar);
597 }
598 }
599 }
600 }
601
602 /**
603 * Indicates whether this frame is resizable by the user.
604 * By default, all frames are initially resizable.
605 * @return <code>true</code> if the user can resize this frame;
606 * <code>false</code> otherwise.
607 * @see java.awt.Frame#setResizable(boolean)
608 */
609 public boolean isResizable() {
610 return resizable;
611 }
612
613 /**
614 * Sets whether this frame is resizable by the user.
615 * @param resizable <code>true</code> if this frame is resizable;
616 * <code>false</code> otherwise.
617 * @see java.awt.Frame#isResizable
618 */
619 public void setResizable(boolean resizable) {
620 boolean oldResizable = this.resizable;
621 boolean testvalid = false;
622
623 synchronized (this) {
624 this.resizable = resizable;
625 FramePeer peer = (FramePeer)this.peer;
626 if (peer != null) {
627 peer.setResizable(resizable);
628 testvalid = true;
629 }
630 }
631
632 // On some platforms, changing the resizable state affects
633 // the insets of the Frame. If we could, we'd call invalidate()
634 // from the peer, but we need to guarantee that we're not holding
635 // the Frame lock when we call invalidate().
636 if (testvalid && valid) {
637 invalidate();
638 }
639 firePropertyChange("resizable", oldResizable, resizable);
640 }
641
642
643 /**
644 * Sets the state of this frame (obsolete).
645 * <p>
646 * In older versions of JDK a frame state could only be NORMAL or
647 * ICONIFIED. Since JDK 1.4 set of supported frame states is
648 * expanded and frame state is represented as a bitwise mask.
649 * <p>
650 * For compatibility with applications developed
651 * earlier this method still accepts
652 * {@code Frame.NORMAL} and
653 * {@code Frame.ICONIFIED} only. The iconic
654 * state of the frame is only changed, other aspects
655 * of frame state are not affected by this method. If
656 * the state passed to this method is neither {@code
657 * Frame.NORMAL} nor {@code Frame.ICONIFIED} the
658 * method performs no actions at all.
659 * <p>Note that if the state is not supported on a
660 * given platform, neither the state nor the return
661 * value of the {@link #getState} method will be
662 * changed. The application may determine whether a
663 * specific state is supported via the {@link
664 * java.awt.Toolkit#isFrameStateSupported} method.
665 * <p><b>If the frame is currently visible on the
666 * screen</b> (the {@link #isShowing} method returns
667 * {@code true}), the developer should examine the
668 * return value of the {@link
669 * java.awt.event.WindowEvent#getNewState} method of
670 * the {@code WindowEvent} received through the
671 * {@link java.awt.event.WindowStateListener} to
672 * determine that the state has actually been
673 * changed.
674 * <p><b>If the frame is not visible on the
675 * screen</b>, the events may or may not be
676 * generated. In this case the developer may assume
677 * that the state changes immediately after this
678 * method returns. Later, when the {@code
679 * setVisible(true)} method is invoked, the frame
680 * will attempt to apply this state. Receiving any
681 * {@link
682 * java.awt.event.WindowEvent#WINDOW_STATE_CHANGED}
683 * events is not guaranteed in this case also.
684 *
685 * @param state either <code>Frame.NORMAL</code> or
686 * <code>Frame.ICONIFIED</code>.
687 * @see #setExtendedState(int)
688 * @see java.awt.Window#addWindowStateListener
689 */
690 public synchronized void setState(int state) {
691 int current = getExtendedState();
692 if (state == ICONIFIED && (current & ICONIFIED) == 0) {
693 setExtendedState(current | ICONIFIED);
694 }
695 else if (state == NORMAL && (current & ICONIFIED) != 0) {
696 setExtendedState(current & ~ICONIFIED);
697 }
698 }
699
700 /**
701 * Sets the state of this frame. The state is
702 * represented as a bitwise mask.
703 * <ul>
704 * <li><code>NORMAL</code>
705 * <br>Indicates that no state bits are set.
706 * <li><code>ICONIFIED</code>
707 * <li><code>MAXIMIZED_HORIZ</code>
708 * <li><code>MAXIMIZED_VERT</code>
709 * <li><code>MAXIMIZED_BOTH</code>
710 * <br>Concatenates <code>MAXIMIZED_HORIZ</code>
711 * and <code>MAXIMIZED_VERT</code>.
712 * </ul>
713 * <p>Note that if the state is not supported on a
714 * given platform, neither the state nor the return
715 * value of the {@link #getExtendedState} method will
716 * be changed. The application may determine whether
717 * a specific state is supported via the {@link
718 * java.awt.Toolkit#isFrameStateSupported} method.
719 * <p><b>If the frame is currently visible on the
720 * screen</b> (the {@link #isShowing} method returns
721 * {@code true}), the developer should examine the
722 * return value of the {@link
723 * java.awt.event.WindowEvent#getNewState} method of
724 * the {@code WindowEvent} received through the
725 * {@link java.awt.event.WindowStateListener} to
726 * determine that the state has actually been
727 * changed.
728 * <p><b>If the frame is not visible on the
729 * screen</b>, the events may or may not be
730 * generated. In this case the developer may assume
731 * that the state changes immediately after this
732 * method returns. Later, when the {@code
733 * setVisible(true)} method is invoked, the frame
734 * will attempt to apply this state. Receiving any
735 * {@link
736 * java.awt.event.WindowEvent#WINDOW_STATE_CHANGED}
737 * events is not guaranteed in this case also.
738 *
739 * @param state a bitwise mask of frame state constants
740 * @since 1.4
741 * @see java.awt.Window#addWindowStateListener
742 */
743 public synchronized void setExtendedState(int state) {
744 if ( !isFrameStateSupported( state ) ) {
745 return;
746 }
747 this.state = state;
748 FramePeer peer = (FramePeer)this.peer;
749 if (peer != null) {
750 peer.setState(state);
751 }
752 }
753 private boolean isFrameStateSupported(int state) {
754 if( !getToolkit().isFrameStateSupported( state ) ) {
755 // * Toolkit.isFrameStateSupported returns always false
756 // on compound state even if all parts are supported;
757 // * if part of state is not supported, state is not supported;
758 // * MAXIMIZED_BOTH is not a compound state.
759 if( ((state & ICONIFIED) != 0) &&
760 !getToolkit().isFrameStateSupported( ICONIFIED )) {
761 return false;
762 }else {
763 state &= ~ICONIFIED;
764 }
765 return getToolkit().isFrameStateSupported( state );
766 }
767 return true;
768 }
769
770 /**
771 * Gets the state of this frame (obsolete).
772 * <p>
773 * In older versions of JDK a frame state could only be NORMAL or
774 * ICONIFIED. Since JDK 1.4 set of supported frame states is
775 * expanded and frame state is represented as a bitwise mask.
776 * <p>
777 * For compatibility with old programs this method still returns
778 * <code>Frame.NORMAL</code> and <code>Frame.ICONIFIED</code> but
779 * it only reports the iconic state of the frame, other aspects of
780 * frame state are not reported by this method.
781 *
782 * @return <code>Frame.NORMAL</code> or <code>Frame.ICONIFIED</code>.
783 * @see #setState(int)
784 * @see #getExtendedState
785 */
786 public synchronized int getState() {
787 return (getExtendedState() & ICONIFIED) != 0 ? ICONIFIED : NORMAL;
788 }
789
790
791 /**
792 * Gets the state of this frame. The state is
793 * represented as a bitwise mask.
794 * <ul>
795 * <li><code>NORMAL</code>
796 * <br>Indicates that no state bits are set.
797 * <li><code>ICONIFIED</code>
798 * <li><code>MAXIMIZED_HORIZ</code>
799 * <li><code>MAXIMIZED_VERT</code>
800 * <li><code>MAXIMIZED_BOTH</code>
801 * <br>Concatenates <code>MAXIMIZED_HORIZ</code>
802 * and <code>MAXIMIZED_VERT</code>.
803 * </ul>
804 *
805 * @return a bitwise mask of frame state constants
806 * @see #setExtendedState(int)
807 * @since 1.4
808 */
809 public synchronized int getExtendedState() {
810 FramePeer peer = (FramePeer)this.peer;
811 if (peer != null) {
812 state = peer.getState();
813 }
814 return state;
815 }
816
817 /**
818 * Sets the maximized bounds for this frame.
819 * <p>
820 * When a frame is in maximized state the system supplies some
821 * defaults bounds. This method allows some or all of those
822 * system supplied values to be overridden.
823 * <p>
824 * If <code>bounds</code> is <code>null</code>, accept bounds
825 * supplied by the system. If non-<code>null</code> you can
826 * override some of the system supplied values while accepting
827 * others by setting those fields you want to accept from system
828 * to <code>Integer.MAX_VALUE</code>.
829 * <p>
830 * On some systems only the size portion of the bounds is taken
831 * into account.
832 *
833 * @param bounds bounds for the maximized state
834 * @see #getMaximizedBounds()
835 * @since 1.4
836 */
837 public synchronized void setMaximizedBounds(Rectangle bounds) {
838 this.maximizedBounds = bounds;
839 FramePeer peer = (FramePeer)this.peer;
840 if (peer != null) {
841 peer.setMaximizedBounds(bounds);
842 }
843 }
844
845 /**
846 * Gets maximized bounds for this frame.
847 * Some fields may contain <code>Integer.MAX_VALUE</code> to indicate
848 * that system supplied values for this field must be used.
849 *
850 * @return maximized bounds for this frame; may be <code>null</code>
851 * @see #setMaximizedBounds(Rectangle)
852 * @since 1.4
853 */
854 public Rectangle getMaximizedBounds() {
855 return maximizedBounds;
856 }
857
858
859 /**
860 * Disables or enables decorations for this frame.
861 * This method can only be called while the frame is not displayable.
862 * @param undecorated <code>true</code> if no frame decorations are
863 * to be enabled;
864 * <code>false</code> if frame decorations are to be enabled.
865 * @throws <code>IllegalComponentStateException</code> if the frame
866 * is displayable.
867 * @see #isUndecorated
868 * @see Component#isDisplayable
869 * @see javax.swing.JFrame#setDefaultLookAndFeelDecorated(boolean)
870 * @since 1.4
871 */
872 public void setUndecorated(boolean undecorated) {
873 /* Make sure we don't run in the middle of peer creation.*/
874 synchronized (getTreeLock()) {
875 if (isDisplayable()) {
876 throw new IllegalComponentStateException("The frame is displayable.");
877 }
878 this.undecorated = undecorated;
879 }
880 }
881
882 /**
883 * Indicates whether this frame is undecorated.
884 * By default, all frames are initially decorated.
885 * @return <code>true</code> if frame is undecorated;
886 * <code>false</code> otherwise.
887 * @see java.awt.Frame#setUndecorated(boolean)
888 * @since 1.4
889 */
890 public boolean isUndecorated() {
891 return undecorated;
892 }
893
894 /**
895 * Removes the specified menu bar from this frame.
896 * @param m the menu component to remove.
897 * If <code>m</code> is <code>null</code>, then
898 * no action is taken
899 */
900 public void remove(MenuComponent m) {
901 if (m == null) {
902 return;
903 }
904 synchronized (getTreeLock()) {
905 if (m == menuBar) {
906 menuBar = null;
907 FramePeer peer = (FramePeer)this.peer;
908 if (peer != null) {
909 mbManagement = true;
910 if (valid) {
911 invalidate();
912 }
913 peer.setMenuBar(null);
914 m.removeNotify();
915 }
916 m.parent = null;
917 } else {
918 super.remove(m);
919 }
920 }
921 }
922
923 /**
924 * Makes this Frame undisplayable by removing its connection
925 * to its native screen resource. Making a Frame undisplayable
926 * will cause any of its children to be made undisplayable.
927 * This method is called by the toolkit internally and should
928 * not be called directly by programs.
929 * @see Component#isDisplayable
930 * @see #addNotify
931 */
932 public void removeNotify() {
933 synchronized (getTreeLock()) {
934 FramePeer peer = (FramePeer)this.peer;
935 if (peer != null) {
936 // get the latest Frame state before disposing
937 getState();
938
939 if (menuBar != null) {
940 mbManagement = true;
941 peer.setMenuBar(null);
942 menuBar.removeNotify();
943 }
944 }
945 super.removeNotify();
946 }
947 }
948
949 void postProcessKeyEvent(KeyEvent e) {
950 if (menuBar != null && menuBar.handleShortcut(e)) {
951 e.consume();
952 return;
953 }
954 super.postProcessKeyEvent(e);
955 }
956
957 /**
958 * Returns a string representing the state of this <code>Frame</code>.
959 * This method is intended to be used only for debugging purposes, and the
960 * content and format of the returned string may vary between
961 * implementations. The returned string may be empty but may not be
962 * <code>null</code>.
963 *
964 * @return the parameter string of this frame
965 */
966 protected String paramString() {
967 String str = super.paramString();
968 if (title != null) {
969 str += ",title=" + title;
970 }
971 if (resizable) {
972 str += ",resizable";
973 }
974 getExtendedState(); // sync with peer
975 if (state == NORMAL) {
976 str += ",normal";
977 }
978 else {
979 if ((state & ICONIFIED) != 0) {
980 str += ",iconified";
981 }
982 if ((state & MAXIMIZED_BOTH) == MAXIMIZED_BOTH) {
983 str += ",maximized";
984 }
985 else if ((state & MAXIMIZED_HORIZ) != 0) {
986 str += ",maximized_horiz";
987 }
988 else if ((state & MAXIMIZED_VERT) != 0) {
989 str += ",maximized_vert";
990 }
991 }
992 return str;
993 }
994
995 /**
996 * @deprecated As of JDK version 1.1,
997 * replaced by <code>Component.setCursor(Cursor)</code>.
998 */
999 @Deprecated
1000 public void setCursor(int cursorType) {
1001 if (cursorType < DEFAULT_CURSOR || cursorType > MOVE_CURSOR) {
1002 throw new IllegalArgumentException("illegal cursor type");
1003 }
1004 setCursor(Cursor.getPredefinedCursor(cursorType));
1005 }
1006
1007 /**
1008 * @deprecated As of JDK version 1.1,
1009 * replaced by <code>Component.getCursor()</code>.
1010 */
1011 @Deprecated
1012 public int getCursorType() {
1013 return (getCursor().getType());
1014 }
1015
1016 /**
1017 * Returns an array of all {@code Frame}s created by this application.
1018 * If called from an applet, the array includes only the {@code Frame}s
1019 * accessible by that applet.
1020 * <p>
1021 * <b>Warning:</b> this method may return system created frames, such
1022 * as a shared, hidden frame which is used by Swing. Applications
1023 * should not assume the existence of these frames, nor should an
1024 * application assume anything about these frames such as component
1025 * positions, <code>LayoutManager</code>s or serialization.
1026 * <p>
1027 * <b>Note</b>: To obtain a list of all ownerless windows, including
1028 * ownerless {@code Dialog}s (introduced in release 1.6), use {@link
1029 * Window#getOwnerlessWindows Window.getOwnerlessWindows}.
1030 *
1031 * @see Window#getWindows()
1032 * @see Window#getOwnerlessWindows
1033 *
1034 * @since 1.2
1035 */
1036 public static Frame[] getFrames() {
1037 Window[] allWindows = Window.getWindows();
1038
1039 int frameCount = 0;
1040 for (Window w : allWindows) {
1041 if (w instanceof Frame) {
1042 frameCount++;
1043 }
1044 }
1045
1046 Frame[] frames = new Frame[frameCount];
1047 int c = 0;
1048 for (Window w : allWindows) {
1049 if (w instanceof Frame) {
1050 frames[c++] = (Frame)w;
1051 }
1052 }
1053
1054 return frames;
1055 }
1056
1057 /* Serialization support. If there's a MenuBar we restore
1058 * its (transient) parent field here. Likewise for top level
1059 * windows that are "owned" by this frame.
1060 */
1061
1062 /**
1063 * <code>Frame</code>'s Serialized Data Version.
1064 *
1065 * @serial
1066 */
1067 private int frameSerializedDataVersion = 1;
1068
1069 /**
1070 * Writes default serializable fields to stream. Writes
1071 * an optional serializable icon <code>Image</code>, which is
1072 * available as of 1.4.
1073 *
1074 * @param s the <code>ObjectOutputStream</code> to write
1075 * @serialData an optional icon <code>Image</code>
1076 * @see java.awt.Image
1077 * @see #getIconImage
1078 * @see #setIconImage(Image)
1079 * @see #readObject(ObjectInputStream)
1080 */
1081 private void writeObject(ObjectOutputStream s)
1082 throws IOException
1083 {
1084 s.defaultWriteObject();
1085 if (icons != null && icons.size() > 0) {
1086 Image icon1 = icons.get(0);
1087 if (icon1 instanceof Serializable) {
1088 s.writeObject(icon1);
1089 return;
1090 }
1091 }
1092 s.writeObject(null);
1093 }
1094
1095 /**
1096 * Reads the <code>ObjectInputStream</code>. Tries
1097 * to read an icon <code>Image</code>, which is optional
1098 * data available as of 1.4. If an icon <code>Image</code>
1099 * is not available, but anything other than an EOF
1100 * is detected, an <code>OptionalDataException</code>
1101 * will be thrown.
1102 * Unrecognized keys or values will be ignored.
1103 *
1104 * @param s the <code>ObjectInputStream</code> to read
1105 * @exception java.io.OptionalDataException if an icon <code>Image</code>
1106 * is not available, but anything other than an EOF
1107 * is detected
1108 * @exception HeadlessException if
1109 * <code>GraphicsEnvironment.isHeadless</code> returns
1110 * <code>true</code>
1111 * @see java.awt.GraphicsEnvironment#isHeadless()
1112 * @see java.awt.Image
1113 * @see #getIconImage
1114 * @see #setIconImage(Image)
1115 * @see #writeObject(ObjectOutputStream)
1116 */
1117 private void readObject(ObjectInputStream s)
1118 throws ClassNotFoundException, IOException, HeadlessException
1119 {
1120 // HeadlessException is thrown by Window's readObject
1121 s.defaultReadObject();
1122 try {
1123 Image icon = (Image) s.readObject();
1124 if (icons == null) {
1125 icons = new ArrayList<Image>();
1126 icons.add(icon);
1127 }
1128 } catch (java.io.OptionalDataException e) {
1129 // pre-1.4 instances will not have this optional data.
1130 // 1.6 and later instances serialize icons in the Window class
1131 // e.eof will be true to indicate that there is no more
1132 // data available for this object.
1133
1134 // If e.eof is not true, throw the exception as it
1135 // might have been caused by unrelated reasons.
1136 if (!e.eof) {
1137 throw (e);
1138 }
1139 }
1140
1141 if (menuBar != null)
1142 menuBar.parent = this;
1143
1144 // Ensure 1.1 serialized Frames can read & hook-up
1145 // owned windows properly
1146 //
1147 if (ownedWindows != null) {
1148 for (int i = 0; i < ownedWindows.size(); i++) {
1149 connectOwnedWindow((Window) ownedWindows.elementAt(i));
1150 }
1151 ownedWindows = null;
1152 }
1153 }
1154
1155 /**
1156 * Initialize JNI field and method IDs
1157 */
1158 private static native void initIDs();
1159
1160 /*
1161 * --- Accessibility Support ---
1162 *
1163 */
1164
1165 /**
1166 * Gets the AccessibleContext associated with this Frame.
1167 * For frames, the AccessibleContext takes the form of an
1168 * AccessibleAWTFrame.
1169 * A new AccessibleAWTFrame instance is created if necessary.
1170 *
1171 * @return an AccessibleAWTFrame that serves as the
1172 * AccessibleContext of this Frame
1173 * @since 1.3
1174 */
1175 public AccessibleContext getAccessibleContext() {
1176 if (accessibleContext == null) {
1177 accessibleContext = new AccessibleAWTFrame();
1178 }
1179 return accessibleContext;
1180 }
1181
1182 /**
1183 * This class implements accessibility support for the
1184 * <code>Frame</code> class. It provides an implementation of the
1185 * Java Accessibility API appropriate to frame user-interface elements.
1186 * @since 1.3
1187 */
1188 protected class AccessibleAWTFrame extends AccessibleAWTWindow
1189 {
1190 /*
1191 * JDK 1.3 serialVersionUID
1192 */
1193 private static final long serialVersionUID = -6172960752956030250L;
1194
1195 /**
1196 * Get the role of this object.
1197 *
1198 * @return an instance of AccessibleRole describing the role of the
1199 * object
1200 * @see AccessibleRole
1201 */
1202 public AccessibleRole getAccessibleRole() {
1203 return AccessibleRole.FRAME;
1204 }
1205
1206 /**
1207 * Get the state of this object.
1208 *
1209 * @return an instance of AccessibleStateSet containing the current
1210 * state set of the object
1211 * @see AccessibleState
1212 */
1213 public AccessibleStateSet getAccessibleStateSet() {
1214 AccessibleStateSet states = super.getAccessibleStateSet();
1215 if (getFocusOwner() != null) {
1216 states.add(AccessibleState.ACTIVE);
1217 }
1218 if (isResizable()) {
1219 states.add(AccessibleState.RESIZABLE);
1220 }
1221 return states;
1222 }
1223
1224
1225 } // inner class AccessibleAWTFrame
1226
1227}