1 package com.calipso.reportgenerator.common; 2 3 import javax.swing.table.AbstractTableModel ; 4 import java.util.Collection ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Vector ; 8 9 12 13 public class HeaderTableModel extends AbstractTableModel { 14 public static final int MODE_ROW = 0; 15 public static final int MODE_COLUMN = 1; 16 private DimensionValueNode dimensionValueNode; 17 private boolean withTotals; 18 private int dimensionCount; 19 private int mode; 20 private int rowCount; 21 private List visibleMetrics; 22 private ReportQuery reportQuery; 23 private ReportSpec reportSpec; 24 private int iterations = 0; 25 26 34 public static HeaderTableModel newRowHeaderTableModel(DimensionValueNode dimensionValueNode, boolean withTotals, int rowCount, ReportQuery reportQuery, ReportSpec reportSpec) { 35 return new HeaderTableModel(dimensionValueNode, withTotals, MODE_ROW, rowCount, reportQuery, reportSpec); 36 } 37 38 46 public static HeaderTableModel newColumnHeaderTableModel(DimensionValueNode dimensionValueNode, boolean withTotals, int rowCount, ReportQuery reportQuery, ReportSpec reportSpec) { 47 return new HeaderTableModel(dimensionValueNode, withTotals, MODE_COLUMN, rowCount, reportQuery, reportSpec); 48 } 49 50 58 59 protected HeaderTableModel(DimensionValueNode dimensionValueNode, boolean withTotals, int mode, int rowCount, ReportQuery reportQuery, ReportSpec reportSpec) { 60 this.dimensionValueNode = dimensionValueNode; 61 this.withTotals = withTotals; 62 this.dimensionCount = dimensionValueNode.getDimensionCount(); 63 this.mode = mode; 64 this.rowCount = rowCount; 65 this.reportQuery = reportQuery; 66 this.reportSpec = reportSpec; 67 initialize(); 68 } 69 70 73 private void initialize() { 74 visibleMetrics = reportQuery.getVisibleMetrics(); 75 } 76 77 78 82 public List getVisibleMetrics() { 83 return visibleMetrics; 84 } 85 86 90 public int getVisibleMetricsCount() { 91 return getVisibleMetrics().size(); 92 } 93 94 100 public DimensionValueNode getDimensionValueNode() { 101 return dimensionValueNode; 102 } 103 104 108 public boolean getWithTotals() { 109 return withTotals; 110 } 111 112 116 117 public void setWithTotals(boolean withTotals) { 118 this.withTotals = withTotals; 119 } 120 121 122 126 public void setDimensionValueNode(DimensionValueNode dimensionValueNode) { 127 this.dimensionValueNode = dimensionValueNode; 128 } 129 130 134 public int getDimensionCount() { 135 return dimensionCount; 136 } 137 138 142 public void setDimensionCount(int dimensionCount) { 143 this.dimensionCount = dimensionCount; 144 } 145 146 151 public int getRowCount() { 152 int count = 0; 153 switch (mode) { 154 case MODE_ROW: 155 return Math.max(doGetRowCount(), 1); 156 case MODE_COLUMN: 157 count = doGetColumnCount(); 158 if ((getVisibleMetricsCount() > 1) || (count == 0)) { 159 count++; 160 } 161 return count; 162 } 163 return count; 164 } 165 166 170 171 public int getColumnCount() { 172 switch (mode) { 173 case MODE_ROW: 174 return Math.max(doGetColumnCount(), 1); 175 case MODE_COLUMN: 176 return doGetRowCount(); 177 } 178 return 0; 179 } 180 181 186 public int doGetRowCount() { 187 return rowCount; 188 } 189 190 195 public void setRowCount(int value) { 196 rowCount = value; 197 } 198 199 204 public int doGetColumnCount() { 205 int count = getDimensionCount(); 206 return count; 207 } 208 209 215 public Object getValueAt(int rowIndex, int columnIndex) { 216 return null; 217 } 218 219 224 public int getNodeColumn(DimensionValueNode node) { 225 return node.getDimesionIndex(); 226 } 227 228 234 public int[][] getNodeCells(DimensionValueNode node, int row, int size) { 235 if (node.getCollapsed()) { 236 return getCollapsedNodeCells(node, row); 237 } 238 else { 239 return getExpandedNodeCells(node, row, size); 240 } 241 } 242 243 249 private int[][] getCollapsedNodeCells(DimensionValueNode node, int row) { 250 int xSize = getDimensionCount() - (node.getDimesionIndex()); 251 int[][] cells = new int[xSize][2]; 252 for (int i = 0; i < xSize; i++) { 253 cells[i] = newCell(row, node.getDimesionIndex() + i); 254 } 255 return cells; 256 } 257 258 265 private int[][] getExpandedNodeCells(DimensionValueNode node, int row, int size) { 266 int xSize = size; 267 int nodeDimIndex = node.getDimesionIndex(); 268 int[][] cells = new int[xSize][2]; 269 for (int i = 0; i < xSize; i++) { 270 cells[i] = newCell(row + i, nodeDimIndex); 271 } 272 return cells; 273 } 274 275 276 282 public int[][] getTotalNodeCells(DimensionValueNode node, int lastRow) { 283 int size = getDimensionCount() - (node.getDimesionIndex() + 1); 284 int[][] cells = new int[size][2]; 285 for (int i = 0; i < size; i++) { 286 cells[i] = newCell(lastRow, node.getDimesionIndex() + 1 + i); 287 } 288 return cells; 289 } 290 291 299 public int[] newCell(int row, int col) { 300 int[] cell = new int[2]; 301 switch (mode) { 302 case MODE_ROW: 303 cell[0] = row; 304 cell[1] = col; 305 break; 306 case MODE_COLUMN: 307 cell[0] = col; 308 cell[1] = row; 309 break; 310 } 311 return cell; 312 } 313 314 318 public int getMode() { 319 return mode; 320 } 321 322 326 public void setMode(int mode) { 327 this.mode = mode; 328 } 329 330 335 public boolean getNodeIsCollapsable(DimensionValueNode node) { 336 return node.getDimesionIndex() < getDimensionValueNode().getDimensionCount() - 1; 337 } 338 339 344 345 public boolean changeNodeState(int row, int col) { 346 DimensionValueNode root = getDimensionValueNode(); 347 int internalRow; 348 int internalCol; 349 if (mode == HeaderTableModel.MODE_ROW) { 350 internalRow = row; 351 internalCol = col; 352 } 353 else { 354 internalRow = col; 355 internalCol = row; 356 } 357 DimensionValueNode node = findCollapsableNode(root, internalRow, internalCol, new RowIndexCarrier()); 358 if (node != null) { 359 node.changeDimensionValueNodeState(); 360 } 361 return node != null; 362 } 363 364 367 private class RowIndexCarrier { 368 private int index = 0; 369 370 public int getIndex() { 371 return index; 372 } 373 374 public void inc(int value) { 375 index += value; 376 } 377 } 378 379 387 private DimensionValueNode findCollapsableNode(DimensionValueNode node, int row, int col, RowIndexCarrier carrier) { 388 DimensionValueNode resultNode = null; 389 Iterator iterator = (node.getSubNodesList()).iterator(); 390 if (carrier.getIndex() == row && node.getDimesionIndex() == col) { 391 resultNode = node; 392 } 393 else { 394 if (!node.getCollapsed()) { 395 while (resultNode == null && iterator.hasNext()) { 396 DimensionValueNode subNode = (DimensionValueNode) iterator.next(); 397 if (carrier.getIndex() <= row && subNode.getDimesionIndex() < getDimensionCount() - 1) { 398 resultNode = findCollapsableNode(subNode, row, col, carrier); 399 } 400 if (subNode.getDimesionIndex() == getDimensionCount() - 1) { 401 carrier.inc(getCarrierIncrement()); 402 } 403 } 404 } 405 if (node.getCollapsed() || getWithTotals()) { 406 carrier.inc(getCarrierIncrement()); 407 } 408 } 409 return resultNode; 410 } 411 412 private int getCarrierIncrement() { 413 if (getMode() == MODE_ROW) { 414 return 1; 415 } 416 else { 417 return getVisibleMetricsCount(); 418 } 419 } 420 421 426 public Object [] getValueFrom(int index) { 427 if (mode == MODE_ROW || getVisibleMetricsCount() == 1) { 428 DimensionValueNode node = (DimensionValueNode) getDimensionValueNode().getAllSubNodes().get(new Integer (index)); 429 return getNodeValues(node); 430 } 431 else { 432 int metricIndex = index % getVisibleMetricsCount(); 433 int actualIndex = index - metricIndex; 434 DimensionValueNode node = (DimensionValueNode) getDimensionValueNode().getAllSubNodes().get(new Integer (actualIndex)); 435 if (node == null && actualIndex == 0) { 436 node = getDimensionValueNode(); 437 } 438 return getNodeValues(node, metricIndex); 439 } 440 } 441 442 public Object [] getValuesFrom(int index) { 443 DimensionValueNode root = getDimensionValueNode(); 444 return getValuesFrom(root, index, new IntegerExt(0)); 445 } 446 447 public Object [] getValuesFrom(DimensionValueNode node, int wantedIndex, IntegerExt integer) { 448 boolean found = false; 449 Object [] returnVal = null; 450 if (node.getSubNodesList().size() == 0) { 451 if (integer.getCurrentValue() == wantedIndex) { 452 returnVal = getValuesFrom(node); 453 return returnVal; 454 } 455 else { 456 integer.sumValue(1); 457 } 458 } 459 else { 460 Collection nodes = node.getSubNodesList(); 461 Iterator iterator = nodes.iterator(); 462 while (iterator.hasNext() && !found) { 463 DimensionValueNode currentNode = (DimensionValueNode) iterator.next(); 464 returnVal = getValuesFrom(currentNode, wantedIndex, integer); 465 if (returnVal != null) { 466 found = true; 467 } 468 } 469 } 470 return returnVal; 471 } 472 473 private Object [] getValuesFrom(DimensionValueNode leaf) { 474 Object returnVal []; 475 if (mode == MODE_ROW) { 476 returnVal = new Object [getColumnCount()]; 477 fillArray(returnVal, leaf, returnVal.length - 1, "ROWS"); 478 } 479 else { 480 returnVal = new Object [getRowCount()]; 481 fillArray(returnVal, leaf, returnVal.length - 1, "COLUMNS"); 482 } 483 return returnVal; 484 } 485 486 private void fillArray(Object [] returnVal, DimensionValueNode node, int i, String mode) { 487 if (!node.getValue().toString().equalsIgnoreCase(mode)) { 488 returnVal[i] = node.getValue().toString(); 489 fillArray(returnVal, node.getParentNode(), i - 1, mode); 490 } 491 } 492 493 498 public String getMetricCaption(int index) { 499 QueryMetric metric = (QueryMetric) reportQuery.getVisibleMetrics().get(index); 500 return reportSpec.getMetricFromName(metric.getName()).getCaption(); 501 } 502 503 510 private Object [] getNodeValues(DimensionValueNode node, int metricIndex) { 511 Object [] auxValues = getNodeValues(node); 512 Object [] result = new Object [auxValues.length + 1]; 513 System.arraycopy(auxValues, 0, result, 0, auxValues.length); 514 result[result.length - 1] = getMetricCaption(metricIndex); 515 return result; 516 } 517 518 523 private Object [] getNodeValues(DimensionValueNode node) { 524 Object [] values = new Object [getDimensionCount()]; 525 DimensionValueNode currentNode = node; 526 while ((currentNode != null) && currentNode.getDimesionIndex() >= 0) { 527 values[currentNode.getDimesionIndex()] = currentNode.getValue().toString(); 528 currentNode = currentNode.getParentNode(); 529 } 530 return values; 531 } 532 533 538 public String getGroupFooterCaption(int dimesionIndex) { 539 if (dimesionIndex >= 0) { 540 List dimensions; 541 if (getMode() == MODE_COLUMN) { 542 dimensions = reportQuery.getColumnDimensions(); 543 } 544 else { 545 dimensions = reportQuery.getRowDimensions(); 546 } 547 QueryDimension dimension = (QueryDimension) dimensions.get(dimesionIndex); 548 String result = reportSpec.getDimensionFromName(dimension.getName()).getGroupFooterCaption(); 549 if ((result!=null)&&(result != "")) return result; 550 } 551 return LanguageTraslator.traslate("358"); 552 } 553 554 559 public boolean isTotalRow(int row) { 560 if(!reportQuery.isVisibleTotals()){ 561 return false; 562 } 563 Vector totals = new Vector (); 564 fillTotalRows(totals, this.getDimensionValueNode()); 565 iterations = 0; 566 return totals.contains(new Integer (row)); 567 } 568 569 574 public boolean isTotalCol(int col) { 575 if(!reportQuery.isVisibleTotals()){ 576 return false; 577 } 578 Vector totals = new Vector (); 579 fillTotalCols(totals, this.getDimensionValueNode()); 580 iterations = 0; 581 return totals.contains(new Integer (col)); 582 } 583 584 589 public void fillTotalCols(Vector totals, DimensionValueNode node) { 590 if (node.getSubNodesList().isEmpty()) { 591 iterations += getVisibleMetricsCount(); 592 return; 593 } 594 Iterator iterator = node.getSubNodesList().iterator(); 595 while (iterator.hasNext()) { 596 DimensionValueNode current = (DimensionValueNode) iterator.next(); 597 fillTotalCols(totals, current); 598 } 599 for (int i = 0; i < getVisibleMetricsCount(); i++) { 600 totals.add(new Integer (iterations)); 601 iterations++; 602 } 603 } 604 605 610 public void fillTotalRows(Vector totals, DimensionValueNode node) { 611 if (node.getSubNodesList().isEmpty()) { 612 iterations++; 613 return; 614 } 615 Iterator iterator = node.getSubNodesList().iterator(); 616 while (iterator.hasNext()) { 617 DimensionValueNode current = (DimensionValueNode) iterator.next(); 618 fillTotalRows(totals, current); 619 } 620 totals.add(new Integer (iterations)); 621 iterations++; 622 } 623 624 } | Popular Tags |