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