001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.crosstabs.base;
029:
030: import java.io.IOException;
031: import java.util.Iterator;
032: import java.util.List;
033:
034: import net.sf.jasperreports.crosstabs.JRCellContents;
035: import net.sf.jasperreports.crosstabs.JRCrosstab;
036: import net.sf.jasperreports.crosstabs.JRCrosstabCell;
037: import net.sf.jasperreports.crosstabs.JRCrosstabColumnGroup;
038: import net.sf.jasperreports.crosstabs.JRCrosstabDataset;
039: import net.sf.jasperreports.crosstabs.JRCrosstabGroup;
040: import net.sf.jasperreports.crosstabs.JRCrosstabMeasure;
041: import net.sf.jasperreports.crosstabs.JRCrosstabParameter;
042: import net.sf.jasperreports.crosstabs.JRCrosstabRowGroup;
043: import net.sf.jasperreports.crosstabs.design.JRDesignCrosstab;
044: import net.sf.jasperreports.engine.JRAbstractObjectFactory;
045: import net.sf.jasperreports.engine.JRChild;
046: import net.sf.jasperreports.engine.JRConstants;
047: import net.sf.jasperreports.engine.JRElement;
048: import net.sf.jasperreports.engine.JRExpression;
049: import net.sf.jasperreports.engine.JRExpressionCollector;
050: import net.sf.jasperreports.engine.JRVariable;
051: import net.sf.jasperreports.engine.base.JRBaseElement;
052: import net.sf.jasperreports.engine.base.JRBaseObjectFactory;
053: import net.sf.jasperreports.engine.util.JRStyleResolver;
054: import net.sf.jasperreports.engine.xml.JRXmlWriter;
055:
056: /**
057: * Base read-only {@link net.sf.jasperreports.crosstabs.JRCrosstab crosstab} implementation.
058: *
059: * @author Lucian Chirita (lucianc@users.sourceforge.net)
060: * @version $Id: JRBaseCrosstab.java 1741 2007-06-08 10:53:33Z lucianc $
061: */
062: public class JRBaseCrosstab extends JRBaseElement implements JRCrosstab {
063: private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
064:
065: protected int id;
066: protected JRCrosstabParameter[] parameters;
067: protected JRVariable[] variables;
068: protected JRExpression parametersMapExpression;
069: protected JRCrosstabDataset dataset;
070: protected JRCrosstabRowGroup[] rowGroups;
071: protected JRCrosstabColumnGroup[] columnGroups;
072: protected JRCrosstabMeasure[] measures;
073: protected int columnBreakOffset;
074: protected boolean repeatColumnHeaders = true;
075: protected boolean repeatRowHeaders = true;
076: protected byte runDirection;
077: protected JRCrosstabCell[][] cells;
078: protected JRCellContents whenNoDataCell;
079: protected JRCellContents headerCell;
080:
081: public JRBaseCrosstab(JRCrosstab crosstab,
082: JRBaseObjectFactory factory, int id) {
083: super (crosstab, factory);
084:
085: this .id = id;
086:
087: this .columnBreakOffset = crosstab.getColumnBreakOffset();
088: this .repeatColumnHeaders = crosstab.isRepeatColumnHeaders();
089: this .repeatRowHeaders = crosstab.isRepeatRowHeaders();
090: this .runDirection = crosstab.getRunDirection();
091:
092: this .dataset = factory
093: .getCrosstabDataset(crosstab.getDataset());
094:
095: copyParameters(crosstab, factory);
096: copyVariables(crosstab, factory);
097: headerCell = factory.getCell(crosstab.getHeaderCell());
098: copyRowGroups(crosstab, factory);
099: copyColumnGroups(crosstab, factory);
100: copyMeasures(crosstab, factory);
101: copyCells(crosstab, factory);
102:
103: whenNoDataCell = factory.getCell(crosstab.getWhenNoDataCell());
104: }
105:
106: /**
107: *
108: */
109: public byte getMode() {
110: return JRStyleResolver.getMode(this , MODE_TRANSPARENT);
111: }
112:
113: private void copyParameters(JRCrosstab crosstab,
114: JRBaseObjectFactory factory) {
115: JRCrosstabParameter[] crossParameters = crosstab
116: .getParameters();
117: if (crossParameters != null) {
118: parameters = new JRCrosstabParameter[crossParameters.length];
119: for (int i = 0; i < parameters.length; i++) {
120: parameters[i] = factory
121: .getCrosstabParameter(crossParameters[i]);
122: }
123: }
124:
125: parametersMapExpression = factory.getExpression(crosstab
126: .getParametersMapExpression());
127: }
128:
129: private void copyVariables(JRCrosstab crosstab,
130: JRBaseObjectFactory factory) {
131: JRVariable[] vars = crosstab.getVariables();
132: if (vars != null) {
133: variables = new JRVariable[vars.length];
134: for (int i = 0; i < vars.length; i++) {
135: variables[i] = factory.getVariable(vars[i]);
136: }
137: }
138: }
139:
140: private void copyRowGroups(JRCrosstab crosstab,
141: JRBaseObjectFactory factory) {
142: JRCrosstabRowGroup[] crossRowGroups = crosstab.getRowGroups();
143: if (crossRowGroups != null) {
144: this .rowGroups = new JRCrosstabRowGroup[crossRowGroups.length];
145: for (int i = 0; i < crossRowGroups.length; ++i) {
146: this .rowGroups[i] = factory
147: .getCrosstabRowGroup(crossRowGroups[i]);
148: }
149: }
150: }
151:
152: private void copyColumnGroups(JRCrosstab crosstab,
153: JRBaseObjectFactory factory) {
154: JRCrosstabColumnGroup[] crossColumnGroups = crosstab
155: .getColumnGroups();
156: if (crossColumnGroups != null) {
157: this .columnGroups = new JRCrosstabColumnGroup[crossColumnGroups.length];
158: for (int i = 0; i < crossColumnGroups.length; ++i) {
159: this .columnGroups[i] = factory
160: .getCrosstabColumnGroup(crossColumnGroups[i]);
161: }
162: }
163: }
164:
165: private void copyMeasures(JRCrosstab crosstab,
166: JRBaseObjectFactory factory) {
167: JRCrosstabMeasure[] crossMeasures = crosstab.getMeasures();
168: if (crossMeasures != null) {
169: this .measures = new JRCrosstabMeasure[crossMeasures.length];
170: for (int i = 0; i < crossMeasures.length; ++i) {
171: this .measures[i] = factory
172: .getCrosstabMeasure(crossMeasures[i]);
173: }
174: }
175: }
176:
177: private void copyCells(JRCrosstab crosstab,
178: JRBaseObjectFactory factory) {
179: JRCrosstabCell[][] crossCells = crosstab.getCells();
180: if (crossCells != null) {
181: this .cells = new JRCrosstabCell[rowGroups.length + 1][columnGroups.length + 1];
182: for (int i = 0; i <= rowGroups.length; i++) {
183: for (int j = 0; j <= columnGroups.length; ++j) {
184: this .cells[i][j] = factory
185: .getCrosstabCell(crossCells[i][j]);
186: }
187: }
188: }
189: }
190:
191: public int getId() {
192: return id;
193: }
194:
195: public JRCrosstabDataset getDataset() {
196: return dataset;
197: }
198:
199: public JRCrosstabRowGroup[] getRowGroups() {
200: return rowGroups;
201: }
202:
203: public JRCrosstabColumnGroup[] getColumnGroups() {
204: return columnGroups;
205: }
206:
207: public JRCrosstabMeasure[] getMeasures() {
208: return measures;
209: }
210:
211: public void collectExpressions(JRExpressionCollector collector) {
212: collector.collect(this );
213: }
214:
215: public JRChild getCopy(JRAbstractObjectFactory factory) {
216: return factory.getCrosstab(this );
217: }
218:
219: public void writeXml(JRXmlWriter writer) throws IOException {
220: writer.writeCrosstab(this );
221: }
222:
223: public int getColumnBreakOffset() {
224: return columnBreakOffset;
225: }
226:
227: public boolean isRepeatColumnHeaders() {
228: return repeatColumnHeaders;
229: }
230:
231: public boolean isRepeatRowHeaders() {
232: return repeatRowHeaders;
233: }
234:
235: public JRCrosstabCell[][] getCells() {
236: return cells;
237: }
238:
239: public JRCrosstabParameter[] getParameters() {
240: return parameters;
241: }
242:
243: public JRExpression getParametersMapExpression() {
244: return parametersMapExpression;
245: }
246:
247: public JRCellContents getWhenNoDataCell() {
248: return whenNoDataCell;
249: }
250:
251: public static JRElement getElementByKey(JRCrosstab crosstab,
252: String key) {
253: JRElement element = null;
254:
255: if (crosstab.getHeaderCell() != null) {
256: element = crosstab.getHeaderCell().getElementByKey(key);
257: }
258:
259: if (element == null) {
260: element = getHeadersElement(crosstab.getRowGroups(), key);
261: }
262:
263: if (element == null) {
264: element = getHeadersElement(crosstab.getColumnGroups(), key);
265: }
266:
267: if (element == null) {
268: if (crosstab instanceof JRDesignCrosstab) {
269: List cellsList = ((JRDesignCrosstab) crosstab)
270: .getCellsList();
271: for (Iterator it = cellsList.iterator(); element == null
272: && it.hasNext();) {
273: JRCrosstabCell cell = (JRCrosstabCell) it.next();
274: element = cell.getContents().getElementByKey(key);
275: }
276: } else {
277: JRCrosstabCell[][] cells = crosstab.getCells();
278: for (int i = cells.length - 1; element == null
279: && i >= 0; --i) {
280: for (int j = cells[i].length - 1; element == null
281: && j >= 0; --j) {
282: JRCrosstabCell cell = cells[i][j];
283: if (cell != null) {
284: element = cell.getContents()
285: .getElementByKey(key);
286: }
287: }
288: }
289: }
290: }
291:
292: if (element == null && crosstab.getWhenNoDataCell() != null) {
293: element = crosstab.getWhenNoDataCell().getElementByKey(key);
294: }
295:
296: return element;
297: }
298:
299: private static JRElement getHeadersElement(
300: JRCrosstabGroup[] groups, String key) {
301: JRElement element = null;
302:
303: if (groups != null) {
304: for (int i = 0; element == null && i < groups.length; i++) {
305: JRCellContents header = groups[i].getHeader();
306: element = header.getElementByKey(key);
307:
308: if (element == null) {
309: JRCellContents totalHeader = groups[i]
310: .getTotalHeader();
311: element = totalHeader.getElementByKey(key);
312: }
313: }
314: }
315:
316: return element;
317: }
318:
319: public JRElement getElementByKey(String elementKey) {
320: return getElementByKey(this , elementKey);
321: }
322:
323: public JRCellContents getHeaderCell() {
324: return headerCell;
325: }
326:
327: public JRVariable[] getVariables() {
328: return variables;
329: }
330:
331: public byte getRunDirection() {
332: return runDirection;
333: }
334:
335: public void setRunDirection(byte runDirection) {
336: this.runDirection = runDirection;
337: }
338: }
|