1 /* Frame.java -- AWT toplevel window
2 Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
42 import java
.awt
.peer
.FramePeer
;
43 import java
.lang
.ref
.WeakReference
;
44 import java
.util
.ArrayList
;
45 import java
.util
.Iterator
;
46 import java
.util
.Vector
;
48 import javax
.accessibility
.AccessibleContext
;
49 import javax
.accessibility
.AccessibleRole
;
50 import javax
.accessibility
.AccessibleState
;
51 import javax
.accessibility
.AccessibleStateSet
;
54 * This class is a top-level window with a title bar and window
57 * @author Aaron M. Renn (arenn@urbanophile.com)
59 public class Frame
extends Window
implements MenuContainer
63 * Constant for the default cursor.
65 * @deprecated Replaced by <code>Cursor.DEFAULT_CURSOR</code> instead.
67 public static final int DEFAULT_CURSOR
= Cursor
.DEFAULT_CURSOR
;
70 * Constant for a cross-hair cursor.
72 * @deprecated Use <code>Cursor.CROSSHAIR_CURSOR</code> instead.
74 public static final int CROSSHAIR_CURSOR
= Cursor
.CROSSHAIR_CURSOR
;
77 * Constant for a cursor over a text field.
79 * @deprecated Use <code>Cursor.TEXT_CURSOR</code> instead.
81 public static final int TEXT_CURSOR
= Cursor
.TEXT_CURSOR
;
84 * Constant for a cursor to display while waiting for an action to complete.
86 * @deprecated Use <code>Cursor.WAIT_CURSOR</code>.
88 public static final int WAIT_CURSOR
= Cursor
.WAIT_CURSOR
;
91 * Cursor used over SW corner of window decorations.
93 * @deprecated Use <code>Cursor.SW_RESIZE_CURSOR</code> instead.
95 public static final int SW_RESIZE_CURSOR
= Cursor
.SW_RESIZE_CURSOR
;
98 * Cursor used over SE corner of window decorations.
99 * @deprecated Use <code>Cursor.SE_RESIZE_CURSOR</code> instead.
101 public static final int SE_RESIZE_CURSOR
= Cursor
.SE_RESIZE_CURSOR
;
104 * Cursor used over NW corner of window decorations.
106 * @deprecated Use <code>Cursor.NW_RESIZE_CURSOR</code> instead.
108 public static final int NW_RESIZE_CURSOR
= Cursor
.NW_RESIZE_CURSOR
;
111 * Cursor used over NE corner of window decorations.
113 * @deprecated Use <code>Cursor.NE_RESIZE_CURSOR</code> instead.
115 public static final int NE_RESIZE_CURSOR
= Cursor
.NE_RESIZE_CURSOR
;
118 * Cursor used over N edge of window decorations.
120 * @deprecated Use <code>Cursor.N_RESIZE_CURSOR</code> instead.
122 public static final int N_RESIZE_CURSOR
= Cursor
.N_RESIZE_CURSOR
;
125 * Cursor used over S edge of window decorations.
127 * @deprecated Use <code>Cursor.S_RESIZE_CURSOR</code> instead.
129 public static final int S_RESIZE_CURSOR
= Cursor
.S_RESIZE_CURSOR
;
132 * Cursor used over E edge of window decorations.
134 * @deprecated Use <code>Cursor.E_RESIZE_CURSOR</code> instead.
136 public static final int E_RESIZE_CURSOR
= Cursor
.E_RESIZE_CURSOR
;
139 * Cursor used over W edge of window decorations.
141 * @deprecated Use <code>Cursor.W_RESIZE_CURSOR</code> instead.
143 public static final int W_RESIZE_CURSOR
= Cursor
.W_RESIZE_CURSOR
;
146 * Constant for a hand cursor.
148 * @deprecated Use <code>Cursor.HAND_CURSOR</code> instead.
150 public static final int HAND_CURSOR
= Cursor
.HAND_CURSOR
;
153 * Constant for a cursor used during window move operations.
155 * @deprecated Use <code>Cursor.MOVE_CURSOR</code> instead.
157 public static final int MOVE_CURSOR
= Cursor
.MOVE_CURSOR
;
159 public static final int ICONIFIED
= 1;
160 public static final int MAXIMIZED_BOTH
= 6;
161 public static final int MAXIMIZED_HORIZ
= 2;
162 public static final int MAXIMIZED_VERT
= 4;
163 public static final int NORMAL
= 0;
165 //Serialization version constant
166 private static final long serialVersionUID
= 2673458971256075116L;
169 * @serial The version of the class data being serialized
170 * FIXME: what is this value?
172 private int frameSerializedDataVersion
;
175 * @serial Image used as the icon when this frame is minimized.
180 * @serial Constant used by the JDK Motif peer set. Not used in
181 * this implementation.
183 private boolean mbManagement
;
186 * @serial The menu bar for this frame.
188 private MenuBar menuBar
;
191 * @serial A list of other top-level windows owned by this window.
193 Vector ownedWindows
= new Vector();
196 * @serial Indicates whether or not this frame is resizable.
198 private boolean resizable
= true;
201 * @serial The state of this frame.
202 * // FIXME: What are the values here?
203 * This is package-private to avoid an accessor method.
208 * @serial The title of the frame.
210 private String title
= "";
213 * Maximized bounds for this frame.
215 private Rectangle maximizedBounds
;
218 * This field indicates whether the frame is undecorated or not.
220 private boolean undecorated
= false;
223 * The number used to generate the name returned by getName.
225 private static transient long next_frame_number
;
228 * Initializes a new instance of <code>Frame</code> that is not visible
238 * Initializes a new instance of <code>Frame</code> that is not visible
239 * and has the specified title.
241 * @param title the title of this frame
243 public Frame(String title
)
247 // Top-level frames are initially invisible.
252 public Frame(GraphicsConfiguration gc
)
259 public Frame(String title
, GraphicsConfiguration gc
)
268 * Returns this frame's title string.
270 * @return this frame's title string
272 public String
getTitle()
278 * Sets this frame's title to the specified value.
280 * @param title the new frame title
282 public synchronized void setTitle(String title
)
286 ((FramePeer
) peer
).setTitle(title
);
290 * Returns this frame's icon.
292 * @return this frame's icon, or <code>null</code> if this frame does not
295 public Image
getIconImage()
301 * Sets this frame's icon to the specified value.
303 * @icon the new icon for this frame
305 public synchronized void setIconImage(Image icon
)
309 ((FramePeer
) peer
).setIconImage(icon
);
313 * Returns this frame's menu bar.
315 * @return this frame's menu bar, or <code>null</code> if this frame
316 * does not have a menu bar
318 public MenuBar
getMenuBar()
324 * Sets this frame's menu bar. Removes any existing menu bar. If the
325 * given menu bar is part of another frame it will be removed from
328 * @param menuBar the new menu bar for this frame
330 public synchronized void setMenuBar(MenuBar menuBar
)
332 if (this.menuBar
!= null)
333 remove(this.menuBar
);
335 this.menuBar
= menuBar
;
338 MenuContainer parent
= menuBar
.getParent();
340 parent
.remove(menuBar
);
341 menuBar
.setParent(this);
348 ((FramePeer
) peer
).setMenuBar(menuBar
);
354 * Tests whether or not this frame is resizable. This will be
355 * <code>true</code> by default.
357 * @return <code>true</code> if this frame is resizable, <code>false</code>
360 public boolean isResizable()
366 * Sets the resizability of this frame to the specified value.
368 * @param resizable <code>true</code> to make the frame resizable,
369 * <code>false</code> to make it non-resizable
371 public synchronized void setResizable(boolean resizable
)
373 this.resizable
= resizable
;
375 ((FramePeer
) peer
).setResizable(resizable
);
379 * Returns the cursor type of the cursor for this window. This will
380 * be one of the constants in this class.
382 * @return the cursor type for this frame
384 * @deprecated Use <code>Component.getCursor()</code> instead.
386 public int getCursorType()
388 return getCursor().getType();
392 * Sets the cursor for this window to the specified type. The specified
393 * type should be one of the constants in this class.
395 * @param type the cursor type
397 * @deprecated Use <code>Component.setCursor(Cursor)</code> instead.
399 public void setCursor(int type
)
401 setCursor(new Cursor(type
));
405 * Removes the specified menu component from this frame. If it is
406 * the current MenuBar it is removed from the frame. If it is a
407 * Popup it is removed from this component. If it is any other menu
408 * component it is ignored.
410 * @param menu the menu component to remove
412 public void remove(MenuComponent menu
)
420 ((FramePeer
) peer
).setMenuBar(null);
421 menuBar
.removeNotify();
423 menuBar
.setParent(null);
431 public void addNotify()
436 peer
= getToolkit ().createFrame (this);
441 public void removeNotify()
444 menuBar
.removeNotify();
445 super.removeNotify();
449 * Returns a debugging string describing this window.
451 * @return a debugging string describing this window
453 protected String
paramString()
455 String title
= getTitle();
457 String resizable
= "";
459 resizable
= ",resizable";
468 state
= ",iconified";
471 state
= ",maximized-both";
473 case MAXIMIZED_HORIZ
:
474 state
= ",maximized-horiz";
477 state
= ",maximized-vert";
481 return super.paramString () + ",title=" + title
+ resizable
+ state
;
484 private static ArrayList weakFrames
= new ArrayList();
486 private static void noteFrame(Frame f
)
488 weakFrames
.add(new WeakReference(f
));
491 public static Frame
[] getFrames()
494 synchronized (weakFrames
)
496 Iterator i
= weakFrames
.iterator();
499 WeakReference wr
= (WeakReference
) i
.next();
500 if (wr
.get() != null)
507 Frame
[] frames
= new Frame
[n
];
509 i
= weakFrames
.iterator();
512 WeakReference wr
= (WeakReference
) i
.next();
513 if (wr
.get() != null)
514 frames
[n
++] = (Frame
) wr
.get();
521 public void setState(int state
)
523 int current_state
= getExtendedState ();
526 && (current_state
& ICONIFIED
) != 0)
527 setExtendedState(current_state
| ICONIFIED
);
529 if (state
== ICONIFIED
530 && (current_state
& ~ICONIFIED
) == 0)
531 setExtendedState(current_state
& ~ICONIFIED
);
534 public int getState()
536 // FIXME: State might have changed in the peer... Must check.
537 return (state
& ICONIFIED
) != 0 ? ICONIFIED
: NORMAL
;
543 public void setExtendedState(int state
)
551 public int getExtendedState()
559 public void setMaximizedBounds(Rectangle maximizedBounds
)
561 this.maximizedBounds
= maximizedBounds
;
565 * Returns the maximized bounds of this frame.
567 * @return the maximized rectangle, may be null
571 public Rectangle
getMaximizedBounds()
573 return maximizedBounds
;
577 * Returns whether this frame is undecorated or not.
581 public boolean isUndecorated()
587 * Disables or enables decorations for this frame. This method can only be
588 * called while the frame is not displayable.
590 * @throws IllegalComponentStateException if this frame is displayable
594 public void setUndecorated(boolean undecorated
)
597 throw new IllegalComponentStateException();
599 this.undecorated
= undecorated
;
603 * Generate a unique name for this frame.
605 * @return a unique name for this frame
607 String
generateName()
609 return "frame" + getUniqueLong();
612 private static synchronized long getUniqueLong()
614 return next_frame_number
++;
617 protected class AccessibleAWTFrame
extends AccessibleAWTWindow
619 private static final long serialVersionUID
= -6172960752956030250L;
621 public AccessibleRole
getAccessibleRole()
623 return AccessibleRole
.FRAME
;
626 public AccessibleStateSet
getAccessibleState()
628 AccessibleStateSet states
= super.getAccessibleStateSet();
630 states
.add(AccessibleState
.RESIZABLE
);
631 if ((state
& ICONIFIED
) != 0)
632 states
.add(AccessibleState
.ICONIFIED
);
638 * Gets the AccessibleContext associated with this <code>Frame</code>.
639 * The context is created, if necessary.
641 * @return the associated context
643 public AccessibleContext
getAccessibleContext()
645 // Create the context if this is the first request.
646 if (accessibleContext
== null)
647 accessibleContext
= new AccessibleAWTFrame();
648 return accessibleContext
;