1 /* 2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing; 26 27 import java.applet.Applet; 28 import java.awt.*; 29 import java.awt.event.*; 30 import java.beans.*; 31 import java.security.AccessController; 32 import javax.accessibility.*; 33 import javax.swing.plaf.RootPaneUI; 34 import java.util.Vector; 35 import java.io.Serializable; 36 import javax.swing.border.*; 37 38 import sun.awt.AWTAccessor; 39 import sun.security.action.GetBooleanAction; 40 41 42 /** 43 * A lightweight container used behind the scenes by 44 * <code>JFrame</code>, <code>JDialog</code>, <code>JWindow</code>, 45 * <code>JApplet</code>, and <code>JInternalFrame</code>. 46 * For task-oriented information on functionality provided by root panes 47 * see <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html">How to Use Root Panes</a>, 48 * a section in <em>The Java Tutorial</em>. 49 * 50 * <p> 51 * The following image shows the relationships between 52 * the classes that use root panes. 53 * <p style="text-align:center"><img src="doc-files/JRootPane-1.gif" 54 * alt="The following text describes this graphic." 55 * HEIGHT=484 WIDTH=629></p> 56 * The "heavyweight" components (those that delegate to a peer, or native 57 * component on the host system) are shown with a darker, heavier box. The four 58 * heavyweight JFC/Swing containers (<code>JFrame</code>, <code>JDialog</code>, 59 * <code>JWindow</code>, and <code>JApplet</code>) are 60 * shown in relation to the AWT classes they extend. 61 * These four components are the 62 * only heavyweight containers in the Swing library. The lightweight container 63 * <code>JInternalFrame</code> is also shown. 64 * All five of these JFC/Swing containers implement the 65 * <code>RootPaneContainer</code> interface, 66 * and they all delegate their operations to a 67 * <code>JRootPane</code> (shown with a little "handle" on top). 68 * <blockquote> 69 * <b>Note:</b> The <code>JComponent</code> method <code>getRootPane</code> 70 * can be used to obtain the <code>JRootPane</code> that contains 71 * a given component. 72 * </blockquote> 73 * <table style="float:right" border="0" summary="layout"> 74 * <tr> 75 * <td align="center"> 76 * <img src="doc-files/JRootPane-2.gif" 77 * alt="The following text describes this graphic." HEIGHT=386 WIDTH=349> 78 * </td> 79 * </tr> 80 * </table> 81 * The diagram at right shows the structure of a <code>JRootPane</code>. 82 * A <code>JRootpane</code> is made up of a <code>glassPane</code>, 83 * an optional <code>menuBar</code>, and a <code>contentPane</code>. 84 * (The <code>JLayeredPane</code> manages the <code>menuBar</code> 85 * and the <code>contentPane</code>.) 86 * The <code>glassPane</code> sits over the top of everything, 87 * where it is in a position to intercept mouse movements. 88 * Since the <code>glassPane</code> (like the <code>contentPane</code>) 89 * can be an arbitrary component, it is also possible to set up the 90 * <code>glassPane</code> for drawing. Lines and images on the 91 * <code>glassPane</code> can then range 92 * over the frames underneath without being limited by their boundaries. 93 * <p> 94 * Although the <code>menuBar</code> component is optional, 95 * the <code>layeredPane</code>, <code>contentPane</code>, 96 * and <code>glassPane</code> always exist. 97 * Attempting to set them to <code>null</code> generates an exception. 98 * <p> 99 * To add components to the <code>JRootPane</code> (other than the 100 * optional menu bar), you add the object to the <code>contentPane</code> 101 * of the <code>JRootPane</code>, like this: 102 * <pre> 103 * rootPane.getContentPane().add(child); 104 * </pre> 105 * The same principle holds true for setting layout managers, removing 106 * components, listing children, etc. All these methods are invoked on 107 * the <code>contentPane</code> instead of on the <code>JRootPane</code>. 108 * <blockquote> 109 * <b>Note:</b> The default layout manager for the <code>contentPane</code> is 110 * a <code>BorderLayout</code> manager. However, the <code>JRootPane</code> 111 * uses a custom <code>LayoutManager</code>. 112 * So, when you want to change the layout manager for the components you added 113 * to a <code>JRootPane</code>, be sure to use code like this: 114 * <pre> 115 * rootPane.getContentPane().setLayout(new BoxLayout()); 116 * </pre></blockquote> 117 * If a <code>JMenuBar</code> component is set on the <code>JRootPane</code>, 118 * it is positioned along the upper edge of the frame. 119 * The <code>contentPane</code> is adjusted in location and size to 120 * fill the remaining area. 121 * (The <code>JMenuBar</code> and the <code>contentPane</code> are added to the 122 * <code>layeredPane</code> component at the 123 * <code>JLayeredPane.FRAME_CONTENT_LAYER</code> layer.) 124 * <p> 125 * The <code>layeredPane</code> is the parent of all children in the 126 * <code>JRootPane</code> -- both as the direct parent of the menu and 127 * the grandparent of all components added to the <code>contentPane</code>. 128 * It is an instance of <code>JLayeredPane</code>, 129 * which provides the ability to add components at several layers. 130 * This capability is very useful when working with menu popups, 131 * dialog boxes, and dragging -- situations in which you need to place 132 * a component on top of all other components in the pane. 133 * <p> 134 * The <code>glassPane</code> sits on top of all other components in the 135 * <code>JRootPane</code>. 136 * That provides a convenient place to draw above all other components, 137 * and makes it possible to intercept mouse events, 138 * which is useful both for dragging and for drawing. 139 * Developers can use <code>setVisible</code> on the <code>glassPane</code> 140 * to control when the <code>glassPane</code> displays over the other children. 141 * By default the <code>glassPane</code> is not visible. 142 * <p> 143 * The custom <code>LayoutManager</code> used by <code>JRootPane</code> 144 * ensures that: 145 * <OL> 146 * <LI>The <code>glassPane</code> fills the entire viewable 147 * area of the <code>JRootPane</code> (bounds - insets). 148 * <LI>The <code>layeredPane</code> fills the entire viewable area of the 149 * <code>JRootPane</code>. (bounds - insets) 150 * <LI>The <code>menuBar</code> is positioned at the upper edge of the 151 * <code>layeredPane</code>. 152 * <LI>The <code>contentPane</code> fills the entire viewable area, 153 * minus the <code>menuBar</code>, if present. 154 * </OL> 155 * Any other views in the <code>JRootPane</code> view hierarchy are ignored. 156 * <p> 157 * If you replace the <code>LayoutManager</code> of the <code>JRootPane</code>, 158 * you are responsible for managing all of these views. 159 * So ordinarily you will want to be sure that you 160 * change the layout manager for the <code>contentPane</code> rather than 161 * for the <code>JRootPane</code> itself! 162 * <p> 163 * The painting architecture of Swing requires an opaque 164 * <code>JComponent</code> 165 * to exist in the containment hierarchy above all other components. This is 166 * typically provided by way of the content pane. If you replace the content 167 * pane, it is recommended that you make the content pane opaque 168 * by way of <code>setOpaque(true)</code>. Additionally, if the content pane 169 * overrides <code>paintComponent</code>, it 170 * will need to completely fill in the background in an opaque color in 171 * <code>paintComponent</code>. 172 * <p> 173 * <strong>Warning:</strong> Swing is not thread safe. For more 174 * information see <a 175 * href="package-summary.html#threading">Swing's Threading 176 * Policy</a>. 177 * <p> 178 * <strong>Warning:</strong> 179 * Serialized objects of this class will not be compatible with 180 * future Swing releases. The current serialization support is 181 * appropriate for short term storage or RMI between applications running 182 * the same version of Swing. As of 1.4, support for long term storage 183 * of all JavaBeans™ 184 * has been added to the <code>java.beans</code> package. 185 * Please see {@link java.beans.XMLEncoder}. 186 * 187 * @see JLayeredPane 188 * @see JMenuBar 189 * @see JWindow 190 * @see JFrame 191 * @see JDialog 192 * @see JApplet 193 * @see JInternalFrame 194 * @see JComponent 195 * @see BoxLayout 196 * 197 * @see <a href="http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html"> 198 * Mixing Heavy and Light Components</a> 199 * 200 * @author David Kloba 201 * @since 1.2 202 */ 203 /// PENDING(klobad) Who should be opaque in this component? 204 @SuppressWarnings("serial") 205 public class JRootPane extends JComponent implements Accessible { 206 207 private static final String uiClassID = "RootPaneUI"; 208 209 /** 210 * Whether or not we should dump the stack when true double buffering 211 * is disabled. Default is false. 212 */ 213 private static final boolean LOG_DISABLE_TRUE_DOUBLE_BUFFERING; 214 215 /** 216 * Whether or not we should ignore requests to disable true double 217 * buffering. Default is false. 218 */ 219 private static final boolean IGNORE_DISABLE_TRUE_DOUBLE_BUFFERING; 220 221 /** 222 * Constant used for the windowDecorationStyle property. Indicates that 223 * the <code>JRootPane</code> should not provide any sort of 224 * Window decorations. 225 * 226 * @since 1.4 227 */ 228 public static final int NONE = 0; 229 230 /** 231 * Constant used for the windowDecorationStyle property. Indicates that 232 * the <code>JRootPane</code> should provide decorations appropriate for 233 * a Frame. 234 * 235 * @since 1.4 236 */ 237 public static final int FRAME = 1; 238 239 /** 240 * Constant used for the windowDecorationStyle property. Indicates that 241 * the <code>JRootPane</code> should provide decorations appropriate for 242 * a Dialog. 243 * 244 * @since 1.4 245 */ 246 public static final int PLAIN_DIALOG = 2; 247 248 /** 249 * Constant used for the windowDecorationStyle property. Indicates that 250 * the <code>JRootPane</code> should provide decorations appropriate for 251 * a Dialog used to display an informational message. 252 * 253 * @since 1.4 254 */ 255 public static final int INFORMATION_DIALOG = 3; 256 257 /** 258 * Constant used for the windowDecorationStyle property. Indicates that 259 * the <code>JRootPane</code> should provide decorations appropriate for 260 * a Dialog used to display an error message. 261 * 262 * @since 1.4 263 */ 264 public static final int ERROR_DIALOG = 4; 265 266 /** 267 * Constant used for the windowDecorationStyle property. Indicates that 268 * the <code>JRootPane</code> should provide decorations appropriate for 269 * a Dialog used to display a <code>JColorChooser</code>. 270 * 271 * @since 1.4 272 */ 273 public static final int COLOR_CHOOSER_DIALOG = 5; 274 275 /** 276 * Constant used for the windowDecorationStyle property. Indicates that 277 * the <code>JRootPane</code> should provide decorations appropriate for 278 * a Dialog used to display a <code>JFileChooser</code>. 279 * 280 * @since 1.4 281 */ 282 public static final int FILE_CHOOSER_DIALOG = 6; 283 284 /** 285 * Constant used for the windowDecorationStyle property. Indicates that 286 * the <code>JRootPane</code> should provide decorations appropriate for 287 * a Dialog used to present a question to the user. 288 * 289 * @since 1.4 290 */ 291 public static final int QUESTION_DIALOG = 7; 292 293 /** 294 * Constant used for the windowDecorationStyle property. Indicates that 295 * the <code>JRootPane</code> should provide decorations appropriate for 296 * a Dialog used to display a warning message. 297 * 298 * @since 1.4 299 */ 300 public static final int WARNING_DIALOG = 8; 301 302 private int windowDecorationStyle; 303 304 /** The menu bar. */ 305 protected JMenuBar menuBar; 306 307 /** The content pane. */ 308 protected Container contentPane; 309 310 /** The layered pane that manages the menu bar and content pane. */ 311 protected JLayeredPane layeredPane; 312 313 /** 314 * The glass pane that overlays the menu bar and content pane, 315 * so it can intercept mouse movements and such. 316 */ 317 protected Component glassPane; 318 /** 319 * The button that gets activated when the pane has the focus and 320 * a UI-specific action like pressing the <b>Enter</b> key occurs. 321 */ 322 protected JButton defaultButton; 323 324 /** 325 * Whether or not true double buffering should be used. This is typically 326 * true, but may be set to false in special situations. For example, 327 * heavy weight popups (backed by a window) set this to false. 328 */ 329 boolean useTrueDoubleBuffering = true; 330 331 static { 332 LOG_DISABLE_TRUE_DOUBLE_BUFFERING = 333 AccessController.doPrivileged(new GetBooleanAction( 334 "swing.logDoubleBufferingDisable")); 335 IGNORE_DISABLE_TRUE_DOUBLE_BUFFERING = 336 AccessController.doPrivileged(new GetBooleanAction( 337 "swing.ignoreDoubleBufferingDisable")); 338 } 339 340 /** 341 * Creates a <code>JRootPane</code>, setting up its 342 * <code>glassPane</code>, <code>layeredPane</code>, 343 * and <code>contentPane</code>. 344 */ 345 public JRootPane() { 346 setGlassPane(createGlassPane()); 347 setLayeredPane(createLayeredPane()); 348 setContentPane(createContentPane()); 349 setLayout(createRootLayout()); 350 setDoubleBuffered(true); 351 updateUI(); 352 } 353 354 /** 355 * {@inheritDoc} 356 * @since 1.6 357 */ 358 public void setDoubleBuffered(boolean aFlag) { 359 if (isDoubleBuffered() != aFlag) { 360 super.setDoubleBuffered(aFlag); 361 RepaintManager.currentManager(this).doubleBufferingChanged(this); 362 } 363 } 364 365 /** 366 * Returns a constant identifying the type of Window decorations the 367 * <code>JRootPane</code> is providing. 368 * 369 * @return One of <code>NONE</code>, <code>FRAME</code>, 370 * <code>PLAIN_DIALOG</code>, <code>INFORMATION_DIALOG</code>, 371 * <code>ERROR_DIALOG</code>, <code>COLOR_CHOOSER_DIALOG</code>, 372 * <code>FILE_CHOOSER_DIALOG</code>, <code>QUESTION_DIALOG</code> or 373 * <code>WARNING_DIALOG</code>. 374 * @see #setWindowDecorationStyle 375 * @since 1.4 376 */ 377 public int getWindowDecorationStyle() { 378 return windowDecorationStyle; 379 } 380 381 /** 382 * Sets the type of Window decorations (such as borders, widgets for 383 * closing a Window, title ...) the <code>JRootPane</code> should 384 * provide. The default is to provide no Window decorations 385 * (<code>NONE</code>). 386 * <p> 387 * This is only a hint, and some look and feels may not support 388 * this. 389 * This is a bound property. 390 * 391 * @param windowDecorationStyle Constant identifying Window decorations 392 * to provide. 393 * @see JDialog#setDefaultLookAndFeelDecorated 394 * @see JFrame#setDefaultLookAndFeelDecorated 395 * @see LookAndFeel#getSupportsWindowDecorations 396 * @throws IllegalArgumentException if <code>style</code> is 397 * not one of: <code>NONE</code>, <code>FRAME</code>, 398 * <code>PLAIN_DIALOG</code>, <code>INFORMATION_DIALOG</code>, 399 * <code>ERROR_DIALOG</code>, <code>COLOR_CHOOSER_DIALOG</code>, 400 * <code>FILE_CHOOSER_DIALOG</code>, <code>QUESTION_DIALOG</code>, or 401 * <code>WARNING_DIALOG</code>. 402 * @since 1.4 403 */ 404 @BeanProperty(expert = true, visualUpdate = true, enumerationValues = { 405 "JRootPane.NONE", 406 "JRootPane.FRAME", 407 "JRootPane.PLAIN_DIALOG", 408 "JRootPane.INFORMATION_DIALOG", 409 "JRootPane.ERROR_DIALOG", 410 "JRootPane.COLOR_CHOOSER_DIALOG", 411 "JRootPane.FILE_CHOOSER_DIALOG", 412 "JRootPane.QUESTION_DIALOG", 413 "JRootPane.WARNING_DIALOG"}, description 414 = "Identifies the type of Window decorations to provide") 415 public void setWindowDecorationStyle(int windowDecorationStyle) { 416 if (windowDecorationStyle < 0 || 417 windowDecorationStyle > WARNING_DIALOG) { 418 throw new IllegalArgumentException("Invalid decoration style"); 419 } 420 int oldWindowDecorationStyle = getWindowDecorationStyle(); 421 this.windowDecorationStyle = windowDecorationStyle; 422 firePropertyChange("windowDecorationStyle", 423 oldWindowDecorationStyle, 424 windowDecorationStyle); 425 } 426 427 /** 428 * Returns the L&F object that renders this component. 429 * 430 * @return <code>LabelUI</code> object 431 * @since 1.3 432 */ 433 public RootPaneUI getUI() { 434 return (RootPaneUI)ui; 435 } 436 437 /** 438 * Sets the L&F object that renders this component. 439 * 440 * @param ui the <code>LabelUI</code> L&F object 441 * @see UIDefaults#getUI 442 * @since 1.3 443 */ 444 @BeanProperty(expert = true, hidden = true, visualUpdate = true, description 445 = "The UI object that implements the Component's LookAndFeel.") 446 public void setUI(RootPaneUI ui) { 447 super.setUI(ui); 448 } 449 450 451 /** 452 * Resets the UI property to a value from the current look and feel. 453 * 454 * @see JComponent#updateUI 455 */ 456 public void updateUI() { 457 setUI((RootPaneUI)UIManager.getUI(this)); 458 } 459 460 461 /** 462 * Returns a string that specifies the name of the L&F class 463 * that renders this component. 464 * 465 * @return the string "RootPaneUI" 466 * 467 * @see JComponent#getUIClassID 468 * @see UIDefaults#getUI 469 */ 470 public String getUIClassID() { 471 return uiClassID; 472 } 473 474 /** 475 * Called by the constructor methods to create the default 476 * <code>layeredPane</code>. 477 * Bt default it creates a new <code>JLayeredPane</code>. 478 * @return the default <code>layeredPane</code> 479 */ 480 protected JLayeredPane createLayeredPane() { 481 JLayeredPane p = new JLayeredPane(); 482 p.setName(this.getName()+".layeredPane"); 483 return p; 484 } 485 486 /** 487 * Called by the constructor methods to create the default 488 * <code>contentPane</code>. 489 * By default this method creates a new <code>JComponent</code> add sets a 490 * <code>BorderLayout</code> as its <code>LayoutManager</code>. 491 * @return the default <code>contentPane</code> 492 */ 493 protected Container createContentPane() { 494 JComponent c = new JPanel(); 495 c.setName(this.getName()+".contentPane"); 496 c.setLayout(new BorderLayout() { 497 /* This BorderLayout subclass maps a null constraint to CENTER. 498 * Although the reference BorderLayout also does this, some VMs 499 * throw an IllegalArgumentException. 500 */ 501 public void addLayoutComponent(Component comp, Object constraints) { 502 if (constraints == null) { 503 constraints = BorderLayout.CENTER; 504 } 505 super.addLayoutComponent(comp, constraints); 506 } 507 }); 508 return c; 509 } 510 511 /** 512 * Called by the constructor methods to create the default 513 * <code>glassPane</code>. 514 * By default this method creates a new <code>JComponent</code> 515 * with visibility set to false. 516 * @return the default <code>glassPane</code> 517 */ 518 protected Component createGlassPane() { 519 JComponent c = new JPanel(); 520 c.setName(this.getName()+".glassPane"); 521 c.setVisible(false); 522 ((JPanel)c).setOpaque(false); 523 return c; 524 } 525 526 /** 527 * Called by the constructor methods to create the default 528 * <code>layoutManager</code>. 529 * @return the default <code>layoutManager</code>. 530 */ 531 protected LayoutManager createRootLayout() { 532 return new RootLayout(); 533 } 534 535 /** 536 * Adds or changes the menu bar used in the layered pane. 537 * @param menu the <code>JMenuBar</code> to add 538 */ 539 public void setJMenuBar(JMenuBar menu) { 540 if(menuBar != null && menuBar.getParent() == layeredPane) 541 layeredPane.remove(menuBar); 542 menuBar = menu; 543 544 if(menuBar != null) 545 layeredPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER); 546 } 547 548 /** 549 * Specifies the menu bar value. 550 * @deprecated As of Swing version 1.0.3 551 * replaced by <code>setJMenuBar(JMenuBar menu)</code>. 552 * @param menu the <code>JMenuBar</code> to add. 553 */ 554 @Deprecated 555 public void setMenuBar(JMenuBar menu){ 556 if(menuBar != null && menuBar.getParent() == layeredPane) 557 layeredPane.remove(menuBar); 558 menuBar = menu; 559 560 if(menuBar != null) 561 layeredPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER); 562 } 563 564 /** 565 * Returns the menu bar from the layered pane. 566 * @return the <code>JMenuBar</code> used in the pane 567 */ 568 public JMenuBar getJMenuBar() { return menuBar; } 569 570 /** 571 * Returns the menu bar value. 572 * @deprecated As of Swing version 1.0.3 573 * replaced by <code>getJMenuBar()</code>. 574 * @return the <code>JMenuBar</code> used in the pane 575 */ 576 @Deprecated 577 public JMenuBar getMenuBar() { return menuBar; } 578 579 /** 580 * Sets the content pane -- the container that holds the components 581 * parented by the root pane. 582 * <p> 583 * Swing's painting architecture requires an opaque <code>JComponent</code> 584 * in the containment hierarchy. This is typically provided by the 585 * content pane. If you replace the content pane it is recommended you 586 * replace it with an opaque <code>JComponent</code>. 587 * 588 * @param content the <code>Container</code> to use for component-contents 589 * @exception java.awt.IllegalComponentStateException (a runtime 590 * exception) if the content pane parameter is <code>null</code> 591 */ 592 public void setContentPane(Container content) { 593 if(content == null) 594 throw new IllegalComponentStateException("contentPane cannot be set to null."); 595 if(contentPane != null && contentPane.getParent() == layeredPane) 596 layeredPane.remove(contentPane); 597 contentPane = content; 598 599 layeredPane.add(contentPane, JLayeredPane.FRAME_CONTENT_LAYER); 600 } 601 602 /** 603 * Returns the content pane -- the container that holds the components 604 * parented by the root pane. 605 * 606 * @return the <code>Container</code> that holds the component-contents 607 */ 608 public Container getContentPane() { return contentPane; } 609 610 // PENDING(klobad) Should this reparent the contentPane and MenuBar? 611 /** 612 * Sets the layered pane for the root pane. The layered pane 613 * typically holds a content pane and an optional <code>JMenuBar</code>. 614 * 615 * @param layered the <code>JLayeredPane</code> to use 616 * @exception java.awt.IllegalComponentStateException (a runtime 617 * exception) if the layered pane parameter is <code>null</code> 618 */ 619 public void setLayeredPane(JLayeredPane layered) { 620 if(layered == null) 621 throw new IllegalComponentStateException("layeredPane cannot be set to null."); 622 if(layeredPane != null && layeredPane.getParent() == this) 623 this.remove(layeredPane); 624 layeredPane = layered; 625 626 this.add(layeredPane, -1); 627 } 628 /** 629 * Gets the layered pane used by the root pane. The layered pane 630 * typically holds a content pane and an optional <code>JMenuBar</code>. 631 * 632 * @return the <code>JLayeredPane</code> currently in use 633 */ 634 public JLayeredPane getLayeredPane() { return layeredPane; } 635 636 /** 637 * Sets a specified <code>Component</code> to be the glass pane for this 638 * root pane. The glass pane should normally be a lightweight, 639 * transparent component, because it will be made visible when 640 * ever the root pane needs to grab input events. 641 * <p> 642 * The new glass pane's visibility is changed to match that of 643 * the current glass pane. An implication of this is that care 644 * must be taken when you want to replace the glass pane and 645 * make it visible. Either of the following will work: 646 * <pre> 647 * root.setGlassPane(newGlassPane); 648 * newGlassPane.setVisible(true); 649 * </pre> 650 * or: 651 * <pre> 652 * root.getGlassPane().setVisible(true); 653 * root.setGlassPane(newGlassPane); 654 * </pre> 655 * 656 * @param glass the <code>Component</code> to use as the glass pane 657 * for this <code>JRootPane</code> 658 * @exception NullPointerException if the <code>glass</code> parameter is 659 * <code>null</code> 660 */ 661 public void setGlassPane(Component glass) { 662 if (glass == null) { 663 throw new NullPointerException("glassPane cannot be set to null."); 664 } 665 666 AWTAccessor.getComponentAccessor().setMixingCutoutShape(glass, 667 new Rectangle()); 668 669 boolean visible = false; 670 if (glassPane != null && glassPane.getParent() == this) { 671 this.remove(glassPane); 672 visible = glassPane.isVisible(); 673 } 674 675 glass.setVisible(visible); 676 glassPane = glass; 677 this.add(glassPane, 0); 678 if (visible) { 679 repaint(); 680 } 681 } 682 683 /** 684 * Returns the current glass pane for this <code>JRootPane</code>. 685 * @return the current glass pane 686 * @see #setGlassPane 687 */ 688 public Component getGlassPane() { 689 return glassPane; 690 } 691 692 /** 693 * If a descendant of this <code>JRootPane</code> calls 694 * <code>revalidate</code>, validate from here on down. 695 *<p> 696 * Deferred requests to layout a component and its descendents again. 697 * For example, calls to <code>revalidate</code>, are pushed upwards to 698 * either a <code>JRootPane</code> or a <code>JScrollPane</code> 699 * because both classes override <code>isValidateRoot</code> to return true. 700 * 701 * @see JComponent#isValidateRoot 702 * @see java.awt.Container#isValidateRoot 703 * @return true 704 */ 705 @Override 706 public boolean isValidateRoot() { 707 return true; 708 } 709 710 /** 711 * The <code>glassPane</code> and <code>contentPane</code> 712 * have the same bounds, which means <code>JRootPane</code> 713 * does not tiles its children and this should return false. 714 * On the other hand, the <code>glassPane</code> 715 * is normally not visible, and so this can return true if the 716 * <code>glassPane</code> isn't visible. Therefore, the 717 * return value here depends upon the visibility of the 718 * <code>glassPane</code>. 719 * 720 * @return true if this component's children don't overlap 721 */ 722 public boolean isOptimizedDrawingEnabled() { 723 return !glassPane.isVisible(); 724 } 725 726 /** 727 * {@inheritDoc} 728 */ 729 public void addNotify() { 730 super.addNotify(); 731 enableEvents(AWTEvent.KEY_EVENT_MASK); 732 } 733 734 /** 735 * {@inheritDoc} 736 */ 737 public void removeNotify() { 738 super.removeNotify(); 739 } 740 741 742 /** 743 * Sets the <code>defaultButton</code> property, 744 * which determines the current default button for this <code>JRootPane</code>. 745 * The default button is the button which will be activated 746 * when a UI-defined activation event (typically the <b>Enter</b> key) 747 * occurs in the root pane regardless of whether or not the button 748 * has keyboard focus (unless there is another component within 749 * the root pane which consumes the activation event, 750 * such as a <code>JTextPane</code>). 751 * For default activation to work, the button must be an enabled 752 * descendent of the root pane when activation occurs. 753 * To remove a default button from this root pane, set this 754 * property to <code>null</code>. 755 * 756 * @see JButton#isDefaultButton 757 * @param defaultButton the <code>JButton</code> which is to be the default button 758 */ 759 @BeanProperty(description 760 = "The button activated by default in this root pane") 761 public void setDefaultButton(JButton defaultButton) { 762 JButton oldDefault = this.defaultButton; 763 764 if (oldDefault != defaultButton) { 765 this.defaultButton = defaultButton; 766 767 if (oldDefault != null) { 768 oldDefault.repaint(); 769 } 770 if (defaultButton != null) { 771 defaultButton.repaint(); 772 } 773 } 774 775 firePropertyChange("defaultButton", oldDefault, defaultButton); 776 } 777 778 /** 779 * Returns the value of the <code>defaultButton</code> property. 780 * @return the <code>JButton</code> which is currently the default button 781 * @see #setDefaultButton 782 */ 783 public JButton getDefaultButton() { 784 return defaultButton; 785 } 786 787 final void setUseTrueDoubleBuffering(boolean useTrueDoubleBuffering) { 788 this.useTrueDoubleBuffering = useTrueDoubleBuffering; 789 } 790 791 final boolean getUseTrueDoubleBuffering() { 792 return useTrueDoubleBuffering; 793 } 794 795 final void disableTrueDoubleBuffering() { 796 if (useTrueDoubleBuffering) { 797 if (!IGNORE_DISABLE_TRUE_DOUBLE_BUFFERING) { 798 if (LOG_DISABLE_TRUE_DOUBLE_BUFFERING) { 799 System.out.println("Disabling true double buffering for " + 800 this); 801 Thread.dumpStack(); 802 } 803 useTrueDoubleBuffering = false; 804 RepaintManager.currentManager(this). 805 doubleBufferingChanged(this); 806 } 807 } 808 } 809 810 /** 811 * Overridden to enforce the position of the glass component as 812 * the zero child. 813 * 814 * @param comp the component to be enhanced 815 * @param constraints the constraints to be respected 816 * @param index the index 817 */ 818 protected void addImpl(Component comp, Object constraints, int index) { 819 super.addImpl(comp, constraints, index); 820 821 /// We are making sure the glassPane is on top. 822 if(glassPane != null 823 && glassPane.getParent() == this 824 && getComponent(0) != glassPane) { 825 add(glassPane, 0); 826 } 827 } 828 829 830 /////////////////////////////////////////////////////////////////////////////// 831 //// Begin Inner Classes 832 /////////////////////////////////////////////////////////////////////////////// 833 834 835 /** 836 * A custom layout manager that is responsible for the layout of 837 * layeredPane, glassPane, and menuBar. 838 * <p> 839 * <strong>Warning:</strong> 840 * Serialized objects of this class will not be compatible with 841 * future Swing releases. The current serialization support is 842 * appropriate for short term storage or RMI between applications running 843 * the same version of Swing. As of 1.4, support for long term storage 844 * of all JavaBeans™ 845 * has been added to the <code>java.beans</code> package. 846 * Please see {@link java.beans.XMLEncoder}. 847 */ 848 @SuppressWarnings("serial") 849 protected class RootLayout implements LayoutManager2, Serializable 850 { 851 /** 852 * Returns the amount of space the layout would like to have. 853 * 854 * @param parent the Container for which this layout manager 855 * is being used 856 * @return a Dimension object containing the layout's preferred size 857 */ 858 public Dimension preferredLayoutSize(Container parent) { 859 Dimension rd, mbd; 860 Insets i = getInsets(); 861 862 if(contentPane != null) { 863 rd = contentPane.getPreferredSize(); 864 } else { 865 rd = parent.getSize(); 866 } 867 if(menuBar != null && menuBar.isVisible()) { 868 mbd = menuBar.getPreferredSize(); 869 } else { 870 mbd = new Dimension(0, 0); 871 } 872 return new Dimension(Math.max(rd.width, mbd.width) + i.left + i.right, 873 rd.height + mbd.height + i.top + i.bottom); 874 } 875 876 /** 877 * Returns the minimum amount of space the layout needs. 878 * 879 * @param parent the Container for which this layout manager 880 * is being used 881 * @return a Dimension object containing the layout's minimum size 882 */ 883 public Dimension minimumLayoutSize(Container parent) { 884 Dimension rd, mbd; 885 Insets i = getInsets(); 886 if(contentPane != null) { 887 rd = contentPane.getMinimumSize(); 888 } else { 889 rd = parent.getSize(); 890 } 891 if(menuBar != null && menuBar.isVisible()) { 892 mbd = menuBar.getMinimumSize(); 893 } else { 894 mbd = new Dimension(0, 0); 895 } 896 return new Dimension(Math.max(rd.width, mbd.width) + i.left + i.right, 897 rd.height + mbd.height + i.top + i.bottom); 898 } 899 900 /** 901 * Returns the maximum amount of space the layout can use. 902 * 903 * @param target the Container for which this layout manager 904 * is being used 905 * @return a Dimension object containing the layout's maximum size 906 */ 907 public Dimension maximumLayoutSize(Container target) { 908 Dimension rd, mbd; 909 Insets i = getInsets(); 910 if(menuBar != null && menuBar.isVisible()) { 911 mbd = menuBar.getMaximumSize(); 912 } else { 913 mbd = new Dimension(0, 0); 914 } 915 if(contentPane != null) { 916 rd = contentPane.getMaximumSize(); 917 } else { 918 // This is silly, but should stop an overflow error 919 rd = new Dimension(Integer.MAX_VALUE, 920 Integer.MAX_VALUE - i.top - i.bottom - mbd.height - 1); 921 } 922 return new Dimension(Math.min(rd.width, mbd.width) + i.left + i.right, 923 rd.height + mbd.height + i.top + i.bottom); 924 } 925 926 /** 927 * Instructs the layout manager to perform the layout for the specified 928 * container. 929 * 930 * @param parent the Container for which this layout manager 931 * is being used 932 */ 933 public void layoutContainer(Container parent) { 934 Rectangle b = parent.getBounds(); 935 Insets i = getInsets(); 936 int contentY = 0; 937 int w = b.width - i.right - i.left; 938 int h = b.height - i.top - i.bottom; 939 940 if(layeredPane != null) { 941 layeredPane.setBounds(i.left, i.top, w, h); 942 } 943 if(glassPane != null) { 944 glassPane.setBounds(i.left, i.top, w, h); 945 } 946 // Note: This is laying out the children in the layeredPane, 947 // technically, these are not our children. 948 if(menuBar != null && menuBar.isVisible()) { 949 Dimension mbd = menuBar.getPreferredSize(); 950 menuBar.setBounds(0, 0, w, mbd.height); 951 contentY += mbd.height; 952 } 953 if(contentPane != null) { 954 contentPane.setBounds(0, contentY, w, h - contentY); 955 } 956 } 957 958 public void addLayoutComponent(String name, Component comp) {} 959 public void removeLayoutComponent(Component comp) {} 960 public void addLayoutComponent(Component comp, Object constraints) {} 961 public float getLayoutAlignmentX(Container target) { return 0.0f; } 962 public float getLayoutAlignmentY(Container target) { return 0.0f; } 963 public void invalidateLayout(Container target) {} 964 } 965 966 /** 967 * Returns a string representation of this <code>JRootPane</code>. 968 * This method is intended to be used only for debugging purposes, 969 * and the content and format of the returned string may vary between 970 * implementations. The returned string may be empty but may not 971 * be <code>null</code>. 972 * 973 * @return a string representation of this <code>JRootPane</code>. 974 */ 975 protected String paramString() { 976 return super.paramString(); 977 } 978 979 ///////////////// 980 // Accessibility support 981 //////////////// 982 983 /** 984 * Gets the <code>AccessibleContext</code> associated with this 985 * <code>JRootPane</code>. For root panes, the 986 * <code>AccessibleContext</code> takes the form of an 987 * <code>AccessibleJRootPane</code>. 988 * A new <code>AccessibleJRootPane</code> instance is created if necessary. 989 * 990 * @return an <code>AccessibleJRootPane</code> that serves as the 991 * <code>AccessibleContext</code> of this <code>JRootPane</code> 992 */ 993 public AccessibleContext getAccessibleContext() { 994 if (accessibleContext == null) { 995 accessibleContext = new AccessibleJRootPane(); 996 } 997 return accessibleContext; 998 } 999 1000 /** 1001 * This class implements accessibility support for the 1002 * <code>JRootPane</code> class. It provides an implementation of the 1003 * Java Accessibility API appropriate to root pane user-interface elements. 1004 * <p> 1005 * <strong>Warning:</strong> 1006 * Serialized objects of this class will not be compatible with 1007 * future Swing releases. The current serialization support is 1008 * appropriate for short term storage or RMI between applications running 1009 * the same version of Swing. As of 1.4, support for long term storage 1010 * of all JavaBeans™ 1011 * has been added to the <code>java.beans</code> package. 1012 * Please see {@link java.beans.XMLEncoder}. 1013 */ 1014 @SuppressWarnings("serial") 1015 protected class AccessibleJRootPane extends AccessibleJComponent { 1016 /** 1017 * Get the role of this object. 1018 * 1019 * @return an instance of AccessibleRole describing the role of 1020 * the object 1021 */ 1022 public AccessibleRole getAccessibleRole() { 1023 return AccessibleRole.ROOT_PANE; 1024 } 1025 1026 /** 1027 * Returns the number of accessible children of the object. 1028 * 1029 * @return the number of accessible children of the object. 1030 */ 1031 public int getAccessibleChildrenCount() { 1032 return super.getAccessibleChildrenCount(); 1033 } 1034 1035 /** 1036 * Returns the specified Accessible child of the object. The Accessible 1037 * children of an Accessible object are zero-based, so the first child 1038 * of an Accessible child is at index 0, the second child is at index 1, 1039 * and so on. 1040 * 1041 * @param i zero-based index of child 1042 * @return the Accessible child of the object 1043 * @see #getAccessibleChildrenCount 1044 */ 1045 public Accessible getAccessibleChild(int i) { 1046 return super.getAccessibleChild(i); 1047 } 1048 } // inner class AccessibleJRootPane 1049 }