1 | package vishnu.builder;
|
---|
2 |
|
---|
3 | import javax.swing.*;
|
---|
4 | import javax.swing.SpringLayout;
|
---|
5 | import java.awt.*;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * A 1.4 file that provides utility methods for
|
---|
9 | * creating form- or grid-style layouts with SpringLayout.
|
---|
10 | * These utilities are used by several programs, such as
|
---|
11 | * SpringBox and SpringCompactGrid.
|
---|
12 | */
|
---|
13 | public class SpringUtilities {
|
---|
14 | /**
|
---|
15 | * A debugging utility that prints to stdout the component's
|
---|
16 | * minimum, preferred, and maximum sizes.
|
---|
17 | */
|
---|
18 | public static void printSizes(Component c) {
|
---|
19 | System.out.println("minimumSize = " + c.getMinimumSize());
|
---|
20 | System.out.println("preferredSize = " + c.getPreferredSize());
|
---|
21 | System.out.println("maximumSize = " + c.getMaximumSize());
|
---|
22 | }
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Aligns the first <code>rows</code> * <code>cols</code>
|
---|
26 | * components of <code>parent</code> in
|
---|
27 | * a grid. Each component is as big as the maximum
|
---|
28 | * preferred width and height of the components.
|
---|
29 | * The parent is made just big enough to fit them all.
|
---|
30 | *
|
---|
31 | * @param rows number of rows
|
---|
32 | * @param cols number of columns
|
---|
33 | * @param initialX x location to start the grid at
|
---|
34 | * @param initialY y location to start the grid at
|
---|
35 | * @param xPad x padding between cells
|
---|
36 | * @param yPad y padding between cells
|
---|
37 | */
|
---|
38 | public static void makeGrid(Container parent,
|
---|
39 | int rows, int cols,
|
---|
40 | int initialX, int initialY,
|
---|
41 | int xPad, int yPad) {
|
---|
42 | SpringLayout layout;
|
---|
43 | try {
|
---|
44 | layout = (SpringLayout)parent.getLayout();
|
---|
45 | } catch (ClassCastException exc) {
|
---|
46 | System.err.println("The first argument to makeGrid must use SpringLayout.");
|
---|
47 | return;
|
---|
48 | }
|
---|
49 |
|
---|
50 | Spring xPadSpring = Spring.constant(xPad);
|
---|
51 | Spring yPadSpring = Spring.constant(yPad);
|
---|
52 | Spring initialXSpring = Spring.constant(initialX);
|
---|
53 | Spring initialYSpring = Spring.constant(initialY);
|
---|
54 | int max = rows * cols;
|
---|
55 |
|
---|
56 | //Calculate Springs that are the max of the width/height so that all
|
---|
57 | //cells have the same size.
|
---|
58 | Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).
|
---|
59 | getWidth();
|
---|
60 | Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).
|
---|
61 | getWidth();
|
---|
62 | for (int i = 1; i < max; i++) {
|
---|
63 | SpringLayout.Constraints cons = layout.getConstraints(
|
---|
64 | parent.getComponent(i));
|
---|
65 |
|
---|
66 | maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
|
---|
67 | maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
|
---|
68 | }
|
---|
69 |
|
---|
70 | //Apply the new width/height Spring. This forces all the
|
---|
71 | //components to have the same size.
|
---|
72 | for (int i = 0; i < max; i++) {
|
---|
73 | SpringLayout.Constraints cons = layout.getConstraints(
|
---|
74 | parent.getComponent(i));
|
---|
75 |
|
---|
76 | cons.setWidth(maxWidthSpring);
|
---|
77 | cons.setHeight(maxHeightSpring);
|
---|
78 | }
|
---|
79 |
|
---|
80 | //Then adjust the x/y constraints of all the cells so that they
|
---|
81 | //are aligned in a grid.
|
---|
82 | SpringLayout.Constraints lastCons = null;
|
---|
83 | SpringLayout.Constraints lastRowCons = null;
|
---|
84 | for (int i = 0; i < max; i++) {
|
---|
85 | SpringLayout.Constraints cons = layout.getConstraints(
|
---|
86 | parent.getComponent(i));
|
---|
87 | if (i % cols == 0) { //start of new row
|
---|
88 | lastRowCons = lastCons;
|
---|
89 | cons.setX(initialXSpring);
|
---|
90 | } else { //x position depends on previous component
|
---|
91 | cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), xPadSpring));
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (i / cols == 0) { //first row
|
---|
95 | cons.setY(initialYSpring);
|
---|
96 | } else { //y position depends on previous row
|
---|
97 | cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH),
|
---|
98 | yPadSpring));
|
---|
99 | }
|
---|
100 | lastCons = cons;
|
---|
101 | }
|
---|
102 |
|
---|
103 | //Set the parent's size.
|
---|
104 | SpringLayout.Constraints pCons = layout.getConstraints(parent);
|
---|
105 | pCons.setConstraint(SpringLayout.SOUTH,
|
---|
106 | Spring.sum(
|
---|
107 | Spring.constant(yPad),
|
---|
108 | lastCons.getConstraint(SpringLayout.SOUTH)));
|
---|
109 | pCons.setConstraint(SpringLayout.EAST,
|
---|
110 | Spring.sum(
|
---|
111 | Spring.constant(xPad),
|
---|
112 | lastCons.getConstraint(SpringLayout.EAST)));
|
---|
113 | }
|
---|
114 |
|
---|
115 | /* Used by makeCompactGrid. */
|
---|
116 | private static SpringLayout.Constraints getConstraintsForCell(
|
---|
117 | int row, int col,
|
---|
118 | Container parent,
|
---|
119 | int cols) {
|
---|
120 | SpringLayout layout = (SpringLayout) parent.getLayout();
|
---|
121 | Component c = parent.getComponent(row * cols + col);
|
---|
122 | return layout.getConstraints(c);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Aligns the first <code>rows</code> * <code>cols</code>
|
---|
127 | * components of <code>parent</code> in
|
---|
128 | * a grid. Each component in a column is as wide as the maximum
|
---|
129 | * preferred width of the components in that column;
|
---|
130 | * height is similarly determined for each row.
|
---|
131 | * The parent is made just big enough to fit them all.
|
---|
132 | *
|
---|
133 | * @param rows number of rows
|
---|
134 | * @param cols number of columns
|
---|
135 | * @param initialX x location to start the grid at
|
---|
136 | * @param initialY y location to start the grid at
|
---|
137 | * @param xPad x padding between cells
|
---|
138 | * @param yPad y padding between cells
|
---|
139 | */
|
---|
140 | public static void makeCompactGrid(Container parent,
|
---|
141 | int rows, int cols,
|
---|
142 | int initialX, int initialY,
|
---|
143 | int xPad, int yPad) {
|
---|
144 | SpringLayout layout;
|
---|
145 | try {
|
---|
146 | layout = (SpringLayout)parent.getLayout();
|
---|
147 | } catch (ClassCastException exc) {
|
---|
148 | System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
|
---|
149 | return;
|
---|
150 | }
|
---|
151 |
|
---|
152 | //Align all cells in each column and make them the same width.
|
---|
153 | Spring x = Spring.constant(initialX);
|
---|
154 | for (int c = 0; c < cols; c++) {
|
---|
155 | Spring width = Spring.constant(0);
|
---|
156 | for (int r = 0; r < rows; r++) {
|
---|
157 | width = Spring.max(width,
|
---|
158 | getConstraintsForCell(r, c, parent, cols).
|
---|
159 | getWidth());
|
---|
160 | }
|
---|
161 | for (int r = 0; r < rows; r++) {
|
---|
162 | SpringLayout.Constraints constraints =
|
---|
163 | getConstraintsForCell(r, c, parent, cols);
|
---|
164 | constraints.setX(x);
|
---|
165 | constraints.setWidth(width);
|
---|
166 | }
|
---|
167 | x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
|
---|
168 | }
|
---|
169 |
|
---|
170 | //Align all cells in each row and make them the same height.
|
---|
171 | Spring y = Spring.constant(initialY);
|
---|
172 | for (int r = 0; r < rows; r++) {
|
---|
173 | Spring height = Spring.constant(0);
|
---|
174 | for (int c = 0; c < cols; c++) {
|
---|
175 | height = Spring.max(height,
|
---|
176 | getConstraintsForCell(r, c, parent, cols).
|
---|
177 | getHeight());
|
---|
178 | }
|
---|
179 | for (int c = 0; c < cols; c++) {
|
---|
180 | SpringLayout.Constraints constraints =
|
---|
181 | getConstraintsForCell(r, c, parent, cols);
|
---|
182 | constraints.setY(y);
|
---|
183 | constraints.setHeight(height);
|
---|
184 | }
|
---|
185 | y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
|
---|
186 | }
|
---|
187 |
|
---|
188 | //Set the parent's size.
|
---|
189 | SpringLayout.Constraints pCons = layout.getConstraints(parent);
|
---|
190 | pCons.setConstraint(SpringLayout.SOUTH, y);
|
---|
191 | pCons.setConstraint(SpringLayout.EAST, x);
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|