1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.xmlcv.util;
28
29 import java.awt.Component;
30 import java.awt.Container;
31
32 import javax.swing.Spring;
33 import javax.swing.SpringLayout;
34
35 /***
36 * A 1.4 file that provides utility methods for creating form- or grid-style
37 * layouts with SpringLayout. These utilities are used by several programs, such
38 * as SpringBox and SpringCompactGrid.
39 *
40 * @version $Revision: 114 $
41 * @author <a href="mailto:drazzib@drazzib.com">Damien Raude-Morvan </a>
42 */
43 public class SpringUtilities {
44
45
46 private static SpringLayout.Constraints getConstraintsForCell(int row,
47 int col, Container parent, int cols) {
48 SpringLayout layout = (SpringLayout) parent.getLayout();
49 Component c = parent.getComponent(row * cols + col);
50 return layout.getConstraints(c);
51 }
52
53 /***
54 * Aligns the first <code>rows</code>*<code>cols</code> components of
55 * <code>parent</code> in a grid. Each component in a column is as wide as
56 * the maximum preferred width of the components in that column; height is
57 * similarly determined for each row. The parent is made just big enough to
58 * fit them all.
59 *
60 * @param parent
61 * container of the components
62 * @param rows
63 * number of rows
64 * @param cols
65 * number of columns
66 * @param initialX
67 * x location to start the grid at
68 * @param initialY
69 * y location to start the grid at
70 * @param xPad
71 * x padding between cells
72 * @param yPad
73 * y padding between cells
74 */
75 public static void makeCompactGrid(Container parent, int rows, int cols,
76 int initialX, int initialY, int xPad, int yPad) {
77 SpringLayout layout;
78 try {
79 layout = (SpringLayout) parent.getLayout();
80 } catch (ClassCastException exc) {
81 System.err
82 .println("The first argument to makeCompactGrid must use SpringLayout.");
83 return;
84 }
85
86
87 Spring x = Spring.constant(initialX);
88 for (int c = 0; c < cols; c++) {
89 Spring width = Spring.constant(0);
90 for (int r = 0; r < rows; r++) {
91 width = Spring.max(width, getConstraintsForCell(r, c, parent,
92 cols).getWidth());
93 }
94 for (int r = 0; r < rows; r++) {
95 SpringLayout.Constraints constraints = getConstraintsForCell(r,
96 c, parent, cols);
97 constraints.setX(x);
98 constraints.setWidth(width);
99 }
100 x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
101 }
102
103
104 Spring y = Spring.constant(initialY);
105 for (int r = 0; r < rows; r++) {
106 Spring height = Spring.constant(0);
107 for (int c = 0; c < cols; c++) {
108 height = Spring.max(height, getConstraintsForCell(r, c, parent,
109 cols).getHeight());
110 }
111 for (int c = 0; c < cols; c++) {
112 SpringLayout.Constraints constraints = getConstraintsForCell(r,
113 c, parent, cols);
114 constraints.setY(y);
115 constraints.setHeight(height);
116 }
117 y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
118 }
119
120
121
122
123
124 }
125 }