1 package com.jbidwatcher.ui.util; 2 3 /* 4 * Copyright (c) 2000-2007, CyberFOX Software, Inc. All Rights Reserved. 5 * 6 * Developed by mrs (Morgan Schweers) 7 */ 8 9 import javax.swing.*; 10 import javax.swing.SpringLayout; 11 import java.awt.*; 12 13 /** 14 * A 1.4 file that provides utility methods for 15 * creating form- or grid-style layouts with SpringLayout. 16 * These utilities are used by several programs, such as 17 * SpringBox and SpringCompactGrid. 18 */ 19 public class SpringUtilities { 20 /** 21 * A debugging utility that prints to stdout the component's 22 * minimum, preferred, and maximum sizes. 23 * 24 * @param c - The component to inspect. 25 */ printSizes(Component c)26 public static void printSizes(Component c) { 27 System.out.println("minimumSize = " + c.getMinimumSize()); 28 System.out.println("preferredSize = " + c.getPreferredSize()); 29 System.out.println("maximumSize = " + c.getMaximumSize()); 30 } 31 32 /** 33 * Aligns the first <code>rows</code> * <code>cols</code> 34 * components of <code>parent</code> in 35 * a grid. Each component is as big as the maximum 36 * preferred width and height of the components. 37 * The parent is made just big enough to fit them all. 38 * 39 * @param parent - The parent container this is going to be in. 40 * @param rows number of rows 41 * @param cols number of columns 42 * @param initialX x location to start the grid at 43 * @param initialY y location to start the grid at 44 * @param xPad x padding between cells 45 * @param yPad y padding between cells 46 */ makeGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad)47 public static void makeGrid(Container parent, 48 int rows, int cols, 49 int initialX, int initialY, 50 int xPad, int yPad) { 51 SpringLayout layout; 52 try { 53 layout = (SpringLayout)parent.getLayout(); 54 } catch (ClassCastException exc) { 55 System.err.println("The first argument to makeGrid must use SpringLayout."); 56 return; 57 } 58 59 Spring xPadSpring = Spring.constant(xPad); 60 Spring yPadSpring = Spring.constant(yPad); 61 Spring initialXSpring = Spring.constant(initialX); 62 Spring initialYSpring = Spring.constant(initialY); 63 int max = rows * cols; 64 65 //Calculate Springs that are the max of the width/height so that all 66 //cells have the same size. 67 Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)). 68 getWidth(); 69 Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)). 70 getWidth(); 71 for (int i = 1; i < max; i++) { 72 SpringLayout.Constraints cons = layout.getConstraints( 73 parent.getComponent(i)); 74 75 maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth()); 76 maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight()); 77 } 78 79 //Apply the new width/height Spring. This forces all the 80 //components to have the same size. 81 for (int i = 0; i < max; i++) { 82 SpringLayout.Constraints cons = layout.getConstraints( 83 parent.getComponent(i)); 84 85 cons.setWidth(maxWidthSpring); 86 cons.setHeight(maxHeightSpring); 87 } 88 89 //Then adjust the x/y constraints of all the cells so that they 90 //are aligned in a grid. 91 SpringLayout.Constraints lastCons = null; 92 SpringLayout.Constraints lastRowCons = null; 93 for (int i = 0; i < max; i++) { 94 SpringLayout.Constraints cons = layout.getConstraints( 95 parent.getComponent(i)); 96 if (i % cols == 0) { //start of new row 97 lastRowCons = lastCons; 98 cons.setX(initialXSpring); 99 } else { //x position depends on previous component 100 cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), 101 xPadSpring)); 102 } 103 104 if (i / cols == 0) { //first row 105 cons.setY(initialYSpring); 106 } else { //y position depends on previous row 107 cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), 108 yPadSpring)); 109 } 110 lastCons = cons; 111 } 112 113 //Set the parent's size. 114 SpringLayout.Constraints pCons = layout.getConstraints(parent); 115 pCons.setConstraint(SpringLayout.SOUTH, 116 Spring.sum( 117 Spring.constant(yPad), 118 lastCons.getConstraint(SpringLayout.SOUTH))); 119 pCons.setConstraint(SpringLayout.EAST, 120 Spring.sum( 121 Spring.constant(xPad), 122 lastCons.getConstraint(SpringLayout.EAST))); 123 } 124 125 /* Used by makeCompactGrid. */ getConstraintsForCell( int row, int col, Container parent, int cols)126 private static SpringLayout.Constraints getConstraintsForCell( 127 int row, int col, 128 Container parent, 129 int cols) { 130 SpringLayout layout = (SpringLayout) parent.getLayout(); 131 Component c = parent.getComponent(row * cols + col); 132 return layout.getConstraints(c); 133 } 134 135 /** 136 * Aligns the first <code>rows</code> * <code>cols</code> 137 * components of <code>parent</code> in 138 * a grid. Each component in a column is as wide as the maximum 139 * preferred width of the components in that column; 140 * height is similarly determined for each row. 141 * The parent is made just big enough to fit them all. 142 * 143 * @param parent - The parent container this is going to be in. 144 * @param rows number of rows 145 * @param cols number of columns 146 * @param initialX x location to start the grid at 147 * @param initialY y location to start the grid at 148 * @param xPad x padding between cells 149 * @param yPad y padding between cells 150 */ makeCompactGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad)151 public static void makeCompactGrid(Container parent, 152 int rows, int cols, 153 int initialX, int initialY, 154 int xPad, int yPad) { 155 SpringLayout layout; 156 try { 157 layout = (SpringLayout)parent.getLayout(); 158 } catch (ClassCastException exc) { 159 System.err.println("The first argument to makeCompactGrid must use SpringLayout."); 160 return; 161 } 162 163 //Align all cells in each column and make them the same width. 164 Spring x = Spring.constant(initialX); 165 for (int c = 0; c < cols; c++) { 166 Spring width = Spring.constant(0); 167 for (int r = 0; r < rows; r++) { 168 width = Spring.max(width, 169 getConstraintsForCell(r, c, parent, cols). 170 getWidth()); 171 } 172 for (int r = 0; r < rows; r++) { 173 SpringLayout.Constraints constraints = 174 getConstraintsForCell(r, c, parent, cols); 175 constraints.setX(x); 176 constraints.setWidth(width); 177 } 178 x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad))); 179 } 180 181 //Align all cells in each row and make them the same height. 182 Spring y = Spring.constant(initialY); 183 for (int r = 0; r < rows; r++) { 184 Spring height = Spring.constant(0); 185 for (int c = 0; c < cols; c++) { 186 height = Spring.max(height, 187 getConstraintsForCell(r, c, parent, cols). 188 getHeight()); 189 } 190 for (int c = 0; c < cols; c++) { 191 SpringLayout.Constraints constraints = 192 getConstraintsForCell(r, c, parent, cols); 193 constraints.setY(y); 194 constraints.setHeight(height); 195 } 196 y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad))); 197 } 198 199 //Set the parent's size. 200 SpringLayout.Constraints pCons = layout.getConstraints(parent); 201 pCons.setConstraint(SpringLayout.SOUTH, y); 202 pCons.setConstraint(SpringLayout.EAST, x); 203 } 204 } 205