001/* 002 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved. 003 * 004 * Redistribution and use in source and binary forms, with or without 005 * modification, are permitted provided that the following conditions 006 * are met: 007 * 008 * - Redistributions of source code must retain the above copyright 009 * notice, this list of conditions and the following disclaimer. 010 * 011 * - Redistributions in binary form must reproduce the above copyright 012 * notice, this list of conditions and the following disclaimer in the 013 * documentation and/or other materials provided with the distribution. 014 * 015 * - Neither the name of Sun Microsystems nor the names of its 016 * contributors may be used to endorse or promote products derived 017 * from this software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 020 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 022 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 026 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 027 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 028 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 029 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 030 */ 031 032package jahuwaldt.swing; 033 034import javax.swing.*; 035import javax.swing.SpringLayout; 036import java.awt.*; 037 038/** 039 * A 1.4 file that provides utility methods for 040 * creating form- or grid-style layouts with SpringLayout. 041 * These utilities are used by several programs, such as 042 * SpringBox and SpringCompactGrid. 043 */ 044public class SpringUtilities { 045 /** 046 * A debugging utility that prints to stdout the component's 047 * minimum, preferred, and maximum sizes. 048 * 049 * @param c The component to be queried for its dimensions. 050 */ 051 public static void printSizes(Component c) { 052 System.out.println("minimumSize = " + c.getMinimumSize()); 053 System.out.println("preferredSize = " + c.getPreferredSize()); 054 System.out.println("maximumSize = " + c.getMaximumSize()); 055 } 056 057 /** 058 * Aligns the first <code>rows</code> * <code>cols</code> 059 * components of <code>parent</code> in 060 * a grid. Each component is as big as the maximum 061 * preferred width and height of the components. 062 * The parent is made just big enough to fit them all. 063 * 064 * @param parent The parent container to be sized to fit the components. 065 * @param rows number of rows 066 * @param cols number of columns 067 * @param initialX x location to start the grid at 068 * @param initialY y location to start the grid at 069 * @param xPad x padding between cells 070 * @param yPad y padding between cells 071 */ 072 public static void makeGrid(Container parent, 073 int rows, int cols, 074 int initialX, int initialY, 075 int xPad, int yPad) { 076 SpringLayout layout; 077 try { 078 layout = (SpringLayout)parent.getLayout(); 079 } catch (ClassCastException exc) { 080 System.err.println("The first argument to makeGrid must use SpringLayout."); 081 return; 082 } 083 084 Spring xPadSpring = Spring.constant(xPad); 085 Spring yPadSpring = Spring.constant(yPad); 086 Spring initialXSpring = Spring.constant(initialX); 087 Spring initialYSpring = Spring.constant(initialY); 088 int max = rows * cols; 089 090 //Calculate Springs that are the max of the width/height so that all 091 //cells have the same size. 092 Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)). 093 getWidth(); 094 Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)). 095 getWidth(); 096 for (int i = 1; i < max; i++) { 097 SpringLayout.Constraints cons = layout.getConstraints( 098 parent.getComponent(i)); 099 100 maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth()); 101 maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight()); 102 } 103 104 //Apply the new width/height Spring. This forces all the 105 //components to have the same size. 106 for (int i = 0; i < max; i++) { 107 SpringLayout.Constraints cons = layout.getConstraints( 108 parent.getComponent(i)); 109 110 cons.setWidth(maxWidthSpring); 111 cons.setHeight(maxHeightSpring); 112 } 113 114 //Then adjust the x/y constraints of all the cells so that they 115 //are aligned in a grid. 116 SpringLayout.Constraints lastCons = null; 117 SpringLayout.Constraints lastRowCons = null; 118 for (int i = 0; i < max; i++) { 119 SpringLayout.Constraints cons = layout.getConstraints( 120 parent.getComponent(i)); 121 if (i % cols == 0) { //start of new row 122 lastRowCons = lastCons; 123 cons.setX(initialXSpring); 124 } else { //x position depends on previous component 125 cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), 126 xPadSpring)); 127 } 128 129 if (i / cols == 0) { //first row 130 cons.setY(initialYSpring); 131 } else { //y position depends on previous row 132 cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), 133 yPadSpring)); 134 } 135 lastCons = cons; 136 } 137 138 //Set the parent's size. 139 SpringLayout.Constraints pCons = layout.getConstraints(parent); 140 pCons.setConstraint(SpringLayout.SOUTH, 141 Spring.sum( 142 Spring.constant(yPad), 143 lastCons.getConstraint(SpringLayout.SOUTH))); 144 pCons.setConstraint(SpringLayout.EAST, 145 Spring.sum( 146 Spring.constant(xPad), 147 lastCons.getConstraint(SpringLayout.EAST))); 148 } 149 150 /* Used by makeCompactGrid. */ 151 private static SpringLayout.Constraints getConstraintsForCell( 152 int row, int col, 153 Container parent, 154 int cols) { 155 SpringLayout layout = (SpringLayout) parent.getLayout(); 156 Component c = parent.getComponent(row * cols + col); 157 return layout.getConstraints(c); 158 } 159 160 /** 161 * Aligns the first <code>rows</code> * <code>cols</code> 162 * components of <code>parent</code> in 163 * a grid. Each component in a column is as wide as the maximum 164 * preferred width of the components in that column; 165 * height is similarly determined for each row. 166 * The parent is made just big enough to fit them all. 167 * 168 * @param parent The parent container to be sized to fit the components. 169 * @param rows number of rows 170 * @param cols number of columns 171 * @param initialX x location to start the grid at 172 * @param initialY y location to start the grid at 173 * @param xPad x padding between cells 174 * @param yPad y padding between cells 175 */ 176 public static void makeCompactGrid(Container parent, 177 int rows, int cols, 178 int initialX, int initialY, 179 int xPad, int yPad) { 180 SpringLayout layout; 181 try { 182 layout = (SpringLayout)parent.getLayout(); 183 } catch (ClassCastException exc) { 184 System.err.println("The first argument to makeCompactGrid must use SpringLayout."); 185 return; 186 } 187 188 //Align all cells in each column and make them the same width. 189 Spring x = Spring.constant(initialX); 190 for (int c = 0; c < cols; c++) { 191 Spring width = Spring.constant(0); 192 for (int r = 0; r < rows; r++) { 193 width = Spring.max(width, 194 getConstraintsForCell(r, c, parent, cols). 195 getWidth()); 196 } 197 for (int r = 0; r < rows; r++) { 198 SpringLayout.Constraints constraints = 199 getConstraintsForCell(r, c, parent, cols); 200 constraints.setX(x); 201 constraints.setWidth(width); 202 } 203 x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad))); 204 } 205 206 //Align all cells in each row and make them the same height. 207 Spring y = Spring.constant(initialY); 208 for (int r = 0; r < rows; r++) { 209 Spring height = Spring.constant(0); 210 for (int c = 0; c < cols; c++) { 211 height = Spring.max(height, 212 getConstraintsForCell(r, c, parent, cols). 213 getHeight()); 214 } 215 for (int c = 0; c < cols; c++) { 216 SpringLayout.Constraints constraints = 217 getConstraintsForCell(r, c, parent, cols); 218 constraints.setY(y); 219 constraints.setHeight(height); 220 } 221 y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad))); 222 } 223 224 //Set the parent's size. 225 SpringLayout.Constraints pCons = layout.getConstraints(parent); 226 pCons.setConstraint(SpringLayout.SOUTH, y); 227 pCons.setConstraint(SpringLayout.EAST, x); 228 } 229}