Source for javax.swing.JDialog

   1: /* JDialog.java --
   2:    Copyright (C) 2002, 2004 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 javax.swing;
  40: 
  41: import java.awt.Component;
  42: import java.awt.Container;
  43: import java.awt.Dialog;
  44: import java.awt.Dimension;
  45: import java.awt.Frame;
  46: import java.awt.Graphics;
  47: import java.awt.GraphicsConfiguration;
  48: import java.awt.IllegalComponentStateException;
  49: import java.awt.LayoutManager;
  50: import java.awt.event.WindowEvent;
  51: 
  52: import javax.accessibility.Accessible;
  53: import javax.accessibility.AccessibleContext;
  54: 
  55: /**
  56:  * A dialog window. This is an extension of {@link java.awt.Dialog} that
  57:  * provides support for the Swing architecture. Most importantly it contains a
  58:  * {@link JRootPane} as it's only top-level child, that manages the content
  59:  * pane, the menu and a glass pane.
  60:  *
  61:  * Also, unlike <code>java.awt.Dialog</code>s, JDialogs support the
  62:  * Swing Pluggable Look &amp; Feel architecture.
  63:  * 
  64:  * @author Ronald Veldema (rveldema@cs.vu.nl)
  65:  */
  66: public class JDialog extends Dialog implements Accessible, WindowConstants,
  67:                                                RootPaneContainer
  68: {
  69:   /**
  70:    * Provides accessibility support for <code>JDialog</code>s.
  71:    */
  72:   protected class AccessibleJDialog extends Dialog.AccessibleAWTDialog
  73:   {
  74:     /**
  75:      * Creates a new instance of <code>AccessibleJDialog</code>.
  76:      */
  77:     public AccessibleJDialog()
  78:     {
  79:       super();
  80:       // Nothing to do here.
  81:     }
  82:   }
  83: 
  84:   private static final long serialVersionUID = -864070866424508218L;
  85: 
  86:   /** DOCUMENT ME! */
  87:   protected AccessibleContext accessibleContext;
  88: 
  89:   /** The single RootPane in the Dialog. */
  90:   protected JRootPane rootPane;
  91: 
  92:   /**
  93:    * Whether checking is enabled on the RootPane.
  94:    *
  95:    * @specnote Should be false to comply with J2SE 5.0
  96:    */ 
  97:   protected boolean rootPaneCheckingEnabled = false;
  98: 
  99:   /** The default action taken when closed. */
 100:   private int close_action = HIDE_ON_CLOSE;
 101:   
 102:   /** Whether JDialogs are decorated by the Look and Feel. */
 103:   private static boolean decorated;
 104: 
 105:   /**
 106:    * Whether we're in the init stage or not.
 107:    * If so, adds and layouts are for top-level, otherwise they're for the
 108:    * content pane
 109:    */
 110:   private boolean initStageDone = false;
 111: 
 112:   /* Creates a new non-modal JDialog with no title 
 113:    * using a shared Frame as the owner.
 114:    */
 115:   public JDialog()
 116:   {
 117:     this(SwingUtilities.getOwnerFrame(), "", false, null);
 118:   }
 119: 
 120:   /**
 121:    * Creates a new non-modal JDialog with no title
 122:    * using the given owner.
 123:    *
 124:    * @param owner The owner of the JDialog.
 125:    */
 126:   public JDialog(Dialog owner)
 127:   {
 128:     this(owner, "", false, null);
 129:   }
 130: 
 131:   /**
 132:    * Creates a new JDialog with no title using the
 133:    * given modal setting and owner.
 134:    *
 135:    * @param owner The owner of the JDialog.
 136:    * @param modal Whether the JDialog is modal.
 137:    */
 138:   public JDialog(Dialog owner, boolean modal)
 139:   {
 140:     this(owner, "", modal, null);
 141:   }
 142: 
 143:   /**
 144:    * Creates a new non-modal JDialog using the 
 145:    * given title and owner.
 146:    *
 147:    * @param owner The owner of the JDialog.
 148:    * @param title The title of the JDialog.
 149:    */
 150:   public JDialog(Dialog owner, String title)
 151:   {
 152:     this(owner, title, false, null);
 153:   }
 154: 
 155:   /**
 156:    * Creates a new JDialog using the given modal 
 157:    * settings, title, and owner.
 158:    *
 159:    * @param owner The owner of the JDialog.
 160:    * @param title The title of the JDialog.
 161:    * @param modal Whether the JDialog is modal.
 162:    */
 163:   public JDialog(Dialog owner, String title, boolean modal)
 164:   {
 165:     this(owner, title, modal, null);
 166:   }
 167: 
 168:   /**
 169:    * Creates a new JDialog using the given modal 
 170:    * settings, title, owner and graphics configuration.
 171:    *
 172:    * @param owner The owner of the JDialog.
 173:    * @param title The title of the JDialog.
 174:    * @param modal Whether the JDialog is modal.
 175:    * @param gc The Graphics Configuration to use.
 176:    */
 177:   public JDialog(Dialog owner, String title, boolean modal,
 178:                  GraphicsConfiguration gc)
 179:   {
 180:     super(owner, title, modal, gc);
 181:     dialogInit();
 182:   }
 183: 
 184:   /**
 185:    * Creates a new non-modal JDialog with no title
 186:    * using the given owner.
 187:    *
 188:    * @param owner The owner of the JDialog.
 189:    */
 190:   public JDialog(Frame owner)
 191:   {
 192:     this(owner, "", false, null);
 193:   }
 194: 
 195:   /**
 196:    * Creates a new JDialog with no title using the
 197:    * given modal setting and owner.
 198:    *
 199:    * @param owner The owner of the JDialog.
 200:    * @param modal Whether the JDialog is modal.
 201:    */
 202:   public JDialog(Frame owner, boolean modal)
 203:   {
 204:     this(owner, "", modal, null);
 205:   }
 206: 
 207:   /**
 208:    * Creates a new non-modal JDialog using the 
 209:    * given title and owner.
 210:    *
 211:    * @param owner The owner of the JDialog.
 212:    * @param title The title of the JDialog.
 213:    */
 214:   public JDialog(Frame owner, String title)
 215:   {
 216:     this(owner, title, false, null);
 217:   }
 218: 
 219:   /**
 220:    * Creates a new JDialog using the given modal 
 221:    * settings, title, and owner.
 222:    *
 223:    * @param owner The owner of the JDialog.
 224:    * @param title The title of the JDialog.
 225:    * @param modal Whether the JDialog is modal.
 226:    */
 227:   public JDialog(Frame owner, String title, boolean modal)
 228:   {
 229:     this(owner, title, modal, null);
 230:   }
 231: 
 232:   /**
 233:    * Creates a new JDialog using the given modal 
 234:    * settings, title, owner and graphics configuration.
 235:    *
 236:    * @param owner The owner of the JDialog.
 237:    * @param title The title of the JDialog.
 238:    * @param modal Whether the JDialog is modal.
 239:    * @param gc The Graphics Configuration to use.
 240:    */
 241:   public JDialog(Frame owner, String title, boolean modal,
 242:                  GraphicsConfiguration gc)
 243:   {
 244:     super((owner == null) ? SwingUtilities.getOwnerFrame() : owner, 
 245:           title, modal, gc);
 246:     dialogInit();
 247:   }
 248: 
 249:   /**
 250:    * This method is called to initialize the 
 251:    * JDialog. It sets the layout used, the locale, 
 252:    * and creates the RootPane.
 253:    */
 254:   protected void dialogInit()
 255:   {
 256:     // FIXME: Do a check on GraphicsEnvironment.isHeadless()
 257:     setLocale(JComponent.getDefaultLocale());
 258:     getRootPane(); // Will do set/create.
 259:     invalidate();
 260:     // Now that initStageDone is true, adds and layouts apply to contentPane,
 261:     // not top-level.
 262:     initStageDone = true;
 263:   }
 264: 
 265:   /**
 266:    * This method returns whether JDialogs will have their
 267:    * window decorations provided by the Look and Feel.
 268:    *
 269:    * @return Whether the window decorations are Look and Feel provided.
 270:    */
 271:   public static boolean isDefaultLookAndFeelDecorated()
 272:   {
 273:     return decorated;
 274:   }
 275: 
 276:   /**
 277:    * This method sets whether JDialogs will have their
 278:    * window decorations provided by the Look and Feel.
 279:    *
 280:    * @param defaultLookAndFeelDecorated Whether the window
 281:    * decorations are Look and Feel provided.
 282:    */
 283:   public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)
 284:   {
 285:     decorated = defaultLookAndFeelDecorated;
 286:   }
 287: 
 288:   /**
 289:    * This method returns the preferred size of 
 290:    * the JDialog.
 291:    *
 292:    * @return The preferred size.
 293:    */
 294:   public Dimension getPreferredSize()
 295:   {
 296:     Dimension d = super.getPreferredSize();
 297:     return d;
 298:   }
 299: 
 300:   /**
 301:    * This method returns the JMenuBar used
 302:    * in this JDialog.
 303:    *
 304:    * @return The JMenuBar in the JDialog.
 305:    */
 306:   public JMenuBar getJMenuBar()
 307:   {
 308:     return getRootPane().getJMenuBar();
 309:   }
 310: 
 311:   /**
 312:    * This method sets the JMenuBar used 
 313:    * in this JDialog.
 314:    *
 315:    * @param menubar The JMenuBar to use.
 316:    */
 317:   public void setJMenuBar(JMenuBar menubar)
 318:   {
 319:     getRootPane().setJMenuBar(menubar);
 320:   }
 321: 
 322:   /**
 323:    * This method sets the LayoutManager used in the JDialog.
 324:    * This method will throw an Error if rootPaneChecking is 
 325:    * enabled.
 326:    *
 327:    * @param manager The LayoutManager to use.
 328:    */
 329:   public void setLayout(LayoutManager manager)
 330:   {
 331:     // Check if we're in initialization stage. If so, call super.setLayout
 332:     // otherwise, valid calls go to the content pane.
 333:     if (initStageDone)
 334:       {
 335:         if (isRootPaneCheckingEnabled())
 336:           throw new Error("Cannot set top-level layout.  Use"
 337:                            + " getConentPane().setLayout instead.");
 338:           getContentPane().setLayout(manager);
 339:       }
 340:     else
 341:       super.setLayout(manager);
 342:   }
 343: 
 344:   /**
 345:    * This method sets the JLayeredPane used in the JDialog.
 346:    * If the given JLayeredPane is null, then this method
 347:    * will throw an Error.
 348:    *
 349:    * @param layeredPane The JLayeredPane to use.
 350:    */
 351:   public void setLayeredPane(JLayeredPane layeredPane)
 352:   {
 353:     if (layeredPane == null)
 354:       throw new IllegalComponentStateException("layeredPane cannot be null.");
 355:     getRootPane().setLayeredPane(layeredPane);
 356:   }
 357: 
 358:   /**
 359:    * This method returns the JLayeredPane used with this JDialog.
 360:    *
 361:    * @return The JLayeredPane used with this JDialog.
 362:    */
 363:   public JLayeredPane getLayeredPane()
 364:   {
 365:     return getRootPane().getLayeredPane();
 366:   }
 367: 
 368:   /**
 369:    * This method returns the JRootPane used with this JDialog.
 370:    *
 371:    * @return The JRootPane used with this JDialog.
 372:    */
 373:   public JRootPane getRootPane()
 374:   {
 375:     if (rootPane == null)
 376:       setRootPane(createRootPane());
 377:     return rootPane;
 378:   }
 379: 
 380:   /**
 381:    * This method sets the JRootPane used with this JDialog.
 382:    *
 383:    * @param root The JRootPane to use.
 384:    */
 385:   protected void setRootPane(JRootPane root)
 386:   {
 387:     if (rootPane != null)
 388:       remove(rootPane);
 389: 
 390:     rootPane = root;
 391:     rootPane.show();
 392:     add(rootPane);
 393:   }
 394: 
 395:   /**
 396:    * This method creates a new JRootPane.
 397:    *
 398:    * @return A new JRootPane.
 399:    */
 400:   protected JRootPane createRootPane()
 401:   {
 402:     return new JRootPane();
 403:   }
 404: 
 405:   /**
 406:    * This method returns the ContentPane
 407:    * in the JRootPane.
 408:    *
 409:    * @return The ContentPane in the JRootPane.
 410:    */
 411:   public Container getContentPane()
 412:   {
 413:     return getRootPane().getContentPane();
 414:   }
 415: 
 416:   /**
 417:    * This method sets the ContentPane to use with this
 418:    * JDialog. If the ContentPane given is null, this method
 419:    * will throw an exception.
 420:    *
 421:    * @param contentPane The ContentPane to use with the JDialog.
 422:    */
 423:   public void setContentPane(Container contentPane)
 424:   {
 425:     if (contentPane == null)
 426:       throw new IllegalComponentStateException("contentPane cannot be null.");
 427:     getRootPane().setContentPane(contentPane);
 428:   }
 429: 
 430:   /**
 431:    * This method returns the GlassPane for this JDialog.
 432:    *
 433:    * @return The GlassPane for this JDialog.
 434:    */
 435:   public Component getGlassPane()
 436:   {
 437:     return getRootPane().getGlassPane();
 438:   }
 439: 
 440:   /**
 441:    * This method sets the GlassPane for this JDialog.
 442:    *
 443:    * @param glassPane The GlassPane for this JDialog.
 444:    */
 445:   public void setGlassPane(Component glassPane)
 446:   {
 447:     getRootPane().setGlassPane(glassPane);
 448:   }
 449: 
 450:   /**
 451:    * This method is called when a component is added to the 
 452:    * the JDialog. Calling this method with rootPaneCheckingEnabled
 453:    * will cause an Error to be thrown.
 454:    *
 455:    * @param comp The component to add.
 456:    * @param constraints The constraints.
 457:    * @param index The position of the component.
 458:    */
 459:   protected void addImpl(Component comp, Object constraints, int index)
 460:   {
 461:     // If we're adding in the initialization stage use super.add.
 462:     // Otherwise pass the add onto the content pane.
 463:     if (!initStageDone)
 464:       super.addImpl(comp, constraints, index);
 465:     else
 466:       {
 467:         if (isRootPaneCheckingEnabled())
 468:           throw new Error("Do not add directly to JDialog."
 469:                           + " Use getContentPane().add instead.");
 470:         getContentPane().add(comp, constraints, index);
 471:       }
 472:   }
 473: 
 474:   /**
 475:    * This method removes a component from the JDialog.
 476:    *
 477:    * @param comp The component to remove.
 478:    */
 479:   public void remove(Component comp)
 480:   {
 481:     // If we're removing the root pane, use super.remove. Otherwise
 482:     // pass it on to the content pane instead.
 483:     if (comp == rootPane)
 484:       super.remove(rootPane);
 485:     else 
 486:       getContentPane().remove(comp);
 487:   }
 488: 
 489:   /**
 490:    * This method returns whether rootPane checking is enabled.
 491:    *
 492:    * @return Whether rootPane checking is enabled.
 493:    */
 494:   protected boolean isRootPaneCheckingEnabled()
 495:   {
 496:     return rootPaneCheckingEnabled;
 497:   }
 498: 
 499:   /**
 500:    * This method sets whether rootPane checking is enabled.
 501:    *
 502:    * @param enabled Whether rootPane checking is enabled.
 503:    */
 504:   protected void setRootPaneCheckingEnabled(boolean enabled)
 505:   {
 506:     rootPaneCheckingEnabled = enabled;
 507:   }
 508: 
 509:   /**
 510:    * This method simply calls paint and returns.
 511:    *
 512:    * @param g The Graphics object to paint with.
 513:    */
 514:   public void update(Graphics g)
 515:   {
 516:     paint(g);
 517:   }
 518:   
 519:   
 520:   /**
 521:    * This method handles window events. This allows the JDialog
 522:    * to honour its default close operation.
 523:    *
 524:    * @param e The WindowEvent.
 525:    */
 526:   protected void processWindowEvent(WindowEvent e)
 527:   {
 528:     //    System.out.println("PROCESS_WIN_EV-1: " + e);
 529:     super.processWindowEvent(e);
 530:     //    System.out.println("PROCESS_WIN_EV-2: " + e);
 531:     switch (e.getID())
 532:       {
 533:       case WindowEvent.WINDOW_CLOSING:
 534:         {
 535:       switch (getDefaultCloseOperation())
 536:         {
 537:         case DISPOSE_ON_CLOSE:
 538:           {
 539:         dispose();
 540:         break;
 541:           }
 542:         case HIDE_ON_CLOSE:
 543:           {
 544:         setVisible(false);
 545:         break;
 546:           }
 547:         case DO_NOTHING_ON_CLOSE:
 548:           break;
 549:         }
 550:       break;
 551:         }
 552:       case WindowEvent.WINDOW_CLOSED:
 553:       case WindowEvent.WINDOW_OPENED:
 554:       case WindowEvent.WINDOW_ICONIFIED:
 555:       case WindowEvent.WINDOW_DEICONIFIED:
 556:       case WindowEvent.WINDOW_ACTIVATED:
 557:       case WindowEvent.WINDOW_DEACTIVATED:
 558:     break;
 559:       }
 560:   }
 561: 
 562:   /**
 563:    * This method sets the action to take
 564:    * when the JDialog is closed.
 565:    *
 566:    * @param operation The action to take.
 567:    */
 568:   public void setDefaultCloseOperation(int operation)
 569:   {
 570:     /* Reference implementation allows invalid operations
 571:        to be specified.  If so, getDefaultCloseOperation
 572:        must return the invalid code, and the behaviour
 573:        defaults to DO_NOTHING_ON_CLOSE.  processWindowEvent
 574:        above handles this */
 575:     close_action = operation;
 576:   }
 577: 
 578:   /**
 579:    * This method returns the action taken when
 580:    * the JDialog is closed.
 581:    *
 582:    * @return The action to take.
 583:    */
 584:   public int getDefaultCloseOperation()
 585:   {
 586:     return close_action;
 587:   }
 588: 
 589:   /**
 590:    * This method returns a String describing the JDialog.
 591:    *
 592:    * @return A String describing the JDialog.
 593:    */
 594:   protected String paramString()
 595:   {
 596:     return "JDialog";
 597:   }
 598: 
 599:   /**
 600:    * DOCUMENT ME!
 601:    *
 602:    * @return DOCUMENT ME!
 603:    */
 604:   public AccessibleContext getAccessibleContext()
 605:   {
 606:     if (accessibleContext == null)
 607:       accessibleContext = new AccessibleJDialog();
 608:     return accessibleContext;
 609:   }
 610: }