OSDN Git Service

2004-05-30 Mark Wielaard <mark@klomp.org>
[pf3gnuchains/gcc-fork.git] / libjava / java / awt / GridLayout.java
1 // GridLayout.java - Grid-based layout engine
2
3 /* Copyright (C) 1999, 2000, 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 grid-based layout scheme.  Components are
45  * all given the same size and are laid out from left to right and top
46  * to bottom.  A GridLayout is configured with a number of rows and a
47  * number of columns.  If both are specified, then the number of
48  * columns is ignored and is derived from the number of rows and the
49  * total number of components.  If either is zero then that dimension
50  * is computed based on the actual size of the container.  An
51  * exception is thrown if an attempt is made to set both the number of
52  * rows and the number of columns to 0.  This class also supports
53  * horizontal and vertical gaps; these are used as spacing between
54  * cells.
55  *
56  * @author Tom Tromey <tromey@redhat.com>
57  * @author Aaron M. Renn (arenn@urbanophile.com)
58  */
59 public class GridLayout implements LayoutManager, Serializable
60 {
61   static final long serialVersionUID = -7411804673224730901L;
62   
63   /** Add a new component to the layout.  This particular implementation
64    * does nothing.
65    * @param name The name of the component to add.
66    * @param component The component to add.
67    */
68   public void addLayoutComponent (String name, Component comp)
69   {
70     // Nothing.
71   }
72
73   /** Return the number of columns in this layout.  */
74   public int getColumns ()
75   {
76     return cols;
77   }
78
79   /** Return the horizontal gap.  */
80   public int getHgap ()
81   {
82     return hgap;
83   }
84
85   /** Return the number of rows in this layout.  */
86   public int getRows ()
87   {
88     return rows;
89   }
90
91   /** Return the vertical gap.  */
92   public int getVgap ()
93   {
94     return vgap;
95   }
96
97   /** Create a new <code>GridLayout</code> with one row and any number
98    * of columns.  Both gaps are set to 0.
99    */
100   public GridLayout ()
101   {
102     this (1, 0, 0, 0);
103   }
104
105   /** Create a new <code>GridLayout</code> with the specified number
106    * of rows and columns.  Both gaps are set to 0.  Note that the row
107    * and column settings cannot both be zero.  If both the row and
108    * column values are non-zero, the rows value takes precedence.
109    * @param rows Number of rows
110    * @param cols Number of columns
111    * @exception IllegalArgumentException If rows and columns are both
112    *        0, or if either are negative
113    */
114   public GridLayout (int rows, int cols)
115   {
116     this (rows, cols, 0, 0);
117   }
118
119   /** Create a new GridLayout with the specified number of rows and
120    * columns and the specified gaps.
121    * Note that the row and column settings cannot both be
122    * zero.  If both the row and column values are non-zero, the rows value
123    * takes precedence.
124    * @param rows Number of rows
125    * @param cols Number of columns
126    * @param hgap The horizontal gap
127    * @param vgap The vertical gap
128    * @exception IllegalArgumentException If rows and columns are both
129    *        0, if either are negative, or if either gap is negative
130    */
131   public GridLayout (int rows, int cols, int hgap, int vgap)
132   {
133     if (rows < 0)
134       throw new IllegalArgumentException ("number of rows cannot be negative");
135     if (cols < 0)
136       throw new IllegalArgumentException ("number of columns cannot be negative");
137     if (rows == 0 && cols == 0)
138       throw new IllegalArgumentException ("both rows and columns cannot be 0");
139     if (hgap < 0)
140       throw new IllegalArgumentException ("horizontal gap must be nonnegative");
141     if (vgap < 0)
142       throw new IllegalArgumentException ("vertical gap must be nonnegative");
143     this.rows = rows;
144     this.cols = cols;
145     this.hgap = hgap;
146     this.vgap = vgap;
147   }
148
149   /** Lay out the container's components based on current settings.
150    * The free space in the container is divided evenly into the specified
151    * number of rows and columns in this object.
152    * @param parent The container to lay out
153    */
154   public void layoutContainer (Container parent)
155   {
156     synchronized (parent.getTreeLock ())
157       {
158         int num = parent.ncomponents;
159
160         // There's no point, and handling this would mean adding special
161         // cases.
162         if (num == 0)
163           return;
164
165         // This is more efficient than calling getComponents().
166         Component[] comps = parent.component;
167
168         int real_rows = rows;
169         int real_cols = cols;
170         if (real_rows == 0)
171           real_rows = (num + real_cols - 1) / real_cols;
172         else
173           real_cols = (num + real_rows - 1) / real_rows;
174
175         // We might have less than a single row.  In this case we expand
176         // to fill.
177         if (num < real_cols)
178           real_cols = num;
179
180         Dimension d = parent.getSize ();
181         Insets ins = parent.getInsets ();
182
183         // Compute width and height of each cell in the grid.
184         int tw = d.width - ins.left - ins.right;
185         tw = (tw - (real_cols - 1) * hgap) / real_cols;
186         int th = d.height - ins.top - ins.bottom;
187         th = (th - (real_rows - 1) * vgap) / real_rows;
188
189         // If the cells are too small, still try to do something.
190         if (tw < 0)
191           tw = 1;
192         if (th < 0)
193           th = 1;
194
195         int x = ins.left;
196         int y = ins.top;
197         int i = 0;
198         int recount = 0;
199
200         while (i < num)
201           {
202             comps[i].setBounds (x, y, tw, th);
203
204             ++i;
205             ++recount;
206             if (recount == real_cols)
207               {
208                 recount = 0;
209                 y += vgap + th;
210                 x = ins.left;
211               }
212             else
213               x += hgap + tw;
214           }
215       }
216   }
217
218   /** Get the minimum layout size of the container.
219    * @param cont The parent container
220    */
221   public Dimension minimumLayoutSize (Container cont)
222   {
223     return getSize (cont, true);
224   }
225
226   /** Get the preferred layout size of the container.
227    * @param cont The parent container
228    */
229   public Dimension preferredLayoutSize (Container cont)
230   {
231     return getSize (cont, false);
232   }
233
234   /** Remove the indicated component from this layout manager.
235    * This particular implementation does nothing.
236    * @param comp The component to remove
237    */
238   public void removeLayoutComponent (Component comp)
239   {
240     // Nothing.
241   }
242
243   /** Set the number of columns.
244    * @param newCols
245    * @exception IllegalArgumentException If the number of columns is
246    *     negative, or if the number of columns is zero and the number
247    *     of rows is already 0.
248    */
249   public void setColumns (int newCols)
250   {
251     if (newCols < 0)
252       throw new IllegalArgumentException ("number of columns cannot be negative");
253     if (newCols == 0 && rows == 0)
254       throw new IllegalArgumentException ("number of rows is already 0");
255     this.cols = newCols;
256   }
257
258   /** Set the horizontal gap
259    * @param hgap The horizontal gap
260    * @exception IllegalArgumentException If the hgap value is less than zero.
261    */
262   public void setHgap (int hgap)
263   {
264     if (hgap < 0)
265       throw new IllegalArgumentException ("horizontal gap must be nonnegative");
266     this.hgap = hgap;
267   }
268
269   /** Set the number of rows
270    * @param newRows
271    * @exception IllegalArgumentException If the number of rows is
272    *     negative, or if the number of rows is zero and the number
273    *     of columns is already 0.
274    */
275   public void setRows (int newRows)
276   {
277     if (newRows < 0)
278       throw new IllegalArgumentException ("number of rows cannot be negative");
279     if (newRows == 0 && cols == 0)
280       throw new IllegalArgumentException ("number of columns is already 0");
281     this.rows = newRows;
282   }
283
284   /** Set the vertical gap.
285    * @param vgap The vertical gap
286    * @exception IllegalArgumentException If the vgap value is less than zero.
287    */
288   public void setVgap (int vgap)
289   {
290     if (vgap < 0)
291       throw new IllegalArgumentException ("vertical gap must be nonnegative");
292     this.vgap = vgap;
293   }
294
295   /** Return String description of this object.  */
296   public String toString ()
297   {
298     return ("[" + getClass ().getName ()
299             + ",hgap=" + hgap + ",vgap=" + vgap
300             + ",rows=" + rows + ",cols=" + cols
301             + "]");
302   }
303
304   // This method is used to compute the various sizes.
305   private Dimension getSize (Container parent, boolean is_min)
306   {
307     synchronized (parent.getTreeLock ())
308       {
309         int w = 0, h = 0, num = parent.ncomponents;
310         // This is more efficient than calling getComponents().
311         Component[] comps = parent.component;
312
313         for (int i = 0; i < num; ++i)
314           {
315             Dimension d;
316
317             if (is_min)
318               d = comps[i].getMinimumSize ();
319             else
320               d = comps[i].getPreferredSize ();
321
322             w = Math.max (d.width, w);
323             h = Math.max (d.height, h);
324           }
325
326         int real_rows = rows;
327         int real_cols = cols;
328         if (real_rows == 0)
329           real_rows = (num + real_cols - 1) / real_cols;
330         else
331           real_cols = (num + real_rows - 1) / real_rows;
332
333         Insets ins = parent.getInsets ();
334         // We subtract out an extra gap here because the gaps are only
335         // between cells.
336         w = ins.left + ins.right + real_cols * (w + hgap) - hgap;
337         h = ins.top + ins.bottom + real_rows * (h + vgap) - vgap;
338         return new Dimension (w, h);
339       }
340   }
341
342   /**
343    * @serial The number of columns in the grid.
344    */
345   private int cols;
346
347   /**
348    * @serial The number of rows in the grid.
349    */
350   private int rows;
351
352   /**
353    * @serial The horizontal gap between columns
354    */
355   private int hgap;
356
357   /**
358    * @serial The vertical gap between rows
359    */
360   private int vgap;
361 }