1 14 package org.wings; 15 16 import org.wings.event.SComponentEvent; 17 import org.wings.event.SComponentListener; 18 19 import java.awt.*; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 23 74 public class SGridBagLayout 75 extends SAbstractLayoutManager implements SComponentListener { 76 79 protected HashMap components = new HashMap (); 80 81 86 protected int nextHorRow = 0; 87 88 91 protected int nextHorCol = 0; 92 93 97 protected int nextVertRow = 0; 98 99 104 protected int nextVertCol = 0; 105 106 109 protected int border = 0; 110 111 116 protected int hgap = 0; 117 118 123 protected int vgap = 0; 124 125 128 protected boolean header = false; 129 130 134 protected GridBagConstraints defaultConstraints = 135 new GridBagConstraints(); 136 137 140 protected Grid currentGrid; 141 142 147 public static final int LAST_CELL = -1; 148 149 152 public SGridBagLayout() {} 153 154 162 public void addComponent(SComponent comp, Object constraint, int index) { 163 currentGrid = null; 165 166 GridBagConstraints c = (GridBagConstraints) constraint; 167 if (c == null) { 168 c = defaultConstraints; 169 } 170 c = (GridBagConstraints) c.clone(); 171 172 if (c.gridx >= 0) { 173 if (c.gridx != nextVertCol) { 174 nextVertRow = 0; 175 } 176 177 if (c.gridy < 0) { 178 c.gridy = nextVertRow; 179 } 180 } else { 181 if (c.gridy >= 0 && c.gridy != nextHorRow) { 182 nextHorCol = 0; 183 } 184 185 if (c.gridy < 0) { 186 c.gridy = nextHorRow; 187 } else if (c.gridy != nextHorRow) { 188 nextHorCol = 0; 189 } 190 c.gridx = nextHorCol; 191 } 192 193 comp.addComponentListener(this); 194 components.put(comp, c); 195 196 if (c.gridx == LAST_CELL) { 197 if (c.gridy == LAST_CELL) { 198 nextHorRow = 0; 199 nextVertRow = 0; 200 } else { 201 nextHorRow = c.gridy + 1; 202 nextVertRow = c.gridy + 1; 203 } 204 nextHorCol = 0; 205 nextVertCol = 0; 206 } else { 207 if (c.gridy == LAST_CELL) { 208 nextHorRow = 0; 209 nextVertRow = 0; 210 nextHorCol = c.gridx + 1; 211 nextVertCol = c.gridx + 1; 212 } else { 213 nextHorCol = c.gridx; 214 nextVertCol = c.gridx; 215 nextHorRow = c.gridy; 216 nextVertRow = c.gridy; 217 218 if (c.gridwidth == GridBagConstraints.RELATIVE) { 219 nextHorCol = LAST_CELL; 220 } else if (c.gridwidth == GridBagConstraints.REMAINDER) { 221 nextHorCol = 0; 222 nextHorRow++; 223 } else { 224 if (c.gridwidth > 0) { 225 nextHorCol += c.gridwidth; 226 } else { 227 nextHorCol++; 228 } 229 } 230 231 if (c.gridheight == GridBagConstraints.RELATIVE) { 232 nextVertRow = LAST_CELL; 233 } else if (c.gridheight == GridBagConstraints.REMAINDER) { 234 nextVertRow = 0; 235 nextVertCol++; 236 } else { 237 if (c.gridheight > 0) { 238 nextVertRow += c.gridheight; 239 } else { 240 nextVertRow++; 241 } 242 } 243 } 244 } 245 } 246 247 public void removeComponent(SComponent c) { 248 currentGrid = null; 250 components.remove(c); 251 c.removeComponentListener(this); 252 } 253 254 public void componentHidden(SComponentEvent e) { 255 currentGrid = null; 257 } 258 259 public void componentMoved(SComponentEvent e) { 260 } 262 263 public void componentResized(SComponentEvent e) { 264 } 266 267 public void componentShown(SComponentEvent e) { 268 currentGrid = null; 270 } 271 272 278 public int getHgap() { 279 return hgap; 280 } 281 282 288 public void setHgap(int hgap) { 289 this.hgap = hgap; 290 } 291 292 298 public int getVgap() { 299 return vgap; 300 } 301 302 308 public void setVgap(int vgap) { 309 this.vgap = vgap; 310 } 311 312 317 public void setBorder(int pixel) { 318 border = pixel; 319 } 320 321 326 public int getBorder() { return border; } 327 328 333 public void setHeader(boolean b) { 334 header = b; 335 } 336 337 342 public boolean getHeader() { return header; } 343 344 346 351 public class Grid { 352 355 public int cols; 356 357 360 public int rows; 361 362 369 public SComponent[][] grid; 370 371 375 public double[] colweight; 376 377 381 public double[] rowweight; 382 383 386 public int firstRow; 387 388 391 public int firstCol; 392 393 396 public Grid() { 397 cols = 0; 398 rows = 0; 399 400 for (Iterator i = components.keySet().iterator(); 401 i.hasNext();) { 402 SComponent comp = (SComponent) i.next(); 403 if (!comp.isVisible()) { 404 continue; 405 } 406 407 GridBagConstraints c = (GridBagConstraints) 408 components.get(comp); 409 if (c.gridx != SGridBagLayout.LAST_CELL) { 410 int col = c.gridx; 411 if (c.gridwidth == GridBagConstraints.RELATIVE) { 412 col++; 413 } else if (c.gridwidth > 1) { 414 col += c.gridwidth - 1; 415 } 416 417 int row = c.gridy; 418 if (c.gridheight == GridBagConstraints.RELATIVE) { 419 row++; 420 } else if (c.gridheight > 1) { 421 row += c.gridheight - 1; 422 } 423 424 if (col >= cols) { 425 cols = col + 1; 426 } 427 if (row >= rows) { 428 rows = row + 1; 429 } 430 } 431 } 432 433 grid = new SComponent[cols][rows]; 434 rowweight = new double[cols]; 435 colweight = new double[rows]; 436 437 for (Iterator i = components.keySet().iterator(); 438 i.hasNext();) { 439 SComponent comp = (SComponent) i.next(); 440 if (!comp.isVisible()) { 441 continue; 442 } 443 GridBagConstraints c = (GridBagConstraints) 444 components.get(comp); 445 446 int maxcol = c.gridx + c.gridwidth; 447 int maxrow = c.gridy + c.gridheight; 448 449 if (c.gridwidth == GridBagConstraints.RELATIVE) { 450 maxcol = cols - 1; 451 } else if (c.gridwidth == GridBagConstraints.REMAINDER) { 452 maxcol = cols; 453 } 454 if (c.gridheight == GridBagConstraints.RELATIVE) { 455 maxrow = rows - 1; 456 } else if (c.gridheight == GridBagConstraints.REMAINDER) { 457 maxrow = rows; 458 } 459 int col = c.gridx; 460 if (col == SGridBagLayout.LAST_CELL) { 461 col = cols - 1; 462 maxcol = cols; 463 } 464 int row = c.gridy; 465 if (row == SGridBagLayout.LAST_CELL) { 466 row = rows - 1; 467 maxrow = rows; 468 } 469 colweight[row] += c.weightx; 470 rowweight[col] += c.weighty; 471 472 for (; col < maxcol; col++) { 473 for (int r = row; r < maxrow; r++) { 474 grid[col][r] = comp; 475 } 476 } 477 } 478 for (firstRow = 0; firstRow < rows; firstRow++) { 479 int col; 480 for (col = 0; col < cols; col++) { 481 if (grid[col][firstRow] != null) { 482 break; 483 } 484 } 485 if (col < cols) { 486 break; 487 } 488 } 489 for (firstCol = 0; firstCol < cols; firstCol++) { 490 int row; 491 for (row = 0; row < rows; row++) { 492 if (grid[firstCol][row] != null) { 493 break; 494 } 495 } 496 if (row < rows) { 497 break; 498 } 499 } 500 } 501 } 502 503 509 public Grid getGrid() { 510 if (currentGrid == null) { 511 currentGrid = new Grid(); 512 } 513 return currentGrid; 514 } 515 516 523 final public GridBagConstraints getConstraints(SComponent comp) { 524 return (GridBagConstraints) components.get(comp); 527 } 528 } 529 530 531 | Popular Tags |