1 13 14 package installer; 15 16 17 import java.awt.*; 18 19 21 71 public class VariableGridLayout implements LayoutManager2, java.io.Serializable 72 { 73 74 public static final int FIXED_NUM_ROWS = 1; 75 public static final int FIXED_NUM_COLUMNS = 2; 76 77 78 public VariableGridLayout(int mode, int size, int hgap, int vgap) { 79 if (mode != FIXED_NUM_ROWS && mode != FIXED_NUM_COLUMNS) { 80 throw new IllegalArgumentException ("illegal mode; value is " + mode); 81 } 82 if (size <= 0) { 83 throw new IllegalArgumentException ("size cannot be zero or less; value is " + size); 84 } 85 if (hgap < 0) { 86 throw new IllegalArgumentException ("hgap cannot be negative; value is " + hgap); 87 } 88 if (vgap < 0) { 89 throw new IllegalArgumentException ("vgap cannot be negative; value is " + vgap); 90 } 91 this.mode = mode; 92 this.size = size; 93 this.hgap = hgap; 94 this.vgap = vgap; 95 } 96 97 98 102 public VariableGridLayout(int mode, int size) { 103 this(mode, size, 0, 0); 104 } 105 106 107 111 public VariableGridLayout() { 112 this(FIXED_NUM_ROWS, 1, 0, 0); 113 } 114 115 116 119 public void addLayoutComponent(String name, Component component) { } 120 121 122 125 public void addLayoutComponent(Component component, Object constraints) { } 126 127 128 131 public void removeLayoutComponent(Component component) { } 132 133 134 137 public float getLayoutAlignmentX(Container container) { 138 return 0.5f; 139 } 140 141 142 145 public float getLayoutAlignmentY(Container container) { 146 return 0.5f; 147 } 148 149 150 public Dimension preferredLayoutSize(Container parent) { 151 return getLayoutSize(parent, 2); 152 } 153 154 155 public Dimension minimumLayoutSize(Container parent) { 156 return getLayoutSize(parent, 0); 157 } 158 159 160 public Dimension maximumLayoutSize(Container parent) { 161 return getLayoutSize(parent, 1); 162 } 163 164 165 public void layoutContainer(Container parent) { 166 synchronized (parent.getTreeLock()) { 167 update(parent); 168 169 int ncomponents = parent.getComponentCount(); 170 171 if (ncomponents == 0) { 172 return; 173 } 174 175 int total_height = 0; 177 for (int r = 0, i = 0; r < nrows; r++) { 178 for (int c = 0; c < ncols; c++, i++) { 179 if (i < ncomponents) { 180 Dimension d = parent.getComponent(i).getPreferredSize(); 181 row_heights[r] = Math.max(row_heights[r], d.height); 182 col_widths[c] = Math.max(col_widths[c], d.width); 183 } else { 184 break; 185 } 186 } 187 total_height += row_heights[r]; 188 } 189 190 int total_width = 0; 191 for (int c = 0; c < ncols; c++) { 192 total_width += col_widths[c]; 193 } 194 195 Dimension parent_size = parent.getSize(); 197 Insets insets = parent.getInsets(); 198 int free_height = parent_size.height - insets.top - insets.bottom - (nrows - 1) * vgap; 199 int free_width = parent_size.width - insets.left - insets.right - (ncols - 1) * hgap; 200 201 if (total_height != free_height) { 202 double dy = (double)free_height / (double)total_height; 203 for (int r = 0; r < nrows; r++) { 204 row_heights[r] = (int) ((double)row_heights[r] * dy); 205 } 206 } 207 208 if (total_width != free_width) { 209 double dx = ((double)free_width) / ((double)total_width); 210 for (int c = 0; c < ncols; c++) { 211 col_widths[c] = (int) ((double)col_widths[c] * dx); 212 } 213 } 214 215 for (int r = 0, y = insets.top, i = 0; r < nrows; y += row_heights[r] + vgap, r++) { 217 for (int c = 0, x = insets.left; c < ncols; x += col_widths[c] + hgap, c++, i++) { 218 if (i < ncomponents) { 219 parent.getComponent(i).setBounds(x, y, col_widths[c], row_heights[r]); 220 } 221 } 222 } 223 224 } } 226 227 228 public void invalidateLayout(Container container) {} 229 230 231 235 public String toString() { 236 return getClass().getName() + "[mode=" + mode + ",size=" + size 237 + ",hgap=" + hgap + ",vgap=" + vgap + "]"; 238 } 239 240 241 246 private Dimension getLayoutSize(Container parent, int which) { 247 synchronized (parent.getTreeLock()){ 248 update(parent); 249 250 int ncomponents = parent.getComponentCount(); 251 int h = 0; 252 int w = 0; 253 254 for (int r = 0, i = 0; r < nrows; r++) { 255 int row_height = 0; 256 for (int c = 0; c < ncols; c++, i++) { 257 if (i < ncomponents) { 258 switch (which) { 259 case 0: 260 row_height = Math.max(row_height, parent.getComponent(i).getMinimumSize().height); 261 break; 262 case 1: 263 row_height = Math.max(row_height, parent.getComponent(i).getMaximumSize().height); 264 break; 265 default: 266 row_height = Math.max(row_height, parent.getComponent(i).getPreferredSize().height); 267 break; 268 } 269 } else { 270 break; 271 } 272 } 273 h += row_height; 274 } 275 276 for (int c = 0; c < ncols; c++) { 277 int col_width = 0; 278 for (int r = 0; r < nrows; r++) { 279 int i = r * ncols + c; 280 if (i < ncomponents) { 281 switch (which) { 282 case 0: 283 col_width = Math.max(col_width, parent.getComponent(i).getMinimumSize().width); 284 break; 285 case 1: 286 col_width = Math.max(col_width, parent.getComponent(i).getMaximumSize().width); 287 break; 288 default: 289 col_width = Math.max(col_width, parent.getComponent(i).getPreferredSize().width); 290 break; 291 } 292 } else { 293 break; 294 } 295 } 296 w += col_width; 297 } 298 299 Insets insets = parent.getInsets(); 300 return new Dimension(w + insets.left + insets.right + ((ncols - 1) * hgap), 301 h + insets.top + insets.bottom + ((nrows - 1) * vgap)); 302 } 303 } 304 305 306 private void update(Container container) { 307 int ncomponents = container.getComponentCount(); 308 int old_nrows = nrows; 309 int old_ncols = ncols; 310 if (this.mode == FIXED_NUM_ROWS) { 311 nrows = this.size; 312 ncols = (ncomponents + nrows - 1) / nrows; 313 } else { 314 ncols = this.size; 315 nrows = (ncomponents + ncols - 1) / ncols; 316 } 317 if (old_nrows != nrows) { 318 row_heights = new int[nrows]; 319 } 320 if (old_ncols != ncols) { 321 col_widths = new int[ncols]; 322 } 323 } 324 325 326 private int mode; 327 private int size; 328 private int hgap; 329 private int vgap; 330 private transient int nrows = -1; 331 private transient int ncols = -1; 332 private transient int[] row_heights = null; 333 private transient int[] col_widths = null; 334 } 335 | Popular Tags |