SpringLayoutUtils.java

00001 /*
00002  * Created on Aug 25, 2004
00003  */
00004 package edu.stanford.hci.r3.util.layout;
00005 
00006 import java.awt.Component;
00007 import java.awt.Container;
00008 
00009 import javax.swing.Spring;
00010 import javax.swing.SpringLayout;
00011 
00024 public class SpringLayoutUtils {
00025         /* Used by makeCompactGrid. */
00026         private static SpringLayout.Constraints getConstraintsForCell(int row, int col,
00027                         Container parent, int cols) {
00028                 SpringLayout layout = (SpringLayout) parent.getLayout();
00029                 Component c = parent.getComponent(row * cols + col);
00030                 return layout.getConstraints(c);
00031         }
00032 
00052         public static void makeCompactGrid(Container parent, int rows, int cols, int initialX,
00053                         int initialY, int xPad, int yPad) {
00054 
00055                 SpringLayout layout;
00056                 try {
00057                         layout = (SpringLayout) parent.getLayout();
00058                 } catch (ClassCastException exc) {
00059                         System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
00060                         return;
00061                 }
00062 
00063                 // Align all cells in each column and make them the same width.
00064                 Spring x = Spring.constant(initialX);
00065                 // for each column
00066                 for (int c = 0; c < cols; c++) {
00067                         Spring width = Spring.constant(0);
00068 
00069                         // find the widest cell (go down the rows) and create a spring out of it
00070                         for (int r = 0; r < rows; r++) {
00071 
00072                                 // get the larger of the two widths
00073                                 width = Spring.max(width, getConstraintsForCell(r, c, parent, cols).getWidth());
00074                         }
00075 
00076                         // apply the spring to each cell
00077                         for (int r = 0; r < rows; r++) {
00078                                 final SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent,
00079                                                 cols);
00080                                 constraints.setX(x);
00081                                 constraints.setWidth(width);
00082                         }
00083                         x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
00084                 }
00085 
00086                 // Align all cells in each row and make them the same height.
00087                 Spring y = Spring.constant(initialY);
00088                 for (int r = 0; r < rows; r++) {
00089                         Spring height = Spring.constant(0);
00090                         for (int c = 0; c < cols; c++) {
00091                                 height = Spring.max(height, getConstraintsForCell(r, c, parent, cols).getHeight());
00092                         }
00093                         for (int c = 0; c < cols; c++) {
00094                                 final SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent,
00095                                                 cols);
00096                                 constraints.setY(y);
00097                                 constraints.setHeight(height);
00098                         }
00099                         y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
00100                 }
00101 
00102                 // Set the parent's size.
00103                 SpringLayout.Constraints pCons = layout.getConstraints(parent);
00104                 pCons.setConstraint(SpringLayout.SOUTH, y);
00105                 pCons.setConstraint(SpringLayout.EAST, x);
00106         }
00107 
00126         public static void makeGrid(Container parent, int rows, int cols, int initialX, int initialY,
00127                         int xPad, int yPad) {
00128                 SpringLayout layout;
00129                 try {
00130                         layout = (SpringLayout) parent.getLayout();
00131                 } catch (ClassCastException exc) {
00132                         System.err.println("The first argument to makeGrid must use SpringLayout.");
00133                         return;
00134                 }
00135 
00136                 Spring xPadSpring = Spring.constant(xPad);
00137                 Spring yPadSpring = Spring.constant(yPad);
00138                 Spring initialXSpring = Spring.constant(initialX);
00139                 Spring initialYSpring = Spring.constant(initialY);
00140                 int max = rows * cols;
00141 
00142                 // Calculate Springs that are the max of the width/height so that all
00143                 // cells have the same size.
00144                 Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
00145                 Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)).getWidth();
00146                 for (int i = 1; i < max; i++) {
00147                         SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
00148 
00149                         maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
00150                         maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
00151                 }
00152 
00153                 // Apply the new width/height Spring. This forces all the
00154                 // components to have the same size.
00155                 for (int i = 0; i < max; i++) {
00156                         SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
00157 
00158                         cons.setWidth(maxWidthSpring);
00159                         cons.setHeight(maxHeightSpring);
00160                 }
00161 
00162                 // Then adjust the x/y constraints of all the cells so that they
00163                 // are aligned in a grid.
00164                 SpringLayout.Constraints lastCons = null;
00165                 SpringLayout.Constraints lastRowCons = null;
00166                 for (int i = 0; i < max; i++) {
00167                         SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i));
00168                         if (i % cols == 0) { // start of new row
00169                                 lastRowCons = lastCons;
00170                                 cons.setX(initialXSpring);
00171                         } else { // x position depends on previous component
00172                                 cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), xPadSpring));
00173                         }
00174 
00175                         if (i / cols == 0) { // first row
00176                                 cons.setY(initialYSpring);
00177                         } else { // y position depends on previous row
00178                                 cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), yPadSpring));
00179                         }
00180                         lastCons = cons;
00181                 }
00182 
00183                 // Set the parent's size.
00184                 SpringLayout.Constraints pCons = layout.getConstraints(parent);
00185                 pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(yPad), lastCons
00186                                 .getConstraint(SpringLayout.SOUTH)));
00187                 pCons.setConstraint(SpringLayout.EAST, Spring.sum(Spring.constant(xPad), lastCons
00188                                 .getConstraint(SpringLayout.EAST)));
00189         }
00190 
00195         public static void printSizes(Component c) {
00196                 System.out.println("minimumSize = " + c.getMinimumSize());
00197                 System.out.println("preferredSize = " + c.getPreferredSize());
00198                 System.out.println("maximumSize = " + c.getMaximumSize());
00199         }
00200 }

Generated on Sat Apr 14 18:21:38 2007 for R3 Paper Toolkit by  doxygen 1.4.7