OSDN Git Service

* javax/naming/CompoundName.java (CompoundName): Don't check for
[pf3gnuchains/gcc-fork.git] / libjava / javax / swing / ScrollPaneLayout.java
1 /* ScrollPaneLayout.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.Component;
42 import java.awt.Container;
43 import java.awt.Dimension;
44 import java.awt.Insets;
45 import java.awt.LayoutManager;
46 import java.awt.Rectangle;
47 import java.awt.Point;
48 import java.io.Serializable;
49 import javax.swing.border.Border;
50
51 /**
52  * ScrollPaneLayout
53  * @author      Andrew Selkirk
54  * @version     1.0
55  */
56 public class ScrollPaneLayout
57   implements LayoutManager, ScrollPaneConstants, Serializable
58 {
59   static final long serialVersionUID = -4480022884523193743L;
60
61   public static class UIResource extends ScrollPaneLayout 
62     implements javax.swing.plaf.UIResource {
63     public UIResource() {
64     }
65   }
66
67   protected JViewport viewport;
68   protected JScrollBar vsb;
69   protected JScrollBar hsb;
70   protected JViewport rowHead;
71   protected JViewport colHead;
72   protected Component lowerLeft;
73   protected Component lowerRight;
74   protected Component upperLeft;
75   protected Component upperRight;
76   protected int vsbPolicy;
77   protected int hsbPolicy;
78
79   public ScrollPaneLayout() {
80                 
81   }
82
83   public void syncWithScrollPane(JScrollPane scrollPane) {
84     viewport = scrollPane.getViewport();
85     rowHead = scrollPane.getRowHeader();
86     colHead = scrollPane.getColumnHeader();
87     vsb = scrollPane.getVerticalScrollBar();
88     hsb = scrollPane.getHorizontalScrollBar();
89     vsbPolicy = scrollPane.getVerticalScrollBarPolicy();
90     hsbPolicy = scrollPane.getHorizontalScrollBarPolicy();
91     lowerLeft = scrollPane.getCorner(LOWER_LEFT_CORNER);
92     lowerRight = scrollPane.getCorner(LOWER_RIGHT_CORNER);
93     upperLeft = scrollPane.getCorner(UPPER_LEFT_CORNER);
94     upperRight = scrollPane.getCorner(UPPER_RIGHT_CORNER);    
95   }
96
97   protected Component addSingletonComponent(Component oldComponent,
98                                             Component newComponent) {
99     return null;
100   }
101
102   public void addLayoutComponent(String key, Component component) 
103   {
104     if (key == VIEWPORT)
105       viewport = (JViewport) component;
106     else if (key == VERTICAL_SCROLLBAR)
107       vsb = (JScrollBar) component;
108     else if (key == HORIZONTAL_SCROLLBAR)
109       hsb = (JScrollBar) component;
110     else if (key == ROW_HEADER)
111       rowHead = (JViewport) component;
112     else if (key == COLUMN_HEADER)
113       colHead = (JViewport) component;
114     else if (key == LOWER_RIGHT_CORNER)
115       lowerRight = component;
116     else if (key == UPPER_RIGHT_CORNER)
117       upperRight = component;
118     else if (key == LOWER_LEFT_CORNER)
119       lowerLeft = component;
120     else if (key == UPPER_LEFT_CORNER)
121       upperLeft = component;
122   }
123
124   public void removeLayoutComponent(Component component) {
125     if (component == viewport)
126       viewport = null;
127     else if (component == vsb)
128       vsb = null;
129     else if (component == hsb)
130       hsb = null;
131     else if (component == rowHead)
132       rowHead = null;
133     else if (component == colHead)
134       colHead = null;
135     else if (component == lowerRight)
136       lowerRight = null;
137     else if (component == upperRight)
138       upperRight = null;
139     else if (component == lowerLeft)
140       lowerLeft = null;
141     else if (component == upperLeft)
142       upperLeft = null;
143   }
144
145   public int getVerticalScrollBarPolicy()
146   {
147     return vsbPolicy;
148   }
149
150   public void setVerticalScrollBarPolicy(int policy)
151   {
152     vsbPolicy = policy;
153   }
154
155   public int getHorizontalScrollBarPolicy()
156   {
157     return hsbPolicy;
158   }
159
160   public void setHorizontalScrollBarPolicy(int policy)
161   {
162     hsbPolicy = policy;
163   }
164
165   public JViewport getViewport()
166   {
167     return viewport;
168   }
169
170   public JScrollBar getHorizontalScrollBar()
171   {
172     return hsb;
173   }
174
175   public JScrollBar getVerticalScrollBar()
176   {
177     return vsb;
178   }
179
180   public JViewport getRowHeader()
181   {
182     return rowHead;
183   }
184
185   public JViewport getColumnHeader()
186   {
187     return colHead;
188   }
189
190   public Component getCorner(String key)
191   {
192     if (key == LOWER_RIGHT_CORNER)
193       return lowerRight;
194     else if (key == UPPER_RIGHT_CORNER)
195       return upperRight;
196     else if (key == LOWER_LEFT_CORNER)
197       return lowerLeft;
198     else if (key == UPPER_LEFT_CORNER)
199       return upperLeft;
200     return null;
201   }
202
203   private static void maybeSetPreferredSize(JComponent src, Dimension dim)
204   {
205     Dimension tmp = null;
206     if (src != null)
207       tmp = src.getPreferredSize();
208     if (tmp != null)
209       dim.setSize(tmp);        
210   }
211
212   private static void maybeSetMinimumSize(JComponent src, Dimension dim)
213   {
214     Dimension tmp = null;
215     if (src != null)
216       tmp = src.getMinimumSize();
217     if (tmp != null)
218       dim.setSize(tmp);
219   }
220
221   public Dimension preferredLayoutSize(Container parent) 
222   {
223     if (parent != null && parent instanceof JScrollPane)
224       {
225         JScrollPane sc = (JScrollPane) parent;
226         synchronized (sc.getTreeLock ())
227           {
228             Dimension insetsSize = new Dimension(0,0); 
229             Dimension viewportSize = new Dimension(0,0); 
230             Dimension viewportInsetsSize = new Dimension(0,0); 
231             Dimension columnHeaderSize = new Dimension(0,0); 
232             Dimension rowHeaderSize = new Dimension(0,0); 
233             Dimension verticalScrollBarSize = new Dimension(0,0); 
234             Dimension horizontalScrollBarSize = new Dimension(0,0); 
235
236             Insets insets = sc.getInsets();
237             Border viewportBorder = sc.getViewportBorder();
238             Insets viewportInsets = null;
239
240             if (viewportBorder != null)
241               {
242               viewportInsets = viewportBorder.getBorderInsets(parent);
243                 if (viewportInsets != null)
244                   viewportInsetsSize.setSize(viewportInsets.left + viewportInsets.right,
245                                              viewportInsets.top + viewportInsets.bottom);
246               }
247
248             if (insets != null)
249               insetsSize.setSize(insets.left + insets.right,
250                                  insets.top + insets.bottom);
251
252             if (viewport != null)
253               {
254                 Component view = null;
255                 Scrollable scr = null;
256                 Dimension pref = null;
257                 
258                 view = viewport.getView();
259                 if (view != null && view instanceof Scrollable)
260                   scr = (Scrollable) view;
261                 if (scr != null)
262                   pref = scr.getPreferredScrollableViewportSize();
263                 if (pref == null)
264                   pref = viewport.getPreferredSize();
265                 if (pref != null)
266                   viewportSize.setSize(pref);
267               }
268                        
269             maybeSetPreferredSize(colHead, columnHeaderSize);
270             maybeSetPreferredSize(rowHead, rowHeaderSize);
271             maybeSetPreferredSize(vsb, verticalScrollBarSize);
272             maybeSetPreferredSize(hsb, horizontalScrollBarSize);
273
274             return new Dimension(insetsSize.width 
275                                  + viewportSize.width
276                                  + viewportInsetsSize.width
277                                  + rowHeaderSize.width
278                                  + verticalScrollBarSize.width,
279                                  insetsSize.height
280                                  + viewportSize.height
281                                  + viewportInsetsSize.height
282                                  + columnHeaderSize.height
283                                  + horizontalScrollBarSize.height);
284           }
285       }
286     else
287       {
288         return new Dimension(0,0);
289       }
290   }
291
292   public Dimension minimumLayoutSize(Container parent)
293   {
294     if (parent instanceof JScrollPane)
295       {
296         JScrollPane sc = (JScrollPane) parent;
297         synchronized (sc.getTreeLock ())
298           {
299             Dimension insetsSize = new Dimension(0,0); 
300             Dimension viewportSize = new Dimension(0,0); 
301             Dimension viewportInsetsSize = new Dimension(0,0); 
302             Dimension columnHeaderSize = new Dimension(0,0); 
303             Dimension rowHeaderSize = new Dimension(0,0); 
304             Dimension verticalScrollBarSize = new Dimension(0,0); 
305             Dimension horizontalScrollBarSize = new Dimension(0,0); 
306
307             Insets insets = sc.getInsets();
308             Border viewportBorder = sc.getViewportBorder();
309             Insets viewportInsets = null;
310
311             if (viewportBorder != null)
312               {
313               viewportInsets = viewportBorder.getBorderInsets(parent);
314                 if (viewportInsets != null)
315                   viewportInsetsSize.setSize(viewportInsets.left + viewportInsets.right,
316                                              viewportInsets.top + viewportInsets.bottom);
317               }
318
319             if (insets != null)
320               insetsSize.setSize(insets.left + insets.right,
321                                  insets.top + insets.bottom);
322
323             maybeSetMinimumSize(viewport, viewportSize);
324             maybeSetMinimumSize(colHead, columnHeaderSize);
325             maybeSetMinimumSize(rowHead, rowHeaderSize);
326
327             if (vsbPolicy != VERTICAL_SCROLLBAR_NEVER)
328               maybeSetMinimumSize(vsb, verticalScrollBarSize);
329
330             if (hsbPolicy != HORIZONTAL_SCROLLBAR_NEVER)
331               maybeSetMinimumSize(hsb, horizontalScrollBarSize);
332             
333             return new Dimension(insetsSize.width 
334                                  + viewportSize.width
335                                  + viewportInsetsSize.width
336                                  + rowHeaderSize.width
337                                  + verticalScrollBarSize.width,
338                                  insetsSize.height
339                                  + viewportSize.height
340                                  + viewportInsetsSize.height
341                                  + columnHeaderSize.height
342                                  + horizontalScrollBarSize.height);
343           }
344       }
345     else
346       {
347         return new Dimension(0,0);
348       }
349   }
350
351   /**
352    *
353    *     +----+--------------------+----+ y1
354    *     | c1 |   column header    | c2 |
355    *     +----+--------------------+----+ y2
356    *     | r  |                    | v  |
357    *     | o  |                    |    |
358    *     | w  |                    | s  |
359    *     |    |                    | r  |
360    *     | h  |                    | o  |
361    *     | e  |      viewport      | l  |
362    *     | a  |                    | l  |
363    *     | d  |                    | b  |
364    *     | e  |                    | a  |
365    *     | r  |                    | r  |
366    *     +----+--------------------+----+ y3
367    *     | c3 |    h scrollbar     | c4 |
368    *     +----+--------------------+----+ y4
369    *    x1   x2                   x3   x4
370    *   
371    */
372   public void layoutContainer(Container parent)
373   {
374     if (parent instanceof JScrollPane)
375       {
376         JScrollPane sc = (JScrollPane) parent;
377         synchronized (sc.getTreeLock ())
378           {
379             JViewport viewport = sc.getViewport();
380             Dimension viewSize = viewport.getViewSize(); 
381             Point viewPos = viewport.getViewPosition(); 
382
383             int x1 = 0, x2 = 0, x3 = 0, x4 = 0;
384             int y1 = 0, y2 = 0, y3 = 0, y4 = 0;
385
386             Rectangle scrollPaneBounds = SwingUtilities.calculateInnerArea(sc, null);
387
388             x1 = scrollPaneBounds.x;
389             y1 = scrollPaneBounds.y;
390             x4 = scrollPaneBounds.x + scrollPaneBounds.width;
391             y4 = scrollPaneBounds.y + scrollPaneBounds.height;
392             
393             if (colHead != null)
394               y2 = y1 + colHead.getPreferredSize().height;
395             else
396               y2 = y1;
397
398             if (rowHead != null)
399               x2 = x1 + rowHead.getPreferredSize().width;
400             else
401               x2 = x1;
402
403             int vsbPolicy = sc.getVerticalScrollBarPolicy();
404             int hsbPolicy = sc.getHorizontalScrollBarPolicy();
405
406             x3 = x4 - vsb.getPreferredSize().width;
407             y3 = y4 - hsb.getPreferredSize().height;
408
409             boolean showVsb = 
410               (vsb != null)
411               && ((vsbPolicy == VERTICAL_SCROLLBAR_ALWAYS)
412                   || (vsbPolicy == VERTICAL_SCROLLBAR_AS_NEEDED 
413                       && viewSize.height > (y3 - y2)));
414
415             boolean showHsb = 
416               (hsb != null)
417               && ((hsbPolicy == HORIZONTAL_SCROLLBAR_ALWAYS)
418                   || (hsbPolicy == HORIZONTAL_SCROLLBAR_AS_NEEDED 
419                       && viewSize.width > (x3 - x2)));
420             
421             if (!showVsb)
422               x3 = x4;
423
424             if (!showHsb)
425               y3 = y4;
426
427             // now set the layout
428
429             if (viewport != null)
430               viewport.setBounds(new Rectangle(x2, y2, x3-x2, y3-y2));
431
432             if (colHead != null)
433               colHead.setBounds(new Rectangle(x2, y1, x3-x2, y2-y1));
434
435             if (rowHead != null)
436               rowHead.setBounds(new Rectangle(x1, y2, x2-x1, y3-y2));
437
438             if (showVsb)
439               {
440                 vsb.setVisible(true);
441               vsb.setBounds(new Rectangle(x3, y2, x4-x3, y3-y2));
442               }
443             else if (vsb != null)
444               vsb.setVisible(false);
445
446             if (showHsb)
447               {
448                 hsb.setVisible(true);
449               hsb.setBounds(new Rectangle(x2, y3, x3-x2, y4-y3));
450               }
451             else if (hsb != null)
452               hsb.setVisible(false);
453
454             if (upperLeft != null)
455               upperLeft.setBounds(new Rectangle(x1, y1, x2-x1, y2-y1));
456
457             if (upperRight != null)
458               upperRight.setBounds(new Rectangle(x3, y1, x4-x3, y2-y1));
459
460             if (lowerLeft != null)
461               lowerLeft.setBounds(new Rectangle(x1, y3, x2-x1, y4-y3));
462
463             if (lowerRight != null)
464               lowerRight.setBounds(new Rectangle(x3, y3, x4-x3, y4-y3));
465
466           }
467       }
468   }
469
470   public Rectangle getViewportBorderBounds(JScrollPane scrollPane) {
471     return null;
472   }
473
474
475 }