1 /* Frame.java -- AWT toplevel window
2 Copyright (C) 1999, 2000, 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.awt
.peer
.FramePeer
;
42 import java
.lang
.ref
.WeakReference
;
43 import java
.util
.ArrayList
;
44 import java
.util
.Iterator
;
45 import java
.util
.Vector
;
47 import javax
.accessibility
.AccessibleContext
;
48 import javax
.accessibility
.AccessibleRole
;
49 import javax
.accessibility
.AccessibleState
;
50 import javax
.accessibility
.AccessibleStateSet
;
53 * This class is a top-level window with a title bar and window
56 * @author Aaron M. Renn (arenn@urbanophile.com)
58 public class Frame
extends Window
implements MenuContainer
61 * Constant for the default cursor.
62 * @deprecated Replaced by <code>Cursor.DEFAULT_CURSOR</code> instead.
64 public static final int DEFAULT_CURSOR
= Cursor
.DEFAULT_CURSOR
;
67 * Constant for a cross-hair cursor.
68 * @deprecated Use <code>Cursor.CROSSHAIR_CURSOR</code> instead.
70 public static final int CROSSHAIR_CURSOR
= Cursor
.CROSSHAIR_CURSOR
;
73 * Constant for a cursor over a text field.
74 * @deprecated Use <code>Cursor.TEXT_CURSOR</code> instead.
76 public static final int TEXT_CURSOR
= Cursor
.TEXT_CURSOR
;
79 * Constant for a cursor to display while waiting for an action to complete.
80 * @deprecated Use <code>Cursor.WAIT_CURSOR</code>.
82 public static final int WAIT_CURSOR
= Cursor
.WAIT_CURSOR
;
85 * Cursor used over SW corner of window decorations.
86 * @deprecated Use <code>Cursor.SW_RESIZE_CURSOR</code> instead.
88 public static final int SW_RESIZE_CURSOR
= Cursor
.SW_RESIZE_CURSOR
;
91 * Cursor used over SE corner of window decorations.
92 * @deprecated Use <code>Cursor.SE_RESIZE_CURSOR</code> instead.
94 public static final int SE_RESIZE_CURSOR
= Cursor
.SE_RESIZE_CURSOR
;
97 * Cursor used over NW corner of window decorations.
98 * @deprecated Use <code>Cursor.NW_RESIZE_CURSOR</code> instead.
100 public static final int NW_RESIZE_CURSOR
= Cursor
.NW_RESIZE_CURSOR
;
103 * Cursor used over NE corner of window decorations.
104 * @deprecated Use <code>Cursor.NE_RESIZE_CURSOR</code> instead.
106 public static final int NE_RESIZE_CURSOR
= Cursor
.NE_RESIZE_CURSOR
;
109 * Cursor used over N edge of window decorations.
110 * @deprecated Use <code>Cursor.N_RESIZE_CURSOR</code> instead.
112 public static final int N_RESIZE_CURSOR
= Cursor
.N_RESIZE_CURSOR
;
115 * Cursor used over S edge of window decorations.
116 * @deprecated Use <code>Cursor.S_RESIZE_CURSOR</code> instead.
118 public static final int S_RESIZE_CURSOR
= Cursor
.S_RESIZE_CURSOR
;
121 * Cursor used over E edge of window decorations.
122 * @deprecated Use <code>Cursor.E_RESIZE_CURSOR</code> instead.
124 public static final int E_RESIZE_CURSOR
= Cursor
.E_RESIZE_CURSOR
;
127 * Cursor used over W edge of window decorations.
128 * @deprecated Use <code>Cursor.W_RESIZE_CURSOR</code> instead.
130 public static final int W_RESIZE_CURSOR
= Cursor
.W_RESIZE_CURSOR
;
133 * Constant for a hand cursor.
134 * @deprecated Use <code>Cursor.HAND_CURSOR</code> instead.
136 public static final int HAND_CURSOR
= Cursor
.HAND_CURSOR
;
139 * Constant for a cursor used during window move operations.
140 * @deprecated Use <code>Cursor.MOVE_CURSOR</code> instead.
142 public static final int MOVE_CURSOR
= Cursor
.MOVE_CURSOR
;
144 public static final int ICONIFIED
= 1;
145 public static final int MAXIMIZED_BOTH
= 6;
146 public static final int MAXIMIZED_HORIZ
= 2;
147 public static final int MAXIMIZED_VERT
= 4;
148 public static final int NORMAL
= 0;
150 // Serialization version constant
151 private static final long serialVersionUID
= 2673458971256075116L;
154 * @serial The version of the class data being serialized
155 * // FIXME: what is this value?
157 private int frameSerializedDataVersion
;
160 * @serial Image used as the icon when this frame is minimized.
165 * @serial Constant used by the JDK Motif peer set. Not used in
166 * this implementation.
168 private boolean mbManagement
;
171 * @serial The menu bar for this frame.
173 //private MenuBar menuBar = new MenuBar();
174 private MenuBar menuBar
;
177 * @serial A list of other top-level windows owned by this window.
179 Vector ownedWindows
= new Vector();
182 * @serial Indicates whether or not this frame is resizable.
184 private boolean resizable
= true;
187 * @serial The state of this frame.
188 * // FIXME: What are the values here?
193 * @serial The title of the frame.
195 private String title
= "";
198 * Maximized bounds for this frame.
200 private Rectangle maximizedBounds
;
203 * This field indicates whether the frame is undecorated or not.
205 private boolean undecorated
= false;
208 * The number used to generate the name returned by getName.
210 private static transient long next_frame_number
;
213 * Initializes a new instance of <code>Frame</code> that is not visible
224 * Initializes a new instance of <code>Frame</code> that is not visible
225 * and has the specified title.
227 * @param title The title of this frame.
234 // Top-level frames are initially invisible.
240 Frame(GraphicsConfiguration gc
)
248 Frame(String title
, GraphicsConfiguration gc
)
257 * Returns this frame's title string.
259 * @return This frame's title string.
268 * Sets this frame's title to the specified value.
270 * @param title The new frame title.
272 public synchronized void
273 setTitle(String title
)
277 ((FramePeer
) peer
).setTitle(title
);
281 * Returns this frame's icon.
283 * @return This frame's icon, or <code>null</code> if this frame does not
293 * Sets this frame's icon to the specified value.
295 * @icon The new icon for this frame.
297 public synchronized void
298 setIconImage(Image icon
)
302 ((FramePeer
) peer
).setIconImage(icon
);
306 * Returns this frame's menu bar.
308 * @return This frame's menu bar, or <code>null</code> if this frame
309 * does not have a menu bar.
318 * Sets this frame's menu bar.
320 * @param menuBar The new menu bar for this frame.
322 public synchronized void
323 setMenuBar(MenuBar menuBar
)
327 if (this.menuBar
!= null)
328 this.menuBar
.removeNotify();
332 ((FramePeer
) peer
).setMenuBar(menuBar
);
334 this.menuBar
= menuBar
;
338 * Tests whether or not this frame is resizable. This will be
339 * <code>true</code> by default.
341 * @return <code>true</code> if this frame is resizable, <code>false</code>
351 * Sets the resizability of this frame to the specified value.
353 * @param resizable <code>true</code> to make the frame resizable,
354 * <code>false</code> to make it non-resizable.
356 public synchronized void
357 setResizable(boolean resizable
)
359 this.resizable
= resizable
;
361 ((FramePeer
) peer
).setResizable(resizable
);
365 * Returns the cursor type of the cursor for this window. This will
366 * be one of the constants in this class.
368 * @return The cursor type for this frame.
370 * @deprecated Use <code>Component.getCursor()</code> instead.
375 return(getCursor().getType());
379 * Sets the cursor for this window to the specified type. The specified
380 * type should be one of the constants in this class.
382 * @param type The cursor type.
384 * @deprecated Use <code>Component.setCursor(Cursor)</code> instead.
389 setCursor(new Cursor(type
));
393 * Removes the specified component from this frame's menu.
395 * @param menu The menu component to remove.
398 remove(MenuComponent menu
)
400 menuBar
.remove(menu
);
404 * Notifies this frame that it should create its native peer.
406 private static void fireDummyEvent()
408 EventQueue
.invokeLater(new Runnable()
423 peer
= getToolkit ().createFrame (this);
425 // We now know there's a Frame (us) with a live peer, so we can start the
426 // fundamental queue and dispatch thread, by inserting a dummy event.
427 if (parent
!= null && parent
.isDisplayable())
433 public void removeNotify()
436 menuBar
.removeNotify();
437 super.removeNotify();
439 // By now we've been disconnected from the peer, and the peer set to
440 // null. This is formally the same as saying "we just became
441 // un-displayable", so we wake up the event queue with a dummy event to
442 // see if it's time to shut down.
447 * Returns a debugging string describing this window.
449 * @return A debugging string describing this window.
451 protected String
paramString ()
453 String title
= getTitle ();
455 String resizable
= "";
457 resizable
= ",resizable";
466 state
= ",iconified";
469 state
= ",maximized-both";
471 case MAXIMIZED_HORIZ
:
472 state
= ",maximized-horiz";
475 state
= ",maximized-vert";
479 return super.paramString () + ",title=" + title
+ resizable
+ state
;
482 private static ArrayList weakFrames
= new ArrayList();
484 private static void noteFrame(Frame f
)
486 weakFrames
.add(new WeakReference(f
));
489 public static Frame
[] getFrames()
492 synchronized (weakFrames
)
494 Iterator i
= weakFrames
.iterator();
497 WeakReference wr
= (WeakReference
) i
.next();
498 if (wr
.get() != null)
505 Frame
[] frames
= new Frame
[n
];
507 i
= weakFrames
.iterator();
510 WeakReference wr
= (WeakReference
) i
.next();
511 if (wr
.get() != null)
512 frames
[n
++] = (Frame
) wr
.get();
519 public void setState (int state
)
521 int current_state
= getExtendedState ();
524 && (current_state
& ICONIFIED
) != 0)
525 setExtendedState (current_state
| ICONIFIED
);
527 if (state
== ICONIFIED
528 && (current_state
& ~ICONIFIED
) == 0)
529 setExtendedState (current_state
& ~ICONIFIED
);
532 public int getState ()
534 /* FIXME: State might have changed in the peer... Must check. */
536 return (state
& ICONIFIED
) != 0 ? ICONIFIED
: NORMAL
;
542 public void setExtendedState (int state
)
550 public int getExtendedState ()
558 public void setMaximizedBounds (Rectangle maximizedBounds
)
560 this.maximizedBounds
= maximizedBounds
;
564 * Returns the maximized bounds of this frame.
566 * @return the maximized rectangle, may be null.
570 public Rectangle
getMaximizedBounds ()
572 return maximizedBounds
;
576 * Returns whether this frame is undecorated or not.
580 public boolean isUndecorated ()
586 * Disables or enables decorations for this frame. This method can only be
587 * called while the frame is not displayable.
589 * @exception IllegalComponentStateException If this frame is displayable.
593 public void setUndecorated (boolean undecorated
)
595 if (!isDisplayable ())
596 throw new IllegalComponentStateException ();
598 this.undecorated
= undecorated
;
602 * Generate a unique name for this frame.
604 * @return A unique name for this frame.
606 String
generateName ()
608 return "frame" + getUniqueLong ();
611 private static synchronized long getUniqueLong ()
613 return next_frame_number
++;
616 protected class AccessibleAWTFrame
extends AccessibleAWTWindow
618 public AccessibleRole
getAccessibleRole()
620 return AccessibleRole
.FRAME
;
623 public AccessibleStateSet
getAccessibleState()
625 AccessibleStateSet states
= super.getAccessibleStateSet();
627 states
.add(AccessibleState
.RESIZABLE
);
628 if ((state
& ICONIFIED
) != 0)
629 states
.add(AccessibleState
.ICONIFIED
);
635 * Gets the AccessibleContext associated with this <code>Frame</code>.
636 * The context is created, if necessary.
638 * @return the associated context
640 public AccessibleContext
getAccessibleContext()
642 /* Create the context if this is the first request */
643 if (accessibleContext
== null)
644 accessibleContext
= new AccessibleAWTFrame();
645 return accessibleContext
;