BoxLayout.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* BoxLayout.java -- A layout for swing components.
  2. Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 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 javax.swing;
  32. import java.awt.AWTError;
  33. import java.awt.Component;
  34. import java.awt.ComponentOrientation;
  35. import java.awt.Container;
  36. import java.awt.Dimension;
  37. import java.awt.Insets;
  38. import java.awt.LayoutManager2;
  39. import java.io.Serializable;
  40. /**
  41. * A layout that stacks the children of a container in a Box, either
  42. * horizontally or vertically.
  43. *
  44. * @author Ronald Veldema (rveldema@cs.vu.nl)
  45. * @author Roman Kennke (roman@kennke.org)
  46. */
  47. public class BoxLayout implements LayoutManager2, Serializable
  48. {
  49. /**
  50. * Specifies that components are laid out left to right.
  51. */
  52. public static final int X_AXIS = 0;
  53. /**
  54. * Specifies that components are laid out top to bottom.
  55. */
  56. public static final int Y_AXIS = 1;
  57. /**
  58. * Specifies that components are laid out in the direction of a line of text.
  59. */
  60. public static final int LINE_AXIS = 2;
  61. /**
  62. * Sepcifies that components are laid out in the direction of the line flow.
  63. */
  64. public static final int PAGE_AXIS = 3;
  65. /*
  66. * Needed for serialization.
  67. */
  68. private static final long serialVersionUID = -2474455742719112368L;
  69. /*
  70. * The container given to the constructor.
  71. */
  72. private Container container;
  73. /**
  74. * Current type of component layouting. Defaults to X_AXIS.
  75. */
  76. private int way = X_AXIS;
  77. /**
  78. * The size requirements of the containers children for the X direction.
  79. */
  80. private SizeRequirements[] xChildren;
  81. /**
  82. * The size requirements of the containers children for the Y direction.
  83. */
  84. private SizeRequirements[] yChildren;
  85. /**
  86. * The size requirements of the container to be laid out for the X direction.
  87. */
  88. private SizeRequirements xTotal;
  89. /**
  90. * The size requirements of the container to be laid out for the Y direction.
  91. */
  92. private SizeRequirements yTotal;
  93. /**
  94. * The offsets of the child components in the X direction.
  95. */
  96. private int[] offsetsX;
  97. /**
  98. * The offsets of the child components in the Y direction.
  99. */
  100. private int[] offsetsY;
  101. /**
  102. * The spans of the child components in the X direction.
  103. */
  104. private int[] spansX;
  105. /**
  106. * The spans of the child components in the Y direction.
  107. */
  108. private int[] spansY;
  109. /**
  110. * Constructs a <code>BoxLayout</code> object.
  111. *
  112. * @param container The container that needs to be laid out.
  113. * @param way The orientation of the components.
  114. *
  115. * @exception AWTError If way has an invalid value.
  116. */
  117. public BoxLayout(Container container, int way)
  118. {
  119. if (way != X_AXIS && way != Y_AXIS && way != LINE_AXIS && way != PAGE_AXIS)
  120. throw new AWTError("Invalid axis");
  121. int width = 0;
  122. int height = 0;
  123. this.container = container;
  124. this.way = way;
  125. }
  126. /**
  127. * Adds a component to the layout. Not used in BoxLayout.
  128. *
  129. * @param name The name of the component to add.
  130. * @param component the component to add to the layout.
  131. */
  132. public void addLayoutComponent(String name, Component component)
  133. {
  134. // Nothing to do here.
  135. }
  136. /**
  137. * Removes a component from the layout. Not used in BoxLayout.
  138. *
  139. * @param component The component to remove from the layout.
  140. */
  141. public void removeLayoutComponent(Component component)
  142. {
  143. // Nothing to do here.
  144. }
  145. private boolean isHorizontalIn(Container parent)
  146. {
  147. ComponentOrientation orientation = parent.getComponentOrientation();
  148. return this.way == X_AXIS
  149. || (this.way == LINE_AXIS
  150. && orientation.isHorizontal())
  151. || (this.way == PAGE_AXIS
  152. && (!orientation.isHorizontal()));
  153. }
  154. /**
  155. * Returns the preferred size of the layout.
  156. *
  157. * @param parent The container that needs to be laid out.
  158. *
  159. * @return The dimension of the layout.
  160. */
  161. public Dimension preferredLayoutSize(Container parent)
  162. {
  163. synchronized (container.getTreeLock())
  164. {
  165. if (container != parent)
  166. throw new AWTError("BoxLayout can't be shared");
  167. checkTotalRequirements();
  168. Insets i = container.getInsets();
  169. return new Dimension(xTotal.preferred + i.left + i.right,
  170. yTotal.preferred + i.top + i.bottom);
  171. }
  172. }
  173. /**
  174. * Returns the minimum size of the layout.
  175. *
  176. * @param parent The container that needs to be laid out.
  177. *
  178. * @return The dimension of the layout.
  179. */
  180. public Dimension minimumLayoutSize(Container parent)
  181. {
  182. synchronized (container.getTreeLock())
  183. {
  184. if (container != parent)
  185. throw new AWTError("BoxLayout can't be shared");
  186. checkTotalRequirements();
  187. Insets i = container.getInsets();
  188. return new Dimension(xTotal.minimum + i.left + i.right,
  189. yTotal.minimum + i.top + i.bottom);
  190. }
  191. }
  192. /**
  193. * Lays out the specified container using this layout.
  194. *
  195. * @param parent The container that needs to be laid out.
  196. */
  197. public void layoutContainer(Container parent)
  198. {
  199. synchronized (container.getTreeLock())
  200. {
  201. if (container != parent)
  202. throw new AWTError("BoxLayout can't be shared");
  203. checkLayout();
  204. Component[] children = container.getComponents();
  205. Insets in = container.getInsets();
  206. for (int i = 0; i < children.length; i++)
  207. children[i].setBounds(offsetsX[i] + in.left, offsetsY[i] + in.top,
  208. spansX[i], spansY[i]);
  209. }
  210. }
  211. /**
  212. * Adds a component to the layout. Not used in BoxLayout
  213. *
  214. * @param child The component to add to the layout.
  215. * @param constraints The constraints for the component in the layout.
  216. */
  217. public void addLayoutComponent(Component child, Object constraints)
  218. {
  219. // Nothing to do here.
  220. }
  221. /**
  222. * Returns the alignment along the X axis for the container.
  223. *
  224. * @param parent The container that needs to be laid out.
  225. *
  226. * @return The alignment.
  227. */
  228. public float getLayoutAlignmentX(Container parent)
  229. {
  230. synchronized (container.getTreeLock())
  231. {
  232. if (container != parent)
  233. throw new AWTError("BoxLayout can't be shared");
  234. checkTotalRequirements();
  235. return xTotal.alignment;
  236. }
  237. }
  238. /**
  239. * Returns the alignment along the Y axis for the container.
  240. *
  241. * @param parent The container that needs to be laid out.
  242. *
  243. * @return The alignment.
  244. */
  245. public float getLayoutAlignmentY(Container parent)
  246. {
  247. synchronized (container.getTreeLock())
  248. {
  249. if (container != parent)
  250. throw new AWTError("BoxLayout can't be shared");
  251. checkTotalRequirements();
  252. return yTotal.alignment;
  253. }
  254. }
  255. /**
  256. * Invalidates the layout.
  257. *
  258. * @param parent The container that needs to be laid out.
  259. */
  260. public void invalidateLayout(Container parent)
  261. {
  262. if (container != parent)
  263. throw new AWTError("BoxLayout can't be shared");
  264. synchronized (container.getTreeLock())
  265. {
  266. xChildren = null;
  267. yChildren = null;
  268. xTotal = null;
  269. yTotal = null;
  270. offsetsX = null;
  271. offsetsY = null;
  272. spansX = null;
  273. spansY = null;
  274. }
  275. }
  276. /**
  277. * Returns the maximum size of the layout gived the components
  278. * in the given container.
  279. *
  280. * @param parent The container that needs to be laid out.
  281. *
  282. * @return The dimension of the layout.
  283. */
  284. public Dimension maximumLayoutSize(Container parent)
  285. {
  286. synchronized (container.getTreeLock())
  287. {
  288. if (container != parent)
  289. throw new AWTError("BoxLayout can't be shared");
  290. checkTotalRequirements();
  291. Insets i = container.getInsets();
  292. int xDim = xTotal.maximum + i.left + i.right;
  293. int yDim = yTotal.maximum + i.top + i.bottom;
  294. // Check for overflow
  295. if (xDim < xTotal.maximum)
  296. xDim = Integer.MAX_VALUE;
  297. if (yDim < yTotal.maximum)
  298. yDim = Integer.MAX_VALUE;
  299. return new Dimension(xDim, yDim);
  300. }
  301. }
  302. /**
  303. * Makes sure that the xTotal and yTotal fields are set up correctly. A call
  304. * to {@link #invalidateLayout} sets these fields to null and they have to be
  305. * recomputed.
  306. */
  307. private void checkTotalRequirements()
  308. {
  309. if (xTotal == null || yTotal == null)
  310. {
  311. checkRequirements();
  312. if (isHorizontalIn(container))
  313. {
  314. xTotal = SizeRequirements.getTiledSizeRequirements(xChildren);
  315. yTotal = SizeRequirements.getAlignedSizeRequirements(yChildren);
  316. }
  317. else
  318. {
  319. xTotal = SizeRequirements.getAlignedSizeRequirements(xChildren);
  320. yTotal = SizeRequirements.getTiledSizeRequirements(yChildren);
  321. }
  322. }
  323. }
  324. /**
  325. * Makes sure that the xChildren and yChildren fields are correctly set up.
  326. * A call to {@link #invalidateLayout(Container)} sets these fields to null,
  327. * so they have to be set up again.
  328. */
  329. private void checkRequirements()
  330. {
  331. if (xChildren == null || yChildren == null)
  332. {
  333. Component[] children = container.getComponents();
  334. xChildren = new SizeRequirements[children.length];
  335. yChildren = new SizeRequirements[children.length];
  336. for (int i = 0; i < children.length; i++)
  337. {
  338. if (! children[i].isVisible())
  339. {
  340. xChildren[i] = new SizeRequirements();
  341. yChildren[i] = new SizeRequirements();
  342. }
  343. else
  344. {
  345. xChildren[i] =
  346. new SizeRequirements(children[i].getMinimumSize().width,
  347. children[i].getPreferredSize().width,
  348. children[i].getMaximumSize().width,
  349. children[i].getAlignmentX());
  350. yChildren[i] =
  351. new SizeRequirements(children[i].getMinimumSize().height,
  352. children[i].getPreferredSize().height,
  353. children[i].getMaximumSize().height,
  354. children[i].getAlignmentY());
  355. }
  356. }
  357. }
  358. }
  359. /**
  360. * Makes sure that the offsetsX, offsetsY, spansX and spansY fields are set
  361. * up correctly. A call to {@link #invalidateLayout} sets these fields
  362. * to null and they have to be recomputed.
  363. */
  364. private void checkLayout()
  365. {
  366. if (offsetsX == null || offsetsY == null || spansX == null
  367. || spansY == null)
  368. {
  369. checkRequirements();
  370. checkTotalRequirements();
  371. int len = container.getComponents().length;
  372. offsetsX = new int[len];
  373. offsetsY = new int[len];
  374. spansX = new int[len];
  375. spansY = new int[len];
  376. Insets in = container.getInsets();
  377. int width = container.getWidth() - in.left - in.right;
  378. int height = container.getHeight() - in.top - in.bottom;
  379. if (isHorizontalIn(container))
  380. {
  381. SizeRequirements.calculateTiledPositions(width,
  382. xTotal, xChildren,
  383. offsetsX, spansX);
  384. SizeRequirements.calculateAlignedPositions(height,
  385. yTotal, yChildren,
  386. offsetsY, spansY);
  387. }
  388. else
  389. {
  390. SizeRequirements.calculateAlignedPositions(width,
  391. xTotal, xChildren,
  392. offsetsX, spansX);
  393. SizeRequirements.calculateTiledPositions(height,
  394. yTotal, yChildren,
  395. offsetsY, spansY);
  396. }
  397. }
  398. }
  399. }