LCBLayout.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * --------------
  28. * LCBLayout.java
  29. * --------------
  30. * (C) Copyright 2000-2005, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * $Id: LCBLayout.java,v 1.5 2005/11/16 15:58:40 taqua Exp $
  36. *
  37. * Changes (from 26-Oct-2001)
  38. * --------------------------
  39. * 26-Oct-2001 : Changed package to com.jrefinery.layout.* (DG);
  40. * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41. */
  42. package org.jfree.layout;
  43. import java.awt.Component;
  44. import java.awt.Container;
  45. import java.awt.Dimension;
  46. import java.awt.Insets;
  47. import java.awt.LayoutManager;
  48. import java.io.Serializable;
  49. /**
  50. * Specialised layout manager for a grid of components.
  51. *
  52. * @author David Gilbert
  53. */
  54. public class LCBLayout implements LayoutManager, Serializable {
  55. /** For serialization. */
  56. private static final long serialVersionUID = -2531780832406163833L;
  57. /** A constant for the number of columns in the layout. */
  58. private static final int COLUMNS = 3;
  59. /** Tracks the column widths. */
  60. private int[] colWidth;
  61. /** Tracks the row heights. */
  62. private int[] rowHeight;
  63. /** The gap between each label and component. */
  64. private int labelGap;
  65. /** The gap between each component and button. */
  66. private int buttonGap;
  67. /** The gap between rows. */
  68. private int vGap;
  69. /**
  70. * Creates a new LCBLayout with the specified maximum number of rows.
  71. *
  72. * @param maxrows the maximum number of rows.
  73. */
  74. public LCBLayout(final int maxrows) {
  75. this.labelGap = 10;
  76. this.buttonGap = 6;
  77. this.vGap = 2;
  78. this.colWidth = new int[COLUMNS];
  79. this.rowHeight = new int[maxrows];
  80. }
  81. /**
  82. * Returns the preferred size using this layout manager.
  83. *
  84. * @param parent the parent.
  85. *
  86. * @return the preferred size using this layout manager.
  87. */
  88. public Dimension preferredLayoutSize(final Container parent) {
  89. synchronized (parent.getTreeLock()) {
  90. final Insets insets = parent.getInsets();
  91. final int ncomponents = parent.getComponentCount();
  92. final int nrows = ncomponents / COLUMNS;
  93. for (int c = 0; c < COLUMNS; c++) {
  94. for (int r = 0; r < nrows; r++) {
  95. final Component component
  96. = parent.getComponent(r * COLUMNS + c);
  97. final Dimension d = component.getPreferredSize();
  98. if (this.colWidth[c] < d.width) {
  99. this.colWidth[c] = d.width;
  100. }
  101. if (this.rowHeight[r] < d.height) {
  102. this.rowHeight[r] = d.height;
  103. }
  104. }
  105. }
  106. int totalHeight = this.vGap * (nrows - 1);
  107. for (int r = 0; r < nrows; r++) {
  108. totalHeight = totalHeight + this.rowHeight[r];
  109. }
  110. final int totalWidth = this.colWidth[0] + this.labelGap
  111. + this.colWidth[1] + this.buttonGap + this.colWidth[2];
  112. return new Dimension(
  113. insets.left + insets.right + totalWidth + this.labelGap
  114. + this.buttonGap,
  115. insets.top + insets.bottom + totalHeight + this.vGap
  116. );
  117. }
  118. }
  119. /**
  120. * Returns the minimum size using this layout manager.
  121. *
  122. * @param parent the parent.
  123. *
  124. * @return the minimum size using this layout manager.
  125. */
  126. public Dimension minimumLayoutSize(final Container parent) {
  127. synchronized (parent.getTreeLock()) {
  128. final Insets insets = parent.getInsets();
  129. final int ncomponents = parent.getComponentCount();
  130. final int nrows = ncomponents / COLUMNS;
  131. for (int c = 0; c < COLUMNS; c++) {
  132. for (int r = 0; r < nrows; r++) {
  133. final Component component
  134. = parent.getComponent(r * COLUMNS + c);
  135. final Dimension d = component.getMinimumSize();
  136. if (this.colWidth[c] < d.width) {
  137. this.colWidth[c] = d.width;
  138. }
  139. if (this.rowHeight[r] < d.height) {
  140. this.rowHeight[r] = d.height;
  141. }
  142. }
  143. }
  144. int totalHeight = this.vGap * (nrows - 1);
  145. for (int r = 0; r < nrows; r++) {
  146. totalHeight = totalHeight + this.rowHeight[r];
  147. }
  148. final int totalWidth = this.colWidth[0] + this.labelGap
  149. + this.colWidth[1] + this.buttonGap + this.colWidth[2];
  150. return new Dimension(
  151. insets.left + insets.right + totalWidth + this.labelGap
  152. + this.buttonGap,
  153. insets.top + insets.bottom + totalHeight + this.vGap
  154. );
  155. }
  156. }
  157. /**
  158. * Lays out the components.
  159. *
  160. * @param parent the parent.
  161. */
  162. public void layoutContainer(final Container parent) {
  163. synchronized (parent.getTreeLock()) {
  164. final Insets insets = parent.getInsets();
  165. final int ncomponents = parent.getComponentCount();
  166. final int nrows = ncomponents / COLUMNS;
  167. for (int c = 0; c < COLUMNS; c++) {
  168. for (int r = 0; r < nrows; r++) {
  169. final Component component
  170. = parent.getComponent(r * COLUMNS + c);
  171. final Dimension d = component.getPreferredSize();
  172. if (this.colWidth[c] < d.width) {
  173. this.colWidth[c] = d.width;
  174. }
  175. if (this.rowHeight[r] < d.height) {
  176. this.rowHeight[r] = d.height;
  177. }
  178. }
  179. }
  180. int totalHeight = this.vGap * (nrows - 1);
  181. for (int r = 0; r < nrows; r++) {
  182. totalHeight = totalHeight + this.rowHeight[r];
  183. }
  184. final int totalWidth = this.colWidth[0] + this.colWidth[1]
  185. + this.colWidth[2];
  186. // adjust the width of the second column to use up all of parent
  187. final int available = parent.getWidth() - insets.left
  188. - insets.right - this.labelGap - this.buttonGap;
  189. this.colWidth[1] = this.colWidth[1] + (available - totalWidth);
  190. // *** DO THE LAYOUT ***
  191. int x = insets.left;
  192. for (int c = 0; c < COLUMNS; c++) {
  193. int y = insets.top;
  194. for (int r = 0; r < nrows; r++) {
  195. final int i = r * COLUMNS + c;
  196. if (i < ncomponents) {
  197. final Component component = parent.getComponent(i);
  198. final Dimension d = component.getPreferredSize();
  199. final int h = d.height;
  200. final int adjust = (this.rowHeight[r] - h) / 2;
  201. parent.getComponent(i).setBounds(x, y + adjust,
  202. this.colWidth[c], h);
  203. }
  204. y = y + this.rowHeight[r] + this.vGap;
  205. }
  206. x = x + this.colWidth[c];
  207. if (c == 0) {
  208. x = x + this.labelGap;
  209. }
  210. if (c == 1) {
  211. x = x + this.buttonGap;
  212. }
  213. }
  214. }
  215. }
  216. /**
  217. * Not used.
  218. *
  219. * @param comp the component.
  220. */
  221. public void addLayoutComponent(final Component comp) {
  222. // not used
  223. }
  224. /**
  225. * Not used.
  226. *
  227. * @param comp the component.
  228. */
  229. public void removeLayoutComponent(final Component comp) {
  230. // not used
  231. }
  232. /**
  233. * Not used.
  234. *
  235. * @param name the component name.
  236. * @param comp the component.
  237. */
  238. public void addLayoutComponent(final String name, final Component comp) {
  239. // not used
  240. }
  241. /**
  242. * Not used.
  243. *
  244. * @param name the component name.
  245. * @param comp the component.
  246. */
  247. public void removeLayoutComponent(final String name, final Component comp) {
  248. // not used
  249. }
  250. }