1 // GridLayout.java - Grid-based layout engine
3 /* Copyright (C) 1999, 2000, 2002 Free Software Foundation
5 This file is part of GNU Classpath.
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)
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.
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
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
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. */
42 import java.io.Serializable;
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
56 * @author Tom Tromey <tromey@redhat.com>
57 * @author Aaron M. Renn (arenn@urbanophile.com)
59 public class GridLayout implements LayoutManager, Serializable
61 static final long serialVersionUID = -7411804673224730901L;
63 /** Add a new component to the layout. This particular implementation
65 * @param name The name of the component to add.
66 * @param component The component to add.
68 public void addLayoutComponent (String name, Component comp)
73 /** Return the number of columns in this layout. */
74 public int getColumns ()
79 /** Return the horizontal gap. */
85 /** Return the number of rows in this layout. */
91 /** Return the vertical gap. */
97 /** Create a new <code>GridLayout</code> with one row and any number
98 * of columns. Both gaps are set to 0.
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
114 public GridLayout (int rows, int cols)
116 this (rows, cols, 0, 0);
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
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
131 public GridLayout (int rows, int cols, int hgap, int vgap)
134 throw new IllegalArgumentException ("number of rows cannot be negative");
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");
140 throw new IllegalArgumentException ("horizontal gap must be nonnegative");
142 throw new IllegalArgumentException ("vertical gap must be nonnegative");
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
154 public void layoutContainer (Container parent)
156 synchronized (parent.getTreeLock ())
158 int num = parent.ncomponents;
160 // There's no point, and handling this would mean adding special
165 // This is more efficient than calling getComponents().
166 Component[] comps = parent.component;
168 int real_rows = rows;
169 int real_cols = cols;
171 real_rows = (num + real_cols - 1) / real_cols;
173 real_cols = (num + real_rows - 1) / real_rows;
175 // We might have less than a single row. In this case we expand
180 Dimension d = parent.getSize ();
181 Insets ins = parent.getInsets ();
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;
189 // If the cells are too small, still try to do something.
202 comps[i].setBounds (x, y, tw, th);
206 if (recount == real_cols)
218 /** Get the minimum layout size of the container.
219 * @param cont The parent container
221 public Dimension minimumLayoutSize (Container cont)
223 return getSize (cont, true);
226 /** Get the preferred layout size of the container.
227 * @param cont The parent container
229 public Dimension preferredLayoutSize (Container cont)
231 return getSize (cont, false);
234 /** Remove the indicated component from this layout manager.
235 * This particular implementation does nothing.
236 * @param comp The component to remove
238 public void removeLayoutComponent (Component comp)
243 /** Set the number of columns.
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.
249 public void setColumns (int newCols)
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");
258 /** Set the horizontal gap
259 * @param hgap The horizontal gap
260 * @exception IllegalArgumentException If the hgap value is less than zero.
262 public void setHgap (int hgap)
265 throw new IllegalArgumentException ("horizontal gap must be nonnegative");
269 /** Set the number of rows
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.
275 public void setRows (int newRows)
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");
284 /** Set the vertical gap.
285 * @param vgap The vertical gap
286 * @exception IllegalArgumentException If the vgap value is less than zero.
288 public void setVgap (int vgap)
291 throw new IllegalArgumentException ("vertical gap must be nonnegative");
295 /** Return String description of this object. */
296 public String toString ()
298 return ("[" + getClass ().getName ()
299 + ",hgap=" + hgap + ",vgap=" + vgap
300 + ",rows=" + rows + ",cols=" + cols
304 // This method is used to compute the various sizes.
305 private Dimension getSize (Container parent, boolean is_min)
307 synchronized (parent.getTreeLock ())
309 int w = 0, h = 0, num = parent.ncomponents;
310 // This is more efficient than calling getComponents().
311 Component[] comps = parent.component;
313 for (int i = 0; i < num; ++i)
318 d = comps[i].getMinimumSize ();
320 d = comps[i].getPreferredSize ();
322 w = Math.max (d.width, w);
323 h = Math.max (d.height, h);
326 int real_rows = rows;
327 int real_cols = cols;
329 real_rows = (num + real_cols - 1) / real_cols;
331 real_cols = (num + real_rows - 1) / real_rows;
333 Insets ins = parent.getInsets ();
334 // We subtract out an extra gap here because the gaps are only
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);
343 * @serial The number of columns in the grid.
348 * @serial The number of rows in the grid.
353 * @serial The horizontal gap between columns
358 * @serial The vertical gap between rows