JRootPane.java revision 14508:2de40053200a
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 */
25package javax.swing;
26
27import java.applet.Applet;
28import java.awt.*;
29import java.awt.event.*;
30import java.beans.*;
31import java.security.AccessController;
32import javax.accessibility.*;
33import javax.swing.plaf.RootPaneUI;
34import java.util.Vector;
35import java.io.Serializable;
36import javax.swing.border.*;
37
38import sun.awt.AWTAccessor;
39import 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 &quot;heavyweight&quot; 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&trade;
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")
205public 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     * As of Java 2 platform v1.3 this unusable field is no longer used.
325     * To override the default button you should replace the <code>Action</code>
326     * in the <code>JRootPane</code>'s <code>ActionMap</code>. Please refer to
327     * the key bindings specification for further details.
328     *
329     * @deprecated As of Java 2 platform v1.3.
330     *  @see #defaultButton
331     */
332    @Deprecated
333    protected DefaultAction defaultPressAction;
334    /**
335     * As of Java 2 platform v1.3 this unusable field is no longer used.
336     * To override the default button you should replace the <code>Action</code>
337     * in the <code>JRootPane</code>'s <code>ActionMap</code>. Please refer to
338     * the key bindings specification for further details.
339     *
340     * @deprecated As of Java 2 platform v1.3.
341     *  @see #defaultButton
342     */
343    @Deprecated
344    protected DefaultAction defaultReleaseAction;
345
346    /**
347     * Whether or not true double buffering should be used.  This is typically
348     * true, but may be set to false in special situations.  For example,
349     * heavy weight popups (backed by a window) set this to false.
350     */
351    boolean useTrueDoubleBuffering = true;
352
353    static {
354        LOG_DISABLE_TRUE_DOUBLE_BUFFERING =
355            AccessController.doPrivileged(new GetBooleanAction(
356                                   "swing.logDoubleBufferingDisable"));
357        IGNORE_DISABLE_TRUE_DOUBLE_BUFFERING =
358            AccessController.doPrivileged(new GetBooleanAction(
359                                   "swing.ignoreDoubleBufferingDisable"));
360    }
361
362    /**
363     * Creates a <code>JRootPane</code>, setting up its
364     * <code>glassPane</code>, <code>layeredPane</code>,
365     * and <code>contentPane</code>.
366     */
367    public JRootPane() {
368        setGlassPane(createGlassPane());
369        setLayeredPane(createLayeredPane());
370        setContentPane(createContentPane());
371        setLayout(createRootLayout());
372        setDoubleBuffered(true);
373        updateUI();
374    }
375
376    /**
377     * {@inheritDoc}
378     * @since 1.6
379     */
380    public void setDoubleBuffered(boolean aFlag) {
381        if (isDoubleBuffered() != aFlag) {
382            super.setDoubleBuffered(aFlag);
383            RepaintManager.currentManager(this).doubleBufferingChanged(this);
384        }
385    }
386
387    /**
388     * Returns a constant identifying the type of Window decorations the
389     * <code>JRootPane</code> is providing.
390     *
391     * @return One of <code>NONE</code>, <code>FRAME</code>,
392     *        <code>PLAIN_DIALOG</code>, <code>INFORMATION_DIALOG</code>,
393     *        <code>ERROR_DIALOG</code>, <code>COLOR_CHOOSER_DIALOG</code>,
394     *        <code>FILE_CHOOSER_DIALOG</code>, <code>QUESTION_DIALOG</code> or
395     *        <code>WARNING_DIALOG</code>.
396     * @see #setWindowDecorationStyle
397     * @since 1.4
398     */
399    public int getWindowDecorationStyle() {
400        return windowDecorationStyle;
401    }
402
403    /**
404     * Sets the type of Window decorations (such as borders, widgets for
405     * closing a Window, title ...) the <code>JRootPane</code> should
406     * provide. The default is to provide no Window decorations
407     * (<code>NONE</code>).
408     * <p>
409     * This is only a hint, and some look and feels may not support
410     * this.
411     * This is a bound property.
412     *
413     * @param windowDecorationStyle Constant identifying Window decorations
414     *        to provide.
415     * @see JDialog#setDefaultLookAndFeelDecorated
416     * @see JFrame#setDefaultLookAndFeelDecorated
417     * @see LookAndFeel#getSupportsWindowDecorations
418     * @throws IllegalArgumentException if <code>style</code> is
419     *        not one of: <code>NONE</code>, <code>FRAME</code>,
420     *        <code>PLAIN_DIALOG</code>, <code>INFORMATION_DIALOG</code>,
421     *        <code>ERROR_DIALOG</code>, <code>COLOR_CHOOSER_DIALOG</code>,
422     *        <code>FILE_CHOOSER_DIALOG</code>, <code>QUESTION_DIALOG</code>, or
423     *        <code>WARNING_DIALOG</code>.
424     * @since 1.4
425     */
426    @BeanProperty(expert = true, visualUpdate = true, enumerationValues = {
427            "JRootPane.NONE",
428            "JRootPane.FRAME",
429            "JRootPane.PLAIN_DIALOG",
430            "JRootPane.INFORMATION_DIALOG",
431            "JRootPane.ERROR_DIALOG",
432            "JRootPane.COLOR_CHOOSER_DIALOG",
433            "JRootPane.FILE_CHOOSER_DIALOG",
434            "JRootPane.QUESTION_DIALOG",
435            "JRootPane.WARNING_DIALOG"}, description
436            = "Identifies the type of Window decorations to provide")
437    public void setWindowDecorationStyle(int windowDecorationStyle) {
438        if (windowDecorationStyle < 0 ||
439                  windowDecorationStyle > WARNING_DIALOG) {
440            throw new IllegalArgumentException("Invalid decoration style");
441        }
442        int oldWindowDecorationStyle = getWindowDecorationStyle();
443        this.windowDecorationStyle = windowDecorationStyle;
444        firePropertyChange("windowDecorationStyle",
445                            oldWindowDecorationStyle,
446                            windowDecorationStyle);
447    }
448
449    /**
450     * Returns the L&amp;F object that renders this component.
451     *
452     * @return <code>LabelUI</code> object
453     * @since 1.3
454     */
455    public RootPaneUI getUI() {
456        return (RootPaneUI)ui;
457    }
458
459    /**
460     * Sets the L&amp;F object that renders this component.
461     *
462     * @param ui  the <code>LabelUI</code> L&amp;F object
463     * @see UIDefaults#getUI
464     * @since 1.3
465     */
466    @BeanProperty(expert = true, hidden = true, visualUpdate = true, description
467            = "The UI object that implements the Component's LookAndFeel.")
468    public void setUI(RootPaneUI ui) {
469        super.setUI(ui);
470    }
471
472
473    /**
474     * Resets the UI property to a value from the current look and feel.
475     *
476     * @see JComponent#updateUI
477     */
478    public void updateUI() {
479        setUI((RootPaneUI)UIManager.getUI(this));
480    }
481
482
483    /**
484     * Returns a string that specifies the name of the L&amp;F class
485     * that renders this component.
486     *
487     * @return the string "RootPaneUI"
488     *
489     * @see JComponent#getUIClassID
490     * @see UIDefaults#getUI
491     */
492    public String getUIClassID() {
493        return uiClassID;
494    }
495
496    /**
497      * Called by the constructor methods to create the default
498      * <code>layeredPane</code>.
499      * Bt default it creates a new <code>JLayeredPane</code>.
500      * @return the default <code>layeredPane</code>
501      */
502    protected JLayeredPane createLayeredPane() {
503        JLayeredPane p = new JLayeredPane();
504        p.setName(this.getName()+".layeredPane");
505        return p;
506    }
507
508    /**
509     * Called by the constructor methods to create the default
510     * <code>contentPane</code>.
511     * By default this method creates a new <code>JComponent</code> add sets a
512     * <code>BorderLayout</code> as its <code>LayoutManager</code>.
513     * @return the default <code>contentPane</code>
514     */
515    protected Container createContentPane() {
516        JComponent c = new JPanel();
517        c.setName(this.getName()+".contentPane");
518        c.setLayout(new BorderLayout() {
519            /* This BorderLayout subclass maps a null constraint to CENTER.
520             * Although the reference BorderLayout also does this, some VMs
521             * throw an IllegalArgumentException.
522             */
523            public void addLayoutComponent(Component comp, Object constraints) {
524                if (constraints == null) {
525                    constraints = BorderLayout.CENTER;
526                }
527                super.addLayoutComponent(comp, constraints);
528            }
529        });
530        return c;
531    }
532
533    /**
534      * Called by the constructor methods to create the default
535      * <code>glassPane</code>.
536      * By default this method creates a new <code>JComponent</code>
537      * with visibility set to false.
538      * @return the default <code>glassPane</code>
539      */
540    protected Component createGlassPane() {
541        JComponent c = new JPanel();
542        c.setName(this.getName()+".glassPane");
543        c.setVisible(false);
544        ((JPanel)c).setOpaque(false);
545        return c;
546    }
547
548    /**
549     * Called by the constructor methods to create the default
550     * <code>layoutManager</code>.
551     * @return the default <code>layoutManager</code>.
552     */
553    protected LayoutManager createRootLayout() {
554        return new RootLayout();
555    }
556
557    /**
558     * Adds or changes the menu bar used in the layered pane.
559     * @param menu the <code>JMenuBar</code> to add
560     */
561    public void setJMenuBar(JMenuBar menu) {
562        if(menuBar != null && menuBar.getParent() == layeredPane)
563            layeredPane.remove(menuBar);
564        menuBar = menu;
565
566        if(menuBar != null)
567            layeredPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER);
568    }
569
570    /**
571     * Specifies the menu bar value.
572     * @deprecated As of Swing version 1.0.3
573     *  replaced by <code>setJMenuBar(JMenuBar menu)</code>.
574     * @param menu the <code>JMenuBar</code> to add.
575     */
576    @Deprecated
577    public void setMenuBar(JMenuBar menu){
578        if(menuBar != null && menuBar.getParent() == layeredPane)
579            layeredPane.remove(menuBar);
580        menuBar = menu;
581
582        if(menuBar != null)
583            layeredPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER);
584    }
585
586    /**
587     * Returns the menu bar from the layered pane.
588     * @return the <code>JMenuBar</code> used in the pane
589     */
590    public JMenuBar getJMenuBar() { return menuBar; }
591
592    /**
593     * Returns the menu bar value.
594     * @deprecated As of Swing version 1.0.3
595     *  replaced by <code>getJMenuBar()</code>.
596     * @return the <code>JMenuBar</code> used in the pane
597     */
598    @Deprecated
599    public JMenuBar getMenuBar() { return menuBar; }
600
601    /**
602     * Sets the content pane -- the container that holds the components
603     * parented by the root pane.
604     * <p>
605     * Swing's painting architecture requires an opaque <code>JComponent</code>
606     * in the containment hierarchy. This is typically provided by the
607     * content pane. If you replace the content pane it is recommended you
608     * replace it with an opaque <code>JComponent</code>.
609     *
610     * @param content the <code>Container</code> to use for component-contents
611     * @exception java.awt.IllegalComponentStateException (a runtime
612     *            exception) if the content pane parameter is <code>null</code>
613     */
614    public void setContentPane(Container content) {
615        if(content == null)
616            throw new IllegalComponentStateException("contentPane cannot be set to null.");
617        if(contentPane != null && contentPane.getParent() == layeredPane)
618            layeredPane.remove(contentPane);
619        contentPane = content;
620
621        layeredPane.add(contentPane, JLayeredPane.FRAME_CONTENT_LAYER);
622    }
623
624    /**
625     * Returns the content pane -- the container that holds the components
626     * parented by the root pane.
627     *
628     * @return the <code>Container</code> that holds the component-contents
629     */
630    public Container getContentPane() { return contentPane; }
631
632// PENDING(klobad) Should this reparent the contentPane and MenuBar?
633    /**
634     * Sets the layered pane for the root pane. The layered pane
635     * typically holds a content pane and an optional <code>JMenuBar</code>.
636     *
637     * @param layered  the <code>JLayeredPane</code> to use
638     * @exception java.awt.IllegalComponentStateException (a runtime
639     *            exception) if the layered pane parameter is <code>null</code>
640     */
641    public void setLayeredPane(JLayeredPane layered) {
642        if(layered == null)
643            throw new IllegalComponentStateException("layeredPane cannot be set to null.");
644        if(layeredPane != null && layeredPane.getParent() == this)
645            this.remove(layeredPane);
646        layeredPane = layered;
647
648        this.add(layeredPane, -1);
649    }
650    /**
651     * Gets the layered pane used by the root pane. The layered pane
652     * typically holds a content pane and an optional <code>JMenuBar</code>.
653     *
654     * @return the <code>JLayeredPane</code> currently in use
655     */
656    public JLayeredPane getLayeredPane() { return layeredPane; }
657
658    /**
659     * Sets a specified <code>Component</code> to be the glass pane for this
660     * root pane.  The glass pane should normally be a lightweight,
661     * transparent component, because it will be made visible when
662     * ever the root pane needs to grab input events.
663     * <p>
664     * The new glass pane's visibility is changed to match that of
665     * the current glass pane.  An implication of this is that care
666     * must be taken when you want to replace the glass pane and
667     * make it visible.  Either of the following will work:
668     * <pre>
669     *   root.setGlassPane(newGlassPane);
670     *   newGlassPane.setVisible(true);
671     * </pre>
672     * or:
673     * <pre>
674     *   root.getGlassPane().setVisible(true);
675     *   root.setGlassPane(newGlassPane);
676     * </pre>
677     *
678     * @param glass the <code>Component</code> to use as the glass pane
679     *              for this <code>JRootPane</code>
680     * @exception NullPointerException if the <code>glass</code> parameter is
681     *          <code>null</code>
682     */
683    public void setGlassPane(Component glass) {
684        if (glass == null) {
685            throw new NullPointerException("glassPane cannot be set to null.");
686        }
687
688        AWTAccessor.getComponentAccessor().setMixingCutoutShape(glass,
689                new Rectangle());
690
691        boolean visible = false;
692        if (glassPane != null && glassPane.getParent() == this) {
693            this.remove(glassPane);
694            visible = glassPane.isVisible();
695        }
696
697        glass.setVisible(visible);
698        glassPane = glass;
699        this.add(glassPane, 0);
700        if (visible) {
701            repaint();
702        }
703    }
704
705    /**
706     * Returns the current glass pane for this <code>JRootPane</code>.
707     * @return the current glass pane
708     * @see #setGlassPane
709     */
710    public Component getGlassPane() {
711        return glassPane;
712    }
713
714    /**
715     * If a descendant of this <code>JRootPane</code> calls
716     * <code>revalidate</code>, validate from here on down.
717     *<p>
718     * Deferred requests to layout a component and its descendents again.
719     * For example, calls to <code>revalidate</code>, are pushed upwards to
720     * either a <code>JRootPane</code> or a <code>JScrollPane</code>
721     * because both classes override <code>isValidateRoot</code> to return true.
722     *
723     * @see JComponent#isValidateRoot
724     * @see java.awt.Container#isValidateRoot
725     * @return true
726     */
727    @Override
728    public boolean isValidateRoot() {
729        return true;
730    }
731
732    /**
733     * The <code>glassPane</code> and <code>contentPane</code>
734     * have the same bounds, which means <code>JRootPane</code>
735     * does not tiles its children and this should return false.
736     * On the other hand, the <code>glassPane</code>
737     * is normally not visible, and so this can return true if the
738     * <code>glassPane</code> isn't visible. Therefore, the
739     * return value here depends upon the visibility of the
740     * <code>glassPane</code>.
741     *
742     * @return true if this component's children don't overlap
743     */
744    public boolean isOptimizedDrawingEnabled() {
745        return !glassPane.isVisible();
746    }
747
748    /**
749     * {@inheritDoc}
750     */
751    public void addNotify() {
752        super.addNotify();
753        enableEvents(AWTEvent.KEY_EVENT_MASK);
754    }
755
756    /**
757     * {@inheritDoc}
758     */
759    public void removeNotify() {
760        super.removeNotify();
761    }
762
763
764    /**
765     * Sets the <code>defaultButton</code> property,
766     * which determines the current default button for this <code>JRootPane</code>.
767     * The default button is the button which will be activated
768     * when a UI-defined activation event (typically the <b>Enter</b> key)
769     * occurs in the root pane regardless of whether or not the button
770     * has keyboard focus (unless there is another component within
771     * the root pane which consumes the activation event,
772     * such as a <code>JTextPane</code>).
773     * For default activation to work, the button must be an enabled
774     * descendent of the root pane when activation occurs.
775     * To remove a default button from this root pane, set this
776     * property to <code>null</code>.
777     *
778     * @see JButton#isDefaultButton
779     * @param defaultButton the <code>JButton</code> which is to be the default button
780     */
781    @BeanProperty(description
782            = "The button activated by default in this root pane")
783    public void setDefaultButton(JButton defaultButton) {
784        JButton oldDefault = this.defaultButton;
785
786        if (oldDefault != defaultButton) {
787            this.defaultButton = defaultButton;
788
789            if (oldDefault != null) {
790                oldDefault.repaint();
791            }
792            if (defaultButton != null) {
793                defaultButton.repaint();
794            }
795        }
796
797        firePropertyChange("defaultButton", oldDefault, defaultButton);
798    }
799
800    /**
801     * Returns the value of the <code>defaultButton</code> property.
802     * @return the <code>JButton</code> which is currently the default button
803     * @see #setDefaultButton
804     */
805    public JButton getDefaultButton() {
806        return defaultButton;
807    }
808
809    final void setUseTrueDoubleBuffering(boolean useTrueDoubleBuffering) {
810        this.useTrueDoubleBuffering = useTrueDoubleBuffering;
811    }
812
813    final boolean getUseTrueDoubleBuffering() {
814        return useTrueDoubleBuffering;
815    }
816
817    final void disableTrueDoubleBuffering() {
818        if (useTrueDoubleBuffering) {
819            if (!IGNORE_DISABLE_TRUE_DOUBLE_BUFFERING) {
820                if (LOG_DISABLE_TRUE_DOUBLE_BUFFERING) {
821                    System.out.println("Disabling true double buffering for " +
822                                       this);
823                    Thread.dumpStack();
824                }
825                useTrueDoubleBuffering = false;
826                RepaintManager.currentManager(this).
827                        doubleBufferingChanged(this);
828            }
829        }
830    }
831
832    @SuppressWarnings("serial")
833    static class DefaultAction extends AbstractAction {
834        JButton owner;
835        JRootPane root;
836        boolean press;
837        DefaultAction(JRootPane root, boolean press) {
838            this.root = root;
839            this.press = press;
840        }
841        public void setOwner(JButton owner) {
842            this.owner = owner;
843        }
844        public void actionPerformed(ActionEvent e) {
845            if (owner != null && SwingUtilities.getRootPane(owner) == root) {
846                ButtonModel model = owner.getModel();
847                if (press) {
848                    model.setArmed(true);
849                    model.setPressed(true);
850                } else {
851                    model.setPressed(false);
852                }
853            }
854        }
855        public boolean isEnabled() {
856            return owner.getModel().isEnabled();
857        }
858    }
859
860
861    /**
862     * Overridden to enforce the position of the glass component as
863     * the zero child.
864     *
865     * @param comp the component to be enhanced
866     * @param constraints the constraints to be respected
867     * @param index the index
868     */
869    protected void addImpl(Component comp, Object constraints, int index) {
870        super.addImpl(comp, constraints, index);
871
872        /// We are making sure the glassPane is on top.
873        if(glassPane != null
874            && glassPane.getParent() == this
875            && getComponent(0) != glassPane) {
876            add(glassPane, 0);
877        }
878    }
879
880
881///////////////////////////////////////////////////////////////////////////////
882//// Begin Inner Classes
883///////////////////////////////////////////////////////////////////////////////
884
885
886    /**
887     * A custom layout manager that is responsible for the layout of
888     * layeredPane, glassPane, and menuBar.
889     * <p>
890     * <strong>Warning:</strong>
891     * Serialized objects of this class will not be compatible with
892     * future Swing releases. The current serialization support is
893     * appropriate for short term storage or RMI between applications running
894     * the same version of Swing.  As of 1.4, support for long term storage
895     * of all JavaBeans&trade;
896     * has been added to the <code>java.beans</code> package.
897     * Please see {@link java.beans.XMLEncoder}.
898     */
899    @SuppressWarnings("serial")
900    protected class RootLayout implements LayoutManager2, Serializable
901    {
902        /**
903         * Returns the amount of space the layout would like to have.
904         *
905         * @param parent the Container for which this layout manager
906         * is being used
907         * @return a Dimension object containing the layout's preferred size
908         */
909        public Dimension preferredLayoutSize(Container parent) {
910            Dimension rd, mbd;
911            Insets i = getInsets();
912
913            if(contentPane != null) {
914                rd = contentPane.getPreferredSize();
915            } else {
916                rd = parent.getSize();
917            }
918            if(menuBar != null && menuBar.isVisible()) {
919                mbd = menuBar.getPreferredSize();
920            } else {
921                mbd = new Dimension(0, 0);
922            }
923            return new Dimension(Math.max(rd.width, mbd.width) + i.left + i.right,
924                                        rd.height + mbd.height + i.top + i.bottom);
925        }
926
927        /**
928         * Returns the minimum amount of space the layout needs.
929         *
930         * @param parent the Container for which this layout manager
931         * is being used
932         * @return a Dimension object containing the layout's minimum size
933         */
934        public Dimension minimumLayoutSize(Container parent) {
935            Dimension rd, mbd;
936            Insets i = getInsets();
937            if(contentPane != null) {
938                rd = contentPane.getMinimumSize();
939            } else {
940                rd = parent.getSize();
941            }
942            if(menuBar != null && menuBar.isVisible()) {
943                mbd = menuBar.getMinimumSize();
944            } else {
945                mbd = new Dimension(0, 0);
946            }
947            return new Dimension(Math.max(rd.width, mbd.width) + i.left + i.right,
948                        rd.height + mbd.height + i.top + i.bottom);
949        }
950
951        /**
952         * Returns the maximum amount of space the layout can use.
953         *
954         * @param target the Container for which this layout manager
955         * is being used
956         * @return a Dimension object containing the layout's maximum size
957         */
958        public Dimension maximumLayoutSize(Container target) {
959            Dimension rd, mbd;
960            Insets i = getInsets();
961            if(menuBar != null && menuBar.isVisible()) {
962                mbd = menuBar.getMaximumSize();
963            } else {
964                mbd = new Dimension(0, 0);
965            }
966            if(contentPane != null) {
967                rd = contentPane.getMaximumSize();
968            } else {
969                // This is silly, but should stop an overflow error
970                rd = new Dimension(Integer.MAX_VALUE,
971                        Integer.MAX_VALUE - i.top - i.bottom - mbd.height - 1);
972            }
973            return new Dimension(Math.min(rd.width, mbd.width) + i.left + i.right,
974                                         rd.height + mbd.height + i.top + i.bottom);
975        }
976
977        /**
978         * Instructs the layout manager to perform the layout for the specified
979         * container.
980         *
981         * @param parent the Container for which this layout manager
982         * is being used
983         */
984        public void layoutContainer(Container parent) {
985            Rectangle b = parent.getBounds();
986            Insets i = getInsets();
987            int contentY = 0;
988            int w = b.width - i.right - i.left;
989            int h = b.height - i.top - i.bottom;
990
991            if(layeredPane != null) {
992                layeredPane.setBounds(i.left, i.top, w, h);
993            }
994            if(glassPane != null) {
995                glassPane.setBounds(i.left, i.top, w, h);
996            }
997            // Note: This is laying out the children in the layeredPane,
998            // technically, these are not our children.
999            if(menuBar != null && menuBar.isVisible()) {
1000                Dimension mbd = menuBar.getPreferredSize();
1001                menuBar.setBounds(0, 0, w, mbd.height);
1002                contentY += mbd.height;
1003            }
1004            if(contentPane != null) {
1005                contentPane.setBounds(0, contentY, w, h - contentY);
1006            }
1007        }
1008
1009        public void addLayoutComponent(String name, Component comp) {}
1010        public void removeLayoutComponent(Component comp) {}
1011        public void addLayoutComponent(Component comp, Object constraints) {}
1012        public float getLayoutAlignmentX(Container target) { return 0.0f; }
1013        public float getLayoutAlignmentY(Container target) { return 0.0f; }
1014        public void invalidateLayout(Container target) {}
1015    }
1016
1017    /**
1018     * Returns a string representation of this <code>JRootPane</code>.
1019     * This method is intended to be used only for debugging purposes,
1020     * and the content and format of the returned string may vary between
1021     * implementations. The returned string may be empty but may not
1022     * be <code>null</code>.
1023     *
1024     * @return  a string representation of this <code>JRootPane</code>.
1025     */
1026    protected String paramString() {
1027        return super.paramString();
1028    }
1029
1030/////////////////
1031// Accessibility support
1032////////////////
1033
1034    /**
1035     * Gets the <code>AccessibleContext</code> associated with this
1036     * <code>JRootPane</code>. For root panes, the
1037     * <code>AccessibleContext</code> takes the form of an
1038     * <code>AccessibleJRootPane</code>.
1039     * A new <code>AccessibleJRootPane</code> instance is created if necessary.
1040     *
1041     * @return an <code>AccessibleJRootPane</code> that serves as the
1042     *         <code>AccessibleContext</code> of this <code>JRootPane</code>
1043     */
1044    public AccessibleContext getAccessibleContext() {
1045        if (accessibleContext == null) {
1046            accessibleContext = new AccessibleJRootPane();
1047        }
1048        return accessibleContext;
1049    }
1050
1051    /**
1052     * This class implements accessibility support for the
1053     * <code>JRootPane</code> class.  It provides an implementation of the
1054     * Java Accessibility API appropriate to root pane user-interface elements.
1055     * <p>
1056     * <strong>Warning:</strong>
1057     * Serialized objects of this class will not be compatible with
1058     * future Swing releases. The current serialization support is
1059     * appropriate for short term storage or RMI between applications running
1060     * the same version of Swing.  As of 1.4, support for long term storage
1061     * of all JavaBeans&trade;
1062     * has been added to the <code>java.beans</code> package.
1063     * Please see {@link java.beans.XMLEncoder}.
1064     */
1065    @SuppressWarnings("serial")
1066    protected class AccessibleJRootPane extends AccessibleJComponent {
1067        /**
1068         * Get the role of this object.
1069         *
1070         * @return an instance of AccessibleRole describing the role of
1071         * the object
1072         */
1073        public AccessibleRole getAccessibleRole() {
1074            return AccessibleRole.ROOT_PANE;
1075        }
1076
1077        /**
1078         * Returns the number of accessible children of the object.
1079         *
1080         * @return the number of accessible children of the object.
1081         */
1082        public int getAccessibleChildrenCount() {
1083            return super.getAccessibleChildrenCount();
1084        }
1085
1086        /**
1087         * Returns the specified Accessible child of the object.  The Accessible
1088         * children of an Accessible object are zero-based, so the first child
1089         * of an Accessible child is at index 0, the second child is at index 1,
1090         * and so on.
1091         *
1092         * @param i zero-based index of child
1093         * @return the Accessible child of the object
1094         * @see #getAccessibleChildrenCount
1095         */
1096        public Accessible getAccessibleChild(int i) {
1097            return super.getAccessibleChild(i);
1098        }
1099    } // inner class AccessibleJRootPane
1100}
1101