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