1 7 package javax.swing; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.beans.*; 12 import javax.accessibility.*; 13 import javax.swing.plaf.RootPaneUI ; 14 import java.util.Vector ; 15 import java.io.Serializable ; 16 import javax.swing.border.*; 17 18 19 175 public class JRootPane extends JComponent implements Accessible { 177 178 private static final String uiClassID = "RootPaneUI"; 179 180 187 public static final int NONE = 0; 188 189 196 public static final int FRAME = 1; 197 198 205 public static final int PLAIN_DIALOG = 2; 206 207 214 public static final int INFORMATION_DIALOG = 3; 215 216 223 public static final int ERROR_DIALOG = 4; 224 225 232 public static final int COLOR_CHOOSER_DIALOG = 5; 233 234 241 public static final int FILE_CHOOSER_DIALOG = 6; 242 243 250 public static final int QUESTION_DIALOG = 7; 251 252 259 public static final int WARNING_DIALOG = 8; 260 261 private Component mostRecentFocusOwner; 262 263 private int windowDecorationStyle; 264 265 266 protected JMenuBar menuBar; 267 268 269 protected Container contentPane; 270 271 272 protected JLayeredPane layeredPane; 273 274 278 protected Component glassPane; 279 283 protected JButton defaultButton; 284 293 @Deprecated 294 protected DefaultAction defaultPressAction; 295 304 @Deprecated 305 protected DefaultAction defaultReleaseAction; 306 307 312 public JRootPane() { 313 setGlassPane(createGlassPane()); 314 setLayeredPane(createLayeredPane()); 315 setContentPane(createContentPane()); 316 setLayout(createRootLayout()); 317 setDoubleBuffered(true); 318 updateUI(); 319 } 320 321 333 public int getWindowDecorationStyle() { 334 return windowDecorationStyle; 335 } 336 337 374 public void setWindowDecorationStyle(int windowDecorationStyle) { 375 if (windowDecorationStyle < 0 || 376 windowDecorationStyle > WARNING_DIALOG) { 377 throw new IllegalArgumentException ("Invalid decoration style"); 378 } 379 int oldWindowDecorationStyle = getWindowDecorationStyle(); 380 this.windowDecorationStyle = windowDecorationStyle; 381 firePropertyChange("windowDecorationStyle", 382 oldWindowDecorationStyle, 383 windowDecorationStyle); 384 } 385 386 392 public RootPaneUI getUI() { 393 return (RootPaneUI )ui; 394 } 395 396 409 public void setUI(RootPaneUI ui) { 410 super.setUI(ui); 411 } 412 413 414 419 public void updateUI() { 420 setUI((RootPaneUI )UIManager.getUI(this)); 421 } 422 423 424 433 public String getUIClassID() { 434 return uiClassID; 435 } 436 437 443 protected JLayeredPane createLayeredPane() { 444 JLayeredPane p = new JLayeredPane (); 445 p.setName(this.getName()+".layeredPane"); 446 return p; 447 } 448 449 456 protected Container createContentPane() { 457 JComponent c = new JPanel (); 458 c.setName(this.getName()+".contentPane"); 459 c.setLayout(new BorderLayout() { 460 464 public void addLayoutComponent(Component comp, Object constraints) { 465 if (constraints == null) { 466 constraints = BorderLayout.CENTER; 467 } 468 super.addLayoutComponent(comp, constraints); 469 } 470 }); 471 return c; 472 } 473 474 481 protected Component createGlassPane() { 482 JComponent c = new JPanel (); 483 c.setName(this.getName()+".glassPane"); 484 c.setVisible(false); 485 ((JPanel )c).setOpaque(false); 486 return c; 487 } 488 489 494 protected LayoutManager createRootLayout() { 495 return new RootLayout(); 496 } 497 498 502 public void setJMenuBar(JMenuBar menu) { 503 if(menuBar != null && menuBar.getParent() == layeredPane) 504 layeredPane.remove(menuBar); 505 menuBar = menu; 506 507 if(menuBar != null) 508 layeredPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER); 509 } 510 511 517 @Deprecated 518 public void setMenuBar(JMenuBar menu){ 519 if(menuBar != null && menuBar.getParent() == layeredPane) 520 layeredPane.remove(menuBar); 521 menuBar = menu; 522 523 if(menuBar != null) 524 layeredPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER); 525 } 526 527 531 public JMenuBar getJMenuBar() { return menuBar; } 532 533 539 @Deprecated 540 public JMenuBar getMenuBar() { return menuBar; } 541 542 555 public void setContentPane(Container content) { 556 if(content == null) 557 throw new IllegalComponentStateException("contentPane cannot be set to null."); 558 if(contentPane != null && contentPane.getParent() == layeredPane) 559 layeredPane.remove(contentPane); 560 contentPane = content; 561 562 layeredPane.add(contentPane, JLayeredPane.FRAME_CONTENT_LAYER); 563 } 564 565 571 public Container getContentPane() { return contentPane; } 572 573 582 public void setLayeredPane(JLayeredPane layered) { 583 if(layered == null) 584 throw new IllegalComponentStateException("layeredPane cannot be set to null."); 585 if(layeredPane != null && layeredPane.getParent() == this) 586 this.remove(layeredPane); 587 layeredPane = layered; 588 589 this.add(layeredPane, -1); 590 } 591 597 public JLayeredPane getLayeredPane() { return layeredPane; } 598 599 614 public void setGlassPane(Component glass) { 615 if (glass == null) { 616 throw new NullPointerException ("glassPane cannot be set to null."); 617 } 618 619 boolean visible = false; 620 if (glassPane != null && glassPane.getParent() == this) { 621 this.remove(glassPane); 622 visible = glassPane.isVisible(); 623 } 624 625 glass.setVisible(visible); 626 glassPane = glass; 627 this.add(glassPane, 0); 628 if (visible) { 629 repaint(); 630 } 631 } 632 633 638 public Component getGlassPane() { 639 return glassPane; 640 } 641 642 654 public boolean isValidateRoot() { 655 return true; 656 } 657 658 670 public boolean isOptimizedDrawingEnabled() { 671 return !glassPane.isVisible(); 672 } 673 674 678 public void addNotify() { 679 SystemEventQueueUtilities.addRunnableCanvas(this); 680 super.addNotify(); 681 enableEvents(AWTEvent.KEY_EVENT_MASK); 682 } 683 684 689 693 public void removeNotify() { 694 SystemEventQueueUtilities.removeRunnableCanvas(this); 695 super.removeNotify(); 696 } 697 698 699 719 public void setDefaultButton(JButton defaultButton) { 720 JButton oldDefault = this.defaultButton; 721 722 if (oldDefault != defaultButton) { 723 this.defaultButton = defaultButton; 724 725 if (oldDefault != null) { 726 oldDefault.repaint(); 727 } 728 if (defaultButton != null) { 729 defaultButton.repaint(); 730 } 731 } 732 733 firePropertyChange("defaultButton", oldDefault, defaultButton); 734 } 735 736 741 public JButton getDefaultButton() { 742 return defaultButton; 743 } 744 745 static class DefaultAction extends AbstractAction { 746 JButton owner; 747 JRootPane root; 748 boolean press; 749 DefaultAction(JRootPane root, boolean press) { 750 this.root = root; 751 this.press = press; 752 } 753 public void setOwner(JButton owner) { 754 this.owner = owner; 755 } 756 public void actionPerformed(ActionEvent e) { 757 if (owner != null && SwingUtilities.getRootPane(owner) == root) { 758 ButtonModel model = owner.getModel(); 759 if (press) { 760 model.setArmed(true); 761 model.setPressed(true); 762 } else { 763 model.setPressed(false); 764 } 765 } 766 } 767 public boolean isEnabled() { 768 return owner.getModel().isEnabled(); 769 } 770 } 771 772 773 781 protected void addImpl(Component comp, Object constraints, int index) { 782 super.addImpl(comp, constraints, index); 783 784 if(glassPane != null 786 && glassPane.getParent() == this 787 && getComponent(0) != glassPane) { 788 add(glassPane, 0); 789 } 790 } 791 792 796 797 810 protected class RootLayout implements LayoutManager2, Serializable 811 { 812 819 public Dimension preferredLayoutSize(Container parent) { 820 Dimension rd, mbd; 821 Insets i = getInsets(); 822 823 if(contentPane != null) { 824 rd = contentPane.getPreferredSize(); 825 } else { 826 rd = parent.getSize(); 827 } 828 if(menuBar != null && menuBar.isVisible()) { 829 mbd = menuBar.getPreferredSize(); 830 } else { 831 mbd = new Dimension(0, 0); 832 } 833 return new Dimension(Math.max(rd.width, mbd.width) + i.left + i.right, 834 rd.height + mbd.height + i.top + i.bottom); 835 } 836 837 844 public Dimension minimumLayoutSize(Container parent) { 845 Dimension rd, mbd; 846 Insets i = getInsets(); 847 if(contentPane != null) { 848 rd = contentPane.getMinimumSize(); 849 } else { 850 rd = parent.getSize(); 851 } 852 if(menuBar != null && menuBar.isVisible()) { 853 mbd = menuBar.getMinimumSize(); 854 } else { 855 mbd = new Dimension(0, 0); 856 } 857 return new Dimension(Math.max(rd.width, mbd.width) + i.left + i.right, 858 rd.height + mbd.height + i.top + i.bottom); 859 } 860 861 868 public Dimension maximumLayoutSize(Container target) { 869 Dimension rd, mbd; 870 Insets i = getInsets(); 871 if(menuBar != null && menuBar.isVisible()) { 872 mbd = menuBar.getMaximumSize(); 873 } else { 874 mbd = new Dimension(0, 0); 875 } 876 if(contentPane != null) { 877 rd = contentPane.getMaximumSize(); 878 } else { 879 rd = new Dimension(Integer.MAX_VALUE, 881 Integer.MAX_VALUE - i.top - i.bottom - mbd.height - 1); 882 } 883 return new Dimension(Math.min(rd.width, mbd.width) + i.left + i.right, 884 rd.height + mbd.height + i.top + i.bottom); 885 } 886 887 894 public void layoutContainer(Container parent) { 895 Rectangle b = parent.getBounds(); 896 Insets i = getInsets(); 897 int contentY = 0; 898 int w = b.width - i.right - i.left; 899 int h = b.height - i.top - i.bottom; 900 901 if(layeredPane != null) { 902 layeredPane.setBounds(i.left, i.top, w, h); 903 } 904 if(glassPane != null) { 905 glassPane.setBounds(i.left, i.top, w, h); 906 } 907 if(menuBar != null && menuBar.isVisible()) { 910 Dimension mbd = menuBar.getPreferredSize(); 911 menuBar.setBounds(0, 0, w, mbd.height); 912 contentY += mbd.height; 913 } 914 if(contentPane != null) { 915 contentPane.setBounds(0, contentY, w, h - contentY); 916 } 917 } 918 919 public void addLayoutComponent(String name, Component comp) {} 920 public void removeLayoutComponent(Component comp) {} 921 public void addLayoutComponent(Component comp, Object constraints) {} 922 public float getLayoutAlignmentX(Container target) { return 0.0f; } 923 public float getLayoutAlignmentY(Container target) { return 0.0f; } 924 public void invalidateLayout(Container target) {} 925 } 926 927 void setMostRecentFocusOwner(Component focusOwner) { 928 mostRecentFocusOwner = focusOwner; 929 } 930 931 Component getMostRecentFocusOwner() { 932 return mostRecentFocusOwner; 933 } 934 935 944 protected String paramString() { 945 return super.paramString(); 946 } 947 948 952 962 public AccessibleContext getAccessibleContext() { 963 if (accessibleContext == null) { 964 accessibleContext = new AccessibleJRootPane(); 965 } 966 return accessibleContext; 967 } 968 969 983 protected class AccessibleJRootPane extends AccessibleJComponent { 984 990 public AccessibleRole getAccessibleRole() { 991 return AccessibleRole.ROOT_PANE; 992 } 993 994 999 public int getAccessibleChildrenCount() { 1000 return super.getAccessibleChildrenCount(); 1001 } 1002 1003 1013 public Accessible getAccessibleChild(int i) { 1014 return super.getAccessibleChild(i); 1015 } 1016 } } 1018 | Popular Tags |