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