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.charts.fill;
029:
030: import java.util.ArrayList;
031: import java.util.HashMap;
032: import java.util.List;
033: import java.util.Map;
034:
035: import net.sf.jasperreports.charts.JRTimePeriodDataset;
036: import net.sf.jasperreports.charts.JRTimePeriodSeries;
037: import net.sf.jasperreports.charts.util.TimePeriodDatasetLabelGenerator;
038: import net.sf.jasperreports.engine.JRChartDataset;
039: import net.sf.jasperreports.engine.JRExpressionCollector;
040: import net.sf.jasperreports.engine.JRRuntimeException;
041: import net.sf.jasperreports.engine.design.JRVerifier;
042: import net.sf.jasperreports.engine.fill.JRCalculator;
043: import net.sf.jasperreports.engine.fill.JRExpressionEvalException;
044: import net.sf.jasperreports.engine.fill.JRFillChartDataset;
045: import net.sf.jasperreports.engine.fill.JRFillObjectFactory;
046:
047: import org.jfree.data.general.Dataset;
048: import org.jfree.data.time.SimpleTimePeriod;
049: import org.jfree.data.time.TimePeriodValues;
050: import org.jfree.data.time.TimePeriodValuesCollection;
051:
052: /**
053: * @author Flavius Sana (flavius_sana@users.sourceforge.net)
054: * @version $Id: JRFillTimePeriodDataset.java 1531 2006-12-21 17:38:30Z teodord $
055: */
056: public class JRFillTimePeriodDataset extends JRFillChartDataset
057: implements JRTimePeriodDataset {
058:
059: /**
060: *
061: */
062: protected JRFillTimePeriodSeries[] timePeriodSeries = null;
063:
064: private List seriesNames = null;
065: private Map seriesMap = null;
066: private Map labelsMap = null;
067: private Map itemHyperlinks;
068:
069: /**
070: *
071: */
072: public JRFillTimePeriodDataset(
073: JRTimePeriodDataset timePeriodDataset,
074: JRFillObjectFactory factory) {
075: super (timePeriodDataset, factory);
076:
077: JRTimePeriodSeries[] srcTimePeriodSeries = timePeriodDataset
078: .getSeries();
079: if (srcTimePeriodSeries != null
080: && srcTimePeriodSeries.length > 0) {
081: timePeriodSeries = new JRFillTimePeriodSeries[srcTimePeriodSeries.length];
082: for (int i = 0; i < timePeriodSeries.length; i++) {
083: timePeriodSeries[i] = (JRFillTimePeriodSeries) factory
084: .getTimePeriodSeries(srcTimePeriodSeries[i]);
085: }
086: }
087: }
088:
089: public JRTimePeriodSeries[] getSeries() {
090: return timePeriodSeries;
091: }
092:
093: protected void customInitialize() {
094: seriesNames = null;
095: seriesMap = null;
096: labelsMap = null;
097: itemHyperlinks = null;
098: }
099:
100: protected void customEvaluate(JRCalculator calculator)
101: throws JRExpressionEvalException {
102: if (timePeriodSeries != null && timePeriodSeries.length > 0) {
103: for (int i = 0; i < timePeriodSeries.length; i++) {
104: timePeriodSeries[i].evaluate(calculator);
105: }
106: }
107: }
108:
109: protected void customIncrement() {
110: if (timePeriodSeries != null && timePeriodSeries.length > 0) {
111: if (seriesNames == null) {
112: seriesNames = new ArrayList();
113: seriesMap = new HashMap();
114: labelsMap = new HashMap();
115: itemHyperlinks = new HashMap();
116: }
117:
118: for (int i = 0; i < timePeriodSeries.length; i++) {
119: JRFillTimePeriodSeries crtTimePeriodSeries = timePeriodSeries[i];
120:
121: Comparable seriesName = crtTimePeriodSeries.getSeries();
122: if (seriesName == null) {
123: throw new JRRuntimeException(
124: "Time period series name is null.");
125: }
126:
127: TimePeriodValues timePeriodValues = (TimePeriodValues) seriesMap
128: .get(seriesName);
129: if (timePeriodValues == null) {
130: timePeriodValues = new TimePeriodValues(seriesName
131: .toString());
132: seriesNames.add(seriesName);
133: seriesMap.put(seriesName, timePeriodValues);
134: }
135:
136: SimpleTimePeriod stp = new SimpleTimePeriod(
137: crtTimePeriodSeries.getStartDate(),
138: crtTimePeriodSeries.getEndDate());
139:
140: timePeriodValues.add(stp, crtTimePeriodSeries
141: .getValue());
142:
143: if (crtTimePeriodSeries.getLabelExpression() != null) {
144: Map seriesLabels = (Map) labelsMap.get(seriesName);
145: if (seriesLabels == null) {
146: seriesLabels = new HashMap();
147: labelsMap.put(seriesName, seriesLabels);
148: }
149:
150: seriesLabels.put(stp, crtTimePeriodSeries
151: .getLabel());
152: }
153:
154: if (crtTimePeriodSeries.hasItemHyperlink()) {
155: Map seriesLinks = (Map) itemHyperlinks
156: .get(seriesName);
157: if (seriesLinks == null) {
158: seriesLinks = new HashMap();
159: itemHyperlinks.put(seriesName, seriesLinks);
160: }
161:
162: seriesLinks.put(stp, crtTimePeriodSeries
163: .getPrintItemHyperlink());
164: }
165: }
166: }
167: }
168:
169: public Dataset getCustomDataset() {
170: TimePeriodValuesCollection dataset = new TimePeriodValuesCollection();
171: if (seriesNames != null) {
172: for (int i = 0; i < seriesNames.size(); i++) {
173: Comparable seriesName = (Comparable) seriesNames.get(i);
174: dataset.addSeries((TimePeriodValues) seriesMap
175: .get(seriesName));
176: }
177: }
178: return dataset;
179: }
180:
181: /**
182: *
183: */
184: public byte getDatasetType() {
185: return JRChartDataset.TIMEPERIOD_DATASET;
186: }
187:
188: /**
189: *
190: */
191: public TimePeriodDatasetLabelGenerator getLabelGenerator() {
192: return new TimePeriodDatasetLabelGenerator(labelsMap);
193: }
194:
195: /**
196: *
197: */
198: public void collectExpressions(JRExpressionCollector collector) {
199: collector.collect(this );
200: }
201:
202: public boolean hasItemHyperlinks() {
203: boolean foundLinks = false;
204: if (timePeriodSeries != null && timePeriodSeries.length > 0) {
205: for (int i = 0; i < timePeriodSeries.length && !foundLinks; i++) {
206: foundLinks = timePeriodSeries[i].hasItemHyperlink();
207: }
208: }
209: return foundLinks;
210: }
211:
212: public Map getItemHyperlinks() {
213: return itemHyperlinks;
214: }
215:
216: public void validate(JRVerifier verifier) {
217: verifier.verify(this);
218: }
219:
220: }
|