001: package org.obe.util;
002:
003: import javax.swing.*;
004: import java.awt.*;
005:
006: /**
007: * Spring layout utilities, courtesy of Sun Microsystems.
008: */
009: public final class SpringUtilities {
010: /**
011: * A debugging utility that prints to stdout the component's minimum,
012: * preferred, and maximum sizes.
013: */
014: public static void printSizes(Component comp) {
015: System.out.println("minimumSize = " + comp.getMinimumSize());
016: System.out
017: .println("preferredSize = " + comp.getPreferredSize());
018: System.out.println("maximumSize = " + comp.getMaximumSize());
019: }
020:
021: /**
022: * Aligns the first <code>rows</code> * <code>cols</code> components of
023: * <code>parent</code> in a grid. Each component is as big as the maximum
024: * preferred width and height of the components. The parent is made just big
025: * enough to fit them all.
026: *
027: * @param rows number of rows
028: * @param cols number of columns
029: * @param initialX x location to start the grid at
030: * @param initialY y location to start the grid at
031: * @param xPad x padding between cells
032: * @param yPad y padding between cells
033: */
034: public static void makeGrid(Container parent, int rows, int cols,
035: int initialX, int initialY, int xPad, int yPad) {
036:
037: SpringLayout layout;
038: try {
039: layout = (SpringLayout) parent.getLayout();
040: } catch (ClassCastException exc) {
041: System.err
042: .println("The first argument to makeGrid must use SpringLayout.");
043: return;
044: }
045:
046: Spring xPadSpring = Spring.constant(xPad);
047: Spring yPadSpring = Spring.constant(yPad);
048: Spring initialXSpring = Spring.constant(initialX);
049: Spring initialYSpring = Spring.constant(initialY);
050: int max = rows * cols;
051:
052: // Calculate Springs that are the max of the width/height so that all
053: // cells have the same size.
054: Spring maxWidthSpring = layout.getConstraints(
055: parent.getComponent(0)).getWidth();
056: Spring maxHeightSpring = layout.getConstraints(
057: parent.getComponent(0)).getWidth();
058: for (int i = 1; i < max; i++) {
059: SpringLayout.Constraints cons = layout
060: .getConstraints(parent.getComponent(i));
061:
062: maxWidthSpring = Spring
063: .max(maxWidthSpring, cons.getWidth());
064: maxHeightSpring = Spring.max(maxHeightSpring, cons
065: .getHeight());
066: }
067:
068: // Apply the new width/height Spring. This forces all the components to
069: // have the same size.
070: for (int i = 0; i < max; i++) {
071: SpringLayout.Constraints cons = layout
072: .getConstraints(parent.getComponent(i));
073:
074: cons.setWidth(maxWidthSpring);
075: cons.setHeight(maxHeightSpring);
076: }
077:
078: // Then adjust the x/y constraints of all the cells so that they are
079: // aligned in a grid.
080: SpringLayout.Constraints lastCons = null;
081: SpringLayout.Constraints lastRowCons = null;
082: for (int i = 0; i < max; i++) {
083: SpringLayout.Constraints cons = layout
084: .getConstraints(parent.getComponent(i));
085: if (i % cols == 0) { //start of new row
086: lastRowCons = lastCons;
087: cons.setX(initialXSpring);
088: } else { //x position depends on previous component
089: cons.setX(Spring.sum(lastCons
090: .getConstraint(SpringLayout.EAST), xPadSpring));
091: }
092:
093: if (i / cols == 0) { //first row
094: cons.setY(initialYSpring);
095: } else { //y position depends on previous row
096: cons
097: .setY(Spring.sum(lastRowCons
098: .getConstraint(SpringLayout.SOUTH),
099: yPadSpring));
100: }
101: lastCons = cons;
102: }
103:
104: // Set the parent's size.
105: SpringLayout.Constraints pCons = layout.getConstraints(parent);
106: pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring
107: .constant(yPad), lastCons
108: .getConstraint(SpringLayout.SOUTH)));
109: pCons.setConstraint(SpringLayout.EAST, Spring.sum(Spring
110: .constant(xPad), lastCons
111: .getConstraint(SpringLayout.EAST)));
112: }
113:
114: /* Used by makeCompactGrid. */
115: private static SpringLayout.Constraints getConstraintsForCell(
116: int row, int col, Container parent, int cols) {
117:
118: SpringLayout layout = (SpringLayout) parent.getLayout();
119: Component comp = parent.getComponent(row * cols + col);
120: return layout.getConstraints(comp);
121: }
122:
123: /**
124: * Aligns the first <code>rows</code> * <code>cols</code> components of
125: * <code>parent</code> in a grid. Each component in a column is as wide as
126: * the maximum preferred width of the components in that column; height is
127: * similarly determined for each row. The parent is made just big enough to
128: * fit them all.
129: *
130: * @param rows number of rows
131: * @param cols number of columns
132: * @param initialX x location to start the grid at
133: * @param initialY y location to start the grid at
134: * @param xPad x padding between cells
135: * @param yPad y padding between cells
136: */
137: public static void makeCompactGrid(Container parent, int rows,
138: int cols, int initialX, int initialY, int xPad, int yPad) {
139:
140: SpringLayout layout;
141: try {
142: layout = (SpringLayout) parent.getLayout();
143: } catch (ClassCastException exc) {
144: System.err
145: .println("The first argument to makeCompactGrid must use SpringLayout.");
146: return;
147: }
148:
149: // Align all cells in each column and make them the same width.
150: Spring x = Spring.constant(initialX);
151: for (int col = 0; col < cols; col++) {
152: Spring width = Spring.constant(0);
153: for (int r = 0; r < rows; r++) {
154: width = Spring.max(width, getConstraintsForCell(r, col,
155: parent, cols).getWidth());
156: }
157: for (int r = 0; r < rows; r++) {
158: SpringLayout.Constraints constraints = getConstraintsForCell(
159: r, col, parent, cols);
160: constraints.setX(x);
161: constraints.setWidth(width);
162: }
163: x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
164: }
165:
166: // Align all cells in each row and make them the same height.
167: Spring y = Spring.constant(initialY);
168: for (int row = 0; row < rows; row++) {
169: Spring height = Spring.constant(0);
170: for (int col = 0; col < cols; col++) {
171: height = Spring.max(height, getConstraintsForCell(row,
172: col, parent, cols).getHeight());
173: }
174: for (int col = 0; col < cols; col++) {
175: SpringLayout.Constraints constraints = getConstraintsForCell(
176: row, col, parent, cols);
177: constraints.setY(y);
178: constraints.setHeight(height);
179: }
180: y = Spring
181: .sum(y, Spring.sum(height, Spring.constant(yPad)));
182: }
183:
184: // Set the parent's size.
185: SpringLayout.Constraints pCons = layout.getConstraints(parent);
186: pCons.setConstraint(SpringLayout.SOUTH, y);
187: pCons.setConstraint(SpringLayout.EAST, x);
188: }
189:
190: private SpringUtilities() {
191: }
192: }
|