Source for java.awt.Frame

   1: /* Frame.java -- AWT toplevel window
   2:    Copyright (C) 1999, 2000, 2002, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package java.awt;
  40: 
  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;
  46: 
  47: import javax.accessibility.AccessibleContext;
  48: import javax.accessibility.AccessibleRole;
  49: import javax.accessibility.AccessibleState;
  50: import javax.accessibility.AccessibleStateSet;
  51: 
  52: /**
  53:   * This class is a top-level window with a title bar and window
  54:   * decorations.
  55:   *
  56:   * @author Aaron M. Renn (arenn@urbanophile.com)
  57:   */
  58: public class Frame extends Window implements MenuContainer
  59: {
  60: /**
  61:   * Constant for the default cursor.
  62:   * @deprecated Replaced by <code>Cursor.DEFAULT_CURSOR</code> instead.
  63:   */
  64: public static final int DEFAULT_CURSOR = Cursor.DEFAULT_CURSOR;
  65: 
  66: /**
  67:   * Constant for a cross-hair cursor.
  68:   * @deprecated Use <code>Cursor.CROSSHAIR_CURSOR</code> instead.
  69:   */
  70: public static final int CROSSHAIR_CURSOR = Cursor.CROSSHAIR_CURSOR;
  71: 
  72: /**
  73:   * Constant for a cursor over a text field.
  74:   * @deprecated Use <code>Cursor.TEXT_CURSOR</code> instead.
  75:   */
  76: public static final int TEXT_CURSOR = Cursor.TEXT_CURSOR;
  77: 
  78: /**
  79:   * Constant for a cursor to display while waiting for an action to complete.
  80:   * @deprecated Use <code>Cursor.WAIT_CURSOR</code>.
  81:   */
  82: public static final int WAIT_CURSOR = Cursor.WAIT_CURSOR;
  83: 
  84: /**
  85:   * Cursor used over SW corner of window decorations.
  86:   * @deprecated Use <code>Cursor.SW_RESIZE_CURSOR</code> instead.
  87:   */
  88: public static final int SW_RESIZE_CURSOR = Cursor.SW_RESIZE_CURSOR;
  89: 
  90: /**
  91:   * Cursor used over SE corner of window decorations.
  92:   * @deprecated Use <code>Cursor.SE_RESIZE_CURSOR</code> instead.
  93:   */
  94: public static final int SE_RESIZE_CURSOR = Cursor.SE_RESIZE_CURSOR;
  95: 
  96: /**
  97:   * Cursor used over NW corner of window decorations.
  98:   * @deprecated Use <code>Cursor.NW_RESIZE_CURSOR</code> instead.
  99:   */
 100: public static final int NW_RESIZE_CURSOR = Cursor.NW_RESIZE_CURSOR;
 101: 
 102: /**
 103:   * Cursor used over NE corner of window decorations.
 104:   * @deprecated Use <code>Cursor.NE_RESIZE_CURSOR</code> instead.
 105:   */
 106: public static final int NE_RESIZE_CURSOR = Cursor.NE_RESIZE_CURSOR;
 107: 
 108: /**
 109:   * Cursor used over N edge of window decorations.
 110:   * @deprecated Use <code>Cursor.N_RESIZE_CURSOR</code> instead.
 111:   */
 112: public static final int N_RESIZE_CURSOR = Cursor.N_RESIZE_CURSOR;
 113: 
 114: /**
 115:   * Cursor used over S edge of window decorations.
 116:   * @deprecated Use <code>Cursor.S_RESIZE_CURSOR</code> instead.
 117:   */
 118: public static final int S_RESIZE_CURSOR = Cursor.S_RESIZE_CURSOR;
 119: 
 120: /**
 121:   * Cursor used over E edge of window decorations.
 122:   * @deprecated Use <code>Cursor.E_RESIZE_CURSOR</code> instead.
 123:   */
 124: public static final int E_RESIZE_CURSOR = Cursor.E_RESIZE_CURSOR;
 125: 
 126: /**
 127:   * Cursor used over W edge of window decorations.
 128:   * @deprecated Use <code>Cursor.W_RESIZE_CURSOR</code> instead.
 129:   */
 130: public static final int W_RESIZE_CURSOR = Cursor.W_RESIZE_CURSOR;
 131: 
 132: /**
 133:   * Constant for a hand cursor.
 134:   * @deprecated Use <code>Cursor.HAND_CURSOR</code> instead.
 135:   */
 136: public static final int HAND_CURSOR = Cursor.HAND_CURSOR;
 137: 
 138: /**
 139:   * Constant for a cursor used during window move operations.
 140:   * @deprecated Use <code>Cursor.MOVE_CURSOR</code> instead.
 141:   */
 142: public static final int MOVE_CURSOR = Cursor.MOVE_CURSOR;
 143: 
 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;
 149: 
 150: // Serialization version constant
 151: private static final long serialVersionUID = 2673458971256075116L;
 152: 
 153: /**
 154:   * @serial The version of the class data being serialized
 155:   * // FIXME: what is this value?
 156:   */
 157: private int frameSerializedDataVersion;
 158: 
 159: /**
 160:   * @serial Image used as the icon when this frame is minimized.
 161:   */
 162: private Image icon;
 163: 
 164: /**
 165:   * @serial Constant used by the JDK Motif peer set.  Not used in
 166:   * this implementation.
 167:   */
 168: private boolean mbManagement;
 169: 
 170: /**
 171:   * @serial The menu bar for this frame.
 172:   */
 173: //private MenuBar menuBar = new MenuBar();
 174: private MenuBar menuBar;
 175: 
 176: /**
 177:   * @serial A list of other top-level windows owned by this window.
 178:   */
 179: Vector ownedWindows = new Vector();
 180: 
 181: /**
 182:   * @serial Indicates whether or not this frame is resizable.
 183:   */
 184: private boolean resizable = true;
 185: 
 186: /**
 187:   * @serial The state of this frame.
 188:   * // FIXME: What are the values here?
 189:   * This is package-private to avoid an accessor method.
 190:   */
 191: int state;
 192: 
 193: /**
 194:   * @serial The title of the frame.
 195:   */
 196: private String title = "";
 197: 
 198:   /**
 199:    * Maximized bounds for this frame.
 200:    */
 201:   private Rectangle maximizedBounds;
 202: 
 203:   /**
 204:    * This field indicates whether the frame is undecorated or not.
 205:    */
 206:   private boolean undecorated = false;
 207: 
 208:   /*
 209:    * The number used to generate the name returned by getName.
 210:    */
 211:   private static transient long next_frame_number;
 212: 
 213: /**
 214:   * Initializes a new instance of <code>Frame</code> that is not visible
 215:   * and has no title.
 216:   */
 217: public
 218: Frame()
 219: {
 220:   this("");
 221:   noteFrame(this);
 222: }
 223: 
 224: /**
 225:   * Initializes a new instance of <code>Frame</code> that is not visible
 226:   * and has the specified title.
 227:   *
 228:   * @param title The title of this frame.
 229:   */
 230: public
 231: Frame(String title)
 232: {
 233:   super();
 234:   this.title = title;
 235:   // Top-level frames are initially invisible.
 236:   visible = false;
 237:   noteFrame(this);
 238: }
 239: 
 240: public
 241: Frame(GraphicsConfiguration gc)
 242: {
 243:   super(gc);
 244:   visible = false;
 245:   noteFrame(this);
 246: }
 247: 
 248: public
 249: Frame(String title, GraphicsConfiguration gc)
 250: {
 251:   super(gc);
 252:   setTitle(title);
 253:   visible = false;
 254:   noteFrame(this);
 255: }
 256: 
 257: /**
 258:   * Returns this frame's title string.
 259:   *
 260:   * @return This frame's title string.
 261:   */
 262: public String
 263: getTitle()
 264: {
 265:   return(title);
 266: }
 267: 
 268: /*
 269:  * Sets this frame's title to the specified value.
 270:  *
 271:  * @param title The new frame title.
 272:  */
 273: public synchronized void
 274: setTitle(String title)
 275: {
 276:   this.title = title;
 277:   if (peer != null)
 278:     ((FramePeer) peer).setTitle(title);
 279: }
 280: 
 281: /**
 282:   * Returns this frame's icon.
 283:   *
 284:   * @return This frame's icon, or <code>null</code> if this frame does not
 285:   * have an icon.
 286:   */
 287: public Image
 288: getIconImage()
 289: {
 290:   return(icon);
 291: }
 292: 
 293: /**
 294:   * Sets this frame's icon to the specified value.
 295:   *
 296:   * @icon The new icon for this frame.
 297:   */
 298: public synchronized void
 299: setIconImage(Image icon)
 300: {
 301:   this.icon = icon;
 302:   if (peer != null)
 303:     ((FramePeer) peer).setIconImage(icon);
 304: }
 305: 
 306: /**
 307:   * Returns this frame's menu bar.
 308:   *
 309:   * @return This frame's menu bar, or <code>null</code> if this frame
 310:   * does not have a menu bar.
 311:   */
 312: public MenuBar
 313: getMenuBar()
 314: {
 315:   return(menuBar);
 316: }
 317: 
 318: /**
 319:   * Sets this frame's menu bar.
 320:   *
 321:   * @param menuBar The new menu bar for this frame.
 322:   */
 323: public synchronized void
 324: setMenuBar(MenuBar menuBar)
 325: {
 326:   if (peer != null)
 327:   {
 328:     if (this.menuBar != null)
 329:       this.menuBar.removeNotify();  
 330:     if (menuBar != null)
 331:       menuBar.addNotify();
 332:     invalidateTree ();
 333:     ((FramePeer) peer).setMenuBar(menuBar);
 334:   }
 335:   this.menuBar = menuBar;
 336: }
 337: 
 338: /**
 339:   * Tests whether or not this frame is resizable.  This will be 
 340:   * <code>true</code> by default.
 341:   *
 342:   * @return <code>true</code> if this frame is resizable, <code>false</code>
 343:   * otherwise.
 344:   */
 345: public boolean
 346: isResizable()
 347: {
 348:   return(resizable);
 349: }
 350: 
 351: /**
 352:   * Sets the resizability of this frame to the specified value.
 353:   *
 354:   * @param resizable <code>true</code> to make the frame resizable,
 355:   * <code>false</code> to make it non-resizable.
 356:   */
 357: public synchronized void
 358: setResizable(boolean resizable)
 359: {
 360:   this.resizable = resizable;
 361:   if (peer != null)
 362:     ((FramePeer) peer).setResizable(resizable);
 363: }
 364: 
 365: /**
 366:   * Returns the cursor type of the cursor for this window.  This will
 367:   * be one of the constants in this class.
 368:   *
 369:   * @return The cursor type for this frame.
 370:   *
 371:   * @deprecated Use <code>Component.getCursor()</code> instead.
 372:   */
 373: public int
 374: getCursorType()
 375: {
 376:   return(getCursor().getType());
 377: }
 378: 
 379: /**
 380:   * Sets the cursor for this window to the specified type.  The specified
 381:   * type should be one of the constants in this class.
 382:   *
 383:   * @param type The cursor type.
 384:   *
 385:   * @deprecated Use <code>Component.setCursor(Cursor)</code> instead.
 386:   */
 387: public void
 388: setCursor(int type)
 389: {
 390:   setCursor(new Cursor(type));
 391: }
 392: 
 393: /**
 394:   * Removes the specified component from this frame's menu.
 395:   *
 396:   * @param menu The menu component to remove.
 397:   */
 398: public void
 399: remove(MenuComponent menu)
 400: {
 401:   menuBar.remove(menu);
 402: }
 403: 
 404: public void
 405: addNotify()
 406: {
 407:   if (menuBar != null)
 408:     menuBar.addNotify();
 409:   if (peer == null)
 410:     peer = getToolkit ().createFrame (this);
 411: 
 412:   super.addNotify();
 413: }
 414: 
 415: public void removeNotify()
 416: {
 417:   if (menuBar != null)
 418:     menuBar.removeNotify();
 419:   super.removeNotify();
 420: }
 421: 
 422:   /**
 423:    * Returns a debugging string describing this window.
 424:    *
 425:    * @return A debugging string describing this window.
 426:    */
 427:   protected String paramString ()
 428:   {
 429:     String title = getTitle ();
 430: 
 431:     String resizable = "";
 432:     if (isResizable ())
 433:       resizable = ",resizable";
 434: 
 435:     String state = "";
 436:     switch (getState ())
 437:       {
 438:       case NORMAL:
 439:         state = ",normal";
 440:         break;
 441:       case ICONIFIED:
 442:         state = ",iconified";
 443:         break;
 444:       case MAXIMIZED_BOTH:
 445:         state = ",maximized-both";
 446:         break;
 447:       case MAXIMIZED_HORIZ:
 448:         state = ",maximized-horiz";
 449:         break;
 450:       case MAXIMIZED_VERT:
 451:         state = ",maximized-vert";
 452:         break;
 453:       }
 454: 
 455:     return super.paramString () + ",title=" + title + resizable + state;
 456:   }
 457: 
 458: private static ArrayList weakFrames = new ArrayList();
 459: 
 460: private static void noteFrame(Frame f)
 461: {
 462:   weakFrames.add(new WeakReference(f));
 463: }
 464: 
 465: public static Frame[] getFrames()
 466: {
 467:   int n = 0;
 468:   synchronized (weakFrames)
 469:     {
 470:       Iterator i = weakFrames.iterator();
 471:       while (i.hasNext())
 472:         {
 473:           WeakReference wr = (WeakReference) i.next();
 474:           if (wr.get() != null)
 475:             ++n;
 476:         }
 477:       if (n == 0)
 478:         return new Frame[0];
 479:       else
 480:         {
 481:           Frame[] frames = new Frame[n];
 482:           n = 0;
 483:           i = weakFrames.iterator();
 484:           while (i.hasNext())
 485:             {
 486:               WeakReference wr = (WeakReference) i.next();
 487:               if (wr.get() != null)
 488:                 frames[n++] = (Frame) wr.get();
 489:             }
 490:           return frames;
 491:         }
 492:     }
 493: }
 494: 
 495:   public void setState (int state)
 496:   {
 497:     int current_state = getExtendedState ();
 498: 
 499:     if (state == NORMAL
 500:         && (current_state & ICONIFIED) != 0)
 501:       setExtendedState (current_state | ICONIFIED);
 502:     
 503:     if (state == ICONIFIED
 504:         && (current_state & ~ICONIFIED) == 0)
 505:       setExtendedState (current_state & ~ICONIFIED);
 506:   }
 507: 
 508:   public int getState ()
 509:   {
 510:     /* FIXME: State might have changed in the peer... Must check. */
 511:   
 512:     return (state & ICONIFIED) != 0 ? ICONIFIED : NORMAL;
 513:   }
 514: 
 515:   /**
 516:    * @since 1.4
 517:    */
 518:   public void setExtendedState (int state)
 519:   {
 520:     this.state = state;
 521:   }
 522: 
 523:   /**
 524:    * @since 1.4
 525:    */
 526:   public int getExtendedState ()
 527:   {
 528:     return state;
 529:   }
 530: 
 531:   /**
 532:    * @since 1.4
 533:    */
 534:   public void setMaximizedBounds (Rectangle maximizedBounds)
 535:   {
 536:     this.maximizedBounds = maximizedBounds;
 537:   }
 538: 
 539:   /**
 540:    * Returns the maximized bounds of this frame.
 541:    *
 542:    * @return the maximized rectangle, may be null.
 543:    *
 544:    * @since 1.4
 545:    */
 546:   public Rectangle getMaximizedBounds ()
 547:   {
 548:     return maximizedBounds;
 549:   }
 550: 
 551:   /**
 552:    * Returns whether this frame is undecorated or not.
 553:    * 
 554:    * @since 1.4
 555:    */
 556:   public boolean isUndecorated ()
 557:   {
 558:     return undecorated;
 559:   }
 560: 
 561:   /**
 562:    * Disables or enables decorations for this frame. This method can only be
 563:    * called while the frame is not displayable.
 564:    * 
 565:    * @exception IllegalComponentStateException If this frame is displayable.
 566:    * 
 567:    * @since 1.4
 568:    */
 569:   public void setUndecorated (boolean undecorated)
 570:   {
 571:     if (isDisplayable ())
 572:       throw new IllegalComponentStateException ();
 573: 
 574:     this.undecorated = undecorated;
 575:   }
 576: 
 577:   /**
 578:    * Generate a unique name for this frame.
 579:    *
 580:    * @return A unique name for this frame.
 581:    */
 582:   String generateName ()
 583:   {
 584:     return "frame" + getUniqueLong ();
 585:   }
 586: 
 587:   private static synchronized long getUniqueLong ()
 588:   {
 589:     return next_frame_number++;
 590:   }
 591:   
 592:   protected class AccessibleAWTFrame extends AccessibleAWTWindow
 593:   {
 594:     private static final long serialVersionUID = -6172960752956030250L;
 595: 
 596:     public AccessibleRole getAccessibleRole()
 597:     {
 598:       return AccessibleRole.FRAME;
 599:     }
 600:     
 601:     public AccessibleStateSet getAccessibleState()
 602:     {
 603:       AccessibleStateSet states = super.getAccessibleStateSet();
 604:       if (isResizable())
 605:         states.add(AccessibleState.RESIZABLE);
 606:       if ((state & ICONIFIED) != 0)
 607:         states.add(AccessibleState.ICONIFIED);
 608:       return states;
 609:     }
 610:   }
 611:   
 612:   /**
 613:    * Gets the AccessibleContext associated with this <code>Frame</code>.
 614:    * The context is created, if necessary.
 615:    *
 616:    * @return the associated context
 617:    */
 618:   public AccessibleContext getAccessibleContext()
 619:   {
 620:     /* Create the context if this is the first request */
 621:     if (accessibleContext == null)
 622:       accessibleContext = new AccessibleAWTFrame();
 623:     return accessibleContext;
 624:   }
 625: 
 626: }