FlowLayout.java 9.7 KB

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