(no commit message)
[sfa.git] / gui / JavaApplication1 / src / javaapplication1 / SpringUtilities.java
1 package javaapplication1;
2 /*
3  * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  *   - Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *
12  *   - Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *
16  *   - Neither the name of Sun Microsystems nor the names of its
17  *     contributors may be used to endorse or promote products derived
18  *     from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */ 
32
33 import javax.swing.*;
34 import javax.swing.SpringLayout;
35 import java.awt.*;
36
37 /**
38  * A 1.4 file that provides utility methods for
39  * creating form- or grid-style layouts with SpringLayout.
40  * These utilities are used by several programs, such as
41  * SpringBox and SpringCompactGrid.
42  */
43 public class SpringUtilities {
44     /**
45      * A debugging utility that prints to stdout the component's
46      * minimum, preferred, and maximum sizes.
47      */
48     public static void printSizes(Component c) {
49         System.out.println("minimumSize = " + c.getMinimumSize());
50         System.out.println("preferredSize = " + c.getPreferredSize());
51         System.out.println("maximumSize = " + c.getMaximumSize());
52     }
53
54     /**
55      * Aligns the first <code>rows</code> * <code>cols</code>
56      * components of <code>parent</code> in
57      * a grid. Each component is as big as the maximum
58      * preferred width and height of the components.
59      * The parent is made just big enough to fit them all.
60      *
61      * @param rows number of rows
62      * @param cols number of columns
63      * @param initialX x location to start the grid at
64      * @param initialY y location to start the grid at
65      * @param xPad x padding between cells
66      * @param yPad y padding between cells
67      */
68     public static void makeGrid(Container parent,
69                                 int rows, int cols,
70                                 int initialX, int initialY,
71                                 int xPad, int yPad) {
72         SpringLayout layout;
73         try {
74             layout = (SpringLayout)parent.getLayout();
75         } catch (ClassCastException exc) {
76             System.err.println("The first argument to makeGrid must use SpringLayout.");
77             return;
78         }
79
80         Spring xPadSpring = Spring.constant(xPad);
81         Spring yPadSpring = Spring.constant(yPad);
82         Spring initialXSpring = Spring.constant(initialX);
83         Spring initialYSpring = Spring.constant(initialY);
84         int max = rows * cols;
85
86         //Calculate Springs that are the max of the width/height so that all
87         //cells have the same size.
88         Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).
89                                     getWidth();
90         Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).
91                                     getWidth();
92         for (int i = 1; i < max; i++) {
93             SpringLayout.Constraints cons = layout.getConstraints(
94                                             parent.getComponent(i));
95
96             maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
97             maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
98         }
99
100         //Apply the new width/height Spring. This forces all the
101         //components to have the same size.
102         for (int i = 0; i < max; i++) {
103             SpringLayout.Constraints cons = layout.getConstraints(
104                                             parent.getComponent(i));
105
106             cons.setWidth(maxWidthSpring);
107             cons.setHeight(maxHeightSpring);
108         }
109
110         //Then adjust the x/y constraints of all the cells so that they
111         //are aligned in a grid.
112         SpringLayout.Constraints lastCons = null;
113         SpringLayout.Constraints lastRowCons = null;
114         for (int i = 0; i < max; i++) {
115             SpringLayout.Constraints cons = layout.getConstraints(
116                                                  parent.getComponent(i));
117             if (i % cols == 0) { //start of new row
118                 lastRowCons = lastCons;
119                 cons.setX(initialXSpring);
120             } else { //x position depends on previous component
121                 cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST),
122                                      xPadSpring));
123             }
124
125             if (i / cols == 0) { //first row
126                 cons.setY(initialYSpring);
127             } else { //y position depends on previous row
128                 cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH),
129                                      yPadSpring));
130             }
131             lastCons = cons;
132         }
133
134         //Set the parent's size.
135         SpringLayout.Constraints pCons = layout.getConstraints(parent);
136         pCons.setConstraint(SpringLayout.SOUTH,
137                             Spring.sum(
138                                 Spring.constant(yPad),
139                                 lastCons.getConstraint(SpringLayout.SOUTH)));
140         pCons.setConstraint(SpringLayout.EAST,
141                             Spring.sum(
142                                 Spring.constant(xPad),
143                                 lastCons.getConstraint(SpringLayout.EAST)));
144     }
145
146     /* Used by makeCompactGrid. */
147     private static SpringLayout.Constraints getConstraintsForCell(
148                                                 int row, int col,
149                                                 Container parent,
150                                                 int cols) {
151         SpringLayout layout = (SpringLayout) parent.getLayout();
152         Component c = parent.getComponent(row * cols + col);
153         return layout.getConstraints(c);
154     }
155
156     /**
157      * Aligns the first <code>rows</code> * <code>cols</code>
158      * components of <code>parent</code> in
159      * a grid. Each component in a column is as wide as the maximum
160      * preferred width of the components in that column;
161      * height is similarly determined for each row.
162      * The parent is made just big enough to fit them all.
163      *
164      * @param rows number of rows
165      * @param cols number of columns
166      * @param initialX x location to start the grid at
167      * @param initialY y location to start the grid at
168      * @param xPad x padding between cells
169      * @param yPad y padding between cells
170      */
171     public static void makeCompactGrid(Container parent,
172                                        int rows, int cols,
173                                        int initialX, int initialY,
174                                        int xPad, int yPad) {
175         SpringLayout layout;
176         try {
177             layout = (SpringLayout)parent.getLayout();
178         } catch (ClassCastException exc) {
179             System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
180             return;
181         }
182
183         //Align all cells in each column and make them the same width.
184         Spring x = Spring.constant(initialX);
185         for (int c = 0; c < cols; c++) {
186             Spring width = Spring.constant(0);
187             for (int r = 0; r < rows; r++) {
188                 width = Spring.max(width,
189                                    getConstraintsForCell(r, c, parent, cols).
190                                        getWidth());
191             }
192             for (int r = 0; r < rows; r++) {
193                 SpringLayout.Constraints constraints =
194                         getConstraintsForCell(r, c, parent, cols);
195                 constraints.setX(x);
196                 constraints.setWidth(width);
197             }
198             x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
199         }
200
201         //Align all cells in each row and make them the same height.
202         Spring y = Spring.constant(initialY);
203         for (int r = 0; r < rows; r++) {
204             Spring height = Spring.constant(0);
205             for (int c = 0; c < cols; c++) {
206                 height = Spring.max(height,
207                                     getConstraintsForCell(r, c, parent, cols).
208                                         getHeight());
209             }
210             for (int c = 0; c < cols; c++) {
211                 SpringLayout.Constraints constraints =
212                         getConstraintsForCell(r, c, parent, cols);
213                 constraints.setY(y);
214                 constraints.setHeight(height);
215             }
216             y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
217         }
218
219         //Set the parent's size.
220         SpringLayout.Constraints pCons = layout.getConstraints(parent);
221         pCons.setConstraint(SpringLayout.SOUTH, y);
222         pCons.setConstraint(SpringLayout.EAST, x);
223     }
224 }