1 | /*
|
---|
2 | * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
7 | *
|
---|
8 | * - Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | *
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | *
|
---|
15 | * - Neither the name of Oracle or the names of its
|
---|
16 | * contributors may be used to endorse or promote products derived
|
---|
17 | * from this software without specific prior written permission.
|
---|
18 | *
|
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
---|
20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
---|
21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
---|
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
30 | */
|
---|
31 |
|
---|
32 | package tools;
|
---|
33 |
|
---|
34 | import java.awt.Component;
|
---|
35 | import java.awt.Container;
|
---|
36 |
|
---|
37 | import javax.swing.Spring;
|
---|
38 | import javax.swing.SpringLayout;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * A 1.4 file that provides utility methods for
|
---|
42 | * creating form- or grid-style layouts with SpringLayout.
|
---|
43 | * These utilities are used by several programs, such as
|
---|
44 | * SpringBox and SpringCompactGrid.
|
---|
45 | */
|
---|
46 | public final class SpringUtilities {
|
---|
47 |
|
---|
48 | private SpringUtilities() {
|
---|
49 | // Hide default constructors for utilities classes
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * A debugging utility that prints to stdout the component's
|
---|
54 | * minimum, preferred, and maximum sizes.
|
---|
55 | */
|
---|
56 | public static void printSizes(Component c) {
|
---|
57 | System.out.println("minimumSize = " + c.getMinimumSize());
|
---|
58 | System.out.println("preferredSize = " + c.getPreferredSize());
|
---|
59 | System.out.println("maximumSize = " + c.getMaximumSize());
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Aligns the first <code>rows</code> * <code>cols</code>
|
---|
64 | * components of <code>parent</code> in
|
---|
65 | * a grid. Each component is as big as the maximum
|
---|
66 | * preferred width and height of the components.
|
---|
67 | * The parent is made just big enough to fit them all.
|
---|
68 | *
|
---|
69 | * @param rows number of rows
|
---|
70 | * @param cols number of columns
|
---|
71 | * @param initialX x location to start the grid at
|
---|
72 | * @param initialY y location to start the grid at
|
---|
73 | * @param xPad x padding between cells
|
---|
74 | * @param yPad y padding between cells
|
---|
75 | */
|
---|
76 | public static void makeGrid(Container parent,
|
---|
77 | int rows, int cols,
|
---|
78 | int initialX, int initialY,
|
---|
79 | int xPad, int yPad) {
|
---|
80 | SpringLayout layout;
|
---|
81 | try {
|
---|
82 | layout = (SpringLayout) parent.getLayout();
|
---|
83 | } catch (ClassCastException exc) {
|
---|
84 | System.err.println("The first argument to makeGrid must use SpringLayout.");
|
---|
85 | return;
|
---|
86 | }
|
---|
87 |
|
---|
88 | Spring xPadSpring = Spring.constant(xPad);
|
---|
89 | Spring yPadSpring = Spring.constant(yPad);
|
---|
90 | Spring initialXSpring = Spring.constant(initialX);
|
---|
91 | Spring initialYSpring = Spring.constant(initialY);
|
---|
92 | int max = rows * cols;
|
---|
93 |
|
---|
94 | //Calculate Springs that are the max of the width/height so that all
|
---|
95 | //cells have the same size.
|
---|
96 | Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).
|
---|
97 | getWidth();
|
---|
98 | Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).
|
---|
99 | getWidth();
|
---|
100 | for (int i = 1; i < max; i++) {
|
---|
101 | SpringLayout.Constraints cons = layout.getConstraints(
|
---|
102 | parent.getComponent(i));
|
---|
103 |
|
---|
104 | maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
|
---|
105 | maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
|
---|
106 | }
|
---|
107 |
|
---|
108 | //Apply the new width/height Spring. This forces all the
|
---|
109 | //components to have the same size.
|
---|
110 | for (int i = 0; i < max; i++) {
|
---|
111 | SpringLayout.Constraints cons = layout.getConstraints(
|
---|
112 | parent.getComponent(i));
|
---|
113 |
|
---|
114 | cons.setWidth(maxWidthSpring);
|
---|
115 | cons.setHeight(maxHeightSpring);
|
---|
116 | }
|
---|
117 |
|
---|
118 | //Then adjust the x/y constraints of all the cells so that they
|
---|
119 | //are aligned in a grid.
|
---|
120 | SpringLayout.Constraints lastCons = null;
|
---|
121 | SpringLayout.Constraints lastRowCons = null;
|
---|
122 | for (int i = 0; i < max; i++) {
|
---|
123 | SpringLayout.Constraints cons = layout.getConstraints(
|
---|
124 | parent.getComponent(i));
|
---|
125 | if (i % cols == 0) { //start of new row
|
---|
126 | lastRowCons = lastCons;
|
---|
127 | cons.setX(initialXSpring);
|
---|
128 | } else { //x position depends on previous component
|
---|
129 | cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST),
|
---|
130 | xPadSpring));
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (i / cols == 0) { //first row
|
---|
134 | cons.setY(initialYSpring);
|
---|
135 | } else { //y position depends on previous row
|
---|
136 | cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH),
|
---|
137 | yPadSpring));
|
---|
138 | }
|
---|
139 | lastCons = cons;
|
---|
140 | }
|
---|
141 |
|
---|
142 | //Set the parent's size.
|
---|
143 | SpringLayout.Constraints pCons = layout.getConstraints(parent);
|
---|
144 | pCons.setConstraint(SpringLayout.SOUTH,
|
---|
145 | Spring.sum(
|
---|
146 | Spring.constant(yPad),
|
---|
147 | lastCons.getConstraint(SpringLayout.SOUTH)));
|
---|
148 | pCons.setConstraint(SpringLayout.EAST,
|
---|
149 | Spring.sum(
|
---|
150 | Spring.constant(xPad),
|
---|
151 | lastCons.getConstraint(SpringLayout.EAST)));
|
---|
152 | }
|
---|
153 |
|
---|
154 | /* Used by makeCompactGrid. */
|
---|
155 | private static SpringLayout.Constraints getConstraintsForCell(
|
---|
156 | int row, int col,
|
---|
157 | Container parent,
|
---|
158 | int cols) {
|
---|
159 | SpringLayout layout = (SpringLayout) parent.getLayout();
|
---|
160 | Component c = parent.getComponent(row * cols + col);
|
---|
161 | return layout.getConstraints(c);
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Aligns the first <code>rows</code> * <code>cols</code>
|
---|
166 | * components of <code>parent</code> in
|
---|
167 | * a grid. Each component in a column is as wide as the maximum
|
---|
168 | * preferred width of the components in that column;
|
---|
169 | * height is similarly determined for each row.
|
---|
170 | * The parent is made just big enough to fit them all.
|
---|
171 | *
|
---|
172 | * @param rows number of rows
|
---|
173 | * @param cols number of columns
|
---|
174 | * @param initialX x location to start the grid at
|
---|
175 | * @param initialY y location to start the grid at
|
---|
176 | * @param xPad x padding between cells
|
---|
177 | * @param yPad y padding between cells
|
---|
178 | */
|
---|
179 | public static void makeCompactGrid(Container parent,
|
---|
180 | int rows, int cols,
|
---|
181 | int initialX, int initialY,
|
---|
182 | int xPad, int yPad) {
|
---|
183 | SpringLayout layout;
|
---|
184 | try {
|
---|
185 | layout = (SpringLayout) parent.getLayout();
|
---|
186 | } catch (ClassCastException exc) {
|
---|
187 | System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
|
---|
188 | return;
|
---|
189 | }
|
---|
190 |
|
---|
191 | //Align all cells in each column and make them the same width.
|
---|
192 | Spring x = Spring.constant(initialX);
|
---|
193 | for (int c = 0; c < cols; c++) {
|
---|
194 | Spring width = Spring.constant(0);
|
---|
195 | for (int r = 0; r < rows; r++) {
|
---|
196 | width = Spring.max(width,
|
---|
197 | getConstraintsForCell(r, c, parent, cols).
|
---|
198 | getWidth());
|
---|
199 | }
|
---|
200 | for (int r = 0; r < rows; r++) {
|
---|
201 | SpringLayout.Constraints constraints =
|
---|
202 | getConstraintsForCell(r, c, parent, cols);
|
---|
203 | constraints.setX(x);
|
---|
204 | constraints.setWidth(width);
|
---|
205 | }
|
---|
206 | x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
|
---|
207 | }
|
---|
208 |
|
---|
209 | //Align all cells in each row and make them the same height.
|
---|
210 | Spring y = Spring.constant(initialY);
|
---|
211 | for (int r = 0; r < rows; r++) {
|
---|
212 | Spring height = Spring.constant(0);
|
---|
213 | for (int c = 0; c < cols; c++) {
|
---|
214 | height = Spring.max(height,
|
---|
215 | getConstraintsForCell(r, c, parent, cols).
|
---|
216 | getHeight());
|
---|
217 | }
|
---|
218 | for (int c = 0; c < cols; c++) {
|
---|
219 | SpringLayout.Constraints constraints =
|
---|
220 | getConstraintsForCell(r, c, parent, cols);
|
---|
221 | constraints.setY(y);
|
---|
222 | constraints.setHeight(height);
|
---|
223 | }
|
---|
224 | y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
|
---|
225 | }
|
---|
226 |
|
---|
227 | //Set the parent's size.
|
---|
228 | SpringLayout.Constraints pCons = layout.getConstraints(parent);
|
---|
229 | pCons.setConstraint(SpringLayout.SOUTH, y);
|
---|
230 | pCons.setConstraint(SpringLayout.EAST, x);
|
---|
231 | }
|
---|
232 | }
|
---|