1 package com.calipso.reportgenerator.userinterface; 2 3 import com.calipso.reportgenerator.common.DimensionValueNode; 4 import com.calipso.reportgenerator.common.HeaderTableModel; 5 6 import javax.swing.*; 7 import java.awt.*; 8 import java.util.Vector ; 9 import java.util.Iterator ; 10 11 16 public class ColumnHeaderTableUI extends HeaderTableUI { 17 18 26 protected void doPaintNode(DimensionValueNode node, Graphics g, int[] columnIndexes, int[] columnSizes, int from, int to) { 27 Rectangle rect = null; 28 int nodeDimIndex = node.getDimesionIndex(); 29 int dimCount = getModel().getDimensionCount(); 30 boolean isLastDim = nodeDimIndex == dimCount - 1; 31 if (isLastDim) { 32 for (int i = 0; i < dimCount - 1; i++) { 33 columnSizes[i] += getModel().getVisibleMetricsCount(); 34 } 35 } 36 if (getModel().getWithTotals() && !isLastDim) { 37 if (node.getCollapsed()) { 38 for (int i = 0; i <= nodeDimIndex; i++) { 39 columnSizes[i] += getModel().getVisibleMetricsCount(); 40 } 41 for (int i = nodeDimIndex + 1; i < dimCount; i++) { 42 columnIndexes[i] += getModel().getVisibleMetricsCount(); 43 } 44 count += getModel().getVisibleMetricsCount(); 45 } 46 else { 47 int index = columnIndexes[nodeDimIndex + 1]; 48 rect = getTotalRectangleFromNode(node, index); 49 getAllSubNodes().put(new Integer (index), node); 50 if((count >= from) && (count <= to)){ 51 paintRectangle(rect, g, getGroupFooterCaption(node), false, false); 52 } 53 count+=getModel().getVisibleMetricsCount(); 54 for (int i = 0; i <= nodeDimIndex; i++) { 55 columnSizes[i] += getModel().getVisibleMetricsCount(); 56 } 57 for (int i = nodeDimIndex + 1; i < dimCount; i++) { 58 columnIndexes[i] += getModel().getVisibleMetricsCount(); 59 } 60 } 61 } 62 if (nodeDimIndex >= 0) { 63 int size = Math.max(columnSizes[nodeDimIndex], getModel().getVisibleMetricsCount()); 64 int index = columnIndexes[nodeDimIndex]; 65 rect = getRectangleFromNode(node, index, size); 66 if((count >= from) && (count <= to)){ 67 paintRectangle(rect, g, node.getValue(), getNodeIsCollapsable(node), node.getCollapsed()); 68 }else{ 69 } 71 if (isLastDim) { 72 getAllSubNodes().put(new Integer (index), node); 73 count += getModel().getVisibleMetricsCount(); 74 } 75 columnIndexes[nodeDimIndex] += size; 76 } 77 } 78 79 80 87 protected Rectangle getRectangleFromNode(DimensionValueNode node, int index, int size) { 88 int metCount = getModel().getVisibleMetricsCount(); 89 if (metCount == 1) { 90 return super.getRectangleFromNode(node, index, size); 91 } 92 else { 93 int[][] cells = ((HeaderTableModel) table.getModel()).getNodeCells(node, index, size); 94 int[][] xCells; 95 if (node.getCollapsed()){ 96 xCells = new int[cells.length * metCount][2]; 97 for (int i = 0; i < cells.length; i++) { 98 for (int j = 0; j < metCount; j++) { 99 xCells[(i * metCount) + j][0] = cells[i][0]; 100 xCells[(i * metCount) + j][1] = cells[i][1] + j; 101 } 102 } 103 } 104 else { 105 xCells = new int[cells.length * metCount][2]; 106 for (int i = 0; i < cells.length; i++) { 107 for (int j = 0; j < metCount; j++) { 108 xCells[(i * metCount) + j][0] = cells[i][0] + j; 109 xCells[(i * metCount) + j][1] = cells[i][1]; 110 } 111 } 112 return super.doGetRectangle(cells); 113 } 114 Rectangle rect = doGetRectangle(xCells); 115 return rect; 116 } 117 } 118 119 126 protected Rectangle getTotalRectangleFromNode(DimensionValueNode node, int lastRow) { 127 int metCount = getModel().getVisibleMetricsCount(); 128 if (metCount == 1) { 129 return super.getTotalRectangleFromNode(node, lastRow); 130 } 131 else { 132 int[][] cells = ((HeaderTableModel) table.getModel()).getTotalNodeCells(node, lastRow); 133 int[][] xCells; 134 xCells = new int[cells.length * metCount][2]; 135 for (int i = 0; i < cells.length; i++) { 136 for (int j = 0; j < metCount; j++) { 137 xCells[(i * metCount) + j][0] = cells[i][0]; 138 xCells[(i * metCount) + j][1] = cells[i][1] + j; 139 } 140 } 141 Rectangle rect = doGetRectangle(xCells); 142 return rect; 143 } 144 } 145 146 151 protected int getVisibleCellCount() { 152 int from = ((JScrollPane)table.getParent().getParent().getParent()).getHorizontalScrollBar().getValue(); 153 int col=0; 154 int acum = 0; 155 int cant = table.getColumnCount(); 156 while(col<cant && acum < from){ 157 acum += table.getColumnModel().getColumn(col).getWidth(); 158 col++; 159 } 160 int width = table.getParent().getParent().getParent().getWidth(); 161 acum = 0; 162 int columns=0; 163 while(col < cant && acum < width){ 164 acum += table.getColumnModel().getColumn(col).getWidth(); 165 columns++; 166 col++; 167 } 168 return columns; 169 } 170 171 177 protected int getFromCell(){ 178 int from = ((JScrollPane)table.getParent().getParent().getParent()).getHorizontalScrollBar().getValue(); 179 int col=0; 180 int acum = 0; 181 int cant = table.getColumnCount(); 182 while(col<cant && acum < from){ 183 acum += table.getColumnModel().getColumn(col).getWidth() + table.getColumnModel().getColumnMargin(); 184 col++; 185 } 186 if(col!=0){ 187 col--; 188 return getFirstColForGroup(col)-1; 189 } 190 return col; 191 } 192 193 199 private int getFirstColForGroup(int col) { 200 DimensionValueNode node = getModel().getDimensionValueNode(); 201 if(getModel().getDimensionCount()==1){ 202 int columnasAlInicioDeGrupo = (int)(col - (Math.floor(col / getModel().getVisibleMetricsCount()) * getModel().getVisibleMetricsCount())); 205 return col - columnasAlInicioDeGrupo; 206 }else{ 207 Vector endings = getEndingsVector(); 208 int i = 0; 209 while(i<endings.size() && (((Integer )endings.elementAt(i)).intValue() < col)){ 210 i++; 211 } 212 if(i > 0){ 213 return ((Integer )endings.elementAt(i-1)).intValue(); 214 }else{ 215 return 0; 216 } 217 } 218 } 219 220 226 protected void fillEndingCellForGroups(Vector endings, boolean withTotals, DimensionValueNode node) { 227 if (node.getSubNodesList().isEmpty() || node.getCollapsed()) { 228 iterations += getModel().getVisibleMetricsCount(); 229 if(node.getCollapsed()){ 230 endings.add(new Integer (iterations-1)); 231 } 232 return; 233 } 234 Iterator iterator = node.getSubNodesList().iterator(); 235 while (iterator.hasNext()) { 236 DimensionValueNode current = (DimensionValueNode) iterator.next(); 237 fillEndingCellForGroups(endings, withTotals, current); 238 } 239 if(withTotals){ 240 iterations += getModel().getVisibleMetricsCount(); 241 if(node.getDimesionIndex() == 0){ 242 endings.add(new Integer (iterations-1)); 243 } 244 }else if(!endings.isEmpty()){ 245 if(node.getDimesionIndex() == 0){ 246 if(((Integer )endings.lastElement()).intValue() != iterations){ 247 endings.add(new Integer (iterations-1)); 248 } 249 } 250 }else if(node.getDimesionIndex() == 0){ 251 endings.add(new Integer (iterations-1)); 252 } 253 } 254 255 261 protected int getToCell(){ 262 int from = ((JScrollPane)table.getParent().getParent().getParent()).getHorizontalScrollBar().getValue(); 263 int to=0; 264 int acum = 0; 265 int cant = table.getColumnCount(); 266 while(to<cant && acum < from){ 267 acum += table.getColumnModel().getColumn(to).getWidth(); 268 to++; 269 } 270 int width = table.getParent().getParent().getParent().getWidth(); 271 acum = 0; 272 while(to < cant && acum < width){ 273 acum += table.getColumnModel().getColumn(to).getWidth(); 274 to++; 275 } 276 int count = getModel().getColumnCount(); 277 if(count < to){ 278 return count; 279 } 280 if(getModel().getDimensionCount() == 1){ 281 return to; 282 }else{ 283 int col = to; 284 to = getLastColForGroup(to); 285 while((to <= col) && (to < count)){ 286 to = getLastColForGroup(to + 1); 287 } 288 } 289 return ++to; 290 } 291 292 297 private int getLastColForGroup(int from) { 298 DimensionValueNode node = getModel().getDimensionValueNode(); 299 if(!node.getSubNodesList().isEmpty()){ 300 if(node.getDimensionCount()==1){ 301 return (from + getVisibleCellCount()); 302 }else{ 303 Vector endings = getEndingsVector(); 304 int i=0; 305 while((i<endings.size()) && (((Integer )endings.elementAt(i)).intValue() < from)){ 306 i++; 307 } 308 if(i==endings.size()){ 309 return getModel().getColumnCount(); 310 }else{ 311 return ((Integer )endings.elementAt(i)).intValue(); 312 } 313 } 314 }else{ 315 return from += getModel().getVisibleMetricsCount(); 317 } 318 } 319 320 324 protected void paintCells(Graphics g) { 325 super.paintCells(g); 326 count = 0; 327 if ((getModel().getVisibleMetricsCount() > 1) || (getModel().getDimensionCount() == 0 )){ 328 paintMetricNames(g); 329 } 330 } 331 332 333 337 private void paintMetricNames(Graphics g) { 338 int row = getModel().getDimensionCount(); 339 int columnCount = getModel().getColumnCount() / getModel().getVisibleMetricsCount(); 340 count = 0; 341 int from = getFromCell(); 342 int to = getToCell(); 343 for (int i = 0; i < columnCount; i++) { 344 for (int j = 0; j < getModel().getVisibleMetricsCount(); j++) { 345 int[] cell = new int[]{row, (i * getModel().getVisibleMetricsCount()) + j}; 346 Rectangle rect = table.getCellRect(cell[0], cell[1], true); 347 if((count >= from) && (count <= to)){ 348 paintRectangle(rect, g, getMetricCaption(j), false, false); 349 } 350 count++; 351 } 352 } 353 } 354 355 360 private String getMetricCaption(int index) { 361 return getModel().getMetricCaption(index); 362 } 363 364 } 365 | Popular Tags |