KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > swing > layout > SpringUtilities


1 /*
2  * Copyright 2002-2004 Greg Hinkle
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.mc4j.console.swing.layout;
18
19 import java.awt.Component JavaDoc;
20 import java.awt.Container JavaDoc;
21
22 import javax.swing.Spring JavaDoc;
23 import javax.swing.SpringLayout JavaDoc;
24
25 /**
26  * A 1.4 file that provides utility methods for
27  * creating form- or grid-style layouts with SpringLayout.
28  * These utilities are used by several programs, such as
29  * SpringBox and SpringCompactGrid.
30  */

31 public class SpringUtilities {
32     /**
33      * A debugging utility that prints to stdout the component's
34      * minimum, preferred, and maximum sizes.
35      */

36     public static void printSizes(Component JavaDoc c) {
37         System.out.println("minimumSize = " + c.getMinimumSize());
38         System.out.println("preferredSize = " + c.getPreferredSize());
39         System.out.println("maximumSize = " + c.getMaximumSize());
40     }
41
42     /**
43      * Aligns the first <code>rows</code> * <code>cols</code>
44      * components of <code>parent</code> in
45      * a grid. Each component is as big as the maximum
46      * preferred width and height of the components.
47      * The parent is made just big enough to fit them all.
48      *
49      * @param rows number of rows
50      * @param cols number of columns
51      * @param initialX x location to start the grid at
52      * @param initialY y location to start the grid at
53      * @param xPad x padding between cells
54      * @param yPad y padding between cells
55      */

56     public static void makeGrid(Container JavaDoc parent,
57                                 int rows, int cols,
58                                 int initialX, int initialY,
59                                 int xPad, int yPad) {
60         SpringLayout JavaDoc layout;
61         try {
62             layout = (SpringLayout JavaDoc)parent.getLayout();
63         } catch (ClassCastException JavaDoc exc) {
64             System.err.println("The first argument to makeGrid must use SpringLayout.");
65             return;
66         }
67
68         Spring JavaDoc xPadSpring = Spring.constant(xPad);
69         Spring JavaDoc yPadSpring = Spring.constant(yPad);
70         Spring JavaDoc initialXSpring = Spring.constant(initialX);
71         Spring JavaDoc initialYSpring = Spring.constant(initialY);
72         int max = rows * cols;
73
74         //Calculate Springs that are the max of the width/height so that all
75
//cells have the same size.
76
Spring JavaDoc maxWidthSpring = layout.getConstraints(parent.getComponent(0)).
77                                     getWidth();
78         Spring JavaDoc maxHeightSpring = layout.getConstraints(parent.getComponent(0)).
79                                     getWidth();
80         for (int i = 1; i < max; i++) {
81             SpringLayout.Constraints JavaDoc cons = layout.getConstraints(
82                                             parent.getComponent(i));
83
84             maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
85             maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
86         }
87
88         //Apply the new width/height Spring. This forces all the
89
//components to have the same size.
90
for (int i = 0; i < max; i++) {
91             SpringLayout.Constraints JavaDoc cons = layout.getConstraints(
92                                             parent.getComponent(i));
93
94             cons.setWidth(maxWidthSpring);
95             cons.setHeight(maxHeightSpring);
96         }
97
98         //Then adjust the x/y constraints of all the cells so that they
99
//are aligned in a grid.
100
SpringLayout.Constraints JavaDoc lastCons = null;
101         SpringLayout.Constraints JavaDoc lastRowCons = null;
102         for (int i = 0; i < max; i++) {
103             SpringLayout.Constraints JavaDoc cons = layout.getConstraints(
104                                                  parent.getComponent(i));
105             if (i % cols == 0) { //start of new row
106
lastRowCons = lastCons;
107                 cons.setX(initialXSpring);
108             } else { //x position depends on previous component
109
cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST),
110                                      xPadSpring));
111             }
112
113             if (i / cols == 0) { //first row
114
cons.setY(initialYSpring);
115             } else { //y position depends on previous row
116
cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH),
117                                      yPadSpring));
118             }
119             lastCons = cons;
120         }
121
122         //Set the parent's size.
123
SpringLayout.Constraints JavaDoc pCons = layout.getConstraints(parent);
124         pCons.setConstraint(SpringLayout.SOUTH,
125                             Spring.sum(
126                                 Spring.constant(yPad),
127                                 lastCons.getConstraint(SpringLayout.SOUTH)));
128         pCons.setConstraint(SpringLayout.EAST,
129                             Spring.sum(
130                                 Spring.constant(xPad),
131                                 lastCons.getConstraint(SpringLayout.EAST)));
132     }
133
134     /* Used by makeCompactGrid. */
135     private static SpringLayout.Constraints JavaDoc getConstraintsForCell(
136                                                 int row, int col,
137                                                 Container JavaDoc parent,
138                                                 int cols) {
139         SpringLayout JavaDoc layout = (SpringLayout JavaDoc) parent.getLayout();
140         Component JavaDoc c = parent.getComponent(row * cols + col);
141         return layout.getConstraints(c);
142     }
143
144     /**
145      * Aligns the first <code>rows</code> * <code>cols</code>
146      * components of <code>parent</code> in
147      * a grid. Each component in a column is as wide as the maximum
148      * preferred width of the components in that column;
149      * height is similarly determined for each row.
150      * The parent is made just big enough to fit them all.
151      *
152      * @param rows number of rows
153      * @param cols number of columns
154      * @param initialX x location to start the grid at
155      * @param initialY y location to start the grid at
156      * @param xPad x padding between cells
157      * @param yPad y padding between cells
158      */

159     public static void makeCompactGrid(Container JavaDoc parent,
160                                        int rows, int cols,
161                                        int initialX, int initialY,
162                                        int xPad, int yPad) {
163         SpringLayout JavaDoc layout;
164         try {
165             layout = (SpringLayout JavaDoc)parent.getLayout();
166         } catch (ClassCastException JavaDoc exc) {
167             System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
168             return;
169         }
170
171         //Align all cells in each column and make them the same width.
172
Spring JavaDoc x = Spring.constant(initialX);
173         for (int c = 0; c < cols; c++) {
174             Spring JavaDoc width = Spring.constant(0);
175             for (int r = 0; r < rows; r++) {
176                 width = Spring.max(width,
177                                    getConstraintsForCell(r, c, parent, cols).
178                                        getWidth());
179             }
180             for (int r = 0; r < rows; r++) {
181                 SpringLayout.Constraints JavaDoc constraints =
182                         getConstraintsForCell(r, c, parent, cols);
183                 constraints.setX(x);
184                 constraints.setWidth(width);
185             }
186             x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
187         }
188
189         //Align all cells in each row and make them the same height.
190
Spring JavaDoc y = Spring.constant(initialY);
191         for (int r = 0; r < rows; r++) {
192             Spring JavaDoc height = Spring.constant(0);
193             for (int c = 0; c < cols; c++) {
194                 height = Spring.max(height,
195                                     getConstraintsForCell(r, c, parent, cols).
196                                         getHeight());
197             }
198             for (int c = 0; c < cols; c++) {
199                 SpringLayout.Constraints JavaDoc constraints =
200                         getConstraintsForCell(r, c, parent, cols);
201                 constraints.setY(y);
202                 constraints.setHeight(height);
203             }
204             y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
205         }
206
207         //Set the parent's size.
208
SpringLayout.Constraints JavaDoc pCons = layout.getConstraints(parent);
209         pCons.setConstraint(SpringLayout.SOUTH, y);
210         pCons.setConstraint(SpringLayout.EAST, x);
211     }
212 }
213
Popular Tags