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