KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > gui2 > SpringUtilities


1 package edu.umd.cs.findbugs.gui2;
2
3 import javax.swing.*;
4 import java.awt.*;
5
6 /**
7  * A 1.4 file that provides utility methods for
8  * creating form- or grid-style layouts with SpringLayout.
9  * These utilities are used by several programs, such as
10  * SpringBox and SpringCompactGrid.
11  *
12  * From the Swing tutorial.
13  *
14  */

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

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

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

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