PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/gui/VariableGridLayout.java

#
Java | 332 lines | 183 code | 52 blank | 97 comment | 40 complexity | e809a3c56e47162c5ddca09b2d0d6c80 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * VariableGridLayout.java - a grid layout manager with variable cell sizes
  3. *
  4. * Originally written by Dirk Moebius for the jEdit project. This work has been
  5. * placed into the public domain. You may use this work in any way and for any
  6. * purpose you wish.
  7. *
  8. * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, NOT EVEN THE
  9. * IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, ASSUMES
  10. * _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE RESULTING FROM THE USE, MODIFICATION,
  11. * OR REDISTRIBUTION OF THIS SOFTWARE.
  12. */
  13. package org.gjt.sp.jedit.gui;
  14. import java.awt.*;
  15. /**
  16. * The <code>VariableGridLayout</code> class is a layout manager
  17. * that lays out a container's components in a rectangular grid
  18. * with variable cell sizes.<p>
  19. *
  20. * The container is divided into rectangles, and one component is placed
  21. * in each rectangle. Each row is as large as the largest component in
  22. * that row, and each column is as wide as the widest component in
  23. * that column.<p>
  24. *
  25. * This behavior is basically the same as in
  26. * <code>java.awt.GridLayout</code>, but with different row heights and
  27. * column widths for each row/column.<p>
  28. *
  29. * For example, the following is an applet that lays out six buttons
  30. * into three rows and two columns:<p>
  31. *
  32. * <blockquote><pre>
  33. * import java.awt.*;
  34. * import java.applet.Applet;
  35. * public class ButtonGrid extends Applet {
  36. * public void init() {
  37. * setLayout(new VariableGridLayout(VariableGridLayout.FIXED_NUM_COLUMNS, 2));
  38. * add(new Button("1"));
  39. * add(new Button("2"));
  40. * add(new Button("3"));
  41. * add(new Button("4"));
  42. * add(new Button("5"));
  43. * add(new Button("6"));
  44. * }
  45. * }
  46. * </pre></blockquote><p>
  47. *
  48. * <b>Programmer's remark:</b> VariableGridLayout could be faster, if it would
  49. * reside in the package java.awt, because then it could access some
  50. * package private fields of <code>Container</code> or
  51. * <code>Component</code>. Instead, it has to call
  52. * <code>Component.getSize()</code>,
  53. * which allocates memory on the heap.<p>
  54. *
  55. * <b>Todo:</b>
  56. * <ul>
  57. * <li>Use alignmentX/Y property if the grid cell is larger than the preferred size of the component.
  58. * <li>Ability to span components over more than one cell horizontally
  59. * </ul>
  60. *
  61. * @author Dirk Moebius
  62. * @version 1.0
  63. * @see java.awt.GridLayout
  64. */
  65. public class VariableGridLayout implements LayoutManager2, java.io.Serializable
  66. {
  67. public static final int FIXED_NUM_ROWS = 1;
  68. public static final int FIXED_NUM_COLUMNS = 2;
  69. public VariableGridLayout(int mode, int size, int hgap, int vgap) {
  70. if (mode != FIXED_NUM_ROWS && mode != FIXED_NUM_COLUMNS) {
  71. throw new IllegalArgumentException("illegal mode; value is " + mode);
  72. }
  73. if (size <= 0) {
  74. throw new IllegalArgumentException("size cannot be zero or less; value is " + size);
  75. }
  76. if (hgap < 0) {
  77. throw new IllegalArgumentException("hgap cannot be negative; value is " + hgap);
  78. }
  79. if (vgap < 0) {
  80. throw new IllegalArgumentException("vgap cannot be negative; value is " + vgap);
  81. }
  82. this.mode = mode;
  83. this.size = size;
  84. this.hgap = hgap;
  85. this.vgap = vgap;
  86. }
  87. /**
  88. * Creates a variable grid layout manager with the specified mode
  89. * and zero horizontal and vertical gap.
  90. */
  91. public VariableGridLayout(int mode, int size) {
  92. this(mode, size, 0, 0);
  93. }
  94. /**
  95. * Creates a variable grid layout manager with mode FIXED_NUM_ROWS,
  96. * number of rows == 1 and zero horizontal and vertical gap.
  97. */
  98. public VariableGridLayout() {
  99. this(FIXED_NUM_ROWS, 1, 0, 0);
  100. }
  101. /**
  102. * Not used in this class.
  103. */
  104. public void addLayoutComponent(String name, Component component) { }
  105. /**
  106. * Not used in this class.
  107. */
  108. public void addLayoutComponent(Component component, Object constraints) { }
  109. /**
  110. * Not used in this class.
  111. */
  112. public void removeLayoutComponent(Component component) { }
  113. /**
  114. * Always returns 0.5.
  115. */
  116. public float getLayoutAlignmentX(Container container) {
  117. return 0.5f;
  118. }
  119. /**
  120. * Always returns 0.5.
  121. */
  122. public float getLayoutAlignmentY(Container container) {
  123. return 0.5f;
  124. }
  125. public Dimension preferredLayoutSize(Container parent) {
  126. return getLayoutSize(parent, 2);
  127. }
  128. public Dimension minimumLayoutSize(Container parent) {
  129. return getLayoutSize(parent, 0);
  130. }
  131. public Dimension maximumLayoutSize(Container parent) {
  132. return getLayoutSize(parent, 1);
  133. }
  134. public void layoutContainer(Container parent) {
  135. synchronized (parent.getTreeLock()) {
  136. update(parent);
  137. int ncomponents = parent.getComponentCount();
  138. if (ncomponents == 0) {
  139. return;
  140. }
  141. // Pass 1: compute preferred row heights / column widths
  142. int total_height = 0;
  143. for (int r = 0, i = 0; r < nrows; r++) {
  144. for (int c = 0; c < ncols; c++, i++) {
  145. if (i < ncomponents) {
  146. Dimension d = parent.getComponent(i).getPreferredSize();
  147. row_heights[r] = Math.max(row_heights[r], d.height);
  148. col_widths[c] = Math.max(col_widths[c], d.width);
  149. } else {
  150. break;
  151. }
  152. }
  153. total_height += row_heights[r];
  154. }
  155. int total_width = 0;
  156. for (int c = 0; c < ncols; c++) {
  157. total_width += col_widths[c];
  158. }
  159. // Pass 2: redistribute free space
  160. Dimension parent_size = parent.getSize();
  161. Insets insets = parent.getInsets();
  162. int free_height = parent_size.height - insets.top - insets.bottom - (nrows - 1) * vgap;
  163. int free_width = parent_size.width - insets.left - insets.right - (ncols - 1) * hgap;
  164. if (total_height != free_height) {
  165. double dy = (double)free_height / (double)total_height;
  166. for (int r = 0; r < nrows; r++) {
  167. row_heights[r] = (int) ((double)row_heights[r] * dy);
  168. }
  169. }
  170. if (total_width != free_width) {
  171. double dx = ((double)free_width) / ((double)total_width);
  172. for (int c = 0; c < ncols; c++) {
  173. col_widths[c] = (int) ((double)col_widths[c] * dx);
  174. }
  175. }
  176. // Pass 3: layout components
  177. for (int r = 0, y = insets.top, i = 0; r < nrows; y += row_heights[r] + vgap, r++) {
  178. for (int c = 0, x = insets.left; c < ncols; x += col_widths[c] + hgap, c++, i++) {
  179. if (i < ncomponents) {
  180. parent.getComponent(i).setBounds(x, y, col_widths[c], row_heights[r]);
  181. }
  182. }
  183. }
  184. } // synchronized
  185. }
  186. public void invalidateLayout(Container container) {}
  187. /**
  188. * Returns the string representation of this variable grid layout's values.
  189. * @return a string representation of this variable grid layout.
  190. */
  191. public String toString() {
  192. return getClass().getName() + "[mode=" + mode + ",size=" + size
  193. + ",hgap=" + hgap + ",vgap=" + vgap + "]";
  194. }
  195. /**
  196. * @param which if 0 compute minimum layout size,
  197. * if 1 compute maximum layout size,
  198. * otherwise compute preferred layout size.
  199. */
  200. private Dimension getLayoutSize(Container parent, int which) {
  201. synchronized (parent.getTreeLock()){
  202. update(parent);
  203. int ncomponents = parent.getComponentCount();
  204. int h = 0;
  205. int w = 0;
  206. for (int r = 0, i = 0; r < nrows; r++) {
  207. int row_height = 0;
  208. for (int c = 0; c < ncols; c++, i++) {
  209. if (i < ncomponents) {
  210. switch (which) {
  211. case 0:
  212. row_height = Math.max(row_height, parent.getComponent(i).getMinimumSize().height);
  213. break;
  214. case 1:
  215. row_height = Math.max(row_height, parent.getComponent(i).getMaximumSize().height);
  216. break;
  217. default:
  218. row_height = Math.max(row_height, parent.getComponent(i).getPreferredSize().height);
  219. break;
  220. }
  221. } else {
  222. break;
  223. }
  224. }
  225. h += row_height;
  226. }
  227. for (int c = 0; c < ncols; c++) {
  228. int col_width = 0;
  229. for (int r = 0; r < nrows; r++) {
  230. int i = r * ncols + c;
  231. if (i < ncomponents) {
  232. switch (which) {
  233. case 0:
  234. col_width = Math.max(col_width, parent.getComponent(i).getMinimumSize().width);
  235. break;
  236. case 1:
  237. col_width = Math.max(col_width, parent.getComponent(i).getMaximumSize().width);
  238. break;
  239. default:
  240. col_width = Math.max(col_width, parent.getComponent(i).getPreferredSize().width);
  241. break;
  242. }
  243. } else {
  244. break;
  245. }
  246. }
  247. w += col_width;
  248. }
  249. Insets insets = parent.getInsets();
  250. return new Dimension(w + insets.left + insets.right + ((ncols - 1) * hgap),
  251. h + insets.top + insets.bottom + ((nrows - 1) * vgap));
  252. }
  253. }
  254. private void update(Container container) {
  255. int ncomponents = container.getComponentCount();
  256. int old_nrows = nrows;
  257. int old_ncols = ncols;
  258. if (this.mode == FIXED_NUM_ROWS) {
  259. nrows = this.size;
  260. ncols = (ncomponents + nrows - 1) / nrows;
  261. } else {
  262. ncols = this.size;
  263. nrows = (ncomponents + ncols - 1) / ncols;
  264. }
  265. if (old_nrows != nrows) {
  266. row_heights = new int[nrows];
  267. }
  268. if (old_ncols != ncols) {
  269. col_widths = new int[ncols];
  270. }
  271. }
  272. private int mode;
  273. private int size;
  274. private int hgap;
  275. private int vgap;
  276. private transient int nrows = -1;
  277. private transient int ncols = -1;
  278. private transient int[] row_heights = null;
  279. private transient int[] col_widths = null;
  280. }