1 7 package javax.swing; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.beans.PropertyChangeListener ; 12 import java.util.Locale ; 13 import java.util.Vector ; 14 import java.io.Serializable ; 15 import javax.accessibility.*; 16 import java.applet.Applet ; 17 18 80 public class JDialog extends Dialog implements WindowConstants , Accessible, RootPaneContainer 81 { 82 86 private static final Object defaultLookAndFeelDecoratedKey = 87 new StringBuffer ("JDialog.defaultLookAndFeelDecorated"); 88 89 private int defaultCloseOperation = HIDE_ON_CLOSE; 90 91 95 protected JRootPane rootPane; 96 97 106 protected boolean rootPaneCheckingEnabled = false; 107 108 109 122 public JDialog() throws HeadlessException { 123 this((Frame)null, false); 124 } 125 126 141 public JDialog(Frame owner) throws HeadlessException { 142 this(owner, false); 143 } 144 145 162 public JDialog(Frame owner, boolean modal) throws HeadlessException { 163 this(owner, null, modal); 164 } 165 166 183 public JDialog(Frame owner, String title) throws HeadlessException { 184 this(owner, title, false); 185 } 186 187 210 public JDialog(Frame owner, String title, boolean modal) 211 throws HeadlessException { 212 super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner, 213 title, modal); 214 if (owner == null) { 215 WindowListener ownerShutdownListener = 216 (WindowListener)SwingUtilities.getSharedOwnerFrameShutdownListener(); 217 addWindowListener(ownerShutdownListener); 218 } 219 dialogInit(); 220 } 221 222 249 public JDialog(Frame owner, String title, boolean modal, 250 GraphicsConfiguration gc) { 251 super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner, 252 title, modal, gc); 253 if (owner == null) { 254 WindowListener ownerShutdownListener = 255 (WindowListener)SwingUtilities.getSharedOwnerFrameShutdownListener(); 256 addWindowListener(ownerShutdownListener); 257 } 258 dialogInit(); 259 } 260 261 274 public JDialog(Dialog owner) throws HeadlessException { 275 this(owner, false); 276 } 277 278 293 public JDialog(Dialog owner, boolean modal) throws HeadlessException { 294 this(owner, null, modal); 295 } 296 297 312 public JDialog(Dialog owner, String title) throws HeadlessException { 313 this(owner, title, false); 314 } 315 316 333 public JDialog(Dialog owner, String title, boolean modal) 334 throws HeadlessException { 335 super(owner, title, modal); 336 dialogInit(); 337 } 338 339 366 public JDialog(Dialog owner, String title, boolean modal, 367 GraphicsConfiguration gc) throws HeadlessException { 368 369 super(owner, title, modal, gc); 370 dialogInit(); 371 } 372 373 376 protected void dialogInit() { 377 enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK); 378 setLocale( JComponent.getDefaultLocale() ); 379 setRootPane(createRootPane()); 380 setRootPaneCheckingEnabled(true); 381 if (JDialog.isDefaultLookAndFeelDecorated()) { 382 boolean supportsWindowDecorations = 383 UIManager.getLookAndFeel().getSupportsWindowDecorations(); 384 if (supportsWindowDecorations) { 385 setUndecorated(true); 386 getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); 387 } 388 } 389 sun.awt.SunToolkit.checkAndSetPolicy(this, true); 390 } 391 392 396 protected JRootPane createRootPane() { 397 JRootPane rp = new JRootPane (); 398 rp.setOpaque(true); 403 return rp; 404 } 405 406 412 protected void processWindowEvent(WindowEvent e) { 413 super.processWindowEvent(e); 414 415 if (e.getID() == WindowEvent.WINDOW_CLOSING) { 416 switch(defaultCloseOperation) { 417 case HIDE_ON_CLOSE: 418 setVisible(false); 419 break; 420 case DISPOSE_ON_CLOSE: 421 setVisible(false); 422 dispose(); 423 break; 424 case DO_NOTHING_ON_CLOSE: 425 default: 426 break; 427 } 428 } 429 } 430 431 432 461 public void setDefaultCloseOperation(int operation) { 462 this.defaultCloseOperation = operation; 463 } 464 465 472 public int getDefaultCloseOperation() { 473 return defaultCloseOperation; 474 } 475 476 477 483 public void update(Graphics g) { 484 paint(g); 485 } 486 487 498 public void setJMenuBar(JMenuBar menu) { 499 getRootPane().setMenuBar(menu); 500 } 501 502 507 public JMenuBar getJMenuBar() { 508 return getRootPane().getMenuBar(); 509 } 510 511 512 524 protected boolean isRootPaneCheckingEnabled() { 525 return rootPaneCheckingEnabled; 526 } 527 528 529 545 protected void setRootPaneCheckingEnabled(boolean enabled) { 546 rootPaneCheckingEnabled = enabled; 547 } 548 549 568 protected void addImpl(Component comp, Object constraints, int index) 569 { 570 if(isRootPaneCheckingEnabled()) { 571 getContentPane().add(comp, constraints, index); 572 } 573 else { 574 super.addImpl(comp, constraints, index); 575 } 576 } 577 578 590 public void remove(Component comp) { 591 if (comp == rootPane) { 592 super.remove(comp); 593 } else { 594 getContentPane().remove(comp); 595 } 596 } 597 598 599 610 public void setLayout(LayoutManager manager) { 611 if(isRootPaneCheckingEnabled()) { 612 getContentPane().setLayout(manager); 613 } 614 else { 615 super.setLayout(manager); 616 } 617 } 618 619 620 626 public JRootPane getRootPane() { 627 return rootPane; 628 } 629 630 631 643 protected void setRootPane(JRootPane root) { 644 if(rootPane != null) { 645 remove(rootPane); 646 } 647 rootPane = root; 648 if(rootPane != null) { 649 boolean checkingEnabled = isRootPaneCheckingEnabled(); 650 try { 651 setRootPaneCheckingEnabled(false); 652 add(rootPane, BorderLayout.CENTER); 653 } 654 finally { 655 setRootPaneCheckingEnabled(checkingEnabled); 656 } 657 } 658 } 659 660 661 669 public Container getContentPane() { 670 return getRootPane().getContentPane(); 671 } 672 673 674 696 public void setContentPane(Container contentPane) { 697 getRootPane().setContentPane(contentPane); 698 } 699 700 708 public JLayeredPane getLayeredPane() { 709 return getRootPane().getLayeredPane(); 710 } 711 712 727 public void setLayeredPane(JLayeredPane layeredPane) { 728 getRootPane().setLayeredPane(layeredPane); 729 } 730 731 739 public Component getGlassPane() { 740 return getRootPane().getGlassPane(); 741 } 742 743 755 public void setGlassPane(Component glassPane) { 756 getRootPane().setGlassPane(glassPane); 757 } 758 759 783 public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) { 784 if (defaultLookAndFeelDecorated) { 785 SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE); 786 } else { 787 SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE); 788 } 789 } 790 791 799 public static boolean isDefaultLookAndFeelDecorated() { 800 Boolean defaultLookAndFeelDecorated = 801 (Boolean ) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey); 802 if (defaultLookAndFeelDecorated == null) { 803 defaultLookAndFeelDecorated = Boolean.FALSE; 804 } 805 return defaultLookAndFeelDecorated.booleanValue(); 806 } 807 808 818 protected String paramString() { 819 String defaultCloseOperationString; 820 if (defaultCloseOperation == HIDE_ON_CLOSE) { 821 defaultCloseOperationString = "HIDE_ON_CLOSE"; 822 } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) { 823 defaultCloseOperationString = "DISPOSE_ON_CLOSE"; 824 } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) { 825 defaultCloseOperationString = "DO_NOTHING_ON_CLOSE"; 826 } else defaultCloseOperationString = ""; 827 String rootPaneString = (rootPane != null ? 828 rootPane.toString() : ""); 829 String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ? 830 "true" : "false"); 831 832 return super.paramString() + 833 ",defaultCloseOperation=" + defaultCloseOperationString + 834 ",rootPane=" + rootPaneString + 835 ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString; 836 } 837 838 839 843 protected AccessibleContext accessibleContext = null; 844 845 854 public AccessibleContext getAccessibleContext() { 855 if (accessibleContext == null) { 856 accessibleContext = new AccessibleJDialog(); 857 } 858 return accessibleContext; 859 } 860 861 867 protected class AccessibleJDialog extends AccessibleAWTDialog { 868 869 877 public String getAccessibleName() { 878 if (accessibleName != null) { 879 return accessibleName; 880 } else { 881 if (getTitle() == null) { 882 return super.getAccessibleName(); 883 } else { 884 return getTitle(); 885 } 886 } 887 } 888 889 896 public AccessibleStateSet getAccessibleStateSet() { 897 AccessibleStateSet states = super.getAccessibleStateSet(); 898 899 if (isResizable()) { 900 states.add(AccessibleState.RESIZABLE); 901 } 902 if (getFocusOwner() != null) { 903 states.add(AccessibleState.ACTIVE); 904 } 905 if (isModal()) { 906 states.add(AccessibleState.MODAL); 907 } 908 return states; 909 } 910 911 } } 913 | Popular Tags |