|
18 | 18 | */ |
19 | 19 | package cfa.vo.iris.gui; |
20 | 20 |
|
| 21 | +import javax.swing.*; |
| 22 | +import java.awt.*; |
21 | 23 | import java.beans.PropertyVetoException; |
22 | | -import javax.swing.JInternalFrame; |
23 | | -import javax.swing.SwingUtilities; |
24 | 24 |
|
25 | 25 | /** |
26 | 26 | * Common GUI utilities accessed via static methods. |
@@ -78,4 +78,207 @@ public static void processOnSwingEventThread(Runnable todo, boolean wait) { |
78 | 78 | } |
79 | 79 | } |
80 | 80 | } |
| 81 | + |
| 82 | +// The following code is taken from Oracle |
| 83 | +/* |
| 84 | + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. |
| 85 | + * |
| 86 | + * Redistribution and use in source and binary forms, with or without |
| 87 | + * modification, are permitted provided that the following conditions |
| 88 | + * are met: |
| 89 | + * |
| 90 | + * - Redistributions of source code must retain the above copyright |
| 91 | + * notice, this list of conditions and the following disclaimer. |
| 92 | + * |
| 93 | + * - Redistributions in binary form must reproduce the above copyright |
| 94 | + * notice, this list of conditions and the following disclaimer in the |
| 95 | + * documentation and/or other materials provided with the distribution. |
| 96 | + * |
| 97 | + * - Neither the name of Oracle or the names of its |
| 98 | + * contributors may be used to endorse or promote products derived |
| 99 | + * from this software without specific prior written permission. |
| 100 | + * |
| 101 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 102 | + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 103 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 104 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 105 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 106 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 107 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 108 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 109 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 110 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 111 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 112 | + */ |
| 113 | + |
| 114 | + /** |
| 115 | + * Aligns the first <code>rows</code> * <code>cols</code> |
| 116 | + * components of <code>parent</code> in |
| 117 | + * a grid. Each component in a column is as wide as the maximum |
| 118 | + * preferred width of the components in that column; |
| 119 | + * height is similarly determined for each row. |
| 120 | + * The parent is made just big enough to fit them all. |
| 121 | + * |
| 122 | + * @param rows number of rows |
| 123 | + * @param cols number of columns |
| 124 | + * @param initialX x location to start the grid at |
| 125 | + * @param initialY y location to start the grid at |
| 126 | + * @param xPad x padding between cells |
| 127 | + * @param yPad y padding between cells |
| 128 | + */ |
| 129 | + public static void makeCompactGrid(Container parent, |
| 130 | + int rows, int cols, |
| 131 | + int initialX, int initialY, |
| 132 | + int xPad, int yPad) { |
| 133 | + SpringLayout layout; |
| 134 | + try { |
| 135 | + layout = (SpringLayout)parent.getLayout(); |
| 136 | + } catch (ClassCastException exc) { |
| 137 | + System.err.println("The first argument to makeCompactGrid must use SpringLayout."); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + //Align all cells in each column and make them the same width. |
| 142 | + Spring x = Spring.constant(initialX); |
| 143 | + for (int c = 0; c < cols; c++) { |
| 144 | + Spring width = Spring.constant(0); |
| 145 | + for (int r = 0; r < rows; r++) { |
| 146 | + width = Spring.max(width, |
| 147 | + getConstraintsForCell(r, c, parent, cols). |
| 148 | + getWidth()); |
| 149 | + } |
| 150 | + for (int r = 0; r < rows; r++) { |
| 151 | + SpringLayout.Constraints constraints = |
| 152 | + getConstraintsForCell(r, c, parent, cols); |
| 153 | + constraints.setX(x); |
| 154 | + constraints.setWidth(width); |
| 155 | + } |
| 156 | + x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad))); |
| 157 | + } |
| 158 | + |
| 159 | + //Align all cells in each row and make them the same height. |
| 160 | + Spring y = Spring.constant(initialY); |
| 161 | + for (int r = 0; r < rows; r++) { |
| 162 | + Spring height = Spring.constant(0); |
| 163 | + for (int c = 0; c < cols; c++) { |
| 164 | + height = Spring.max(height, |
| 165 | + getConstraintsForCell(r, c, parent, cols). |
| 166 | + getHeight()); |
| 167 | + } |
| 168 | + for (int c = 0; c < cols; c++) { |
| 169 | + SpringLayout.Constraints constraints = |
| 170 | + getConstraintsForCell(r, c, parent, cols); |
| 171 | + constraints.setY(y); |
| 172 | + constraints.setHeight(height); |
| 173 | + } |
| 174 | + y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad))); |
| 175 | + } |
| 176 | + |
| 177 | + //Set the parent's size. |
| 178 | + SpringLayout.Constraints pCons = layout.getConstraints(parent); |
| 179 | + pCons.setConstraint(SpringLayout.SOUTH, y); |
| 180 | + pCons.setConstraint(SpringLayout.EAST, x); |
| 181 | + } |
| 182 | + |
| 183 | + /* Used by makeCompactGrid. */ |
| 184 | + private static SpringLayout.Constraints getConstraintsForCell( |
| 185 | + int row, int col, |
| 186 | + Container parent, |
| 187 | + int cols) { |
| 188 | + SpringLayout layout = (SpringLayout) parent.getLayout(); |
| 189 | + Component c = parent.getComponent(row * cols + col); |
| 190 | + return layout.getConstraints(c); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Aligns the first <code>rows</code> * <code>cols</code> |
| 195 | + * components of <code>parent</code> in |
| 196 | + * a grid. Each component is as big as the maximum |
| 197 | + * preferred width and height of the components. |
| 198 | + * The parent is made just big enough to fit them all. |
| 199 | + * |
| 200 | + * @param rows number of rows |
| 201 | + * @param cols number of columns |
| 202 | + * @param initialX x location to start the grid at |
| 203 | + * @param initialY y location to start the grid at |
| 204 | + * @param xPad x padding between cells |
| 205 | + * @param yPad y padding between cells |
| 206 | + */ |
| 207 | + public static void makeGrid(Container parent, |
| 208 | + int rows, int cols, |
| 209 | + int initialX, int initialY, |
| 210 | + int xPad, int yPad) { |
| 211 | + SpringLayout layout; |
| 212 | + try { |
| 213 | + layout = (SpringLayout)parent.getLayout(); |
| 214 | + } catch (ClassCastException exc) { |
| 215 | + System.err.println("The first argument to makeGrid must use SpringLayout."); |
| 216 | + return; |
| 217 | + } |
| 218 | + |
| 219 | + Spring xPadSpring = Spring.constant(xPad); |
| 220 | + Spring yPadSpring = Spring.constant(yPad); |
| 221 | + Spring initialXSpring = Spring.constant(initialX); |
| 222 | + Spring initialYSpring = Spring.constant(initialY); |
| 223 | + int max = rows * cols; |
| 224 | + |
| 225 | + //Calculate Springs that are the max of the width/height so that all |
| 226 | + //cells have the same size. |
| 227 | + Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)). |
| 228 | + getWidth(); |
| 229 | + Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)). |
| 230 | + getHeight(); |
| 231 | + for (int i = 1; i < max; i++) { |
| 232 | + SpringLayout.Constraints cons = layout.getConstraints( |
| 233 | + parent.getComponent(i)); |
| 234 | + |
| 235 | + maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth()); |
| 236 | + maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight()); |
| 237 | + } |
| 238 | + |
| 239 | + //Apply the new width/height Spring. This forces all the |
| 240 | + //components to have the same size. |
| 241 | + for (int i = 0; i < max; i++) { |
| 242 | + SpringLayout.Constraints cons = layout.getConstraints( |
| 243 | + parent.getComponent(i)); |
| 244 | + |
| 245 | + cons.setWidth(maxWidthSpring); |
| 246 | + cons.setHeight(maxHeightSpring); |
| 247 | + } |
| 248 | + |
| 249 | + //Then adjust the x/y constraints of all the cells so that they |
| 250 | + //are aligned in a grid. |
| 251 | + SpringLayout.Constraints lastCons = null; |
| 252 | + SpringLayout.Constraints lastRowCons = null; |
| 253 | + for (int i = 0; i < max; i++) { |
| 254 | + SpringLayout.Constraints cons = layout.getConstraints( |
| 255 | + parent.getComponent(i)); |
| 256 | + if (i % cols == 0) { //start of new row |
| 257 | + lastRowCons = lastCons; |
| 258 | + cons.setX(initialXSpring); |
| 259 | + } else { //x position depends on previous component |
| 260 | + cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), |
| 261 | + xPadSpring)); |
| 262 | + } |
| 263 | + |
| 264 | + if (i / cols == 0) { //first row |
| 265 | + cons.setY(initialYSpring); |
| 266 | + } else { //y position depends on previous row |
| 267 | + cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), |
| 268 | + yPadSpring)); |
| 269 | + } |
| 270 | + lastCons = cons; |
| 271 | + } |
| 272 | + |
| 273 | + //Set the parent's size. |
| 274 | + SpringLayout.Constraints pCons = layout.getConstraints(parent); |
| 275 | + pCons.setConstraint(SpringLayout.SOUTH, |
| 276 | + Spring.sum( |
| 277 | + Spring.constant(yPad), |
| 278 | + lastCons.getConstraint(SpringLayout.SOUTH))); |
| 279 | + pCons.setConstraint(SpringLayout.EAST, |
| 280 | + Spring.sum( |
| 281 | + Spring.constant(xPad), |
| 282 | + lastCons.getConstraint(SpringLayout.EAST))); |
| 283 | + } |
81 | 284 | } |
0 commit comments