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., 59 Temple Place, Suite 330, 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
.LayoutManager
;
46 import java
.awt
.LayoutManager2
;
47 import java
.io
.Serializable
;
49 import javax
.accessibility
.AccessibleRole
;
50 import javax
.swing
.plaf
.RootPaneUI
;
53 * This class is where JComponents are added to. Unlike awt where you could
54 * just say frame.add(), with swing you need to say frame.getRootPane()
55 * (which delivers an instance of this class) and add your components to
56 * that. It is implemented by several 'layers' (pane() should be read as
57 * plane()) each on top of the others where you can add components to.
58 * (getContentPane(), getGlassPane(), getLayeredPane())
60 * @author Ronald Veldema (rveldema@cs.vu.nl)
62 public class JRootPane
extends JComponent
64 // The class used to obtain the accessible role for this object.
65 protected static class AccessibleJRootPane
68 * For compatability with Sun's JDK
70 private static final long serialVersionUID
= 1082432482784468088L;
73 * Creates a new <code>AccessibleJRootPane</code> object.
75 protected AccessibleJRootPane()
82 * @return DOCUMENT ME!
84 public AccessibleRole
getAccessibleRole()
86 return AccessibleRole
.ROOT_PANE
;
90 // Custom Layout Manager for JRootPane. It positions contentPane and
91 // menuBar withing its layeredPane.
92 protected class RootLayout
implements LayoutManager2
, Serializable
95 private static final long serialVersionUID
= -4100116998559815027L;
98 * Creates a new <code>RootLayout</code> object.
100 protected RootLayout()
107 * @param comp DOCUMENT ME!
108 * @param constraints DOCUMENT ME!
110 public void addLayoutComponent(Component comp
, Object constraints
)
117 * @param name DOCUMENT ME!
118 * @param comp DOCUMENT ME!
120 public void addLayoutComponent(String name
, Component comp
)
127 * @param target DOCUMENT ME!
129 * @return DOCUMENT ME!
131 public float getLayoutAlignmentX(Container target
)
133 return target
.getAlignmentX();
139 * @param target DOCUMENT ME!
141 * @return DOCUMENT ME!
143 public float getLayoutAlignmentY(Container target
)
145 return target
.getAlignmentY();
151 * @param target DOCUMENT ME!
153 public void invalidateLayout(Container target
)
160 * @param c DOCUMENT ME!
162 public void layoutContainer(Container c
)
164 Dimension menuBarSize
;
165 Dimension containerSize
= c
.getSize(null);
166 Dimension contentPaneSize
= contentPane
.getPreferredSize();
169 if size of top-level window wasn't set then just set
170 contentPane and menuBar to its preferred sizes.
171 Otherwise, if the size of top-level window was specified then
172 set menuBar to its preferred size and make content pane
173 to fit into the remaining space
176 +-------------------------------+
178 | +--------------------------+ |
180 | +--------------------------+ |
181 | +--------------------------+ |
186 | +--------------------------+ |
187 +-------------------------------+
190 if (containerSize
.width
== 0 && containerSize
.height
== 0)
195 menuBarSize
= menuBar
.getPreferredSize();
196 maxWidth
= Math
.max(menuBarSize
.width
, contentPaneSize
.width
);
197 menuBar
.setBounds(0, 0, maxWidth
, menuBarSize
.height
);
198 glassPane
.setBounds(0, menuBarSize
.height
, maxWidth
,
199 contentPaneSize
.height
);
200 contentPane
.setBounds(0, menuBarSize
.height
, maxWidth
,
201 contentPaneSize
.height
);
202 layeredPane
.setSize(maxWidth
,
203 menuBarSize
.height
+ contentPaneSize
.height
);
207 glassPane
.setBounds(0, 0, contentPaneSize
.width
,
208 contentPaneSize
.height
);
209 contentPane
.setBounds(0, 0, contentPaneSize
.width
,
210 contentPaneSize
.height
);
211 layeredPane
.setSize(contentPaneSize
.width
, contentPaneSize
.height
);
218 menuBarSize
= menuBar
.getPreferredSize();
219 if (menuBarSize
.height
> containerSize
.height
)
220 menuBarSize
.height
= containerSize
.height
;
221 menuBar
.setBounds(0, 0, containerSize
.width
, menuBarSize
.height
);
222 int remainingHeight
= containerSize
.height
- menuBarSize
.height
;
223 glassPane
.setBounds(0, menuBarSize
.height
, containerSize
.width
,
224 containerSize
.height
- menuBarSize
.height
);
225 contentPane
.setBounds(0, menuBarSize
.height
,
227 (containerSize
.height
- menuBarSize
.height
));
231 glassPane
.setBounds(0, 0, containerSize
.width
,
232 containerSize
.height
);
233 contentPane
.setBounds(0, 0, containerSize
.width
,
234 containerSize
.height
);
237 layeredPane
.setSize(containerSize
.width
, containerSize
.height
);
244 * @param target DOCUMENT ME!
246 * @return DOCUMENT ME!
248 public Dimension
maximumLayoutSize(Container target
)
250 return preferredLayoutSize(target
);
256 * @param target DOCUMENT ME!
258 * @return DOCUMENT ME!
260 public Dimension
minimumLayoutSize(Container target
)
262 return preferredLayoutSize(target
);
268 * @param c DOCUMENT ME!
270 * @return DOCUMENT ME!
272 public Dimension
preferredLayoutSize(Container c
)
274 Dimension menuBarSize
;
277 Dimension containerSize
= c
.getSize();
278 Dimension contentPaneSize
= contentPane
.getPreferredSize();
280 if (containerSize
.width
== 0 && containerSize
.height
== 0)
285 menuBarSize
= menuBar
.getPreferredSize();
286 maxWidth
= Math
.max(menuBarSize
.width
, contentPaneSize
.width
);
287 prefSize
= new Dimension(maxWidth
,
288 contentPaneSize
.height
289 + menuBarSize
.height
);
292 prefSize
= contentPaneSize
;
295 prefSize
= c
.getSize();
303 * @param comp DOCUMENT ME!
305 public void removeLayoutComponent(Component comp
)
311 private static final long serialVersionUID
= 8690748000348575668L;
313 public static final int NONE
= 0;
314 public static final int FRAME
= 1;
315 public static final int PLAIN_DIALOG
= 2;
316 public static final int INFORMATION_DIALOG
= 3;
317 public static final int ERROR_DIALOG
= 4;
318 public static final int COLOR_CHOOSER_DIALOG
= 5;
319 public static final int FILE_CHOOSER_DIALOG
= 6;
320 public static final int QUESTION_DIALOG
= 7;
321 public static final int WARNING_DIALOG
= 8;
324 protected Component glassPane
;
327 protected JLayeredPane layeredPane
;
330 protected JMenuBar menuBar
;
333 protected Container contentPane
;
335 protected JButton defaultButton
;
340 private int windowDecorationStyle
= NONE
;
345 * @param m DOCUMENT ME!
347 public void setJMenuBar(JMenuBar m
)
349 JLayeredPane jlPane
= getLayeredPane();
351 jlPane
.remove(menuBar
);
354 jlPane
.add(menuBar
, JLayeredPane
.FRAME_CONTENT_LAYER
);
358 * @deprecated Replaced by <code>setJMenuBar()</code>
360 public void setMenuBar(JMenuBar m
)
368 * @return DOCUMENT ME!
370 public JMenuBar
getJMenuBar()
376 * @deprecated Replaced by <code>getJMenuBar()</code>
378 public JMenuBar
getMenuBar()
380 return getJMenuBar();
386 * @return DOCUMENT ME!
388 public boolean isValidateRoot()
396 * @return DOCUMENT ME!
398 public Container
getContentPane()
400 if (contentPane
== null)
401 setContentPane(createContentPane());
408 * @param p DOCUMENT ME!
410 public void setContentPane(Container p
)
413 getLayeredPane().add(contentPane
, JLayeredPane
.FRAME_CONTENT_LAYER
);
419 * @param comp DOCUMENT ME!
420 * @param constraints DOCUMENT ME!
421 * @param index DOCUMENT ME!
423 protected void addImpl(Component comp
, Object constraints
, int index
)
425 super.addImpl(comp
, constraints
, index
);
431 * @return DOCUMENT ME!
433 public Component
getGlassPane()
435 if (glassPane
== null)
436 setGlassPane(createGlassPane());
443 * @param f DOCUMENT ME!
445 public void setGlassPane(Component f
)
447 if (glassPane
!= null)
452 glassPane
.setVisible(false);
459 * @return DOCUMENT ME!
461 public JLayeredPane
getLayeredPane()
463 if (layeredPane
== null)
464 setLayeredPane(createLayeredPane());
471 * @param f DOCUMENT ME!
473 public void setLayeredPane(JLayeredPane f
)
475 if (layeredPane
!= null)
483 * Creates a new <code>JRootPane</code> object.
487 setLayout(createRootLayout());
491 setDoubleBuffered(true);
498 * @return DOCUMENT ME!
500 protected LayoutManager
createRootLayout()
502 return new RootLayout();
508 * @return DOCUMENT ME!
510 protected Container
createContentPane()
512 JPanel p
= new JPanel();
513 p
.setName(this.getName() + ".contentPane");
514 p
.setLayout(new BorderLayout());
521 * @return DOCUMENT ME!
523 protected Component
createGlassPane()
525 JPanel p
= new JPanel();
526 p
.setName(this.getName() + ".glassPane");
527 p
.setLayout(new BorderLayout());
536 * @return DOCUMENT ME!
538 protected JLayeredPane
createLayeredPane()
540 JLayeredPane l
= new JLayeredPane();
548 * @return DOCUMENT ME!
550 public RootPaneUI
getUI()
552 return (RootPaneUI
) ui
;
558 * @param ui DOCUMENT ME!
560 public void setUI(RootPaneUI ui
)
568 public void updateUI()
570 setUI((RootPaneUI
) UIManager
.getUI(this));
576 * @return DOCUMENT ME!
578 public String
getUIClassID()
583 public JButton
getDefaultButton()
585 return defaultButton
;
588 public void setDefaultButton(JButton newButton
)
590 if (defaultButton
== newButton
)
593 JButton oldButton
= defaultButton
;
594 defaultButton
= newButton
;
595 firePropertyChange("defaultButton", oldButton
, newButton
);
601 public int getWindowDecorationStyle()
603 return windowDecorationStyle
;
609 public void setWindowDecorationStyle(int style
)
613 && style
!= INFORMATION_DIALOG
614 && style
!= ERROR_DIALOG
615 && style
!= COLOR_CHOOSER_DIALOG
616 && style
!= FILE_CHOOSER_DIALOG
617 && style
!= QUESTION_DIALOG
618 && style
!= WARNING_DIALOG
)
619 throw new IllegalArgumentException("invalid style");
621 int oldStyle
= windowDecorationStyle
;
622 windowDecorationStyle
= style
;
623 firePropertyChange("windowDecorationStyle", oldStyle
, style
);