mev-4.0.01/source/org/tigr/microarray/mev/cluster/gui/impl/usc/SpringUtilities.java

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