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