KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > p2p > peerconfiguration > SpringUtilities


1 package org.objectweb.proactive.p2p.peerconfiguration;
2 import javax.swing.*;
3 import javax.swing.SpringLayout JavaDoc;
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 public class SpringUtilities {
13     /**
14      * A debugging utility that prints to stdout the component's
15      * minimum, preferred, and maximum sizes.
16      */

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

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

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