View Javadoc

1   package net.sf.jameleon.ui;
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  public class SpringUtilities {
13      
14      /***
15       * don't instantiate this class
16       */
17      private SpringUtilities(){
18      }
19  
20      /***
21       * A debugging utility that prints to stdout the component's
22       * minimum, preferred, and maximum sizes.
23       */
24      public static void printSizes(Component c) {
25          System.out.println("minimumSize = " + c.getMinimumSize());
26          System.out.println("preferredSize = " + c.getPreferredSize());
27          System.out.println("maximumSize = " + c.getMaximumSize());
28      }
29  
30      /***
31       * Aligns the first <code>rows</code> * <code>cols</code>
32       * components of <code>parent</code> in
33       * a grid. Each component is as big as the maximum
34       * preferred width and height of the components.
35       * The parent is made just big enough to fit them all.
36       *
37       * @param rows number of rows
38       * @param cols number of columns
39       * @param initialX x location to start the grid at
40       * @param initialY y location to start the grid at
41       * @param xPad x padding between cells
42       * @param yPad y padding between cells
43       */
44      public static void makeGrid(Container parent,
45                                  int rows, int cols,
46                                  int initialX, int initialY,
47                                  int xPad, int yPad) {
48          SpringLayout layout;
49          try {
50              layout = (SpringLayout)parent.getLayout();
51          } catch (ClassCastException exc) {
52              System.err.println("The first argument to makeGrid must use SpringLayout.");
53              return;
54          }
55  
56          Spring xPadSpring = Spring.constant(xPad);
57          Spring yPadSpring = Spring.constant(yPad);
58          Spring initialXSpring = Spring.constant(initialX);
59          Spring initialYSpring = Spring.constant(initialY);
60          int max = rows * cols;
61  
62          //Calculate Springs that are the max of the width/height so that all
63          //cells have the same size.
64          Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).
65                                      getWidth();
66          Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).
67                                      getHeight();
68          for (int i = 1; i < max; i++) {
69              SpringLayout.Constraints cons = layout.getConstraints(
70                                              parent.getComponent(i));
71  
72              maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
73              maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
74          }
75  
76          //Apply the new width/height Spring. This forces all the
77          //components to have the same size.
78          for (int i = 0; i < max; i++) {
79              SpringLayout.Constraints cons = layout.getConstraints(
80                                              parent.getComponent(i));
81  
82              cons.setWidth(maxWidthSpring);
83              cons.setHeight(maxHeightSpring);
84          }
85  
86          //Then adjust the x/y constraints of all the cells so that they
87          //are aligned in a grid.
88          SpringLayout.Constraints lastCons = null;
89          SpringLayout.Constraints lastRowCons = null;
90          for (int i = 0; i < max; i++) {
91              SpringLayout.Constraints cons = layout.getConstraints(
92                                                   parent.getComponent(i));
93              if (i % cols == 0) { //start of new row
94                  lastRowCons = lastCons;
95                  cons.setX(initialXSpring);
96              } else { //x position depends on previous component
97                  cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST),
98                                       xPadSpring));
99              }
100 
101             if (i / cols == 0) { //first row
102                 cons.setY(initialYSpring);
103             } else { //y position depends on previous row
104                 cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH),
105                                      yPadSpring));
106             }
107             lastCons = cons;
108         }
109 
110         //Set the parent's size.
111         SpringLayout.Constraints pCons = layout.getConstraints(parent);
112         pCons.setConstraint(SpringLayout.SOUTH,
113                             Spring.sum(
114                                 Spring.constant(yPad),
115                                 lastCons.getConstraint(SpringLayout.SOUTH)));
116         pCons.setConstraint(SpringLayout.EAST,
117                             Spring.sum(
118                                 Spring.constant(xPad),
119                                 lastCons.getConstraint(SpringLayout.EAST)));
120     }
121 
122     /* Used by makeCompactGrid. */
123     private static SpringLayout.Constraints getConstraintsForCell(
124                                                 int row, int col,
125                                                 Container parent,
126                                                 int cols) {
127         SpringLayout layout = (SpringLayout) parent.getLayout();
128         Component c = parent.getComponent(row * cols + col);
129         return layout.getConstraints(c);
130     }
131 
132     /***
133      * Aligns the first <code>rows</code> * <code>cols</code>
134      * components of <code>parent</code> in
135      * a grid. Each component in a column is as wide as the maximum
136      * preferred width of the components in that column;
137      * height is similarly determined for each row.
138      * The parent is made just big enough to fit them all.
139      *
140      * @param rows number of rows
141      * @param cols number of columns
142      * @param initialX x location to start the grid at
143      * @param initialY y location to start the grid at
144      * @param xPad x padding between cells
145      * @param yPad y padding between cells
146      */
147     public static void makeCompactGrid(Container parent,
148                                        int rows, int cols,
149                                        int initialX, int initialY,
150                                        int xPad, int yPad) {
151         SpringLayout layout;
152         try {
153             layout = (SpringLayout)parent.getLayout();
154         } catch (ClassCastException exc) {
155             System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
156             return;
157         }
158 
159         //Align all cells in each column and make them the same width.
160         Spring x = Spring.constant(initialX);
161         for (int c = 0; c < cols; c++) {
162             Spring width = Spring.constant(0);
163             for (int r = 0; r < rows; r++) {
164                 width = Spring.max(width,
165                                    getConstraintsForCell(r, c, parent, cols).
166                                        getWidth());
167             }
168             for (int r = 0; r < rows; r++) {
169                 SpringLayout.Constraints constraints =
170                         getConstraintsForCell(r, c, parent, cols);
171                 constraints.setX(x);
172                 constraints.setWidth(width);
173             }
174             x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
175         }
176 
177         //Align all cells in each row and make them the same height.
178         Spring y = Spring.constant(initialY);
179         for (int r = 0; r < rows; r++) {
180             Spring height = Spring.constant(0);
181             for (int c = 0; c < cols; c++) {
182                 height = Spring.max(height,
183                                     getConstraintsForCell(r, c, parent, cols).
184                                         getHeight());
185             }
186             for (int c = 0; c < cols; c++) {
187                 SpringLayout.Constraints constraints =
188                         getConstraintsForCell(r, c, parent, cols);
189                 constraints.setY(y);
190                 constraints.setHeight(height);
191             }
192             y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
193         }
194 
195         //Set the parent's size.
196         SpringLayout.Constraints pCons = layout.getConstraints(parent);
197         pCons.setConstraint(SpringLayout.SOUTH, y);
198         pCons.setConstraint(SpringLayout.EAST, x);
199     }
200 }