001    /**
002     * LayoutUtils.java
003     * jCOLIBRI2 framework. 
004     * @author Juan A. Recio-García.
005     * GAIA - Group for Artificial Intelligence Applications
006     * http://gaia.fdi.ucm.es
007     * 02/11/2007
008     */
009    package jcolibri.method.gui.utils;
010    
011    import java.awt.Component;
012    import java.awt.Container;
013    
014    import javax.swing.Spring;
015    import javax.swing.SpringLayout;
016    
017    /**
018     * Utility classes for dealing with the spring layout.
019     * @author Juan A. Recio-Garcia
020     * @version 1.0
021     */
022    public class LayoutUtils {
023            /**
024         * Aligns the first <code>rows</code> * <code>cols</code>
025         * components of <code>parent</code> in
026         * a grid. Each component in a column is as wide as the maximum
027         * preferred width of the components in that column;
028         * height is similarly determined for each row.
029         * The parent is made just big enough to fit them all.
030         *
031         * @param rows number of rows
032         * @param cols number of columns
033         * @param initialX x location to start the grid at
034         * @param initialY y location to start the grid at
035         * @param xPad x padding between cells
036         * @param yPad y padding between cells
037         */
038        public static void makeCompactGrid(Container parent,
039                                           int rows, int cols,
040                                           int initialX, int initialY,
041                                           int xPad, int yPad) {
042            SpringLayout layout;
043            try {
044                layout = (SpringLayout)parent.getLayout();
045            } catch (ClassCastException exc) {
046                System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
047                return;
048            }
049    
050            //Align all cells in each column and make them the same width.
051            Spring x = Spring.constant(initialX);
052            for (int c = 0; c < cols; c++) {
053                Spring width = Spring.constant(0);
054                for (int r = 0; r < rows; r++) {
055                    width = Spring.max(width,
056                                       getConstraintsForCell(r, c, parent, cols).
057                                           getWidth());
058                }
059                for (int r = 0; r < rows; r++) {
060                    SpringLayout.Constraints constraints =
061                            getConstraintsForCell(r, c, parent, cols);
062                    constraints.setX(x);
063                    constraints.setWidth(width);
064                }
065                x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
066            }
067    
068            //Align all cells in each row and make them the same height.
069            Spring y = Spring.constant(initialY);
070            for (int r = 0; r < rows; r++) {
071                Spring height = Spring.constant(0);
072                for (int c = 0; c < cols; c++) {
073                    height = Spring.max(height,
074                                        getConstraintsForCell(r, c, parent, cols).
075                                            getHeight());
076                }
077                for (int c = 0; c < cols; c++) {
078                    SpringLayout.Constraints constraints =
079                            getConstraintsForCell(r, c, parent, cols);
080                    constraints.setY(y);
081                    constraints.setHeight(height);
082                }
083                y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
084            }
085    
086            //Set the parent's size.
087            SpringLayout.Constraints pCons = layout.getConstraints(parent);
088            pCons.setConstraint(SpringLayout.SOUTH, y);
089            pCons.setConstraint(SpringLayout.EAST, x);
090        }
091        /* Used by makeCompactGrid. */
092        private static SpringLayout.Constraints getConstraintsForCell(
093                                                    int row, int col,
094                                                    Container parent,
095                                                    int cols) {
096            SpringLayout layout = (SpringLayout) parent.getLayout();
097            Component c = parent.getComponent(row * cols + col);
098            return layout.getConstraints(c);
099        }
100    }