1 28 package net.sf.jasperreports.crosstabs.base; 29 30 import java.io.IOException ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import net.sf.jasperreports.crosstabs.JRCellContents; 35 import net.sf.jasperreports.crosstabs.JRCrosstab; 36 import net.sf.jasperreports.crosstabs.JRCrosstabCell; 37 import net.sf.jasperreports.crosstabs.JRCrosstabColumnGroup; 38 import net.sf.jasperreports.crosstabs.JRCrosstabDataset; 39 import net.sf.jasperreports.crosstabs.JRCrosstabGroup; 40 import net.sf.jasperreports.crosstabs.JRCrosstabMeasure; 41 import net.sf.jasperreports.crosstabs.JRCrosstabParameter; 42 import net.sf.jasperreports.crosstabs.JRCrosstabRowGroup; 43 import net.sf.jasperreports.crosstabs.design.JRDesignCrosstab; 44 import net.sf.jasperreports.engine.JRAbstractObjectFactory; 45 import net.sf.jasperreports.engine.JRChild; 46 import net.sf.jasperreports.engine.JRConstants; 47 import net.sf.jasperreports.engine.JRElement; 48 import net.sf.jasperreports.engine.JRExpression; 49 import net.sf.jasperreports.engine.JRExpressionCollector; 50 import net.sf.jasperreports.engine.JRVariable; 51 import net.sf.jasperreports.engine.base.JRBaseElement; 52 import net.sf.jasperreports.engine.base.JRBaseObjectFactory; 53 import net.sf.jasperreports.engine.util.JRStyleResolver; 54 import net.sf.jasperreports.engine.xml.JRXmlWriter; 55 56 62 public class JRBaseCrosstab extends JRBaseElement implements JRCrosstab 63 { 64 private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID; 65 66 protected int id; 67 protected JRCrosstabParameter[] parameters; 68 protected JRVariable[] variables; 69 protected JRExpression parametersMapExpression; 70 protected JRCrosstabDataset dataset; 71 protected JRCrosstabRowGroup[] rowGroups; 72 protected JRCrosstabColumnGroup[] columnGroups; 73 protected JRCrosstabMeasure[] measures; 74 protected int columnBreakOffset; 75 protected boolean repeatColumnHeaders = true; 76 protected boolean repeatRowHeaders = true; 77 protected JRCrosstabCell[][] cells; 78 protected JRCellContents whenNoDataCell; 79 protected JRCellContents headerCell; 80 81 public JRBaseCrosstab(JRCrosstab crosstab, JRBaseObjectFactory factory, int id) 82 { 83 super(crosstab, factory); 84 85 this.id = id; 86 87 this.columnBreakOffset = crosstab.getColumnBreakOffset(); 88 this.repeatColumnHeaders = crosstab.isRepeatColumnHeaders(); 89 this.repeatRowHeaders = crosstab.isRepeatRowHeaders(); 90 91 this.dataset = factory.getCrosstabDataset(crosstab.getDataset()); 92 93 copyParameters(crosstab, factory); 94 copyVariables(crosstab, factory); 95 headerCell = factory.getCell(crosstab.getHeaderCell()); 96 copyRowGroups(crosstab, factory); 97 copyColumnGroups(crosstab, factory); 98 copyMeasures(crosstab, factory); 99 copyCells(crosstab, factory); 100 101 whenNoDataCell = factory.getCell(crosstab.getWhenNoDataCell()); 102 } 103 104 107 public byte getMode() 108 { 109 return JRStyleResolver.getMode(this, MODE_TRANSPARENT); 110 } 111 112 private void copyParameters(JRCrosstab crosstab, JRBaseObjectFactory factory) 113 { 114 JRCrosstabParameter[] crossParameters = crosstab.getParameters(); 115 if (crossParameters != null) 116 { 117 parameters = new JRCrosstabParameter[crossParameters.length]; 118 for (int i = 0; i < parameters.length; i++) 119 { 120 parameters[i] = factory.getCrosstabParameter(crossParameters[i]); 121 } 122 } 123 124 parametersMapExpression = factory.getExpression(crosstab.getParametersMapExpression()); 125 } 126 127 private void copyVariables(JRCrosstab crosstab, JRBaseObjectFactory factory) 128 { 129 JRVariable[] vars = crosstab.getVariables(); 130 if (vars != null) 131 { 132 variables = new JRVariable[vars.length]; 133 for (int i = 0; i < vars.length; i++) 134 { 135 variables[i] = factory.getVariable(vars[i]); 136 } 137 } 138 } 139 140 private void copyRowGroups(JRCrosstab crosstab, JRBaseObjectFactory factory) 141 { 142 JRCrosstabRowGroup[] crossRowGroups = crosstab.getRowGroups(); 143 if (crossRowGroups != null) 144 { 145 this.rowGroups = new JRCrosstabRowGroup[crossRowGroups.length]; 146 for (int i = 0; i < crossRowGroups.length; ++i) 147 { 148 this.rowGroups[i] = factory.getCrosstabRowGroup(crossRowGroups[i]); 149 } 150 } 151 } 152 153 private void copyColumnGroups(JRCrosstab crosstab, JRBaseObjectFactory factory) 154 { 155 JRCrosstabColumnGroup[] crossColumnGroups = crosstab.getColumnGroups(); 156 if (crossColumnGroups != null) 157 { 158 this.columnGroups = new JRCrosstabColumnGroup[crossColumnGroups.length]; 159 for (int i = 0; i < crossColumnGroups.length; ++i) 160 { 161 this.columnGroups[i] = factory.getCrosstabColumnGroup(crossColumnGroups[i]); 162 } 163 } 164 } 165 166 private void copyMeasures(JRCrosstab crosstab, JRBaseObjectFactory factory) 167 { 168 JRCrosstabMeasure[] crossMeasures = crosstab.getMeasures(); 169 if (crossMeasures != null) 170 { 171 this.measures = new JRCrosstabMeasure[crossMeasures.length]; 172 for (int i = 0; i < crossMeasures.length; ++i) 173 { 174 this.measures[i] = factory.getCrosstabMeasure(crossMeasures[i]); 175 } 176 } 177 } 178 179 private void copyCells(JRCrosstab crosstab, JRBaseObjectFactory factory) 180 { 181 JRCrosstabCell[][] crossCells = crosstab.getCells(); 182 if (crossCells != null) 183 { 184 this.cells = new JRCrosstabCell[rowGroups.length + 1][columnGroups.length + 1]; 185 for (int i = 0; i <= rowGroups.length; i++) 186 { 187 for (int j = 0; j <= columnGroups.length; ++j) 188 { 189 this.cells[i][j] = factory.getCrosstabCell(crossCells[i][j]); 190 } 191 } 192 } 193 } 194 195 public int getId() 196 { 197 return id; 198 } 199 200 public JRCrosstabDataset getDataset() 201 { 202 return dataset; 203 } 204 205 public JRCrosstabRowGroup[] getRowGroups() 206 { 207 return rowGroups; 208 } 209 210 public JRCrosstabColumnGroup[] getColumnGroups() 211 { 212 return columnGroups; 213 } 214 215 public JRCrosstabMeasure[] getMeasures() 216 { 217 return measures; 218 } 219 220 public void collectExpressions(JRExpressionCollector collector) 221 { 222 collector.collect(this); 223 } 224 225 public JRChild getCopy(JRAbstractObjectFactory factory) 226 { 227 return factory.getCrosstab(this); 228 } 229 230 public void writeXml(JRXmlWriter writer) throws IOException 231 { 232 writer.writeCrosstab(this); 233 } 234 235 public int getColumnBreakOffset() 236 { 237 return columnBreakOffset; 238 } 239 240 public boolean isRepeatColumnHeaders() 241 { 242 return repeatColumnHeaders; 243 } 244 245 public boolean isRepeatRowHeaders() 246 { 247 return repeatRowHeaders; 248 } 249 250 public JRCrosstabCell[][] getCells() 251 { 252 return cells; 253 } 254 255 public JRCrosstabParameter[] getParameters() 256 { 257 return parameters; 258 } 259 260 public JRExpression getParametersMapExpression() 261 { 262 return parametersMapExpression; 263 } 264 265 public JRCellContents getWhenNoDataCell() 266 { 267 return whenNoDataCell; 268 } 269 270 public static JRElement getElementByKey(JRCrosstab crosstab, String key) 271 { 272 JRElement element = null; 273 274 if (crosstab.getHeaderCell() != null) 275 { 276 element = crosstab.getHeaderCell().getElementByKey(key); 277 } 278 279 element = getHeadersElement(crosstab.getRowGroups(), key); 280 281 if (element == null) 282 { 283 element = getHeadersElement(crosstab.getColumnGroups(), key); 284 } 285 286 if (element == null) 287 { 288 if (crosstab instanceof JRDesignCrosstab) 289 { 290 List cellsList = ((JRDesignCrosstab) crosstab).getCellsList(); 291 for (Iterator it = cellsList.iterator(); element == null && it.hasNext();) 292 { 293 JRCrosstabCell cell = (JRCrosstabCell) it.next(); 294 element = cell.getContents().getElementByKey(key); 295 } 296 } 297 else 298 { 299 JRCrosstabCell[][] cells = crosstab.getCells(); 300 for (int i = cells.length - 1; element == null && i >= 0; --i) 301 { 302 for (int j = cells[i].length - 1; element == null && j >= 0; --j) 303 { 304 JRCrosstabCell cell = cells[i][j]; 305 if (cell != null) 306 { 307 element = cell.getContents().getElementByKey(key); 308 } 309 } 310 } 311 } 312 } 313 314 if (element == null && crosstab.getWhenNoDataCell() != null) 315 { 316 element = crosstab.getWhenNoDataCell().getElementByKey(key); 317 } 318 319 return element; 320 } 321 322 private static JRElement getHeadersElement(JRCrosstabGroup[] groups, String key) 323 { 324 JRElement element = null; 325 326 if (groups != null) 327 { 328 for (int i = 0; element == null && i < groups.length; i++) 329 { 330 JRCellContents header = groups[i].getHeader(); 331 element = header.getElementByKey(key); 332 333 if (element == null) 334 { 335 JRCellContents totalHeader = groups[i].getTotalHeader(); 336 element = totalHeader.getElementByKey(key); 337 } 338 } 339 } 340 341 return element; 342 } 343 344 345 public JRElement getElementByKey(String elementKey) 346 { 347 return getElementByKey(this, elementKey); 348 } 349 350 public JRCellContents getHeaderCell() 351 { 352 return headerCell; 353 } 354 355 public JRVariable[] getVariables() 356 { 357 return variables; 358 } 359 } 360 | Popular Tags |