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