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 glass.setMixingCutoutShape(new Rectangle()); 667 668 boolean visible = false; 669 if (glassPane != null && glassPane.getParent() == this) { 670 this.remove(glassPane); 671 visible = glassPane.isVisible(); 672 } 673 674 glass.setVisible(visible); 675 glassPane = glass; 676 this.add(glassPane, 0); 677 if (visible) { 678 repaint(); 679 } 680 } 681 682 /** 683 * Returns the current glass pane for this <code>JRootPane</code>. 684 * @return the current glass pane 685 * @see #setGlassPane 686 */ 687 public Component getGlassPane() { 688 return glassPane; 689 } 690 691 /** 692 * If a descendant of this <code>JRootPane</code> calls 693 * <code>revalidate</code>, validate from here on down. 694 *<p> 695 * Deferred requests to layout a component and its descendents again. 696 * For example, calls to <code>revalidate</code>, are pushed upwards to 697 * either a <code>JRootPane</code> or a <code>JScrollPane</code> 698 * because both classes override <code>isValidateRoot</code> to return true. 699 * 700 * @see JComponent#isValidateRoot 701 * @see java.awt.Container#isValidateRoot 702 * @return true 703 */ 704 @Override 705 public boolean isValidateRoot() { 706 return true; 707 } 708 709 /** 710 * The <code>glassPane</code> and <code>contentPane</code> 711 * have the same bounds, which means <code>JRootPane</code> 712 * does not tiles its children and this should return false. 713 * On the other hand, the <code>glassPane</code> 714 * is normally not visible, and so this can return true if the 715 * <code>glassPane</code> isn't visible. Therefore, the 716 * return value here depends upon the visibility of the 717 * <code>glassPane</code>. 718 * 719 * @return true if this component's children don't overlap 720 */ 721 public boolean isOptimizedDrawingEnabled() { 722 return !glassPane.isVisible(); 723 } 724 725 /** 726 * {@inheritDoc} 727 */ 728 public void addNotify() { 729 super.addNotify(); 730 enableEvents(AWTEvent.KEY_EVENT_MASK); 731 } 732 733 /** 734 * {@inheritDoc} 735 */ 736 public void removeNotify() { 737 super.removeNotify(); 738 } 739 740 741 /** 742 * Sets the <code>defaultButton</code> property, 743 * which determines the current default button for this <code>JRootPane</code>. 744 * The default button is the button which will be activated 745 * when a UI-defined activation event (typically the <b>Enter</b> key) 746 * occurs in the root pane regardless of whether or not the button 747 * has keyboard focus (unless there is another component within 748 * the root pane which consumes the activation event, 749 * such as a <code>JTextPane</code>). 750 * For default activation to work, the button must be an enabled 751 * descendent of the root pane when activation occurs. 752 * To remove a default button from this root pane, set this 753 * property to <code>null</code>. 754 * 755 * @see JButton#isDefaultButton 756 * @param defaultButton the <code>JButton</code> which is to be the default button 757 */ 758 @BeanProperty(description 759 = "The button activated by default in this root pane") 760 public void setDefaultButton(JButton defaultButton) { 761 JButton oldDefault = this.defaultButton; 762 763 if (oldDefault != defaultButton) { 764 this.defaultButton = defaultButton; 765 766 if (oldDefault != null) { 767 oldDefault.repaint(); 768 } 769 if (defaultButton != null) { 770 defaultButton.repaint(); 771 } 772 } 773 774 firePropertyChange("defaultButton", oldDefault, defaultButton); 775 } 776 777 /** 778 * Returns the value of the <code>defaultButton</code> property. 779 * @return the <code>JButton</code> which is currently the default button 780 * @see #setDefaultButton 781 */ 782 public JButton getDefaultButton() { 783 return defaultButton; 784 } 785 786 final void setUseTrueDoubleBuffering(boolean useTrueDoubleBuffering) { 787 this.useTrueDoubleBuffering = useTrueDoubleBuffering; 788 } 789 790 final boolean getUseTrueDoubleBuffering() { 791 return useTrueDoubleBuffering; 792 } 793 794 final void disableTrueDoubleBuffering() { 795 if (useTrueDoubleBuffering) { 796 if (!IGNORE_DISABLE_TRUE_DOUBLE_BUFFERING) { 797 if (LOG_DISABLE_TRUE_DOUBLE_BUFFERING) { 798 System.out.println("Disabling true double buffering for " + 799 this); 800 Thread.dumpStack(); 801 } 802 useTrueDoubleBuffering = false; 803 RepaintManager.currentManager(this). 804 doubleBufferingChanged(this); 805 } 806 } 807 } 808 809 /** 810 * Overridden to enforce the position of the glass component as 811 * the zero child. 812 * 813 * @param comp the component to be enhanced 814 * @param constraints the constraints to be respected 815 * @param index the index 816 */ 817 protected void addImpl(Component comp, Object constraints, int index) { 818 super.addImpl(comp, constraints, index); 819 820 /// We are making sure the glassPane is on top. 821 if(glassPane != null 822 && glassPane.getParent() == this 823 && getComponent(0) != glassPane) { 824 add(glassPane, 0); 825 } 826 } 827 828 829 /////////////////////////////////////////////////////////////////////////////// 830 //// Begin Inner Classes 831 /////////////////////////////////////////////////////////////////////////////// 832 833 834 /** 835 * A custom layout manager that is responsible for the layout of 836 * layeredPane, glassPane, and menuBar. 837 * <p> 838 * <strong>Warning:</strong> 839 * Serialized objects of this class will not be compatible with 840 * future Swing releases. The current serialization support is 841 * appropriate for short term storage or RMI between applications running 842 * the same version of Swing. As of 1.4, support for long term storage 843 * of all JavaBeans™ 844 * has been added to the <code>java.beans</code> package. 845 * Please see {@link java.beans.XMLEncoder}. 846 */ 847 @SuppressWarnings("serial") 848 protected class RootLayout implements LayoutManager2, Serializable 849 { 850 /** 851 * Returns the amount of space the layout would like to have. 852 * 853 * @param parent the Container for which this layout manager 854 * is being used 855 * @return a Dimension object containing the layout's preferred size 856 */ 857 public Dimension preferredLayoutSize(Container parent) { 858 Dimension rd, mbd; 859 Insets i = getInsets(); 860 861 if(contentPane != null) { 862 rd = contentPane.getPreferredSize(); 863 } else { 864 rd = parent.getSize(); 865 } 866 if(menuBar != null && menuBar.isVisible()) { 867 mbd = menuBar.getPreferredSize(); 868 } else { 869 mbd = new Dimension(0, 0); 870 } 871 return new Dimension(Math.max(rd.width, mbd.width) + i.left + i.right, 872 rd.height + mbd.height + i.top + i.bottom); 873 } 874 875 /** 876 * Returns the minimum amount of space the layout needs. 877 * 878 * @param parent the Container for which this layout manager 879 * is being used 880 * @return a Dimension object containing the layout's minimum size 881 */ 882 public Dimension minimumLayoutSize(Container parent) { 883 Dimension rd, mbd; 884 Insets i = getInsets(); 885 if(contentPane != null) { 886 rd = contentPane.getMinimumSize(); 887 } else { 888 rd = parent.getSize(); 889 } 890 if(menuBar != null && menuBar.isVisible()) { 891 mbd = menuBar.getMinimumSize(); 892 } else { 893 mbd = new Dimension(0, 0); 894 } 895 return new Dimension(Math.max(rd.width, mbd.width) + i.left + i.right, 896 rd.height + mbd.height + i.top + i.bottom); 897 } 898 899 /** 900 * Returns the maximum amount of space the layout can use. 901 * 902 * @param target the Container for which this layout manager 903 * is being used 904 * @return a Dimension object containing the layout's maximum size 905 */ 906 public Dimension maximumLayoutSize(Container target) { 907 Dimension rd, mbd; 908 Insets i = getInsets(); 909 if(menuBar != null && menuBar.isVisible()) { 910 mbd = menuBar.getMaximumSize(); 911 } else { 912 mbd = new Dimension(0, 0); 913 } 914 if(contentPane != null) { 915 rd = contentPane.getMaximumSize(); 916 } else { 917 // This is silly, but should stop an overflow error 918 rd = new Dimension(Integer.MAX_VALUE, 919 Integer.MAX_VALUE - i.top - i.bottom - mbd.height - 1); 920 } 921 return new Dimension(Math.min(rd.width, mbd.width) + i.left + i.right, 922 rd.height + mbd.height + i.top + i.bottom); 923 } 924 925 /** 926 * Instructs the layout manager to perform the layout for the specified 927 * container. 928 * 929 * @param parent the Container for which this layout manager 930 * is being used 931 */ 932 public void layoutContainer(Container parent) { 933 Rectangle b = parent.getBounds(); 934 Insets i = getInsets(); 935 int contentY = 0; 936 int w = b.width - i.right - i.left; 937 int h = b.height - i.top - i.bottom; 938 939 if(layeredPane != null) { 940 layeredPane.setBounds(i.left, i.top, w, h); 941 } 942 if(glassPane != null) { 943 glassPane.setBounds(i.left, i.top, w, h); 944 } 945 // Note: This is laying out the children in the layeredPane, 946 // technically, these are not our children. 947 if(menuBar != null && menuBar.isVisible()) { 948 Dimension mbd = menuBar.getPreferredSize(); 949 menuBar.setBounds(0, 0, w, mbd.height); 950 contentY += mbd.height; 951 } 952 if(contentPane != null) { 953 contentPane.setBounds(0, contentY, w, h - contentY); 954 } 955 } 956 957 public void addLayoutComponent(String name, Component comp) {} 958 public void removeLayoutComponent(Component comp) {} 959 public void addLayoutComponent(Component comp, Object constraints) {} 960 public float getLayoutAlignmentX(Container target) { return 0.0f; } 961 public float getLayoutAlignmentY(Container target) { return 0.0f; } 962 public void invalidateLayout(Container target) {} 963 } 964 965 /** 966 * Returns a string representation of this <code>JRootPane</code>. 967 * This method is intended to be used only for debugging purposes, 968 * and the content and format of the returned string may vary between 969 * implementations. The returned string may be empty but may not 970 * be <code>null</code>. 971 * 972 * @return a string representation of this <code>JRootPane</code>. 973 */ 974 protected String paramString() { 975 return super.paramString(); 976 } 977 978 ///////////////// 979 // Accessibility support 980 //////////////// 981 982 /** 983 * Gets the <code>AccessibleContext</code> associated with this 984 * <code>JRootPane</code>. For root panes, the 985 * <code>AccessibleContext</code> takes the form of an 986 * <code>AccessibleJRootPane</code>. 987 * A new <code>AccessibleJRootPane</code> instance is created if necessary. 988 * 989 * @return an <code>AccessibleJRootPane</code> that serves as the 990 * <code>AccessibleContext</code> of this <code>JRootPane</code> 991 */ 992 public AccessibleContext getAccessibleContext() { 993 if (accessibleContext == null) { 994 accessibleContext = new AccessibleJRootPane(); 995 } 996 return accessibleContext; 997 } 998 999 /** 1000 * This class implements accessibility support for the 1001 * <code>JRootPane</code> class. It provides an implementation of the 1002 * Java Accessibility API appropriate to root pane user-interface elements. 1003 * <p> 1004 * <strong>Warning:</strong> 1005 * Serialized objects of this class will not be compatible with 1006 * future Swing releases. The current serialization support is 1007 * appropriate for short term storage or RMI between applications running 1008 * the same version of Swing. As of 1.4, support for long term storage 1009 * of all JavaBeans™ 1010 * has been added to the <code>java.beans</code> package. 1011 * Please see {@link java.beans.XMLEncoder}. 1012 */ 1013 @SuppressWarnings("serial") 1014 protected class AccessibleJRootPane extends AccessibleJComponent { 1015 /** 1016 * Get the role of this object. 1017 * 1018 * @return an instance of AccessibleRole describing the role of 1019 * the object 1020 */ 1021 public AccessibleRole getAccessibleRole() { 1022 return AccessibleRole.ROOT_PANE; 1023 } 1024 1025 /** 1026 * Returns the number of accessible children of the object. 1027 * 1028 * @return the number of accessible children of the object. 1029 */ 1030 public int getAccessibleChildrenCount() { 1031 return super.getAccessibleChildrenCount(); 1032 } 1033 1034 /** 1035 * Returns the specified Accessible child of the object. The Accessible 1036 * children of an Accessible object are zero-based, so the first child 1037 * of an Accessible child is at index 0, the second child is at index 1, 1038 * and so on. 1039 * 1040 * @param i zero-based index of child 1041 * @return the Accessible child of the object 1042 * @see #getAccessibleChildrenCount 1043 */ 1044 public Accessible getAccessibleChild(int i) { 1045 return super.getAccessibleChild(i); 1046 } 1047 } // inner class AccessibleJRootPane 1048 }