KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > util > SpringUtilities


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

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

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

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

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