Source for java.awt.Dialog

   1: /* Dialog.java -- An AWT dialog box
   2:    Copyright (C) 1999, 2000, 2001, 2002, 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.DialogPeer;
  42: 
  43: import javax.accessibility.AccessibleContext;
  44: import javax.accessibility.AccessibleRole;
  45: import javax.accessibility.AccessibleState;
  46: import javax.accessibility.AccessibleStateSet;
  47: 
  48: /**
  49:  * A dialog box widget class.
  50:  *
  51:  * @author Aaron M. Renn (arenn@urbanophile.com)
  52:  * @author Tom Tromey (tromey@redhat.com)
  53:  */
  54: public class Dialog extends Window
  55: {
  56: 
  57: /*
  58:  * Static Variables
  59:  */
  60: 
  61: // Serialization constant
  62: private static final long serialVersionUID = 5920926903803293709L;
  63: 
  64: /*************************************************************************/
  65: 
  66: /*
  67:  * Instance Variables
  68:  */
  69: 
  70: /**
  71:   * @serial Indicates whether or not this dialog box is modal.
  72:   */
  73: private boolean modal;
  74: 
  75: /**
  76:   * @serial Indicates whether or not this dialog box is resizable.
  77:   */
  78: private boolean resizable = true;
  79: 
  80: /**
  81:   * @serial The title string for this dialog box, which can be
  82:   * <code>null</code>.
  83:   */
  84: private String title;
  85: 
  86: /**
  87:   * This field indicates whether the dialog is undecorated or not.
  88:   */
  89: private boolean undecorated = false;
  90: 
  91: /**
  92:   * Indicates that we are blocked for modality in show
  93:   */
  94: private boolean blocked = false;
  95: 
  96: /**
  97:   * Secondary EventQueue to handle AWT events while
  98:   * we are blocked for modality in show
  99:   */
 100: private EventQueue eq2 = null;
 101: 
 102: /*************************************************************************/
 103: 
 104: /*
 105:  * Constructors
 106:  */
 107: 
 108: /**
 109:   * Initializes a new instance of <code>Dialog</code> with the specified
 110:   * parent, that is resizable and not modal, and which has no title.
 111:   *
 112:   * @param parent The parent frame of this dialog box.
 113:   *
 114:   * @exception IllegalArgumentException If the owner's GraphicsConfiguration
 115:   * is not from a screen device, or if owner is null. This exception is always
 116:   * thrown when GraphicsEnvironment.isHeadless() returns true.
 117:   */
 118: public
 119: Dialog(Frame parent)
 120: {
 121:   this(parent, "", false);
 122: }
 123: 
 124: /*************************************************************************/
 125: 
 126: /**
 127:   * Initializes a new instance of <code>Dialog</code> with the specified
 128:   * parent and modality, that is resizable and which has no title.
 129:   *
 130:   * @param parent The parent frame of this dialog box.
 131:   * @param modal <code>true</code> if this dialog box is modal,
 132:   * <code>false</code> otherwise.
 133:   *
 134:   * @exception IllegalArgumentException If the owner's GraphicsConfiguration
 135:   * is not from a screen device, or if owner is null. This exception is always
 136:   * thrown when GraphicsEnvironment.isHeadless() returns true.
 137:   */
 138: public
 139: Dialog(Frame parent, boolean modal)
 140: {
 141:   this(parent, "", modal);
 142: }
 143: 
 144: /*************************************************************************/
 145: 
 146: /**
 147:   * Initializes a new instance of <code>Dialog</code> with the specified
 148:   * parent, that is resizable and not modal, and which has the specified
 149:   * title.
 150:   *
 151:   * @param parent The parent frame of this dialog box.
 152:   * @param title The title string for this dialog box.
 153:   *
 154:   * @exception IllegalArgumentException If the owner's GraphicsConfiguration
 155:   * is not from a screen device, or if owner is null. This exception is always
 156:   * thrown when GraphicsEnvironment.isHeadless() returns true.
 157:   */
 158: public
 159: Dialog(Frame parent, String title)
 160: {
 161:   this(parent, title, false);
 162: }
 163: 
 164: /*************************************************************************/
 165: 
 166: /**
 167:   * Initializes a new instance of <code>Dialog</code> with the specified,
 168:   * parent, title, and modality, that is resizable.
 169:   *
 170:   * @param parent The parent frame of this dialog box.
 171:   * @param title The title string for this dialog box.
 172:   * @param modal <code>true</code> if this dialog box is modal,
 173:   * <code>false</code> otherwise.
 174:   *
 175:   * @exception IllegalArgumentException If owner is null or
 176:   * GraphicsEnvironment.isHeadless() returns true.
 177:   */
 178: public
 179: Dialog(Frame parent, String title, boolean modal)
 180: {
 181:   this (parent, title, modal, parent.getGraphicsConfiguration ());
 182: }
 183: 
 184: /**
 185:  * Initializes a new instance of <code>Dialog</code> with the specified,
 186:  * parent, title, modality and <code>GraphicsConfiguration</code>,
 187:  * that is resizable.
 188:  *
 189:  * @param parent The parent frame of this dialog box.
 190:  * @param title The title string for this dialog box.
 191:  * @param modal <code>true</code> if this dialog box is modal,
 192:  * <code>false</code> otherwise.
 193:  * @param gc The <code>GraphicsConfiguration</code> object to use.
 194:  *
 195:  * @exception IllegalArgumentException If owner is null, the
 196:  * GraphicsConfiguration is not a screen device or
 197:  * GraphicsEnvironment.isHeadless() returns true.
 198:  *
 199:  * @since 1.4
 200:  */
 201: public
 202: Dialog (Frame parent, String title, boolean modal, GraphicsConfiguration gc)
 203: {
 204:   super (parent, gc);
 205: 
 206:   // A null title is equivalent to an empty title  
 207:   this.title = (title != null) ? title : "";
 208:   this.modal = modal;
 209:   visible = false;
 210: 
 211:   setLayout(new BorderLayout());
 212: }
 213: 
 214: /**
 215:  * Initializes a new instance of <code>Dialog</code> with the specified,
 216:  * parent, that is resizable.
 217:  *
 218:  * @exception IllegalArgumentException If parent is null. This exception is
 219:  * always thrown when GraphicsEnvironment.isHeadless() returns true.
 220:  *
 221:  * @since 1.2
 222:  */
 223: public
 224: Dialog (Dialog owner)
 225: {
 226:   this (owner, "", false, owner.getGraphicsConfiguration ());
 227: }
 228: 
 229: /**
 230:  * Initializes a new instance of <code>Dialog</code> with the specified,
 231:  * parent and title, that is resizable.
 232:  *
 233:  * @exception IllegalArgumentException If parent is null. This exception is
 234:  * always thrown when GraphicsEnvironment.isHeadless() returns true.
 235:  *
 236:  * @since 1.2
 237:  */
 238: public
 239: Dialog (Dialog owner, String title)
 240: {
 241:   this (owner, title, false, owner.getGraphicsConfiguration ());
 242: }
 243: 
 244: /**
 245:  * Initializes a new instance of <code>Dialog</code> with the specified,
 246:  * parent, title and modality, that is resizable.
 247:  *
 248:  * @exception IllegalArgumentException If parent is null. This exception is
 249:  * always thrown when GraphicsEnvironment.isHeadless() returns true.
 250:  *
 251:  * @since 1.2
 252:  */
 253: public
 254: Dialog (Dialog owner, String title, boolean modal)
 255: {
 256:   this (owner, title, modal, owner.getGraphicsConfiguration ());
 257: }
 258: 
 259: /**
 260:  * Initializes a new instance of <code>Dialog</code> with the specified,
 261:  * parent, title, modality and <code>GraphicsConfiguration</code>,
 262:  * that is resizable.
 263:  *
 264:  * @exception IllegalArgumentException If parent is null, the
 265:  * GraphicsConfiguration is not a screen device or
 266:  * GraphicsEnvironment.isHeadless() returns true.
 267:  *
 268:  * @since 1.4
 269:  */
 270: public
 271: Dialog (Dialog parent, String title, boolean modal, GraphicsConfiguration gc)
 272: {
 273:   super (parent, parent.getGraphicsConfiguration ());
 274: 
 275:   // A null title is equivalent to an empty title  
 276:   this.title = (title != null) ? title : "";
 277:   this.modal = modal;
 278:   visible = false;
 279: 
 280:   setLayout (new BorderLayout ());
 281: }
 282: 
 283: /*************************************************************************/
 284: 
 285: /*
 286:  * Instance Variables
 287:  */
 288: 
 289: /**
 290:   * Returns the title of this dialog box.
 291:   * 
 292:   * @return The title of this dialog box.
 293:   */
 294: public String
 295: getTitle()
 296: {
 297:   return(title);
 298: }
 299: 
 300: /*************************************************************************/
 301: 
 302: /**
 303:   * Sets the title of this dialog box to the specified string.
 304:   *
 305:   * @param title The new title.
 306:   */
 307: public synchronized void
 308: setTitle(String title)
 309: {
 310:   // A null title is equivalent to an empty title  
 311:   this.title = (title != null) ? title : "";
 312: 
 313:   if (peer != null)
 314:     {
 315:       DialogPeer d = (DialogPeer) peer;
 316:       d.setTitle (title);
 317:     }
 318: }
 319: 
 320: /*************************************************************************/
 321: 
 322: /**
 323:   * Tests whether or not this dialog box is modal.
 324:   *
 325:   * @return <code>true</code> if this dialog box is modal,
 326:   * <code>false</code> otherwise.
 327:   */
 328: public boolean
 329: isModal()
 330: {
 331:   return(modal);
 332: }
 333: 
 334: /*************************************************************************/
 335: 
 336: /**
 337:   * Changes the modality of this dialog box.  This can only be done before
 338:   * the peer is created.
 339:   *
 340:   * @param modal <code>true</code> to make this dialog box modal,
 341:   * <code>false</code> to make it non-modal.
 342:   */
 343: public void
 344: setModal(boolean modal)
 345: {
 346:   this.modal = modal;
 347: }
 348: 
 349: /*************************************************************************/
 350: 
 351: /**
 352:   * Tests whether or not this dialog box is resizable.
 353:   *
 354:   * @return <code>true</code> if this dialog is resizable, <code>false</code>,
 355:   * otherwise.
 356:   */
 357: public boolean
 358: isResizable()
 359: {
 360:   return(resizable);
 361: }
 362: 
 363: /*************************************************************************/
 364: 
 365: /**
 366:   * Changes the resizability of this dialog box.
 367:   *
 368:   * @param resizable <code>true</code> to make this dialog resizable,
 369:   * <code>false</code> to make it non-resizable.
 370:   */
 371: public synchronized void
 372: setResizable(boolean resizable)
 373: {
 374:   this.resizable = resizable;
 375:   if (peer != null)
 376:     {
 377:       DialogPeer d = (DialogPeer) peer;
 378:       d.setResizable (resizable);
 379:     }
 380: }
 381: 
 382: /*************************************************************************/
 383: 
 384: /**
 385:   * Creates this object's native peer.
 386:   */
 387: public synchronized void
 388: addNotify()
 389: {
 390:   if (peer == null)
 391:     peer = getToolkit ().createDialog (this);
 392:   super.addNotify ();
 393: }
 394: 
 395: /*************************************************************************/
 396: 
 397: /**
 398:   * Makes this dialog visible and brings it to the front.
 399:   * If the dialog is modal and is not already visible, this call will not
 400:   *  return until the dialog is hidden by someone calling hide or dispose.
 401:   * If this is the event dispatching thread we must ensure that another event
 402:   *  thread runs while the one which invoked this method is blocked. 
 403:   */
 404: public synchronized void
 405: show()
 406: {
 407:   super.show();
 408:   
 409:   if (isModal())
 410:     {
 411:       // If already shown (and blocked) just return
 412:       if (blocked)
 413:     return;
 414: 
 415:       /* If show is called in the dispatch thread for a modal dialog it will
 416:          block so we must run another thread so the events keep being
 417:      dispatched.*/
 418:       if (EventQueue.isDispatchThread ())
 419:         {
 420:       EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
 421:           eq2 = new EventQueue ();
 422:       eq.push (eq2);
 423:     }
 424:       
 425:       try 
 426:         {
 427:       blocked = true;
 428:       wait ();
 429:       blocked = false;
 430:         } 
 431:       catch (InterruptedException e)
 432:         {
 433:       blocked = false;
 434:         }
 435:     
 436:       if (eq2 != null)
 437:         {
 438:       eq2.pop ();
 439:       eq2 = null;
 440:     }
 441:     }  
 442: }
 443: 
 444: /*************************************************************************/
 445: 
 446: /**
 447:   * Hides the Dialog and then
 448:   * causes show() to return if it is currently blocked.
 449:   */
 450: 
 451: public synchronized void 
 452: hide ()
 453: {
 454:   if (blocked)
 455:     {
 456:       notifyAll ();
 457:     }
 458: 
 459:   super.hide();
 460: }
 461: 
 462: /*************************************************************************/
 463: 
 464: /**
 465:   * Disposes the Dialog and then causes show() to return
 466:   * if it is currently blocked.
 467:   */
 468: 
 469: public synchronized void 
 470: dispose ()
 471: {
 472:   if (blocked)
 473:     {
 474:       notifyAll ();
 475:     }
 476: 
 477:   super.dispose();
 478: }
 479: 
 480: /*************************************************************************/
 481: 
 482: /**
 483:   * Returns a debugging string for this component.
 484:   * 
 485:   * @return A debugging string for this component.
 486:   */
 487: protected String
 488: paramString()
 489: {
 490:   return ("title+" + title + ",modal=" + modal +
 491:       ",resizable=" + resizable + "," + super.paramString());
 492: }
 493: 
 494:   /**
 495:    * Returns whether this frame is undecorated or not.
 496:    * 
 497:    * @since 1.4
 498:    */
 499:   public boolean isUndecorated ()
 500:   {
 501:     return undecorated;
 502:   }
 503: 
 504:   /**
 505:    * Disables or enables decorations for this frame. This method can only be
 506:    * called while the frame is not displayable.
 507:    * 
 508:    * @exception IllegalComponentStateException If this frame is displayable.
 509:    * 
 510:    * @since 1.4
 511:    */
 512:   public void setUndecorated (boolean undecorated)
 513:   {
 514:     if (isDisplayable ())
 515:       throw new IllegalComponentStateException ();
 516: 
 517:     this.undecorated = undecorated;
 518:   }
 519:   
 520:   protected class AccessibleAWTDialog extends AccessibleAWTWindow
 521:   {
 522:     private static final long serialVersionUID = 4837230331833941201L;
 523: 
 524:     public AccessibleRole getAccessibleRole()
 525:     {
 526:       return AccessibleRole.DIALOG;
 527:     }
 528:     
 529:     public AccessibleStateSet getAccessibleState()
 530:     {
 531:       AccessibleStateSet states = super.getAccessibleStateSet();
 532:       if (isResizable())
 533:         states.add(AccessibleState.RESIZABLE);
 534:       if (isModal())
 535:         states.add(AccessibleState.MODAL);
 536:       return states;
 537:     }
 538:   }
 539:   
 540:   /**
 541:    * Gets the AccessibleContext associated with this <code>Dialog</code>.
 542:    * The context is created, if necessary.
 543:    *
 544:    * @return the associated context
 545:    */
 546:   public AccessibleContext getAccessibleContext()
 547:   {
 548:     /* Create the context if this is the first request */
 549:     if (accessibleContext == null)
 550:       accessibleContext = new AccessibleAWTDialog();
 551:     return accessibleContext;
 552:   }
 553: 
 554: } // class Dialog