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