1 package org.objectweb.proactive.p2p.peerconfiguration; 2 import javax.swing.*; 3 import javax.swing.SpringLayout ; 4 import java.awt.*; 5 6 12 public class SpringUtilities { 13 17 public static void printSizes(Component c) { 18 System.out.println("minimumSize = " + c.getMinimumSize()); 19 System.out.println("preferredSize = " + c.getPreferredSize()); 20 System.out.println("maximumSize = " + c.getMaximumSize()); 21 } 22 23 37 public static void makeGrid(Container parent, 38 int rows, int cols, 39 int initialX, int initialY, 40 int xPad, int yPad) { 41 SpringLayout layout; 42 try { 43 layout = (SpringLayout )parent.getLayout(); 44 } catch (ClassCastException exc) { 45 System.err.println("The first argument to makeGrid must use SpringLayout."); 46 return; 47 } 48 49 Spring xPadSpring = Spring.constant(xPad); 50 Spring yPadSpring = Spring.constant(yPad); 51 Spring initialXSpring = Spring.constant(initialX); 52 Spring initialYSpring = Spring.constant(initialY); 53 int max = rows * cols; 54 55 Spring maxWidthSpring = layout.getConstraints(parent.getComponent(0)). 58 getWidth(); 59 Spring maxHeightSpring = layout.getConstraints(parent.getComponent(0)). 60 getWidth(); 61 for (int i = 1; i < max; i++) { 62 SpringLayout.Constraints cons = layout.getConstraints( 63 parent.getComponent(i)); 64 65 maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth()); 66 maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight()); 67 } 68 69 for (int i = 0; i < max; i++) { 72 SpringLayout.Constraints cons = layout.getConstraints( 73 parent.getComponent(i)); 74 75 cons.setWidth(maxWidthSpring); 76 cons.setHeight(maxHeightSpring); 77 } 78 79 SpringLayout.Constraints lastCons = null; 82 SpringLayout.Constraints lastRowCons = null; 83 for (int i = 0; i < max; i++) { 84 SpringLayout.Constraints cons = layout.getConstraints( 85 parent.getComponent(i)); 86 if (i % cols == 0) { lastRowCons = lastCons; 88 cons.setX(initialXSpring); 89 } else { cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), 91 xPadSpring)); 92 } 93 94 if (i / cols == 0) { cons.setY(initialYSpring); 96 } else { cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), 98 yPadSpring)); 99 } 100 lastCons = cons; 101 } 102 103 SpringLayout.Constraints pCons = layout.getConstraints(parent); 105 pCons.setConstraint(SpringLayout.SOUTH, 106 Spring.sum( 107 Spring.constant(yPad), 108 lastCons.getConstraint(SpringLayout.SOUTH))); 109 pCons.setConstraint(SpringLayout.EAST, 110 Spring.sum( 111 Spring.constant(xPad), 112 lastCons.getConstraint(SpringLayout.EAST))); 113 } 114 115 116 private static SpringLayout.Constraints getConstraintsForCell( 117 int row, int col, 118 Container parent, 119 int cols) { 120 SpringLayout layout = (SpringLayout ) parent.getLayout(); 121 Component c = parent.getComponent(row * cols + col); 122 return layout.getConstraints(c); 123 } 124 125 140 public static void makeCompactGrid(Container parent, 141 int rows, int cols, 142 int initialX, int initialY, 143 int xPad, int yPad) { 144 SpringLayout layout; 145 try { 146 layout = (SpringLayout )parent.getLayout(); 147 } catch (ClassCastException exc) { 148 System.err.println("The first argument to makeCompactGrid must use SpringLayout."); 149 return; 150 } 151 152 Spring x = Spring.constant(initialX); 154 for (int c = 0; c < cols; c++) { 155 Spring width = Spring.constant(0); 156 for (int r = 0; r < rows; r++) { 157 width = Spring.max(width, 158 getConstraintsForCell(r, c, parent, cols). 159 getWidth()); 160 } 161 for (int r = 0; r < rows; r++) { 162 SpringLayout.Constraints constraints = 163 getConstraintsForCell(r, c, parent, cols); 164 constraints.setX(x); 165 constraints.setWidth(width); 166 } 167 x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad))); 168 } 169 170 Spring y = Spring.constant(initialY); 172 for (int r = 0; r < rows; r++) { 173 Spring height = Spring.constant(0); 174 for (int c = 0; c < cols; c++) { 175 height = Spring.max(height, 176 getConstraintsForCell(r, c, parent, cols). 177 getHeight()); 178 } 179 for (int c = 0; c < cols; c++) { 180 SpringLayout.Constraints constraints = 181 getConstraintsForCell(r, c, parent, cols); 182 constraints.setY(y); 183 constraints.setHeight(height); 184 } 185 y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad))); 186 } 187 188 SpringLayout.Constraints pCons = layout.getConstraints(parent); 190 pCons.setConstraint(SpringLayout.SOUTH, y); 191 pCons.setConstraint(SpringLayout.EAST, x); 192 } 193 } 194 | Popular Tags |