|
| 1 | +/* |
| 2 | + * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product |
| 5 | + * that is described in this document. In particular, and without limitation, these intellectual property |
| 6 | + * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or |
| 7 | + * more additional patents or pending patent applications in the U.S. and in other countries. |
| 8 | + * |
| 9 | + * U.S. Government Rights - Commercial software. Government users are subject to the Sun |
| 10 | + * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its |
| 11 | + * supplements. |
| 12 | + * |
| 13 | + * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or |
| 14 | + * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks |
| 15 | + * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the |
| 16 | + * U.S. and other countries. |
| 17 | + * |
| 18 | + * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open |
| 19 | + * Company, Ltd. |
| 20 | + */ |
| 21 | +package com.sun.max.gui; |
| 22 | + |
| 23 | +import java.awt.*; |
| 24 | + |
| 25 | +import javax.swing.*; |
| 26 | +/** |
| 27 | + * Partially copied from the "Swing Tutorial". |
| 28 | + * Minor changes to comply with our CheckStyle settings. |
| 29 | + * |
| 30 | + * A 1.4 file that provides utility methods for |
| 31 | + * creating form- or grid-style layouts with SpringLayout. |
| 32 | + * These utilities are used by several programs, such as |
| 33 | + * SpringBox and SpringCompactGrid. |
| 34 | + * |
| 35 | + * @author original SpringUtilities author |
| 36 | + * @author Bernd Mathiske |
| 37 | + */ |
| 38 | +public final class SpringUtilities { |
| 39 | + private SpringUtilities() { |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * A debugging utility that prints to stdout the component's |
| 44 | + * minimum, preferred, and maximum sizes. |
| 45 | + */ |
| 46 | + public static void printSizes(Component c) { |
| 47 | + System.out.println("minimumSize = " + c.getMinimumSize()); |
| 48 | + System.out.println("preferredSize = " + c.getPreferredSize()); |
| 49 | + System.out.println("maximumSize = " + c.getMaximumSize()); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Aligns the first {@code rows} * {@code cols} |
| 54 | + * components of {@code parent} in |
| 55 | + * a grid. Each component is as big as the maximum |
| 56 | + * preferred width and height of the components. |
| 57 | + * The parent is made just big enough to fit them all. |
| 58 | + * |
| 59 | + * @param rows number of rows |
| 60 | + * @param cols number of columns |
| 61 | + * @param initialX x location to start the grid at |
| 62 | + * @param initialY y location to start the grid at |
| 63 | + * @param xPad x padding between cells |
| 64 | + * @param yPad y padding between cells |
| 65 | + */ |
| 66 | + public static void makeGrid(Container parent, |
| 67 | + int rows, int cols, |
| 68 | + int initialX, int initialY, |
| 69 | + int xPad, int yPad) { |
| 70 | + SpringLayout layout; |
| 71 | + try { |
| 72 | + layout = (SpringLayout) parent.getLayout(); |
| 73 | + } catch (ClassCastException exc) { |
| 74 | + System.err.println("The first argument to makeGrid must use SpringLayout."); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + final Spring xPadSpring = Spring.constant(xPad); |
| 79 | + final Spring yPadSpring = Spring.constant(yPad); |
| 80 | + final Spring initialXSpring = Spring.constant(initialX); |
| 81 | + final Spring initialYSpring = Spring.constant(initialY); |
| 82 | + final int max = rows * cols; |
| 83 | + |
| 84 | + //Calculate Springs that are the max of the width/height so that all |
| 85 | + //cells have the same size. |
| 86 | + Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)). |
| 87 | + getWidth(); |
| 88 | + Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)). |
| 89 | + getWidth(); |
| 90 | + for (int i = 1; i < max; i++) { |
| 91 | + final SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i)); |
| 92 | + |
| 93 | + maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth()); |
| 94 | + maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight()); |
| 95 | + } |
| 96 | + |
| 97 | + //Apply the new width/height Spring. This forces all the |
| 98 | + //components to have the same size. |
| 99 | + for (int i = 0; i < max; i++) { |
| 100 | + final SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i)); |
| 101 | + |
| 102 | + cons.setWidth(maxWidthSpring); |
| 103 | + cons.setHeight(maxHeightSpring); |
| 104 | + } |
| 105 | + |
| 106 | + //Then adjust the x/y constraints of all the cells so that they |
| 107 | + //are aligned in a grid. |
| 108 | + SpringLayout.Constraints lastCons = null; |
| 109 | + SpringLayout.Constraints lastRowCons = null; |
| 110 | + for (int i = 0; i < max; i++) { |
| 111 | + final SpringLayout.Constraints cons = layout.getConstraints(parent.getComponent(i)); |
| 112 | + if (i % cols == 0) { //start of new row |
| 113 | + lastRowCons = lastCons; |
| 114 | + cons.setX(initialXSpring); |
| 115 | + } else if (lastCons != null) { //x position depends on previous component |
| 116 | + cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), |
| 117 | + xPadSpring)); |
| 118 | + } |
| 119 | + |
| 120 | + if (i / cols == 0) { //first row |
| 121 | + cons.setY(initialYSpring); |
| 122 | + } else if (lastRowCons != null) { //y position depends on previous row |
| 123 | + cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), |
| 124 | + yPadSpring)); |
| 125 | + } |
| 126 | + lastCons = cons; |
| 127 | + } |
| 128 | + |
| 129 | + //Set the parent's size. |
| 130 | + final SpringLayout.Constraints pCons = layout.getConstraints(parent); |
| 131 | + if (lastCons != null) { |
| 132 | + pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(yPad), lastCons.getConstraint(SpringLayout.SOUTH))); |
| 133 | + pCons.setConstraint(SpringLayout.EAST, Spring.sum(Spring.constant(xPad), lastCons.getConstraint(SpringLayout.EAST))); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public static void makeGrid(Container parent, int numberOfColumns) { |
| 138 | + makeGrid(parent, parent.getComponentCount() / numberOfColumns, numberOfColumns, 0, 0, 5, 5); |
| 139 | + } |
| 140 | + |
| 141 | + /* Used by makeCompactGrid. */ |
| 142 | + private static SpringLayout.Constraints getConstraintsForCell( |
| 143 | + int row, int col, |
| 144 | + Container parent, |
| 145 | + int cols) { |
| 146 | + final SpringLayout layout = (SpringLayout) parent.getLayout(); |
| 147 | + final Component c = parent.getComponent(row * cols + col); |
| 148 | + return layout.getConstraints(c); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Aligns the first {@code rows} * {@code cols} |
| 153 | + * components of {@code parent} in |
| 154 | + * a grid. Each component in a column is as wide as the maximum |
| 155 | + * preferred width of the components in that column; |
| 156 | + * height is similarly determined for each row. |
| 157 | + * The parent is made just big enough to fit them all. |
| 158 | + * |
| 159 | + * @param rows number of rows |
| 160 | + * @param cols number of columns |
| 161 | + * @param initialX x location to start the grid at |
| 162 | + * @param initialY y location to start the grid at |
| 163 | + * @param xPad x padding between cells |
| 164 | + * @param yPad y padding between cells |
| 165 | + */ |
| 166 | + public static void makeCompactGrid(Container parent, |
| 167 | + int rows, int cols, |
| 168 | + int initialX, int initialY, |
| 169 | + int xPad, int yPad) { |
| 170 | + SpringLayout layout; |
| 171 | + try { |
| 172 | + layout = (SpringLayout) parent.getLayout(); |
| 173 | + } catch (ClassCastException exc) { |
| 174 | + System.err.println("The first argument to makeCompactGrid must use SpringLayout."); |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + //Align all cells in each column and make them the same width. |
| 179 | + Spring x = Spring.constant(initialX); |
| 180 | + for (int c = 0; c < cols; c++) { |
| 181 | + Spring width = Spring.constant(0); |
| 182 | + for (int r = 0; r < rows; r++) { |
| 183 | + width = Spring.max(width, |
| 184 | + getConstraintsForCell(r, c, parent, cols). |
| 185 | + getWidth()); |
| 186 | + } |
| 187 | + for (int r = 0; r < rows; r++) { |
| 188 | + final SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols); |
| 189 | + constraints.setX(x); |
| 190 | + constraints.setWidth(width); |
| 191 | + } |
| 192 | + x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad))); |
| 193 | + } |
| 194 | + |
| 195 | + //Align all cells in each row and make them the same height. |
| 196 | + Spring y = Spring.constant(initialY); |
| 197 | + for (int r = 0; r < rows; r++) { |
| 198 | + Spring height = Spring.constant(0); |
| 199 | + for (int c = 0; c < cols; c++) { |
| 200 | + height = Spring.max(height, |
| 201 | + getConstraintsForCell(r, c, parent, cols). |
| 202 | + getHeight()); |
| 203 | + } |
| 204 | + for (int c = 0; c < cols; c++) { |
| 205 | + final SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols); |
| 206 | + constraints.setY(y); |
| 207 | + constraints.setHeight(height); |
| 208 | + } |
| 209 | + y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad))); |
| 210 | + } |
| 211 | + |
| 212 | + //Set the parent's size. |
| 213 | + final SpringLayout.Constraints pCons = layout.getConstraints(parent); |
| 214 | + pCons.setConstraint(SpringLayout.SOUTH, y); |
| 215 | + pCons.setConstraint(SpringLayout.EAST, x); |
| 216 | + } |
| 217 | + |
| 218 | + public static void makeCompactGrid(Container parent, int numberOfColumns) { |
| 219 | + makeCompactGrid(parent, parent.getComponentCount() / numberOfColumns, numberOfColumns, 0, 0, 3, 3); |
| 220 | + } |
| 221 | + |
| 222 | +} |
0 commit comments