comparison gui/javax/swing/layout/SpringUtilities.java @ 2:e16016635b2a

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