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