JRootPane.java revision 17034:a1a3ef8fd66f
1/*
2 * Copyright (c) 1997, 2017, 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    /**
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&amp;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&amp;F object that renders this component.
439     *
440     * @param ui  the <code>LabelUI</code> L&amp;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&amp;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            menuBar.updateUI();
546            layeredPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER);
547        }
548    }
549
550    /**
551     * Specifies the menu bar value.
552     * @deprecated As of Swing version 1.0.3
553     *  replaced by <code>setJMenuBar(JMenuBar menu)</code>.
554     * @param menu the <code>JMenuBar</code> to add.
555     */
556    @Deprecated
557    public void setMenuBar(JMenuBar menu){
558        if(menuBar != null && menuBar.getParent() == layeredPane)
559            layeredPane.remove(menuBar);
560        menuBar = menu;
561
562        if(menuBar != null)
563            layeredPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER);
564    }
565
566    /**
567     * Returns the menu bar from the layered pane.
568     * @return the <code>JMenuBar</code> used in the pane
569     */
570    public JMenuBar getJMenuBar() { return menuBar; }
571
572    /**
573     * Returns the menu bar value.
574     * @deprecated As of Swing version 1.0.3
575     *  replaced by <code>getJMenuBar()</code>.
576     * @return the <code>JMenuBar</code> used in the pane
577     */
578    @Deprecated
579    public JMenuBar getMenuBar() { return menuBar; }
580
581    /**
582     * Sets the content pane -- the container that holds the components
583     * parented by the root pane.
584     * <p>
585     * Swing's painting architecture requires an opaque <code>JComponent</code>
586     * in the containment hierarchy. This is typically provided by the
587     * content pane. If you replace the content pane it is recommended you
588     * replace it with an opaque <code>JComponent</code>.
589     *
590     * @param content the <code>Container</code> to use for component-contents
591     * @exception java.awt.IllegalComponentStateException (a runtime
592     *            exception) if the content pane parameter is <code>null</code>
593     */
594    public void setContentPane(Container content) {
595        if(content == null)
596            throw new IllegalComponentStateException("contentPane cannot be set to null.");
597        if(contentPane != null && contentPane.getParent() == layeredPane)
598            layeredPane.remove(contentPane);
599        contentPane = content;
600
601        layeredPane.add(contentPane, JLayeredPane.FRAME_CONTENT_LAYER);
602    }
603
604    /**
605     * Returns the content pane -- the container that holds the components
606     * parented by the root pane.
607     *
608     * @return the <code>Container</code> that holds the component-contents
609     */
610    public Container getContentPane() { return contentPane; }
611
612// PENDING(klobad) Should this reparent the contentPane and MenuBar?
613    /**
614     * Sets the layered pane for the root pane. The layered pane
615     * typically holds a content pane and an optional <code>JMenuBar</code>.
616     *
617     * @param layered  the <code>JLayeredPane</code> to use
618     * @exception java.awt.IllegalComponentStateException (a runtime
619     *            exception) if the layered pane parameter is <code>null</code>
620     */
621    public void setLayeredPane(JLayeredPane layered) {
622        if(layered == null)
623            throw new IllegalComponentStateException("layeredPane cannot be set to null.");
624        if(layeredPane != null && layeredPane.getParent() == this)
625            this.remove(layeredPane);
626        layeredPane = layered;
627
628        this.add(layeredPane, -1);
629    }
630    /**
631     * Gets the layered pane used by the root pane. The layered pane
632     * typically holds a content pane and an optional <code>JMenuBar</code>.
633     *
634     * @return the <code>JLayeredPane</code> currently in use
635     */
636    public JLayeredPane getLayeredPane() { return layeredPane; }
637
638    /**
639     * Sets a specified <code>Component</code> to be the glass pane for this
640     * root pane.  The glass pane should normally be a lightweight,
641     * transparent component, because it will be made visible when
642     * ever the root pane needs to grab input events.
643     * <p>
644     * The new glass pane's visibility is changed to match that of
645     * the current glass pane.  An implication of this is that care
646     * must be taken when you want to replace the glass pane and
647     * make it visible.  Either of the following will work:
648     * <pre>
649     *   root.setGlassPane(newGlassPane);
650     *   newGlassPane.setVisible(true);
651     * </pre>
652     * or:
653     * <pre>
654     *   root.getGlassPane().setVisible(true);
655     *   root.setGlassPane(newGlassPane);
656     * </pre>
657     *
658     * @param glass the <code>Component</code> to use as the glass pane
659     *              for this <code>JRootPane</code>
660     * @exception NullPointerException if the <code>glass</code> parameter is
661     *          <code>null</code>
662     */
663    public void setGlassPane(Component glass) {
664        if (glass == null) {
665            throw new NullPointerException("glassPane cannot be set to null.");
666        }
667
668        glass.setMixingCutoutShape(new Rectangle());
669
670        boolean visible = false;
671        if (glassPane != null && glassPane.getParent() == this) {
672            this.remove(glassPane);
673            visible = glassPane.isVisible();
674        }
675
676        glass.setVisible(visible);
677        glassPane = glass;
678        this.add(glassPane, 0);
679        if (visible) {
680            repaint();
681        }
682    }
683
684    /**
685     * Returns the current glass pane for this <code>JRootPane</code>.
686     * @return the current glass pane
687     * @see #setGlassPane
688     */
689    public Component getGlassPane() {
690        return glassPane;
691    }
692
693    /**
694     * If a descendant of this <code>JRootPane</code> calls
695     * <code>revalidate</code>, validate from here on down.
696     *<p>
697     * Deferred requests to layout a component and its descendents again.
698     * For example, calls to <code>revalidate</code>, are pushed upwards to
699     * either a <code>JRootPane</code> or a <code>JScrollPane</code>
700     * because both classes override <code>isValidateRoot</code> to return true.
701     *
702     * @see JComponent#isValidateRoot
703     * @see java.awt.Container#isValidateRoot
704     * @return true
705     */
706    @Override
707    public boolean isValidateRoot() {
708        return true;
709    }
710
711    /**
712     * The <code>glassPane</code> and <code>contentPane</code>
713     * have the same bounds, which means <code>JRootPane</code>
714     * does not tiles its children and this should return false.
715     * On the other hand, the <code>glassPane</code>
716     * is normally not visible, and so this can return true if the
717     * <code>glassPane</code> isn't visible. Therefore, the
718     * return value here depends upon the visibility of the
719     * <code>glassPane</code>.
720     *
721     * @return true if this component's children don't overlap
722     */
723    public boolean isOptimizedDrawingEnabled() {
724        return !glassPane.isVisible();
725    }
726
727    /**
728     * {@inheritDoc}
729     */
730    public void addNotify() {
731        super.addNotify();
732        enableEvents(AWTEvent.KEY_EVENT_MASK);
733    }
734
735    /**
736     * {@inheritDoc}
737     */
738    public void removeNotify() {
739        super.removeNotify();
740    }
741
742
743    /**
744     * Sets the <code>defaultButton</code> property,
745     * which determines the current default button for this <code>JRootPane</code>.
746     * The default button is the button which will be activated
747     * when a UI-defined activation event (typically the <b>Enter</b> key)
748     * occurs in the root pane regardless of whether or not the button
749     * has keyboard focus (unless there is another component within
750     * the root pane which consumes the activation event,
751     * such as a <code>JTextPane</code>).
752     * For default activation to work, the button must be an enabled
753     * descendent of the root pane when activation occurs.
754     * To remove a default button from this root pane, set this
755     * property to <code>null</code>.
756     *
757     * @see JButton#isDefaultButton
758     * @param defaultButton the <code>JButton</code> which is to be the default button
759     */
760    @BeanProperty(description
761            = "The button activated by default in this root pane")
762    public void setDefaultButton(JButton defaultButton) {
763        JButton oldDefault = this.defaultButton;
764
765        if (oldDefault != defaultButton) {
766            this.defaultButton = defaultButton;
767
768            if (oldDefault != null) {
769                oldDefault.repaint();
770            }
771            if (defaultButton != null) {
772                defaultButton.repaint();
773            }
774        }
775
776        firePropertyChange("defaultButton", oldDefault, defaultButton);
777    }
778
779    /**
780     * Returns the value of the <code>defaultButton</code> property.
781     * @return the <code>JButton</code> which is currently the default button
782     * @see #setDefaultButton
783     */
784    public JButton getDefaultButton() {
785        return defaultButton;
786    }
787
788    final void setUseTrueDoubleBuffering(boolean useTrueDoubleBuffering) {
789        this.useTrueDoubleBuffering = useTrueDoubleBuffering;
790    }
791
792    final boolean getUseTrueDoubleBuffering() {
793        return useTrueDoubleBuffering;
794    }
795
796    final void disableTrueDoubleBuffering() {
797        if (useTrueDoubleBuffering) {
798            if (!IGNORE_DISABLE_TRUE_DOUBLE_BUFFERING) {
799                if (LOG_DISABLE_TRUE_DOUBLE_BUFFERING) {
800                    System.out.println("Disabling true double buffering for " +
801                                       this);
802                    Thread.dumpStack();
803                }
804                useTrueDoubleBuffering = false;
805                RepaintManager.currentManager(this).
806                        doubleBufferingChanged(this);
807            }
808        }
809    }
810
811    /**
812     * Overridden to enforce the position of the glass component as
813     * the zero child.
814     *
815     * @param comp the component to be enhanced
816     * @param constraints the constraints to be respected
817     * @param index the index
818     */
819    protected void addImpl(Component comp, Object constraints, int index) {
820        super.addImpl(comp, constraints, index);
821
822        /// We are making sure the glassPane is on top.
823        if(glassPane != null
824            && glassPane.getParent() == this
825            && getComponent(0) != glassPane) {
826            add(glassPane, 0);
827        }
828    }
829
830
831///////////////////////////////////////////////////////////////////////////////
832//// Begin Inner Classes
833///////////////////////////////////////////////////////////////////////////////
834
835
836    /**
837     * A custom layout manager that is responsible for the layout of
838     * layeredPane, glassPane, and menuBar.
839     * <p>
840     * <strong>Warning:</strong>
841     * Serialized objects of this class will not be compatible with
842     * future Swing releases. The current serialization support is
843     * appropriate for short term storage or RMI between applications running
844     * the same version of Swing.  As of 1.4, support for long term storage
845     * of all JavaBeans&trade;
846     * has been added to the <code>java.beans</code> package.
847     * Please see {@link java.beans.XMLEncoder}.
848     */
849    @SuppressWarnings("serial")
850    protected class RootLayout implements LayoutManager2, Serializable
851    {
852        /**
853         * Returns the amount of space the layout would like to have.
854         *
855         * @param parent the Container for which this layout manager
856         * is being used
857         * @return a Dimension object containing the layout's preferred size
858         */
859        public Dimension preferredLayoutSize(Container parent) {
860            Dimension rd, mbd;
861            Insets i = getInsets();
862
863            if(contentPane != null) {
864                rd = contentPane.getPreferredSize();
865            } else {
866                rd = parent.getSize();
867            }
868            if(menuBar != null && menuBar.isVisible()) {
869                mbd = menuBar.getPreferredSize();
870            } else {
871                mbd = new Dimension(0, 0);
872            }
873            return new Dimension(Math.max(rd.width, mbd.width) + i.left + i.right,
874                                        rd.height + mbd.height + i.top + i.bottom);
875        }
876
877        /**
878         * Returns the minimum amount of space the layout needs.
879         *
880         * @param parent the Container for which this layout manager
881         * is being used
882         * @return a Dimension object containing the layout's minimum size
883         */
884        public Dimension minimumLayoutSize(Container parent) {
885            Dimension rd, mbd;
886            Insets i = getInsets();
887            if(contentPane != null) {
888                rd = contentPane.getMinimumSize();
889            } else {
890                rd = parent.getSize();
891            }
892            if(menuBar != null && menuBar.isVisible()) {
893                mbd = menuBar.getMinimumSize();
894            } else {
895                mbd = new Dimension(0, 0);
896            }
897            return new Dimension(Math.max(rd.width, mbd.width) + i.left + i.right,
898                        rd.height + mbd.height + i.top + i.bottom);
899        }
900
901        /**
902         * Returns the maximum amount of space the layout can use.
903         *
904         * @param target the Container for which this layout manager
905         * is being used
906         * @return a Dimension object containing the layout's maximum size
907         */
908        public Dimension maximumLayoutSize(Container target) {
909            Dimension rd, mbd;
910            Insets i = getInsets();
911            if(menuBar != null && menuBar.isVisible()) {
912                mbd = menuBar.getMaximumSize();
913            } else {
914                mbd = new Dimension(0, 0);
915            }
916            if(contentPane != null) {
917                rd = contentPane.getMaximumSize();
918            } else {
919                // This is silly, but should stop an overflow error
920                rd = new Dimension(Integer.MAX_VALUE,
921                        Integer.MAX_VALUE - i.top - i.bottom - mbd.height - 1);
922            }
923            return new Dimension(Math.min(rd.width, mbd.width) + i.left + i.right,
924                                         rd.height + mbd.height + i.top + i.bottom);
925        }
926
927        /**
928         * Instructs the layout manager to perform the layout for the specified
929         * container.
930         *
931         * @param parent the Container for which this layout manager
932         * is being used
933         */
934        public void layoutContainer(Container parent) {
935            Rectangle b = parent.getBounds();
936            Insets i = getInsets();
937            int contentY = 0;
938            int w = b.width - i.right - i.left;
939            int h = b.height - i.top - i.bottom;
940
941            if(layeredPane != null) {
942                layeredPane.setBounds(i.left, i.top, w, h);
943            }
944            if(glassPane != null) {
945                glassPane.setBounds(i.left, i.top, w, h);
946            }
947            // Note: This is laying out the children in the layeredPane,
948            // technically, these are not our children.
949            if(menuBar != null && menuBar.isVisible()) {
950                Dimension mbd = menuBar.getPreferredSize();
951                menuBar.setBounds(0, 0, w, mbd.height);
952                contentY += mbd.height;
953            }
954            if(contentPane != null) {
955                contentPane.setBounds(0, contentY, w, h - contentY);
956            }
957        }
958
959        public void addLayoutComponent(String name, Component comp) {}
960        public void removeLayoutComponent(Component comp) {}
961        public void addLayoutComponent(Component comp, Object constraints) {}
962        public float getLayoutAlignmentX(Container target) { return 0.0f; }
963        public float getLayoutAlignmentY(Container target) { return 0.0f; }
964        public void invalidateLayout(Container target) {}
965    }
966
967    /**
968     * Returns a string representation of this <code>JRootPane</code>.
969     * This method is intended to be used only for debugging purposes,
970     * and the content and format of the returned string may vary between
971     * implementations. The returned string may be empty but may not
972     * be <code>null</code>.
973     *
974     * @return  a string representation of this <code>JRootPane</code>.
975     */
976    protected String paramString() {
977        return super.paramString();
978    }
979
980/////////////////
981// Accessibility support
982////////////////
983
984    /**
985     * Gets the <code>AccessibleContext</code> associated with this
986     * <code>JRootPane</code>. For root panes, the
987     * <code>AccessibleContext</code> takes the form of an
988     * <code>AccessibleJRootPane</code>.
989     * A new <code>AccessibleJRootPane</code> instance is created if necessary.
990     *
991     * @return an <code>AccessibleJRootPane</code> that serves as the
992     *         <code>AccessibleContext</code> of this <code>JRootPane</code>
993     */
994    public AccessibleContext getAccessibleContext() {
995        if (accessibleContext == null) {
996            accessibleContext = new AccessibleJRootPane();
997        }
998        return accessibleContext;
999    }
1000
1001    /**
1002     * This class implements accessibility support for the
1003     * <code>JRootPane</code> class.  It provides an implementation of the
1004     * Java Accessibility API appropriate to root pane user-interface elements.
1005     * <p>
1006     * <strong>Warning:</strong>
1007     * Serialized objects of this class will not be compatible with
1008     * future Swing releases. The current serialization support is
1009     * appropriate for short term storage or RMI between applications running
1010     * the same version of Swing.  As of 1.4, support for long term storage
1011     * of all JavaBeans&trade;
1012     * has been added to the <code>java.beans</code> package.
1013     * Please see {@link java.beans.XMLEncoder}.
1014     */
1015    @SuppressWarnings("serial")
1016    protected class AccessibleJRootPane extends AccessibleJComponent {
1017        /**
1018         * Get the role of this object.
1019         *
1020         * @return an instance of AccessibleRole describing the role of
1021         * the object
1022         */
1023        public AccessibleRole getAccessibleRole() {
1024            return AccessibleRole.ROOT_PANE;
1025        }
1026
1027        /**
1028         * Returns the number of accessible children of the object.
1029         *
1030         * @return the number of accessible children of the object.
1031         */
1032        public int getAccessibleChildrenCount() {
1033            return super.getAccessibleChildrenCount();
1034        }
1035
1036        /**
1037         * Returns the specified Accessible child of the object.  The Accessible
1038         * children of an Accessible object are zero-based, so the first child
1039         * of an Accessible child is at index 0, the second child is at index 1,
1040         * and so on.
1041         *
1042         * @param i zero-based index of child
1043         * @return the Accessible child of the object
1044         * @see #getAccessibleChildrenCount
1045         */
1046        public Accessible getAccessibleChild(int i) {
1047            return super.getAccessibleChild(i);
1048        }
1049    } // inner class AccessibleJRootPane
1050}
1051