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 javax.swing.plaf.RootPaneUI;
50 * This class is where JComponents are added to.
51 * Unlike awt where you could just say frame.add(),
52 * with swing you need to say frame.getRootPane()
53 * (which delivers an instance of this class)
54 * and add your components to that.
56 * It is implemented by several 'layers' (pane() should be read as plane())
57 * each on top of the others
58 * where you can add components to.
59 * (getContentPane(), getGlassPane(), getLayeredPane())
61 * @author Ronald Veldema (rveldema@cs.vu.nl)
63 public class JRootPane extends JComponent
65 // The class used to obtain the accessible role for this object.
66 static protected class AccessibleJRootPane
70 // Custom Layout Manager for JRootPane. It positions contentPane and
71 // menuBar withing its layeredPane.
72 protected class RootLayout extends Object implements LayoutManager2
74 public void addLayoutComponent(Component comp, Object constraints)
78 public void addLayoutComponent(String name, Component comp)
82 public float getLayoutAlignmentX(Container target)
84 return target.getAlignmentX();
87 public float getLayoutAlignmentY(Container target)
89 return target.getAlignmentY();
92 public void invalidateLayout(Container target)
96 public void layoutContainer(Container c)
98 Dimension menuBarSize;
99 Dimension containerSize = c.getSize(null);
100 Dimension contentPaneSize = contentPane.getPreferredSize();
103 if size of top-level window wasn't set then just set
104 contentPane and menuBar to its preferred sizes.
105 Otherwise, if the size of top-level window was specified then
106 set menuBar to its preferred size and make content pane
107 to fit into the remaining space
110 +-------------------------------+
112 | +--------------------------+ |
114 | +--------------------------+ |
115 | +--------------------------+ |
120 | +--------------------------+ |
121 +-------------------------------+
124 if (containerSize.width == 0 && containerSize.height == 0)
129 menuBarSize = menuBar.getPreferredSize();
130 maxWidth = Math.max(menuBarSize.width, contentPaneSize.width);
131 menuBar.setBounds(0, 0, maxWidth, menuBarSize.height);
132 contentPane.setBounds(0, menuBarSize.height, maxWidth,
133 contentPaneSize.height);
134 layeredPane.setSize(maxWidth,
135 menuBarSize.height + contentPaneSize.height);
139 contentPane.setBounds(0, 0, contentPaneSize.width,
140 contentPaneSize.height);
141 layeredPane.setSize(contentPaneSize.width, contentPaneSize.height);
148 menuBarSize = menuBar.getPreferredSize();
149 if (menuBarSize.height > containerSize.height)
150 menuBarSize.height = containerSize.height;
151 menuBar.setBounds(0, 0, containerSize.width, menuBarSize.height);
152 int remainingHeight = containerSize.height - menuBarSize.height;
153 contentPane.setBounds(0, menuBarSize.height,
155 (containerSize.height - menuBarSize.height));
158 contentPane.setBounds(0, 0, containerSize.width,
159 containerSize.height);
161 layeredPane.setSize(containerSize.width, containerSize.height);
165 public Dimension maximumLayoutSize(Container target)
167 return preferredLayoutSize(target);
170 public Dimension minimumLayoutSize(Container target)
172 return preferredLayoutSize(target);
175 public Dimension preferredLayoutSize(Container c)
177 Dimension menuBarSize;
180 Dimension containerSize = c.getSize();
181 Dimension contentPaneSize = contentPane.getPreferredSize();
183 if (containerSize.width == 0 && containerSize.height == 0)
188 menuBarSize = menuBar.getPreferredSize();
189 maxWidth = Math.max(menuBarSize.width, contentPaneSize.width);
190 prefSize = new Dimension(maxWidth,
191 contentPaneSize.height
192 + menuBarSize.height);
195 prefSize = contentPaneSize;
198 prefSize = c.getSize();
203 public void removeLayoutComponent(Component comp)
208 protected Component glassPane;
209 protected JLayeredPane layeredPane;
210 protected JMenuBar menuBar;
211 protected Container contentPane;
214 void setJMenuBar(JMenuBar m)
217 getLayeredPane().add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER);
220 JMenuBar getJMenuBar()
223 public boolean isValidateRoot()
229 public Container getContentPane()
231 if (contentPane == null)
233 setContentPane(createContentPane());
238 public void setContentPane(Container p)
241 getLayeredPane().add(contentPane, JLayeredPane.FRAME_CONTENT_LAYER);
244 protected void addImpl(Component comp,
248 super.addImpl(comp, constraints, index);
251 public Component getGlassPane()
253 if (glassPane == null)
254 setGlassPane(createGlassPane());
258 public void setGlassPane(Component f)
260 if (glassPane != null)
265 glassPane.setVisible(false);
269 public JLayeredPane getLayeredPane()
271 if (layeredPane == null)
272 setLayeredPane(createLayeredPane());
275 public void setLayeredPane(JLayeredPane f)
277 if (layeredPane != null)
287 setLayout(createRootLayout());
291 setDoubleBuffered(true);
295 protected LayoutManager createRootLayout() {
296 return new RootLayout();
299 JComponent createContentPane()
301 JPanel p = new JPanel();
302 p.setName(this.getName()+".contentPane");
303 p.setLayout(new BorderLayout());
307 Component createGlassPane()
309 JPanel p = new JPanel();
310 p.setName(this.getName()+".glassPane");
311 p.setLayout(new BorderLayout());
316 JLayeredPane createLayeredPane()
318 JLayeredPane l = new JLayeredPane();
324 public RootPaneUI getUI()
326 return (RootPaneUI) ui;
329 public void setUI(RootPaneUI ui)
334 public void updateUI()
336 setUI((RootPaneUI) UIManager.getUI(this));
339 public String getUIClassID()