KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SpringUtilities


1 import javax.swing.*;
2 import javax.swing.SpringLayout JavaDoc;
3 import java.awt.*;
4
5 /**
6  * A 1.4 file that provides utility methods for
7  * creating form- or grid-style layouts with SpringLayout.
8  * These utilities are used by several programs, such as
9  * SpringBox and SpringCompactGrid.
10  */

11 public class SpringUtilities {
12     /**
13      * A debugging utility that prints to stdout the component's
14      * minimum, preferred, and maximum sizes.
15      */

16     public static void printSizes(Component c) {
17         System.out.println("minimumSize = " + c.getMinimumSize());
18         System.out.println("preferredSize = " + c.getPreferredSize());
19         System.out.println("maximumSize = " + c.getMaximumSize());
20     }
21
22     /**
23      * Aligns the first <code>rows</code> * <code>cols</code>
24      * components of <code>parent</code> in
25      * a grid. Each component is as big as the maximum
26      * preferred width and height of the components.
27      * The parent is made just big enough to fit them all.
28      *
29      * @param rows number of rows
30      * @param cols number of columns
31      * @param initialX x location to start the grid at
32      * @param initialY y location to start the grid at
33      * @param xPad x padding between cells
34      * @param yPad y padding between cells
35      */

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

139     public static void makeCompactGrid(Container parent,
140                                        int rows, int cols,
141                                        int initialX, int initialY,
142                                        int xPad, int yPad) {
143         SpringLayout JavaDoc layout;
144         try {
145             layout = (SpringLayout JavaDoc)parent.getLayout();
146         } catch (ClassCastException JavaDoc exc) {
147             System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
148             return;
149         }
150
151         //Align all cells in each column and make them the same width.
152
Spring x = Spring.constant(initialX);
153         for (int c = 0; c < cols; c++) {
154             Spring width = Spring.constant(0);
155             for (int r = 0; r < rows; r++) {
156                 width = Spring.max(width,
157                                    getConstraintsForCell(r, c, parent, cols).
158                                        getWidth());
159             }
160             for (int r = 0; r < rows; r++) {
161                 SpringLayout.Constraints JavaDoc constraints =
162                         getConstraintsForCell(r, c, parent, cols);
163                 constraints.setX(x);
164                 constraints.setWidth(width);
165             }
166             x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
167         }
168
169         //Align all cells in each row and make them the same height.
170
Spring y = Spring.constant(initialY);
171         for (int r = 0; r < rows; r++) {
172             Spring height = Spring.constant(0);
173             for (int c = 0; c < cols; c++) {
174                 height = Spring.max(height,
175                                     getConstraintsForCell(r, c, parent, cols).
176                                         getHeight());
177             }
178             for (int c = 0; c < cols; c++) {
179                 SpringLayout.Constraints JavaDoc constraints =
180                         getConstraintsForCell(r, c, parent, cols);
181                 constraints.setY(y);
182                 constraints.setHeight(height);
183             }
184             y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
185         }
186
187         //Set the parent's size.
188
SpringLayout.Constraints JavaDoc pCons = layout.getConstraints(parent);
189         pCons.setConstraint(SpringLayout.SOUTH, y);
190         pCons.setConstraint(SpringLayout.EAST, x);
191     }
192 }
193
Popular Tags