2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.awt
.BorderLayout
;
42 import java
.awt
.Component
;
43 import java
.awt
.Container
;
44 import java
.awt
.Dimension
;
45 import java
.awt
.IllegalComponentStateException
;
46 import java
.awt
.Insets
;
47 import java
.awt
.LayoutManager
;
48 import java
.awt
.LayoutManager2
;
49 import java
.awt
.Rectangle
;
50 import java
.io
.Serializable
;
52 import javax
.accessibility
.Accessible
;
53 import javax
.accessibility
.AccessibleRole
;
54 import javax
.swing
.plaf
.RootPaneUI
;
57 * This class is where JComponents are added to. Unlike awt where you could
58 * just say frame.add(), with swing you need to say frame.getRootPane()
59 * (which delivers an instance of this class) and add your components to
60 * that. It is implemented by several 'layers' (pane() should be read as
61 * plane()) each on top of the others where you can add components to.
62 * (getContentPane(), getGlassPane(), getLayeredPane())
64 * @author Ronald Veldema (rveldema@cs.vu.nl)
66 public class JRootPane
extends JComponent
implements Accessible
68 // The class used to obtain the accessible role for this object.
69 protected class AccessibleJRootPane
extends AccessibleJComponent
72 * For compatability with Sun's JDK
74 private static final long serialVersionUID
= 1082432482784468088L;
77 * Creates a new <code>AccessibleJRootPane</code> object.
79 protected AccessibleJRootPane()
81 // Nothing to do here.
87 * @return DOCUMENT ME!
89 public AccessibleRole
getAccessibleRole()
91 return AccessibleRole
.ROOT_PANE
;
95 // Custom Layout Manager for JRootPane. It positions contentPane and
96 // menuBar withing its layeredPane.
97 protected class RootLayout
implements LayoutManager2
, Serializable
100 private static final long serialVersionUID
= -4100116998559815027L;
103 * The cached layout info for the glass pane.
105 private Rectangle glassPaneBounds
;
108 * The cached layout info for the layered pane.
110 private Rectangle layeredPaneBounds
;
113 * The cached layout info for the content pane.
115 private Rectangle contentPaneBounds
;
118 * The cached layout info for the menu bar.
120 private Rectangle menuBarBounds
;
123 * The cached preferred size.
125 private Dimension prefSize
;
128 * Creates a new <code>RootLayout</code> object.
130 protected RootLayout()
132 // Nothing to do here.
138 * @param comp DOCUMENT ME!
139 * @param constraints DOCUMENT ME!
141 public void addLayoutComponent(Component comp
, Object constraints
)
143 // Nothing to do here.
149 * @param name DOCUMENT ME!
150 * @param comp DOCUMENT ME!
152 public void addLayoutComponent(String name
, Component comp
)
154 // Nothing to do here.
160 * @param target DOCUMENT ME!
162 * @return DOCUMENT ME!
164 public float getLayoutAlignmentX(Container target
)
172 * @param target DOCUMENT ME!
174 * @return DOCUMENT ME!
176 public float getLayoutAlignmentY(Container target
)
184 * @param target DOCUMENT ME!
186 public void invalidateLayout(Container target
)
190 glassPaneBounds
= null;
191 layeredPaneBounds
= null;
192 contentPaneBounds
= null;
193 menuBarBounds
= null;
201 * @param c DOCUMENT ME!
203 public void layoutContainer(Container c
)
205 if (glassPaneBounds
== null || layeredPaneBounds
== null
206 || contentPaneBounds
== null || menuBarBounds
== null)
208 Insets i
= getInsets();
209 int containerWidth
= c
.getBounds().width
- i
.left
- i
.right
;
210 int containerHeight
= c
.getBounds().height
- i
.top
- i
.bottom
;
212 // 1. the glassPane fills entire viewable region (bounds - insets).
213 // 2. the layeredPane filles entire viewable region.
214 // 3. the menuBar is positioned at the upper edge of layeredPane.
215 // 4. the contentPane fills viewable region minus menuBar, if present.
218 // +-------------------------------+
220 // | +--------------------------+ |
222 // | +--------------------------+ |
223 // | +--------------------------+ |
224 // | |contentPane | |
228 // | +--------------------------+ |
229 // +-------------------------------+
233 Dimension menuBarSize
= menuBar
.getPreferredSize();
234 if (menuBarSize
.height
> containerHeight
)
235 menuBarSize
.height
= containerHeight
;
236 menuBarBounds
= new Rectangle(0, 0, containerWidth
,
238 contentPaneBounds
= new Rectangle(0, menuBarSize
.height
,
240 containerHeight
- menuBarSize
.height
);
243 contentPaneBounds
= new Rectangle(0, 0, containerWidth
,
246 glassPaneBounds
= new Rectangle(i
.left
, i
.top
, containerWidth
, containerHeight
);
247 layeredPaneBounds
= new Rectangle(i
.left
, i
.top
, containerWidth
, containerHeight
);
250 glassPane
.setBounds(glassPaneBounds
);
251 layeredPane
.setBounds(layeredPaneBounds
);
253 menuBar
.setBounds(menuBarBounds
);
254 contentPane
.setBounds(contentPaneBounds
);
260 * @param target DOCUMENT ME!
262 * @return DOCUMENT ME!
264 public Dimension
maximumLayoutSize(Container target
)
266 return preferredLayoutSize(target
);
272 * @param target DOCUMENT ME!
274 * @return DOCUMENT ME!
276 public Dimension
minimumLayoutSize(Container target
)
278 return preferredLayoutSize(target
);
284 * @param c DOCUMENT ME!
286 * @return DOCUMENT ME!
288 public Dimension
preferredLayoutSize(Container c
)
290 // We must synchronize here, otherwise we cannot guarantee that the
291 // prefSize is still non-null when returning.
294 if (prefSize
== null)
296 Insets i
= getInsets();
297 prefSize
= new Dimension(i
.left
+ i
.right
, i
.top
+ i
.bottom
);
298 Dimension contentPrefSize
= contentPane
.getPreferredSize();
299 prefSize
.width
+= contentPrefSize
.width
;
300 prefSize
.height
+= contentPrefSize
.height
;
303 Dimension menuBarSize
= menuBar
.getPreferredSize();
304 if (menuBarSize
.width
> contentPrefSize
.width
)
305 prefSize
.width
+= menuBarSize
.width
- contentPrefSize
.width
;
306 prefSize
.height
+= menuBarSize
.height
;
309 // Return a copy here so the cached value won't get trashed by some
311 return new Dimension(prefSize
);
318 * @param comp DOCUMENT ME!
320 public void removeLayoutComponent(Component comp
)
322 // Nothing to do here.
327 private static final long serialVersionUID
= 8690748000348575668L;
329 public static final int NONE
= 0;
330 public static final int FRAME
= 1;
331 public static final int PLAIN_DIALOG
= 2;
332 public static final int INFORMATION_DIALOG
= 3;
333 public static final int ERROR_DIALOG
= 4;
334 public static final int COLOR_CHOOSER_DIALOG
= 5;
335 public static final int FILE_CHOOSER_DIALOG
= 6;
336 public static final int QUESTION_DIALOG
= 7;
337 public static final int WARNING_DIALOG
= 8;
340 protected Component glassPane
;
343 protected JLayeredPane layeredPane
;
346 protected JMenuBar menuBar
;
349 protected Container contentPane
;
351 protected JButton defaultButton
;
354 * This field is unused since JDK1.3. To override the default action you
355 * should modify the JRootPane's ActionMap.
357 * @deprecated since JDK1.3
359 * @specnote the specs indicate that the type of this field is
360 * a package private inner class
361 * javax.swing.JRootPane.DefaultAction. I assume that the closest
362 * public superclass is javax.swing.Action.
364 protected Action defaultPressAction
;
367 * This field is unused since JDK1.3. To override the default action you
368 * should modify the JRootPane's ActionMap.
370 * @deprecated since JDK1.3
372 * @specnote the specs indicate that the type of this field is
373 * a package private inner class
374 * javax.swing.JRootPane.DefaultAction. I assume that the closest
375 * public superclass is javax.swing.Action.
377 protected Action defaultReleaseAction
;
382 private int windowDecorationStyle
= NONE
;
387 * @param m DOCUMENT ME!
389 public void setJMenuBar(JMenuBar m
)
391 JLayeredPane jlPane
= getLayeredPane();
393 jlPane
.remove(menuBar
);
396 jlPane
.add(menuBar
, JLayeredPane
.FRAME_CONTENT_LAYER
);
400 * @deprecated Replaced by <code>setJMenuBar()</code>
402 public void setMenuBar(JMenuBar m
)
410 * @return DOCUMENT ME!
412 public JMenuBar
getJMenuBar()
418 * @deprecated Replaced by <code>getJMenuBar()</code>
420 public JMenuBar
getMenuBar()
422 return getJMenuBar();
428 * @return DOCUMENT ME!
430 public boolean isValidateRoot()
438 * @return DOCUMENT ME!
440 public Container
getContentPane()
442 if (contentPane
== null)
443 setContentPane(createContentPane());
448 * Sets the JRootPane's content pane. The content pane should typically be
449 * opaque for painting to work properly. This method also
450 * removes the old content pane from the layered pane.
452 * @param p the Container that will be the content pane
453 * @throws IllegalComponentStateException if p is null
455 public void setContentPane(Container p
)
458 throw new IllegalComponentStateException ("cannot " +
459 "have a null content pane");
462 if (contentPane
!= null && contentPane
.getParent() == layeredPane
)
463 layeredPane
.remove(contentPane
);
465 getLayeredPane().add(contentPane
, JLayeredPane
.FRAME_CONTENT_LAYER
);
472 * @param comp DOCUMENT ME!
473 * @param constraints DOCUMENT ME!
474 * @param index DOCUMENT ME!
476 protected void addImpl(Component comp
, Object constraints
, int index
)
478 super.addImpl(comp
, constraints
, index
);
484 * @return DOCUMENT ME!
486 public Component
getGlassPane()
488 if (glassPane
== null)
489 setGlassPane(createGlassPane());
496 * @param f DOCUMENT ME!
498 public void setGlassPane(Component f
)
500 if (glassPane
!= null)
505 glassPane
.setVisible(false);
512 * @return DOCUMENT ME!
514 public JLayeredPane
getLayeredPane()
516 if (layeredPane
== null)
517 setLayeredPane(createLayeredPane());
524 * @param f DOCUMENT ME!
526 public void setLayeredPane(JLayeredPane f
)
528 if (layeredPane
!= null)
536 * Creates a new <code>JRootPane</code> object.
540 setLayout(createRootLayout());
550 * @return DOCUMENT ME!
552 protected LayoutManager
createRootLayout()
554 return new RootLayout();
560 * @return DOCUMENT ME!
562 protected Container
createContentPane()
564 JPanel p
= new JPanel();
565 p
.setName(this.getName() + ".contentPane");
566 p
.setLayout(new BorderLayout());
573 * @return DOCUMENT ME!
575 protected Component
createGlassPane()
577 JPanel p
= new JPanel();
578 p
.setName(this.getName() + ".glassPane");
587 * @return DOCUMENT ME!
589 protected JLayeredPane
createLayeredPane()
591 JLayeredPane l
= new JLayeredPane();
599 * @return DOCUMENT ME!
601 public RootPaneUI
getUI()
603 return (RootPaneUI
) ui
;
609 * @param ui DOCUMENT ME!
611 public void setUI(RootPaneUI ui
)
619 public void updateUI()
621 setUI((RootPaneUI
) UIManager
.getUI(this));
627 * @return DOCUMENT ME!
629 public String
getUIClassID()
634 public JButton
getDefaultButton()
636 return defaultButton
;
639 public void setDefaultButton(JButton newButton
)
641 if (defaultButton
== newButton
)
644 JButton oldButton
= defaultButton
;
645 defaultButton
= newButton
;
646 firePropertyChange("defaultButton", oldButton
, newButton
);
652 public int getWindowDecorationStyle()
654 return windowDecorationStyle
;
660 public void setWindowDecorationStyle(int style
)
664 && style
!= INFORMATION_DIALOG
665 && style
!= ERROR_DIALOG
666 && style
!= COLOR_CHOOSER_DIALOG
667 && style
!= FILE_CHOOSER_DIALOG
668 && style
!= QUESTION_DIALOG
669 && style
!= WARNING_DIALOG
670 && style
!= PLAIN_DIALOG
)
671 throw new IllegalArgumentException("invalid style");
673 int oldStyle
= windowDecorationStyle
;
674 windowDecorationStyle
= style
;
675 firePropertyChange("windowDecorationStyle", oldStyle
, style
);