| 1 | + | /* |
| 2 | + | * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. |
| 3 | + | * |
| 4 | + | * Redistribution and use in source and binary forms, with or without |
| 5 | + | * modification, are permitted provided that the following conditions |
| 6 | + | * are met: |
| 7 | + | * |
| 8 | + | * - Redistributions of source code must retain the above copyright |
| 9 | + | * notice, this list of conditions and the following disclaimer. |
| 10 | + | * |
| 11 | + | * - Redistributions in binary form must reproduce the above copyright |
| 12 | + | * notice, this list of conditions and the following disclaimer in the |
| 13 | + | * documentation and/or other materials provided with the distribution. |
| 14 | + | * |
| 15 | + | * - Neither the name of Oracle or the names of its |
| 16 | + | * contributors may be used to endorse or promote products derived |
| 17 | + | * from this software without specific prior written permission. |
| 18 | + | * |
| 19 | + | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 20 | + | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | + | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | + | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | + | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | + | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | + | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | + | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | + | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | + | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | + | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + | */ |
| 31 | + | package burp; |
| 32 | + | |
| 33 | + | import javax.swing.*; |
| 34 | + | import java.awt.*; |
| 35 | + | |
| 36 | + | /** |
| 37 | + | * A 1.4 file that provides utility methods for |
| 38 | + | * creating form- or grid-style layouts with SpringLayout. |
| 39 | + | * These utilities are used by several programs, such as |
| 40 | + | * SpringBox and SpringCompactGrid. |
| 41 | + | */ |
| 42 | + | public class SpringUtilities { |
| 43 | + | /** |
| 44 | + | * A debugging utility that prints to stdout the component's |
| 45 | + | * minimum, preferred, and maximum sizes. |
| 46 | + | */ |
| 47 | + | public static void printSizes(Component c) { |
| 48 | + | System.out.println("minimumSize = " + c.getMinimumSize()); |
| 49 | + | System.out.println("preferredSize = " + c.getPreferredSize()); |
| 50 | + | System.out.println("maximumSize = " + c.getMaximumSize()); |
| 51 | + | } |
| 52 | + | |
| 53 | + | /** |
| 54 | + | * Aligns the first <code>rows</code> * <code>cols</code> |
| 55 | + | * components of <code>parent</code> in |
| 56 | + | * a grid. Each component is as big as the maximum |
| 57 | + | * preferred width and height of the components. |
| 58 | + | * The parent is made just big enough to fit them all. |
| 59 | + | * |
| 60 | + | * @param rows number of rows |
| 61 | + | * @param cols number of columns |
| 62 | + | * @param initialX x location to start the grid at |
| 63 | + | * @param initialY y location to start the grid at |
| 64 | + | * @param xPad x padding between cells |
| 65 | + | * @param yPad y padding between cells |
| 66 | + | */ |
| 67 | + | public static void makeGrid(Container parent, |
| 68 | + | int rows, int cols, |
| 69 | + | int initialX, int initialY, |
| 70 | + | int xPad, int yPad) { |
| 71 | + | SpringLayout layout; |
| 72 | + | try { |
| 73 | + | layout = (SpringLayout) parent.getLayout(); |
| 74 | + | } catch (ClassCastException exc) { |
| 75 | + | System.err.println("The first argument to makeGrid must use SpringLayout."); |
| 76 | + | return; |
| 77 | + | } |
| 78 | + | Spring xPadSpring = Spring.constant(xPad); |
| 79 | + | Spring yPadSpring = Spring.constant(yPad); |
| 80 | + | Spring initialXSpring = Spring.constant(initialX); |
| 81 | + | Spring initialYSpring = Spring.constant(initialY); |
| 82 | + | int max = rows * cols; |
| 83 | + | //Calculate Springs that are the max of the width/height so that all |
| 84 | + | //cells have the same size. |
| 85 | + | Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)). |
| 86 | + | getWidth(); |
| 87 | + | Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)). |
| 88 | + | getHeight(); |
| 89 | + | for (int i = 1; i < max; i++) { |
| 90 | + | SpringLayout.Constraints cons = layout.getConstraints( |
| 91 | + | parent.getComponent(i)); |
| 92 | + | maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth()); |
| 93 | + | maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight()); |
| 94 | + | } |
| 95 | + | //Apply the new width/height Spring. This forces all the |
| 96 | + | //components to have the same size. |
| 97 | + | for (int i = 0; i < max; i++) { |
| 98 | + | SpringLayout.Constraints cons = layout.getConstraints( |
| 99 | + | parent.getComponent(i)); |
| 100 | + | cons.setWidth(maxWidthSpring); |
| 101 | + | cons.setHeight(maxHeightSpring); |
| 102 | + | } |
| 103 | + | //Then adjust the x/y constraints of all the cells so that they |
| 104 | + | //are aligned in a grid. |
| 105 | + | SpringLayout.Constraints lastCons = null; |
| 106 | + | SpringLayout.Constraints lastRowCons = null; |
| 107 | + | for (int i = 0; i < max; i++) { |
| 108 | + | SpringLayout.Constraints cons = layout.getConstraints( |
| 109 | + | parent.getComponent(i)); |
| 110 | + | if (i % cols == 0) { //start of new row |
| 111 | + | lastRowCons = lastCons; |
| 112 | + | cons.setX(initialXSpring); |
| 113 | + | } else { //x position depends on previous component |
| 114 | + | cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), |
| 115 | + | xPadSpring)); |
| 116 | + | } |
| 117 | + | if (i / cols == 0) { //first row |
| 118 | + | cons.setY(initialYSpring); |
| 119 | + | } else { //y position depends on previous row |
| 120 | + | cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), |
| 121 | + | yPadSpring)); |
| 122 | + | } |
| 123 | + | lastCons = cons; |
| 124 | + | } |
| 125 | + | //Set the parent's size. |
| 126 | + | SpringLayout.Constraints pCons = layout.getConstraints(parent); |
| 127 | + | pCons.setConstraint(SpringLayout.SOUTH, |
| 128 | + | Spring.sum( |
| 129 | + | Spring.constant(yPad), |
| 130 | + | lastCons.getConstraint(SpringLayout.SOUTH))); |
| 131 | + | pCons.setConstraint(SpringLayout.EAST, |
| 132 | + | Spring.sum( |
| 133 | + | Spring.constant(xPad), |
| 134 | + | lastCons.getConstraint(SpringLayout.EAST))); |
| 135 | + | } |
| 136 | + | |
| 137 | + | /* Used by makeCompactGrid. */ |
| 138 | + | private static SpringLayout.Constraints getConstraintsForCell( |
| 139 | + | int row, int col, |
| 140 | + | Container parent, |
| 141 | + | int cols) { |
| 142 | + | SpringLayout layout = (SpringLayout) parent.getLayout(); |
| 143 | + | Component c = parent.getComponent(row * cols + col); |
| 144 | + | return layout.getConstraints(c); |
| 145 | + | } |
| 146 | + | |
| 147 | + | /** |
| 148 | + | * Aligns the first <code>rows</code> * <code>cols</code> |
| 149 | + | * components of <code>parent</code> in |
| 150 | + | * a grid. Each component in a column is as wide as the maximum |
| 151 | + | * preferred width of the components in that column; |
| 152 | + | * height is similarly determined for each row. |
| 153 | + | * The parent is made just big enough to fit them all. |
| 154 | + | * |
| 155 | + | * @param rows number of rows |
| 156 | + | * @param cols number of columns |
| 157 | + | * @param initialX x location to start the grid at |
| 158 | + | * @param initialY y location to start the grid at |
| 159 | + | * @param xPad x padding between cells |
| 160 | + | * @param yPad y padding between cells |
| 161 | + | */ |
| 162 | + | public static void makeCompactGrid(Container parent, |
| 163 | + | int rows, int cols, |
| 164 | + | int initialX, int initialY, |
| 165 | + | int xPad, int yPad) { |
| 166 | + | SpringLayout layout; |
| 167 | + | try { |
| 168 | + | layout = (SpringLayout) parent.getLayout(); |
| 169 | + | } catch (ClassCastException exc) { |
| 170 | + | System.err.println("The first argument to makeCompactGrid must use SpringLayout."); |
| 171 | + | return; |
| 172 | + | } |
| 173 | + | //Align all cells in each column and make them the same width. |
| 174 | + | Spring x = Spring.constant(initialX); |
| 175 | + | for (int c = 0; c < cols; c++) { |
| 176 | + | Spring width = Spring.constant(0); |
| 177 | + | for (int r = 0; r < rows; r++) { |
| 178 | + | width = Spring.max(width, |
| 179 | + | getConstraintsForCell(r, c, parent, cols). |
| 180 | + | getWidth()); |
| 181 | + | } |
| 182 | + | for (int r = 0; r < rows; r++) { |
| 183 | + | SpringLayout.Constraints constraints = |
| 184 | + | getConstraintsForCell(r, c, parent, cols); |
| 185 | + | constraints.setX(x); |
| 186 | + | constraints.setWidth(width); |
| 187 | + | } |
| 188 | + | x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad))); |
| 189 | + | } |
| 190 | + | //Align all cells in each row and make them the same height. |
| 191 | + | Spring y = Spring.constant(initialY); |
| 192 | + | for (int r = 0; r < rows; r++) { |
| 193 | + | Spring height = Spring.constant(0); |
| 194 | + | for (int c = 0; c < cols; c++) { |
| 195 | + | height = Spring.max(height, |
| 196 | + | getConstraintsForCell(r, c, parent, cols). |
| 197 | + | getHeight()); |
| 198 | + | } |
| 199 | + | for (int c = 0; c < cols; c++) { |
| 200 | + | SpringLayout.Constraints constraints = |
| 201 | + | getConstraintsForCell(r, c, parent, cols); |
| 202 | + | constraints.setY(y); |
| 203 | + | constraints.setHeight(height); |
| 204 | + | } |
| 205 | + | y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad))); |
| 206 | + | } |
| 207 | + | //Set the parent's size. |
| 208 | + | SpringLayout.Constraints pCons = layout.getConstraints(parent); |
| 209 | + | pCons.setConstraint(SpringLayout.SOUTH, y); |
| 210 | + | pCons.setConstraint(SpringLayout.EAST, x); |
| 211 | + | } |
| 212 | + | } |