Merge from the pain train
[official-gcc.git] / libjava / javax / swing / JRootPane.java
blob145750758a439b5c9337ef1c5a94caff7f71deae
1 /* JRootPane.java --
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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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. */
39 package javax.swing;
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;
52 /**
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
67 /**
68 * For compatability with Sun's JDK
70 private static final long serialVersionUID = 1082432482784468088L;
72 /**
73 * Creates a new <code>AccessibleJRootPane</code> object.
75 protected AccessibleJRootPane()
79 /**
80 * DOCUMENT ME!
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
94 /** DOCUMENT ME! */
95 private static final long serialVersionUID = -4100116998559815027L;
97 /**
98 * Creates a new <code>RootLayout</code> object.
100 protected RootLayout()
105 * DOCUMENT ME!
107 * @param comp DOCUMENT ME!
108 * @param constraints DOCUMENT ME!
110 public void addLayoutComponent(Component comp, Object constraints)
115 * DOCUMENT ME!
117 * @param name DOCUMENT ME!
118 * @param comp DOCUMENT ME!
120 public void addLayoutComponent(String name, Component comp)
125 * DOCUMENT ME!
127 * @param target DOCUMENT ME!
129 * @return DOCUMENT ME!
131 public float getLayoutAlignmentX(Container target)
133 return target.getAlignmentX();
137 * DOCUMENT ME!
139 * @param target DOCUMENT ME!
141 * @return DOCUMENT ME!
143 public float getLayoutAlignmentY(Container target)
145 return target.getAlignmentY();
149 * DOCUMENT ME!
151 * @param target DOCUMENT ME!
153 public void invalidateLayout(Container target)
158 * DOCUMENT ME!
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 +-------------------------------+
177 | JLayeredPane |
178 | +--------------------------+ |
179 | | menuBar | |
180 | +--------------------------+ |
181 | +--------------------------+ |
182 | |contentPane | |
183 | | | |
184 | | | |
185 | | | |
186 | +--------------------------+ |
187 +-------------------------------+
190 if (containerSize.width == 0 && containerSize.height == 0)
192 if (menuBar != null)
194 int maxWidth;
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);
205 else
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);
214 else
216 if (menuBar != null)
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,
226 containerSize.width,
227 (containerSize.height - menuBarSize.height));
229 else
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);
242 * DOCUMENT ME!
244 * @param target DOCUMENT ME!
246 * @return DOCUMENT ME!
248 public Dimension maximumLayoutSize(Container target)
250 return preferredLayoutSize(target);
254 * DOCUMENT ME!
256 * @param target DOCUMENT ME!
258 * @return DOCUMENT ME!
260 public Dimension minimumLayoutSize(Container target)
262 return preferredLayoutSize(target);
266 * DOCUMENT ME!
268 * @param c DOCUMENT ME!
270 * @return DOCUMENT ME!
272 public Dimension preferredLayoutSize(Container c)
274 Dimension menuBarSize;
275 Dimension prefSize;
277 Dimension containerSize = c.getSize();
278 Dimension contentPaneSize = contentPane.getPreferredSize();
280 if (containerSize.width == 0 && containerSize.height == 0)
282 if (menuBar != null)
284 int maxWidth;
285 menuBarSize = menuBar.getPreferredSize();
286 maxWidth = Math.max(menuBarSize.width, contentPaneSize.width);
287 prefSize = new Dimension(maxWidth,
288 contentPaneSize.height
289 + menuBarSize.height);
291 else
292 prefSize = contentPaneSize;
294 else
295 prefSize = c.getSize();
297 return prefSize;
301 * DOCUMENT ME!
303 * @param comp DOCUMENT ME!
305 public void removeLayoutComponent(Component comp)
310 /** DOCUMENT ME! */
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;
323 /** DOCUMENT ME! */
324 protected Component glassPane;
326 /** DOCUMENT ME! */
327 protected JLayeredPane layeredPane;
329 /** DOCUMENT ME! */
330 protected JMenuBar menuBar;
332 /** DOCUMENT ME! */
333 protected Container contentPane;
335 protected JButton defaultButton;
338 * @since 1.4
340 private int windowDecorationStyle = NONE;
343 * DOCUMENT ME!
345 * @param m DOCUMENT ME!
347 public void setJMenuBar(JMenuBar m)
349 JLayeredPane jlPane = getLayeredPane();
350 if (menuBar != null)
351 jlPane.remove(menuBar);
352 menuBar = m;
353 if (menuBar != null)
354 jlPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER);
358 * @deprecated Replaced by <code>setJMenuBar()</code>
360 public void setMenuBar(JMenuBar m)
362 setJMenuBar(m);
366 * DOCUMENT ME!
368 * @return DOCUMENT ME!
370 public JMenuBar getJMenuBar()
372 return menuBar;
376 * @deprecated Replaced by <code>getJMenuBar()</code>
378 public JMenuBar getMenuBar()
380 return getJMenuBar();
384 * DOCUMENT ME!
386 * @return DOCUMENT ME!
388 public boolean isValidateRoot()
390 return true;
394 * DOCUMENT ME!
396 * @return DOCUMENT ME!
398 public Container getContentPane()
400 if (contentPane == null)
401 setContentPane(createContentPane());
402 return contentPane;
406 * DOCUMENT ME!
408 * @param p DOCUMENT ME!
410 public void setContentPane(Container p)
412 contentPane = p;
413 getLayeredPane().add(contentPane, JLayeredPane.FRAME_CONTENT_LAYER);
417 * DOCUMENT ME!
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);
429 * DOCUMENT ME!
431 * @return DOCUMENT ME!
433 public Component getGlassPane()
435 if (glassPane == null)
436 setGlassPane(createGlassPane());
437 return glassPane;
441 * DOCUMENT ME!
443 * @param f DOCUMENT ME!
445 public void setGlassPane(Component f)
447 if (glassPane != null)
448 remove(glassPane);
450 glassPane = f;
452 glassPane.setVisible(false);
453 add(glassPane, 0);
457 * DOCUMENT ME!
459 * @return DOCUMENT ME!
461 public JLayeredPane getLayeredPane()
463 if (layeredPane == null)
464 setLayeredPane(createLayeredPane());
465 return layeredPane;
469 * DOCUMENT ME!
471 * @param f DOCUMENT ME!
473 public void setLayeredPane(JLayeredPane f)
475 if (layeredPane != null)
476 remove(layeredPane);
478 layeredPane = f;
479 add(f, -1);
483 * Creates a new <code>JRootPane</code> object.
485 public JRootPane()
487 setLayout(createRootLayout());
488 getGlassPane();
489 getLayeredPane();
490 getContentPane();
491 setDoubleBuffered(true);
492 updateUI();
496 * DOCUMENT ME!
498 * @return DOCUMENT ME!
500 protected LayoutManager createRootLayout()
502 return new RootLayout();
506 * DOCUMENT ME!
508 * @return DOCUMENT ME!
510 protected Container createContentPane()
512 JPanel p = new JPanel();
513 p.setName(this.getName() + ".contentPane");
514 p.setLayout(new BorderLayout());
515 return p;
519 * DOCUMENT ME!
521 * @return DOCUMENT ME!
523 protected Component createGlassPane()
525 JPanel p = new JPanel();
526 p.setName(this.getName() + ".glassPane");
527 p.setLayout(new BorderLayout());
528 p.setVisible(false);
529 p.setOpaque(false);
530 return p;
534 * DOCUMENT ME!
536 * @return DOCUMENT ME!
538 protected JLayeredPane createLayeredPane()
540 JLayeredPane l = new JLayeredPane();
541 l.setLayout(null);
542 return l;
546 * DOCUMENT ME!
548 * @return DOCUMENT ME!
550 public RootPaneUI getUI()
552 return (RootPaneUI) ui;
556 * DOCUMENT ME!
558 * @param ui DOCUMENT ME!
560 public void setUI(RootPaneUI ui)
562 super.setUI(ui);
566 * DOCUMENT ME!
568 public void updateUI()
570 setUI((RootPaneUI) UIManager.getUI(this));
574 * DOCUMENT ME!
576 * @return DOCUMENT ME!
578 public String getUIClassID()
580 return "RootPaneUI";
583 public JButton getDefaultButton()
585 return defaultButton;
588 public void setDefaultButton(JButton newButton)
590 if (defaultButton == newButton)
591 return;
593 JButton oldButton = defaultButton;
594 defaultButton = newButton;
595 firePropertyChange("defaultButton", oldButton, newButton);
599 * @since 1.4
601 public int getWindowDecorationStyle()
603 return windowDecorationStyle;
607 * @since 1.4
609 public void setWindowDecorationStyle(int style)
611 if (style != NONE
612 && style != FRAME
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);