1 /* ScrollPaneLayout.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)
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., 51 Franklin Street, Fifth Floor, 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
.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
.io
.Serializable
;
51 * @author Andrew Selkirk
54 public class ScrollPaneLayout
55 implements LayoutManager
, ScrollPaneConstants
, Serializable
57 private static final long serialVersionUID
= -4480022884523193743L;
59 public static class UIResource
extends ScrollPaneLayout
60 implements javax
.swing
.plaf
.UIResource
68 protected JViewport viewport
;
69 protected JScrollBar vsb
;
70 protected JScrollBar hsb
;
71 protected JViewport rowHead
;
72 protected JViewport colHead
;
73 protected Component lowerLeft
;
74 protected Component lowerRight
;
75 protected Component upperLeft
;
76 protected Component upperRight
;
77 protected int vsbPolicy
;
78 protected int hsbPolicy
;
80 public ScrollPaneLayout()
82 // Nothing to do here.
85 public void syncWithScrollPane(JScrollPane scrollPane
) {
86 viewport
= scrollPane
.getViewport();
87 rowHead
= scrollPane
.getRowHeader();
88 colHead
= scrollPane
.getColumnHeader();
89 vsb
= scrollPane
.getVerticalScrollBar();
90 hsb
= scrollPane
.getHorizontalScrollBar();
91 vsbPolicy
= scrollPane
.getVerticalScrollBarPolicy();
92 hsbPolicy
= scrollPane
.getHorizontalScrollBarPolicy();
93 lowerLeft
= scrollPane
.getCorner(LOWER_LEFT_CORNER
);
94 lowerRight
= scrollPane
.getCorner(LOWER_RIGHT_CORNER
);
95 upperLeft
= scrollPane
.getCorner(UPPER_LEFT_CORNER
);
96 upperRight
= scrollPane
.getCorner(UPPER_RIGHT_CORNER
);
100 * Removes an existing component. If oldComponent is not null
101 * and is not equal to newComponent, oldComponent must be removed
103 * @param oldComponent the old Component that may need to be removed.
104 * @param newComponent the Component to add.
105 * @return the newComponent
107 protected Component
addSingletonComponent(Component oldComponent
,
108 Component newComponent
)
110 if (oldComponent
!= null && oldComponent
!= newComponent
)
111 oldComponent
.getParent().remove(oldComponent
);
116 * Add the specified component to the layout.
117 * @param key must be one of VIEWPORT, VERTICAL_SCROLLBAR,
118 * HORIZONTAL_SCROLLBAR, ROW_HEADER, COLUMN_HEADER,
119 * LOWER_RIGHT_CORNER, LOWER_LEFT_CORNER, UPPER_RIGHT_CORNER,
121 * @param component the Component to add
122 * @throws IllegalArgumentException if key is not as above
124 public void addLayoutComponent(String key
, Component component
)
127 viewport
= (JViewport
) component
;
128 else if (key
== VERTICAL_SCROLLBAR
)
129 vsb
= (JScrollBar
) component
;
130 else if (key
== HORIZONTAL_SCROLLBAR
)
131 hsb
= (JScrollBar
) component
;
132 else if (key
== ROW_HEADER
)
133 rowHead
= (JViewport
) component
;
134 else if (key
== COLUMN_HEADER
)
135 colHead
= (JViewport
) component
;
136 else if (key
== LOWER_RIGHT_CORNER
)
137 lowerRight
= component
;
138 else if (key
== UPPER_RIGHT_CORNER
)
139 upperRight
= component
;
140 else if (key
== LOWER_LEFT_CORNER
)
141 lowerLeft
= component
;
142 else if (key
== UPPER_LEFT_CORNER
)
143 upperLeft
= component
;
145 throw new IllegalArgumentException();
148 public void removeLayoutComponent(Component component
) {
149 if (component
== viewport
)
151 else if (component
== vsb
)
153 else if (component
== hsb
)
155 else if (component
== rowHead
)
157 else if (component
== colHead
)
159 else if (component
== lowerRight
)
161 else if (component
== upperRight
)
163 else if (component
== lowerLeft
)
165 else if (component
== upperLeft
)
169 public int getVerticalScrollBarPolicy()
175 * Sets the vertical scrollbar policy.
176 * @param policy must be one of VERTICAL_SCROLLBAR_AS_NEEDED,
177 * VERTICAL_SCROLLBAR_NEVER, VERTICAL_SCROLLBAR_ALWAYS.
178 * @throws IllegalArgumentException if policy is not one of the valid
179 * JScrollBar policies.
181 public void setVerticalScrollBarPolicy(int policy
)
183 if (policy
!= VERTICAL_SCROLLBAR_AS_NEEDED
&&
184 policy
!= VERTICAL_SCROLLBAR_NEVER
&&
185 policy
!= VERTICAL_SCROLLBAR_ALWAYS
)
186 throw new IllegalArgumentException("Illegal Scrollbar Policy");
190 public int getHorizontalScrollBarPolicy()
196 * Sets the horizontal scrollbar policy.
197 * @param policy must be one of HORIZONTAL_SCROLLBAR_AS_NEEDED,
198 * HORIZONTAL_SCROLLBAR_NEVER, HORIZONTAL_SCROLLBAR_ALWAYS.
199 * @throws IllegalArgumentException if policy is not one of the valid
200 * JScrollbar policies.
202 public void setHorizontalScrollBarPolicy(int policy
)
204 if (policy
!= HORIZONTAL_SCROLLBAR_AS_NEEDED
&&
205 policy
!= HORIZONTAL_SCROLLBAR_NEVER
&&
206 policy
!= HORIZONTAL_SCROLLBAR_ALWAYS
)
207 throw new IllegalArgumentException("Illegal Scrollbar Policy");
211 public JViewport
getViewport()
216 public JScrollBar
getHorizontalScrollBar()
221 public JScrollBar
getVerticalScrollBar()
226 public JViewport
getRowHeader()
231 public JViewport
getColumnHeader()
237 * Returns the Component at the specified corner.
238 * @param key the corner.
239 * @return the Component at the specified corner, or null if
240 * key is not one of the four valid corners.
242 public Component
getCorner(String key
)
244 if (key
== LOWER_RIGHT_CORNER
)
246 else if (key
== UPPER_RIGHT_CORNER
)
248 else if (key
== LOWER_LEFT_CORNER
)
250 else if (key
== UPPER_LEFT_CORNER
)
255 public Dimension
preferredLayoutSize(Container parent
)
257 // Sun's implementation simply throws a ClassCastException if
258 // parent is no JScrollPane, so do we.
259 JScrollPane sc
= (JScrollPane
) parent
;
260 Dimension viewportSize
= viewport
.getPreferredSize();
261 Dimension viewSize
= viewport
.getViewSize();
262 int width
= viewportSize
.width
;
263 int height
= viewportSize
.height
;
265 // horizontal scrollbar needed if the view's preferred width
266 // is larger than the viewport's preferred width
267 if (hsb
!= null && viewSize
.width
> viewportSize
.width
)
268 height
+= hsb
.getPreferredSize().height
;
270 // vertical scrollbar needed if the view's preferred height
271 // is larger than the viewport's preferred height
272 if (vsb
!= null && viewSize
.height
> viewportSize
.height
)
273 width
+= vsb
.getPreferredSize().width
;
274 if (rowHead
!= null && rowHead
.isVisible())
275 width
+= rowHead
.getPreferredSize().width
;
276 if (colHead
!= null && colHead
.isVisible())
277 height
+= colHead
.getPreferredSize().height
;
278 Insets i
= sc
.getInsets();
279 return new Dimension(width
+ i
.left
+ i
.right
,
280 height
+ i
.left
+ i
.right
);
283 public Dimension
minimumLayoutSize(Container parent
)
285 // Sun's implementation simply throws a ClassCastException if
286 // parent is no JScrollPane, so do we.
287 JScrollPane sc
= (JScrollPane
) parent
;
288 Insets i
= sc
.getInsets();
289 Dimension viewportMinSize
= sc
.getViewport().getMinimumSize();
291 int width
= i
.left
+ i
.right
+ viewportMinSize
.width
;
292 if (sc
.getVerticalScrollBarPolicy()
293 != JScrollPane
.VERTICAL_SCROLLBAR_NEVER
)
294 width
+= sc
.getVerticalScrollBar().getMinimumSize().width
;
296 int height
= i
.top
+ i
.bottom
+ viewportMinSize
.height
;
297 if (sc
.getHorizontalScrollBarPolicy()
298 != JScrollPane
.HORIZONTAL_SCROLLBAR_NEVER
)
299 height
+= sc
.getHorizontalScrollBar().getMinimumSize().height
;
301 return new Dimension(width
, height
);
306 * +----+--------------------+----+ y1
307 * | c1 | column header | c2 |
308 * +----+--------------------+----+ y2
314 * | e | viewport | l |
319 * +----+--------------------+----+ y3
320 * | c3 | h scrollbar | c4 |
321 * +----+--------------------+----+ y4
325 public void layoutContainer(Container parent
)
327 // Sun's implementation simply throws a ClassCastException if
328 // parent is no JScrollPane, so do we.
329 JScrollPane sc
= (JScrollPane
) parent
;
330 JViewport viewport
= sc
.getViewport();
331 Dimension viewSize
= viewport
.getViewSize();
333 int x1
= 0, x2
= 0, x3
= 0, x4
= 0;
334 int y1
= 0, y2
= 0, y3
= 0, y4
= 0;
335 Rectangle scrollPaneBounds
= SwingUtilities
.calculateInnerArea(sc
, null);
337 x1
= scrollPaneBounds
.x
;
338 y1
= scrollPaneBounds
.y
;
339 x4
= scrollPaneBounds
.x
+ scrollPaneBounds
.width
;
340 y4
= scrollPaneBounds
.y
+ scrollPaneBounds
.height
;
342 y2
= y1
+ colHead
.getPreferredSize().height
;
347 x2
= x1
+ rowHead
.getPreferredSize().width
;
351 int vsbPolicy
= sc
.getVerticalScrollBarPolicy();
352 int hsbPolicy
= sc
.getHorizontalScrollBarPolicy();
356 && ((vsbPolicy
== VERTICAL_SCROLLBAR_ALWAYS
)
357 || (vsbPolicy
== VERTICAL_SCROLLBAR_AS_NEEDED
358 && viewSize
.height
> (y4
- y2
)));
361 && ((hsbPolicy
== HORIZONTAL_SCROLLBAR_ALWAYS
)
362 || (hsbPolicy
== HORIZONTAL_SCROLLBAR_AS_NEEDED
363 && viewSize
.width
> (x4
- x2
)));
368 x3
= x4
- vsb
.getPreferredSize().width
;
373 y3
= y4
- hsb
.getPreferredSize().height
;
375 // now set the layout
376 if (viewport
!= null)
377 viewport
.setBounds(new Rectangle(x2
, y2
, x3
- x2
, y3
- y2
));
380 colHead
.setBounds(new Rectangle(x2
, y1
, x3
- x2
, y2
- y1
));
383 rowHead
.setBounds(new Rectangle(x1
, y2
, x2
- x1
, y3
- y2
));
387 vsb
.setVisible(true);
388 vsb
.setBounds(new Rectangle(x3
, y2
, x4
- x3
, y3
- y2
));
390 else if (vsb
!= null)
391 vsb
.setVisible(false);
395 hsb
.setVisible(true);
396 hsb
.setBounds(new Rectangle(x2
, y3
, x3
- x2
, y4
- y3
));
398 else if (hsb
!= null)
399 hsb
.setVisible(false);
401 if (upperLeft
!= null)
402 upperLeft
.setBounds(new Rectangle(x1
, y1
, x2
- x1
, y2
- y1
));
404 if (upperRight
!= null)
405 upperRight
.setBounds(new Rectangle(x3
, y1
, x4
- x3
, y2
- y1
));
407 if (lowerLeft
!= null)
408 lowerLeft
.setBounds(new Rectangle(x1
, y3
, x2
- x1
, y4
- y3
));
410 if (lowerRight
!= null)
411 lowerRight
.setBounds(new Rectangle(x3
, y3
, x4
- x3
, y4
- y3
));
415 * Returns the bounds of the border around a ScrollPane's viewport.
417 * @param scrollPane the ScrollPane for which's viewport the border
420 * @deprecated As of Swing 1.1 replaced by
421 * {@link javax.swing.JScrollPane#getViewportBorderBounds}.
423 public Rectangle
getViewportBorderBounds(JScrollPane scrollPane
) {