OSDN Git Service

* javax/swing/RepaintManager.java
[pf3gnuchains/gcc-fork.git] / libjava / javax / swing / JRootPane.java
1 /* JRootPane.java --
2    Copyright (C) 2002, 2004  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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.
20
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.
25
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. */
37
38
39 package javax.swing;
40
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;
48
49 /**
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.
55  *
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())
60  *
61  * @author Ronald Veldema (rveldema@cs.vu.nl)
62  */
63 public class JRootPane extends JComponent
64 {
65     //  The class used to obtain the accessible role for this object.
66     static protected class AccessibleJRootPane
67     {
68     }
69
70     // Custom Layout Manager for JRootPane. It positions contentPane and 
71     // menuBar withing its layeredPane.
72     protected class RootLayout extends Object implements LayoutManager2
73     {
74       public void addLayoutComponent(Component comp, Object constraints)
75       {
76       }
77
78       public void addLayoutComponent(String name, Component comp)
79       {
80       }
81
82       public float getLayoutAlignmentX(Container target)
83       {
84         return target.getAlignmentX();
85       }
86
87       public float getLayoutAlignmentY(Container target)
88       {
89         return target.getAlignmentY();
90       }
91
92       public void invalidateLayout(Container target)
93       {
94       }
95
96       public void layoutContainer(Container c)
97       {
98         Dimension menuBarSize;
99         Dimension containerSize = c.getSize(null);
100         Dimension contentPaneSize = contentPane.getPreferredSize();
101
102         /*
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
108
109
110          +-------------------------------+
111          |  JLayeredPane                 |  
112          |  +--------------------------+ |
113          |  | menuBar                  | |
114          |  +--------------------------+ |
115          |  +--------------------------+ |
116          |  |contentPane               | |
117          |  |                          | |
118          |  |                          | |
119          |  |                          | |
120          |  +--------------------------+ |
121          +-------------------------------+
122
123         */
124         if (containerSize.width == 0 && containerSize.height == 0)
125           {
126               if (menuBar != null)
127               {
128                 int maxWidth;
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);
136               }
137             else
138               {
139                 contentPane.setBounds(0, 0, contentPaneSize.width,
140                                       contentPaneSize.height);
141                 layeredPane.setSize(contentPaneSize.width, contentPaneSize.height);
142               }
143           }
144         else
145           {
146             if (menuBar != null)
147               {
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,
154                                       containerSize.width,
155                                       (containerSize.height - menuBarSize.height));
156               }
157             else
158               contentPane.setBounds(0, 0, containerSize.width,
159                                     containerSize.height);
160
161             layeredPane.setSize(containerSize.width, containerSize.height);
162           }
163       }
164       
165       public Dimension maximumLayoutSize(Container target)
166       {
167         return preferredLayoutSize(target);
168       }
169
170       public Dimension minimumLayoutSize(Container target)
171       {
172         return preferredLayoutSize(target);
173       }
174
175       public Dimension preferredLayoutSize(Container c)
176       {
177         Dimension menuBarSize;
178         Dimension prefSize;
179
180         Dimension containerSize = c.getSize();
181         Dimension contentPaneSize = contentPane.getPreferredSize();
182
183         if (containerSize.width == 0 && containerSize.height == 0)
184           {
185             if (menuBar != null)
186               {
187                 int maxWidth;
188                 menuBarSize = menuBar.getPreferredSize();
189                 maxWidth = Math.max(menuBarSize.width, contentPaneSize.width);
190                 prefSize = new Dimension(maxWidth,
191                                        contentPaneSize.height
192                                        + menuBarSize.height);
193               }
194             else
195               prefSize = contentPaneSize;
196           }
197         else
198           prefSize = c.getSize();
199
200         return prefSize;
201       }
202
203       public void removeLayoutComponent(Component comp)
204       {
205       }
206     }
207   
208     protected  Component glassPane;
209     protected  JLayeredPane layeredPane;  
210     protected  JMenuBar menuBar;  
211     protected Container contentPane;
212   
213     
214     void setJMenuBar(JMenuBar m)
215     {  
216       menuBar = m; 
217       getLayeredPane().add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER);
218     }
219
220     JMenuBar getJMenuBar()
221     {  return menuBar; }
222
223   public boolean isValidateRoot()
224   {
225     return true;
226   }
227     
228
229     public Container getContentPane()
230     {
231         if (contentPane == null)
232             {
233                 setContentPane(createContentPane());
234             }
235         return contentPane;
236     }
237
238     public void setContentPane(Container p)
239     {
240         contentPane = p;    
241         getLayeredPane().add(contentPane, JLayeredPane.FRAME_CONTENT_LAYER);
242     }
243
244     protected void addImpl(Component comp,
245                           Object constraints,
246                           int index)
247     {
248         super.addImpl(comp, constraints, index);
249     } 
250
251     public Component getGlassPane() 
252     {
253         if (glassPane == null)
254             setGlassPane(createGlassPane());
255         return glassPane;    
256     }
257
258     public void setGlassPane(Component f)
259     {
260         if (glassPane != null)
261             remove(glassPane);
262
263         glassPane = f; 
264
265         glassPane.setVisible(false);
266         add(glassPane, 0);
267     }
268
269     public JLayeredPane getLayeredPane() 
270     {
271         if (layeredPane == null)
272             setLayeredPane(createLayeredPane());
273         return layeredPane;    
274     }
275     public void setLayeredPane(JLayeredPane f)
276     {
277         if (layeredPane != null)
278             remove(layeredPane);
279         
280         layeredPane = f; 
281         add(f, -1);
282     }
283     
284
285     JRootPane()
286     {
287         setLayout(createRootLayout());
288         getGlassPane();
289         getLayeredPane();
290         getContentPane();
291         setDoubleBuffered(true);
292         updateUI();
293     }
294
295     protected LayoutManager createRootLayout() {
296         return new RootLayout();
297     } 
298
299     JComponent createContentPane()
300     {
301         JPanel p = new JPanel();
302         p.setName(this.getName()+".contentPane");
303         p.setLayout(new BorderLayout());
304         return p;
305     }
306
307     Component createGlassPane()
308     {
309         JPanel p = new JPanel();
310         p.setName(this.getName()+".glassPane");
311         p.setLayout(new BorderLayout());
312         p.setVisible(false);
313         return p;
314     }
315
316     JLayeredPane createLayeredPane()
317     {
318         JLayeredPane l = new JLayeredPane();
319         l.setLayout(null);
320         return l;
321     }    
322
323
324   public RootPaneUI getUI()
325   {
326     return (RootPaneUI) ui;
327   }
328
329   public void setUI(RootPaneUI ui)
330   {
331     super.setUI(ui);
332   }
333
334   public void updateUI()
335   {
336     setUI((RootPaneUI) UIManager.getUI(this));
337   }
338
339   public String getUIClassID()
340   {
341     return "RootPaneUI";
342   }
343 }