1 | // FlowLayout.java - Grid-based layout engine
|
---|
2 |
|
---|
3 | /* Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation
|
---|
4 |
|
---|
5 | This file is part of GNU Classpath.
|
---|
6 |
|
---|
7 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 2, or (at your option)
|
---|
10 | any later version.
|
---|
11 |
|
---|
12 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
13 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
19 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
20 | 02111-1307 USA.
|
---|
21 |
|
---|
22 | Linking this library statically or dynamically with other modules is
|
---|
23 | making a combined work based on this library. Thus, the terms and
|
---|
24 | conditions of the GNU General Public License cover the whole
|
---|
25 | combination.
|
---|
26 |
|
---|
27 | As a special exception, the copyright holders of this library give you
|
---|
28 | permission to link this library with independent modules to produce an
|
---|
29 | executable, regardless of the license terms of these independent
|
---|
30 | modules, and to copy and distribute the resulting executable under
|
---|
31 | terms of your choice, provided that you also meet, for each linked
|
---|
32 | independent module, the terms and conditions of the license of that
|
---|
33 | module. An independent module is a module which is not derived from
|
---|
34 | or based on this library. If you modify this library, you may extend
|
---|
35 | this exception to your version of the library, but you are not
|
---|
36 | obligated to do so. If you do not wish to do so, delete this
|
---|
37 | exception statement from your version. */
|
---|
38 |
|
---|
39 |
|
---|
40 | package java.awt;
|
---|
41 |
|
---|
42 | import java.io.Serializable;
|
---|
43 |
|
---|
44 | /** This class implements a flow-based layout. Components are laid
|
---|
45 | * out in order from left to right. When a component cannot be placed
|
---|
46 | * without horizontal clipping, a new row is started. This class
|
---|
47 | * supports horizontal and vertical gaps. These are used for spacing
|
---|
48 | * between components.
|
---|
49 | *
|
---|
50 | * @author Tom Tromey <tromey@redhat.com>
|
---|
51 | * @author Aaron M. Renn (arenn@urbanophile.com)
|
---|
52 | */
|
---|
53 | public class FlowLayout implements LayoutManager, Serializable
|
---|
54 | {
|
---|
55 | /** Constant that specifies left alignment. */
|
---|
56 | public static final int LEFT = 0;
|
---|
57 | /** Constant that specifies center alignment. */
|
---|
58 | public static final int CENTER = 1;
|
---|
59 | /** Constant that specifies right alignment. */
|
---|
60 | public static final int RIGHT = 2;
|
---|
61 |
|
---|
62 | /** Constant that specifies alignment to leading edge of container's
|
---|
63 | * orientation. */
|
---|
64 | public static final int LEADING = 3;
|
---|
65 | /** Constant that specifies alignment to trailing edge of container's
|
---|
66 | * orientation. */
|
---|
67 | public static final int TRAILING = 4;
|
---|
68 |
|
---|
69 | // Serialization constant
|
---|
70 | private static final long serialVersionUID = -7262534875583282631L;
|
---|
71 |
|
---|
72 | /** Add a new component to the layout. This particular implementation
|
---|
73 | * does nothing.
|
---|
74 | */
|
---|
75 | public void addLayoutComponent (String name, Component comp)
|
---|
76 | {
|
---|
77 | // Nothing.
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Returns the current justification value for this object.
|
---|
82 | *
|
---|
83 | * @return The current justification value for this object.
|
---|
84 | */
|
---|
85 | public int getAlignment ()
|
---|
86 | {
|
---|
87 | return align;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Returns the horizontal gap between components.
|
---|
92 | *
|
---|
93 | * @return The horizontal gap between components.
|
---|
94 | */
|
---|
95 | public int getHgap ()
|
---|
96 | {
|
---|
97 | return hgap;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Returns the vertical gap between lines of components.
|
---|
102 | *
|
---|
103 | * @return The vertical gap between lines of components.
|
---|
104 | */
|
---|
105 | public int getVgap ()
|
---|
106 | {
|
---|
107 | return vgap;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Initializes a new instance of <code>FlowLayout</code> with a center
|
---|
112 | * justification and a default horizontal and vertical gap of 5.
|
---|
113 | */
|
---|
114 | public FlowLayout ()
|
---|
115 | {
|
---|
116 | this (CENTER, 5, 5);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Initializes a new instance of <code>FlowLayout</code> with the specified
|
---|
121 | * justification and a default horizontal and vertical gap of 5.
|
---|
122 | *
|
---|
123 | * @param align The justification setting, which should be one of the
|
---|
124 | * contants in this class.
|
---|
125 | */
|
---|
126 | public FlowLayout (int align)
|
---|
127 | {
|
---|
128 | this (align, 5, 5);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Initializes a new instance of <code>FlowLayout</code> with the specified
|
---|
133 | * justification and gap values
|
---|
134 | * @param align Alignment
|
---|
135 | * @param hgap The horizontal gap
|
---|
136 | * @param vgap The vertical gap
|
---|
137 | * @exception IllegalArgumentException If either gap is negative
|
---|
138 | */
|
---|
139 | public FlowLayout (int align, int hgap, int vgap)
|
---|
140 | {
|
---|
141 | // Use methods to set fields so that we can have all the checking
|
---|
142 | // in one place.
|
---|
143 | setVgap (vgap);
|
---|
144 | setHgap (hgap);
|
---|
145 | setAlignment (align);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Lay out the container's components based on current settings.
|
---|
149 | * @param parent The parent container
|
---|
150 | */
|
---|
151 | public void layoutContainer (Container parent)
|
---|
152 | {
|
---|
153 | synchronized (parent.getTreeLock ())
|
---|
154 | {
|
---|
155 | int num = parent.getComponentCount ();
|
---|
156 | // This is more efficient than calling getComponents().
|
---|
157 | Component[] comps = parent.component;
|
---|
158 |
|
---|
159 | Dimension d = parent.getSize ();
|
---|
160 | Insets ins = parent.getInsets ();
|
---|
161 |
|
---|
162 | ComponentOrientation orient = parent.getComponentOrientation ();
|
---|
163 | boolean left_to_right = orient.isLeftToRight ();
|
---|
164 |
|
---|
165 | int y = ins.top + vgap;
|
---|
166 | int i = 0;
|
---|
167 | while (i < num)
|
---|
168 | {
|
---|
169 | // Find the components which go in the current row.
|
---|
170 | int new_w = ins.left + hgap + ins.right;
|
---|
171 | int new_h = 0;
|
---|
172 | int j;
|
---|
173 | boolean found_one = false;
|
---|
174 | for (j = i; j < num && ! found_one; ++j)
|
---|
175 | {
|
---|
176 | // Skip invisible items.
|
---|
177 | if (! comps[i].visible)
|
---|
178 | continue;
|
---|
179 |
|
---|
180 | Dimension c = comps[i].getPreferredSize ();
|
---|
181 |
|
---|
182 | int next_w = new_w + hgap + c.width;
|
---|
183 | if (next_w <= d.width || ! found_one)
|
---|
184 | {
|
---|
185 | new_w = next_w;
|
---|
186 | new_h = Math.max (new_h, c.height);
|
---|
187 | found_one = true;
|
---|
188 | }
|
---|
189 | else
|
---|
190 | {
|
---|
191 | // Must start a new row, and we already found an item
|
---|
192 | break;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | // Set the location of each component for this row.
|
---|
197 | int x;
|
---|
198 |
|
---|
199 | int myalign = align;
|
---|
200 | if (align == LEADING)
|
---|
201 | myalign = left_to_right ? LEFT : RIGHT;
|
---|
202 | else if (align == TRAILING)
|
---|
203 | myalign = left_to_right ? RIGHT : LEFT;
|
---|
204 |
|
---|
205 | if (myalign == LEFT)
|
---|
206 | x = ins.left + hgap;
|
---|
207 | else if (myalign == CENTER)
|
---|
208 | x = (d.width - new_w) / 2;
|
---|
209 | else
|
---|
210 | x = d.width - new_w;
|
---|
211 |
|
---|
212 | for (int k = i; k < j; ++k)
|
---|
213 | {
|
---|
214 | if (comps[k].visible)
|
---|
215 | {
|
---|
216 | Dimension c = comps[k].getPreferredSize ();
|
---|
217 | comps[k].setBounds (x, y, c.width, new_h);
|
---|
218 | x += c.width + hgap;
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | // Advance to next row.
|
---|
223 | i = j;
|
---|
224 | y += new_h + vgap;
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Returns the minimum layout size for the specified container using
|
---|
231 | * this layout.
|
---|
232 | * @param cont The parent container
|
---|
233 | * @return The minimum layout size.
|
---|
234 | */
|
---|
235 | public Dimension minimumLayoutSize (Container cont)
|
---|
236 | {
|
---|
237 | return getSize (cont, true);
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Returns the preferred layout size for the specified container using
|
---|
242 | * this layout.
|
---|
243 | * @param cont The parent container
|
---|
244 | * @return The preferred layout size.
|
---|
245 | */
|
---|
246 | public Dimension preferredLayoutSize (Container cont)
|
---|
247 | {
|
---|
248 | return getSize (cont, false);
|
---|
249 | }
|
---|
250 |
|
---|
251 | /** Remove the indicated component from this layout manager.
|
---|
252 | * This particular implementation does nothing.
|
---|
253 | * @param comp The component to remove
|
---|
254 | */
|
---|
255 | public void removeLayoutComponent (Component comp)
|
---|
256 | {
|
---|
257 | // Nothing.
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Sets the justification value for this object to the specified value.
|
---|
262 | *
|
---|
263 | * @param align The new justification value for this object, which must
|
---|
264 | * be one of the constants in this class.
|
---|
265 | */
|
---|
266 | public void setAlignment (int align)
|
---|
267 | {
|
---|
268 | if (align != LEFT && align != RIGHT && align != CENTER
|
---|
269 | && align != LEADING && align != TRAILING)
|
---|
270 | throw new IllegalArgumentException ("invalid alignment: " + align);
|
---|
271 | this.align = align;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Sets the horizontal gap between components to the specified value.
|
---|
276 | *
|
---|
277 | * @param hgap The new horizontal gap between components.
|
---|
278 | */
|
---|
279 | public void setHgap (int hgap)
|
---|
280 | {
|
---|
281 | if (hgap < 0)
|
---|
282 | throw new IllegalArgumentException ("horizontal gap must be nonnegative");
|
---|
283 | this.hgap = hgap;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Sets the vertical gap between lines of components to the specified value.
|
---|
288 | *
|
---|
289 | * @param vgap The new vertical gap.
|
---|
290 | */
|
---|
291 | public void setVgap (int vgap)
|
---|
292 | {
|
---|
293 | if (vgap < 0)
|
---|
294 | throw new IllegalArgumentException ("vertical gap must be nonnegative");
|
---|
295 | this.vgap = vgap;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /** Return String description of this object.
|
---|
299 | * @return A string representation of this object.
|
---|
300 | */
|
---|
301 | public String toString ()
|
---|
302 | {
|
---|
303 | return ("[" + getClass ().getName () + ",hgap=" + hgap + ",vgap=" + vgap
|
---|
304 | + ",align=" + align + "]");
|
---|
305 | }
|
---|
306 |
|
---|
307 | // This method is used to compute the various sizes.
|
---|
308 | private Dimension getSize (Container parent, boolean is_min)
|
---|
309 | {
|
---|
310 | synchronized (parent.getTreeLock ())
|
---|
311 | {
|
---|
312 | int w, h, num = parent.getComponentCount ();
|
---|
313 | // This is more efficient than calling getComponents().
|
---|
314 | Component[] comps = parent.component;
|
---|
315 |
|
---|
316 | w = 0;
|
---|
317 | h = 0;
|
---|
318 | for (int i = 0; i < num; ++i)
|
---|
319 | {
|
---|
320 | if (! comps[i].visible)
|
---|
321 | continue;
|
---|
322 |
|
---|
323 | // FIXME: can we just directly read the fields in Component?
|
---|
324 | // Or will that not work with subclassing?
|
---|
325 | Dimension d;
|
---|
326 |
|
---|
327 | if (is_min)
|
---|
328 | d = comps[i].getMinimumSize ();
|
---|
329 | else
|
---|
330 | d = comps[i].getPreferredSize ();
|
---|
331 |
|
---|
332 | w += d.width;
|
---|
333 | h = Math.max (d.height, h);
|
---|
334 | }
|
---|
335 |
|
---|
336 | Insets ins = parent.getInsets ();
|
---|
337 |
|
---|
338 | w += (num + 1) * hgap + ins.left + ins.right;
|
---|
339 | h += 2 * vgap + ins.top + ins.bottom;
|
---|
340 |
|
---|
341 | return new Dimension (w, h);
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * @serial The justification alignment of the lines of components, which
|
---|
347 | * will be one of the constants defined in this class.
|
---|
348 | */
|
---|
349 | private int align;
|
---|
350 |
|
---|
351 | /**
|
---|
352 | * @serial The horizontal gap between components.
|
---|
353 | */
|
---|
354 | private int hgap;
|
---|
355 |
|
---|
356 | /**
|
---|
357 | * @serial The vertical gap between lines of components.
|
---|
358 | */
|
---|
359 | private int vgap;
|
---|
360 | }
|
---|