001    package de.mospace.swing;
002    
003    import javax.swing.*;
004    import javax.swing.SpringLayout;
005    import java.awt.*;
006    import java.util.logging.Logger;
007    
008    /**
009     * This is SpringUtilities from the java tutorial.
010     * http://java.sun.com/docs/books/tutorial/uiswing/layout/examples/SpringUtilities.java
011     * A 1.4 file that provides utility methods for
012     * creating form- or grid-style layouts with SpringLayout.
013     * These utilities are used by several programs, such as
014     * SpringBox and SpringCompactGrid.
015     */
016    public class SpringUtilities {
017        private static final Logger logger = Logger.getLogger(SpringUtilities.class.getPackage().getName());
018        /**
019         * A debugging utility that prints to stdout the component's
020         * minimum, preferred, and maximum sizes.
021         */
022        public static void printSizes(Component c) {
023            System.out.println("minimumSize = " + c.getMinimumSize());
024            System.out.println("preferredSize = " + c.getPreferredSize());
025            System.out.println("maximumSize = " + c.getMaximumSize());
026        }
027    
028        /**
029         * Aligns the first <code>rows</code> * <code>cols</code>
030         * components of <code>parent</code> in
031         * a grid. Each component is as big as the maximum
032         * preferred width and height of the components.
033         * The parent is made just big enough to fit them all.
034         *
035         * @param rows number of rows
036         * @param cols number of columns
037         * @param initialX x location to start the grid at
038         * @param initialY y location to start the grid at
039         * @param xPad x padding between cells
040         * @param yPad y padding between cells
041         */
042        public static void makeGrid(Container parent,
043                                    int rows, int cols,
044                                    int initialX, int initialY,
045                                    int xPad, int yPad) {
046            SpringLayout layout;
047            try {
048                layout = (SpringLayout)parent.getLayout();
049            } catch (ClassCastException exc) {
050                logger.severe("The first argument to makeGrid must use SpringLayout.");
051                return;
052            }
053    
054            Spring xPadSpring = Spring.constant(xPad);
055            Spring yPadSpring = Spring.constant(yPad);
056            Spring initialXSpring = Spring.constant(initialX);
057            Spring initialYSpring = Spring.constant(initialY);
058            int max = rows * cols;
059    
060            //Calculate Springs that are the max of the width/height so that all
061            //cells have the same size.
062            Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).
063                                        getWidth();
064            Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).
065                                        getWidth();
066            for (int i = 1; i < max; i++) {
067                SpringLayout.Constraints cons = layout.getConstraints(
068                                                parent.getComponent(i));
069    
070                maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
071                maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
072            }
073    
074            //Apply the new width/height Spring. This forces all the
075            //components to have the same size.
076            for (int i = 0; i < max; i++) {
077                SpringLayout.Constraints cons = layout.getConstraints(
078                                                parent.getComponent(i));
079    
080                cons.setWidth(maxWidthSpring);
081                cons.setHeight(maxHeightSpring);
082            }
083    
084            //Then adjust the x/y constraints of all the cells so that they
085            //are aligned in a grid.
086            SpringLayout.Constraints lastCons = null;
087            SpringLayout.Constraints lastRowCons = null;
088            for (int i = 0; i < max; i++) {
089                SpringLayout.Constraints cons = layout.getConstraints(
090                                                     parent.getComponent(i));
091                if (i % cols == 0) { //start of new row
092                    lastRowCons = lastCons;
093                    cons.setX(initialXSpring);
094                } else { //x position depends on previous component
095                    cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST),
096                                         xPadSpring));
097                }
098    
099                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 exc) {
153                logger.severe("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    }