1 27 28 package nextapp.echo2.webcontainer.syncpeer; 29 30 import java.util.ArrayList ; 31 import java.util.BitSet ; 32 import java.util.List ; 33 34 import nextapp.echo2.app.Component; 35 import nextapp.echo2.app.Extent; 36 import nextapp.echo2.app.Grid; 37 import nextapp.echo2.app.LayoutData; 38 import nextapp.echo2.app.layout.GridLayoutData; 39 40 65 public class GridProcessor { 66 67 private static final Integer DEFAULT_SIZE = new Integer (Grid.DEFAULT_SIZE); 68 69 72 private class Cell { 73 74 83 private Cell(Component component, int index, int xSpan, int ySpan) { 84 super(); 85 this.component = component; 86 this.index = index; 87 this.xSpan = xSpan; 88 this.ySpan = ySpan; 89 } 90 91 92 private int xSpan; 93 94 95 private int ySpan; 96 97 98 private Component component; 99 100 104 private int index; 105 } 106 107 private Grid grid; 108 109 118 private List cellArrays; 119 120 121 private int gridXSize; 122 123 124 private int gridYSize; 125 126 130 private List xExtents; 131 132 136 private List yExtents; 137 138 141 private boolean horizontalOrientation; 142 143 152 public GridProcessor(Grid grid) { 153 super(); 154 this.grid = grid; 155 cellArrays = new ArrayList (); 156 157 Integer orientationValue = (Integer ) grid.getRenderProperty(Grid.PROPERTY_ORIENTATION); 158 int orientation = orientationValue == null ? Grid.ORIENTATION_HORIZONTAL : orientationValue.intValue(); 159 horizontalOrientation = orientation != Grid.ORIENTATION_VERTICAL; 160 161 Cell[] cells = createCells(); 162 if (cells == null) { 163 gridXSize = 0; 165 gridYSize = 0; 166 return; 167 } 168 renderCellMatrix(cells); 169 170 calculateExtents(); 171 172 reduceY(); 173 reduceX(); 174 trimX(); 175 } 176 177 private void calculateExtents() { 178 String xProperty = horizontalOrientation ? Grid.PROPERTY_COLUMN_WIDTH : Grid.PROPERTY_ROW_HEIGHT; 179 String yProperty = horizontalOrientation ? Grid.PROPERTY_ROW_HEIGHT : Grid.PROPERTY_COLUMN_WIDTH; 180 181 xExtents = new ArrayList (); 182 for (int i = 0; i < gridXSize; ++i) { 183 xExtents.add(grid.getRenderIndexedProperty(xProperty, i)); 184 } 185 186 yExtents = new ArrayList (); 187 for (int i = 0; i < gridYSize; ++i) { 188 yExtents.add(grid.getRenderIndexedProperty(yProperty, i)); 189 } 190 } 191 192 199 private Cell[] createCells() { 200 Component[] children = grid.getVisibleComponents(); 201 202 if (children.length == 0) { 203 return null; 205 } 206 207 Cell[] cells = new Cell[children.length]; 208 209 for (int i = 0; i < children.length; ++i) { 210 LayoutData layoutData = (LayoutData) children[i].getRenderProperty(Grid.PROPERTY_LAYOUT_DATA); 211 if (layoutData instanceof GridLayoutData) { 212 GridLayoutData gcLayoutData = (GridLayoutData) layoutData; 213 int xSpan = horizontalOrientation ? gcLayoutData.getColumnSpan() : gcLayoutData.getRowSpan(); 214 int ySpan = horizontalOrientation ? gcLayoutData.getRowSpan() : gcLayoutData.getColumnSpan(); 215 cells[i] = new Cell(children[i], i, xSpan, ySpan); 216 } else { 217 cells[i] = new Cell(children[i], i, 1, 1); 218 } 219 } 220 return cells; 221 } 222 223 233 private Cell[] getCellArray(int y, boolean expand) { 234 while (expand && y >= cellArrays.size()) { 235 cellArrays.add(new Cell[gridXSize]); 236 } 237 return (Cell[]) cellArrays.get(y); 238 } 239 240 248 public Component getContent(int column, int row) { 249 if (horizontalOrientation) { 250 Cell cell = getCellArray(row, false)[column]; 251 return cell == null ? null : cell.component; 252 } else { 253 Cell cell = getCellArray(column, false)[row]; 254 return cell == null ? null : cell.component; 255 } 256 } 257 258 268 public int getComponentIndex(int column, int row) { 269 if (horizontalOrientation) { 270 Cell cell = getCellArray(row, false)[column]; 271 return cell == null ? -1 : cell.index; 272 } else { 273 Cell cell = getCellArray(column, false)[row]; 274 return cell == null ? -1 : cell.index; 275 } 276 } 277 278 283 public int getColumnCount() { 284 return horizontalOrientation ? gridXSize : gridYSize; 285 } 286 287 292 public int getRowCount() { 293 return horizontalOrientation ? gridYSize : gridXSize; 294 } 295 296 302 public Extent getColumnWidth(int column) { 303 return (Extent) (horizontalOrientation ? xExtents : yExtents).get(column); 304 } 305 306 312 public Extent getRowHeight(int row) { 313 return (Extent) (horizontalOrientation ? yExtents : xExtents).get(row); 314 } 315 316 324 public int getColumnSpan(int column, int row) { 325 if (horizontalOrientation) { 326 Cell cell = getCellArray(row, false)[column]; 327 return cell == null ? -1 : cell.xSpan; 328 } else { 329 Cell cell = getCellArray(column, false)[row]; 330 return cell == null ? -1 : cell.ySpan; 331 } 332 } 333 334 342 public int getRowSpan(int column, int row) { 343 if (horizontalOrientation) { 344 Cell cell = getCellArray(row, false)[column]; 345 return cell == null ? -1 : cell.ySpan; 346 } else { 347 Cell cell = getCellArray(column, false)[row]; 348 return cell == null ? -1 : cell.xSpan; 349 } 350 } 351 352 356 private void reduceX() { 357 BitSet xRemoves = new BitSet (); 359 int x = 1; 360 int length = getCellArray(0, false).length; 361 while (x < length) { 362 int y = 0; 363 boolean identical = true; 364 while (y < cellArrays.size()) { 365 if (getCellArray(y, false)[x] != getCellArray(y, false)[x - 1]) { 366 identical = false; 367 break; 368 } 369 ++y; 370 } 371 if (identical) { 372 xRemoves.set(x, true); 373 } 374 ++x; 375 } 376 377 if (xRemoves.nextSetBit(0) == -1) { 379 return; 380 } 381 382 for (int removedX = gridXSize - 1; removedX >= 0; --removedX) { 383 if (!xRemoves.get(removedX)) { 384 continue; 385 } 386 387 for (int y = 0; y < gridYSize; ++y) { 388 if (y == 0 || getCellArray(y, false)[removedX - 1] != getCellArray(y - 1, false)[removedX - 1]) { 389 Cell[] cellArray = getCellArray(y, false); 391 if (cellArray[removedX - 1] != null) { 392 --getCellArray(y, false)[removedX - 1].xSpan; 393 } 394 } 395 for (x = removedX; x < gridXSize - 1; ++x) { 396 getCellArray(y, false)[x] = getCellArray(y, false)[x + 1]; 397 } 398 } 399 400 Extent retainedExtent = (Extent) xExtents.get(removedX - 1); 402 Extent removedExtent = (Extent) xExtents.get(removedX); 403 xExtents.remove(removedX); 404 if (removedExtent != null) { 405 xExtents.set(removedX - 1, Extent.add(removedExtent, retainedExtent)); 406 } 407 408 --gridXSize; 409 } 410 } 411 412 416 private void reduceY() { 417 BitSet yRemoves = new BitSet (); 419 int y = 1; 420 421 int size = cellArrays.size(); 422 Cell[] previousCellArray; 423 Cell[] currentCellArray = getCellArray(0, false); 424 425 while (y < size) { 426 previousCellArray = currentCellArray; 427 currentCellArray = getCellArray(y, false); 428 429 int x = 0; 430 boolean identical = true; 431 432 while (x < currentCellArray.length) { 433 if (currentCellArray[x] != previousCellArray[x]) { 434 identical = false; 435 break; 436 } 437 ++x; 438 } 439 if (identical) { 440 yRemoves.set(y, true); 441 } 442 443 ++y; 444 } 445 446 if (yRemoves.nextSetBit(0) == -1) { 448 return; 449 } 450 451 for (int removedY = gridYSize -1; removedY >= 0; --removedY) { 452 if (!yRemoves.get(removedY)) { 453 continue; 454 } 455 456 Cell[] retainedCellArray = getCellArray(removedY - 1, false); 459 for (int x = 0; x < gridXSize; ++x) { 460 if (x == 0 || retainedCellArray[x] != retainedCellArray[x - 1]) { 461 if (retainedCellArray[x] != null) { 463 --retainedCellArray[x].ySpan; 464 } 465 } 466 } 467 468 cellArrays.remove(removedY); 470 471 Extent retainedExtent = (Extent) yExtents.get(removedY - 1); 473 Extent removedExtent = (Extent) yExtents.get(removedY); 474 yExtents.remove(removedY); 475 if (removedExtent != null) { 476 yExtents.set(removedY - 1, Extent.add(removedExtent, retainedExtent)); 477 } 478 479 --gridYSize; 481 } 482 } 483 484 private void renderCellMatrix(Cell[] cells) { 485 gridXSize = ((Integer ) grid.getRenderProperty(Grid.PROPERTY_SIZE, DEFAULT_SIZE)).intValue(); 486 487 int x = 0, y = 0; 488 Cell[] yCells = getCellArray(y, true); 489 for (int componentIndex = 0; componentIndex < cells.length; ++componentIndex) { 490 491 if (cells[componentIndex].xSpan == GridLayoutData.SPAN_FILL || cells[componentIndex].xSpan > gridXSize - x) { 494 cells[componentIndex].xSpan = gridXSize - x; 495 } 496 497 if (cells[componentIndex].xSpan < 1) { 499 cells[componentIndex].xSpan = 1; 500 } 501 if (cells[componentIndex].ySpan < 1) { 503 cells[componentIndex].ySpan = 1; 504 } 505 506 if (cells[componentIndex].xSpan != 1 || cells[componentIndex].ySpan != 1) { 507 for (int xIndex = 1; xIndex < cells[componentIndex].xSpan; ++xIndex) { 511 if (yCells[x + xIndex] != null) { 512 cells[componentIndex].xSpan = xIndex; 514 break; 515 } 516 } 517 for (int yIndex = 0; yIndex < cells[componentIndex].ySpan; ++yIndex) { 518 Cell[] yIndexCells = getCellArray(y + yIndex, true); 519 for (int xIndex = 0; xIndex < cells[componentIndex].xSpan; ++xIndex) { 520 yIndexCells[x + xIndex] = cells[componentIndex]; 521 } 522 } 523 } 524 yCells[x] = cells[componentIndex]; 525 526 if (componentIndex < cells.length - 1) { 527 boolean nextRenderPointFound = false; 529 while (!nextRenderPointFound) { 530 if (x < gridXSize - 1) { 531 ++x; 532 } else { 533 x = 0; 535 ++y; 536 yCells = getCellArray(y, true); 537 538 } 539 nextRenderPointFound = yCells[x] == null; 540 } 541 } 542 } 543 544 gridYSize = cellArrays.size(); 546 } 547 548 552 private void trimX() { 553 if (gridYSize == 1) { 554 Cell[] cellArray = getCellArray(0, false); 555 for (int i = 0; i < gridXSize; ++i) { 556 if (cellArray[i] == null) { 557 gridXSize = i; 558 } 559 } 560 } 561 } 562 } 563 | Popular Tags |