Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / awt / Frame.java
blobd180984ab9d539e93634a7b0f99bde824bc7cc91
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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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. */
39 package java.awt;
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;
52 /**
53 * This class is a top-level window with a title bar and window
54 * decorations.
56 * @author Aaron M. Renn (arenn@urbanophile.com)
58 public class Frame extends Window implements MenuContainer
60 /**
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;
66 /**
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;
72 /**
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;
78 /**
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;
84 /**
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;
90 /**
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;
96 /**
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.
162 private Image icon;
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?
190 private int state;
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
214 * and has no title.
216 public
217 Frame()
219 this("");
220 noteFrame(this);
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.
229 public
230 Frame(String title)
232 super();
233 this.title = title;
234 // Top-level frames are initially invisible.
235 visible = false;
236 noteFrame(this);
239 public
240 Frame(GraphicsConfiguration gc)
242 super(gc);
243 visible = false;
244 noteFrame(this);
247 public
248 Frame(String title, GraphicsConfiguration gc)
250 super(gc);
251 setTitle(title);
252 visible = false;
253 noteFrame(this);
257 * Returns this frame's title string.
259 * @return This frame's title string.
261 public String
262 getTitle()
264 return(title);
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)
275 this.title = title;
276 if (peer != null)
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
284 * have an icon.
286 public Image
287 getIconImage()
289 return(icon);
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)
300 this.icon = icon;
301 if (peer != null)
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.
311 public MenuBar
312 getMenuBar()
314 return(menuBar);
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)
325 if (peer != null)
327 if (this.menuBar != null)
328 this.menuBar.removeNotify();
329 if (menuBar != null)
330 menuBar.addNotify();
331 invalidateTree ();
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>
342 * otherwise.
344 public boolean
345 isResizable()
347 return(resizable);
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;
360 if (peer != null)
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.
372 public int
373 getCursorType()
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.
386 public void
387 setCursor(int type)
389 setCursor(new Cursor(type));
393 * Removes the specified component from this frame's menu.
395 * @param menu The menu component to remove.
397 public void
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()
410 public void run()
412 // Do nothing here.
417 public void
418 addNotify()
420 if (menuBar != null)
421 menuBar.addNotify();
422 if (peer == null)
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())
428 fireDummyEvent();
430 super.addNotify();
433 public void removeNotify()
435 if (menuBar != null)
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.
443 fireDummyEvent();
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 = "";
456 if (isResizable ())
457 resizable = ",resizable";
459 String state = "";
460 switch (getState ())
462 case NORMAL:
463 state = ",normal";
464 break;
465 case ICONIFIED:
466 state = ",iconified";
467 break;
468 case MAXIMIZED_BOTH:
469 state = ",maximized-both";
470 break;
471 case MAXIMIZED_HORIZ:
472 state = ",maximized-horiz";
473 break;
474 case MAXIMIZED_VERT:
475 state = ",maximized-vert";
476 break;
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()
491 int n = 0;
492 synchronized (weakFrames)
494 Iterator i = weakFrames.iterator();
495 while (i.hasNext())
497 WeakReference wr = (WeakReference) i.next();
498 if (wr.get() != null)
499 ++n;
501 if (n == 0)
502 return new Frame[0];
503 else
505 Frame[] frames = new Frame[n];
506 n = 0;
507 i = weakFrames.iterator();
508 while (i.hasNext())
510 WeakReference wr = (WeakReference) i.next();
511 if (wr.get() != null)
512 frames[n++] = (Frame) wr.get();
514 return frames;
519 public void setState (int state)
521 int current_state = getExtendedState ();
523 if (state == NORMAL
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;
540 * @since 1.4
542 public void setExtendedState (int state)
544 this.state = state;
548 * @since 1.4
550 public int getExtendedState ()
552 return state;
556 * @since 1.4
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.
568 * @since 1.4
570 public Rectangle getMaximizedBounds ()
572 return maximizedBounds;
576 * Returns whether this frame is undecorated or not.
578 * @since 1.4
580 public boolean isUndecorated ()
582 return undecorated;
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.
591 * @since 1.4
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();
626 if (isResizable())
627 states.add(AccessibleState.RESIZABLE);
628 if ((state & ICONIFIED) != 0)
629 states.add(AccessibleState.ICONIFIED);
630 return states;
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;