FlowLayout.java 9.74 KB
Newer Older
1
// FlowLayout.java - Grid-based layout engine
Tom Tromey committed
2

3
/* Copyright (C) 1999, 2000, 2001, 2002  Free Software Foundation
Tom Tromey committed
4

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */
Tom Tromey committed
38 39 40 41 42 43 44 45 46 47 48


package java.awt;

import java.io.Serializable;

/** This class implements a flow-based layout.  Components are laid
 * out in order from left to right.  When a component cannot be placed
 * without horizontal clipping, a new row is started.  This class
 * supports horizontal and vertical gaps.  These are used for spacing
 * between components.
49 50 51
 *
 * @author Tom Tromey <tromey@redhat.com>
 * @author Aaron M. Renn (arenn@urbanophile.com)
Tom Tromey committed
52 53 54 55 56 57 58 59 60 61
 */
public class FlowLayout implements LayoutManager, Serializable
{
  /** Constant that specifies left alignment.  */
  public static final int LEFT = 0;
  /** Constant that specifies center alignment.  */
  public static final int CENTER = 1;
  /** Constant that specifies right alignment.  */
  public static final int RIGHT = 2;

62 63 64 65 66 67 68
  /** Constant that specifies alignment to leading edge of container's
   * orientation.  */
  public static final int LEADING = 3;
  /** Constant that specifies alignment to trailing edge of container's
   * orientation.  */
  public static final int TRAILING = 4;

69 70 71
  // Serialization constant
  private static final long serialVersionUID = -7262534875583282631L;

Tom Tromey committed
72 73 74 75 76 77 78 79
  /** Add a new component to the layout.  This particular implementation
   * does nothing.
   */
  public void addLayoutComponent (String name, Component comp)
  {
    // Nothing.
  }

80 81 82 83 84
  /**
   * Returns the current justification value for this object.
   *
   * @return The current justification value for this object.
   */
Tom Tromey committed
85 86 87 88 89
  public int getAlignment ()
  {
    return align;
  }

90 91 92 93 94
  /**
   * Returns the horizontal gap between components.
   *
   * @return The horizontal gap between components.
   */
Tom Tromey committed
95 96 97 98 99
  public int getHgap ()
  {
    return hgap;
  }

100 101 102 103 104
  /**
   * Returns the vertical gap between lines of components.
   *
   * @return The vertical gap between lines of components.
   */
Tom Tromey committed
105 106 107 108 109
  public int getVgap ()
  {
    return vgap;
  }

110 111 112
  /**
   * Initializes a new instance of <code>FlowLayout</code> with a center
   * justification and a default horizontal and vertical gap of 5.
Tom Tromey committed
113 114 115
   */
  public FlowLayout ()
  {
116
    this (CENTER, 5, 5);
Tom Tromey committed
117 118
  }

119 120 121 122 123 124
  /**
   * Initializes a new instance of <code>FlowLayout</code> with the specified
   * justification and a default horizontal and vertical gap of 5.
   *
   * @param align The justification setting, which should be one of the
   * contants in this class.
Tom Tromey committed
125 126 127
   */
  public FlowLayout (int align)
  {
128
    this (align, 5, 5);
Tom Tromey committed
129 130
  }

131 132 133
  /**
   * Initializes a new instance of <code>FlowLayout</code> with the specified
   * justification and gap values
Tom Tromey committed
134 135 136 137 138 139 140
   * @param align Alignment
   * @param hgap The horizontal gap
   * @param vgap The vertical gap
   * @exception IllegalArgumentException If either gap is negative
   */
  public FlowLayout (int align, int hgap, int vgap)
  {
141 142 143 144 145
    // Use methods to set fields so that we can have all the checking
    // in one place.
    setVgap (vgap);
    setHgap (hgap);
    setAlignment (align);
Tom Tromey committed
146 147 148 149 150 151 152
  }

  /** Lay out the container's components based on current settings.
   * @param parent The parent container
   */
  public void layoutContainer (Container parent)
  {
153 154 155 156 157
    synchronized (parent.getTreeLock ())
      {
	int num = parent.getComponentCount ();
	// This is more efficient than calling getComponents().
	Component[] comps = parent.component;
Tom Tromey committed
158

159 160
	Dimension d = parent.getSize ();
	Insets ins = parent.getInsets ();
Tom Tromey committed
161

162 163
	ComponentOrientation orient = parent.getComponentOrientation ();
	boolean left_to_right = orient.isLeftToRight ();
164

165 166 167
	int y = ins.top + vgap;
	int i = 0;
	while (i < num)
Tom Tromey committed
168
	  {
169 170 171 172 173
	    // Find the components which go in the current row.
	    int new_w = ins.left + hgap + ins.right;
	    int new_h = 0;
	    int j;
	    boolean found_one = false;
174
	    for (j = i; j < num; ++j)
175
	      {
176
		// Skip invisible items.
177
		if (! comps[j].visible)
178 179
		  continue;

180
		Dimension c = comps[j].getPreferredSize ();
181 182 183 184 185 186 187 188 189 190 191 192 193

		int next_w = new_w + hgap + c.width;
		if (next_w <= d.width || ! found_one)
		  {
		    new_w = next_w;
		    new_h = Math.max (new_h, c.height);
		    found_one = true;
		  }
		else
		  {
		    // Must start a new row, and we already found an item
		    break;
		  }
194
	      }
Tom Tromey committed
195

196 197
	    // Set the location of each component for this row.
	    int x;
198

199 200 201 202 203
	    int myalign = align;
	    if (align == LEADING)
	      myalign = left_to_right ? LEFT : RIGHT;
	    else if (align == TRAILING)
	      myalign = left_to_right ? RIGHT : LEFT;
204

205 206 207
	    if (myalign == LEFT)
	      x = ins.left + hgap;
	    else if (myalign == CENTER)
208
	      x = ins.left + (d.width - new_w) / 2 + hgap;
209
	    else
210
	      x = ins.left + (d.width - new_w) + hgap;
211

212
	    for (int k = i; k < j; ++k)
213
	      {
214 215 216 217 218 219
		if (comps[k].visible)
		  {
		    Dimension c = comps[k].getPreferredSize ();
		    comps[k].setBounds (x, y, c.width, new_h);
		    x += c.width + hgap;
		  }
220
	      }
Tom Tromey committed
221

222 223 224 225
	    // Advance to next row.
	    i = j;
	    y += new_h + vgap;
	  }
Tom Tromey committed
226 227 228
      }
  }

229 230 231
  /**
   * Returns the minimum layout size for the specified container using
   * this layout.
Tom Tromey committed
232
   * @param cont The parent container
233
   * @return The minimum layout size.
Tom Tromey committed
234 235 236 237 238 239
   */
  public Dimension minimumLayoutSize (Container cont)
  {
    return getSize (cont, true);
  }

240 241 242
  /**
   * Returns the preferred layout size for the specified container using
   * this layout.
Tom Tromey committed
243
   * @param cont The parent container
244
   * @return The preferred layout size.
Tom Tromey committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
   */
  public Dimension preferredLayoutSize (Container cont)
  {
    return getSize (cont, false);
  }

  /** Remove the indicated component from this layout manager.
   * This particular implementation does nothing.
   * @param comp The component to remove
   */
  public void removeLayoutComponent (Component comp)
  {
    // Nothing.
  }

260 261 262 263 264
  /**
   * Sets the justification value for this object to the specified value.
   *
   * @param align The new justification value for this object, which must
   * be one of the constants in this class.
Tom Tromey committed
265 266 267
   */
  public void setAlignment (int align)
  {
268 269
    if (align != LEFT && align != RIGHT && align != CENTER
	&& align != LEADING && align != TRAILING)
270
      throw new IllegalArgumentException ("invalid alignment: " + align);
Tom Tromey committed
271 272 273
    this.align = align;
  }

274 275 276 277
  /**
   * Sets the horizontal gap between components to the specified value.
   *
   * @param hgap The new horizontal gap between components.
Tom Tromey committed
278 279 280 281 282 283 284 285
   */
  public void setHgap (int hgap)
  {
    if (hgap < 0)
      throw new IllegalArgumentException ("horizontal gap must be nonnegative");
    this.hgap = hgap;
  }

286 287 288 289
  /**
   * Sets the vertical gap between lines of components to the specified value.
   *
   * @param vgap The new vertical gap.
Tom Tromey committed
290 291 292 293 294 295 296 297
   */
  public void setVgap (int vgap)
  {
    if (vgap < 0)
      throw new IllegalArgumentException ("vertical gap must be nonnegative");
    this.vgap = vgap;
  }

298 299 300
  /** Return String description of this object.
   * @return A string representation of this object.
   */
Tom Tromey committed
301 302 303 304 305 306 307 308 309
  public String toString ()
  {
    return ("[" + getClass ().getName () + ",hgap=" + hgap + ",vgap=" + vgap
	    + ",align=" + align + "]");
  }

  // This method is used to compute the various sizes.
  private Dimension getSize (Container parent, boolean is_min)
  {
310
    synchronized (parent.getTreeLock ())
Tom Tromey committed
311
      {
312 313 314
	int w, h, num = parent.getComponentCount ();
	// This is more efficient than calling getComponents().
	Component[] comps = parent.component;
315

316 317 318 319 320 321
	w = 0;
	h = 0;
	for (int i = 0; i < num; ++i)
	  {
	    if (! comps[i].visible)
	      continue;
Tom Tromey committed
322

323 324 325
	    // FIXME: can we just directly read the fields in Component?
	    // Or will that not work with subclassing?
	    Dimension d;
Tom Tromey committed
326

327 328 329 330
	    if (is_min)
	      d = comps[i].getMinimumSize ();
	    else
	      d = comps[i].getPreferredSize ();
Tom Tromey committed
331

332 333 334
	    w += d.width;
	    h = Math.max (d.height, h);
	  }
Tom Tromey committed
335

336
	Insets ins = parent.getInsets ();
Tom Tromey committed
337

338 339 340 341 342
	w += (num + 1) * hgap + ins.left + ins.right;
	h += 2 * vgap + ins.top + ins.bottom;

	return new Dimension (w, h);
      }
Tom Tromey committed
343 344
  }

345 346 347 348
  /**
   * @serial The justification alignment of the lines of components, which
   * will be one of the constants defined in this class.
   */
Tom Tromey committed
349
  private int align;
350 351 352 353

  /**
   * @serial The horizontal gap between components.
   */
Tom Tromey committed
354
  private int hgap;
355 356 357 358

  /**
   * @serial The vertical gap between lines of components.
   */
Tom Tromey committed
359 360
  private int vgap;
}