KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > charts > fill > JRFillTimePeriodDataset


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.charts.fill;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import net.sf.jasperreports.charts.JRTimePeriodDataset;
36 import net.sf.jasperreports.charts.JRTimePeriodSeries;
37 import net.sf.jasperreports.charts.util.TimePeriodDatasetLabelGenerator;
38 import net.sf.jasperreports.engine.JRChartDataset;
39 import net.sf.jasperreports.engine.JRExpressionCollector;
40 import net.sf.jasperreports.engine.JRRuntimeException;
41 import net.sf.jasperreports.engine.design.JRVerifier;
42 import net.sf.jasperreports.engine.fill.JRCalculator;
43 import net.sf.jasperreports.engine.fill.JRExpressionEvalException;
44 import net.sf.jasperreports.engine.fill.JRFillChartDataset;
45 import net.sf.jasperreports.engine.fill.JRFillObjectFactory;
46
47 import org.jfree.data.general.Dataset;
48 import org.jfree.data.time.SimpleTimePeriod;
49 import org.jfree.data.time.TimePeriodValues;
50 import org.jfree.data.time.TimePeriodValuesCollection;
51
52
53 /**
54  * @author Flavius Sana (flavius_sana@users.sourceforge.net)
55  * @version $Id: JRFillTimePeriodDataset.java 1531 2006-12-21 19:38:30 +0200 (Thu, 21 Dec 2006) teodord $
56  */

57 public class JRFillTimePeriodDataset extends JRFillChartDataset implements JRTimePeriodDataset
58 {
59
60     /**
61      *
62      */

63     protected JRFillTimePeriodSeries[] timePeriodSeries = null;
64
65     private List JavaDoc seriesNames = null;
66     private Map JavaDoc seriesMap = null;
67     private Map JavaDoc labelsMap = null;
68     private Map JavaDoc itemHyperlinks;
69
70
71     /**
72      *
73      */

74     public JRFillTimePeriodDataset(
75         JRTimePeriodDataset timePeriodDataset,
76         JRFillObjectFactory factory
77         )
78     {
79         super(timePeriodDataset, factory);
80
81         JRTimePeriodSeries[] srcTimePeriodSeries = timePeriodDataset.getSeries();
82         if (srcTimePeriodSeries != null && srcTimePeriodSeries.length > 0)
83         {
84             timePeriodSeries = new JRFillTimePeriodSeries[srcTimePeriodSeries.length];
85             for (int i = 0; i < timePeriodSeries.length; i++)
86             {
87                 timePeriodSeries[i] =
88                     (JRFillTimePeriodSeries)factory.getTimePeriodSeries(srcTimePeriodSeries[i]);
89             }
90         }
91     }
92
93     public JRTimePeriodSeries[] getSeries()
94     {
95         return timePeriodSeries;
96     }
97
98     protected void customInitialize()
99     {
100         seriesNames = null;
101         seriesMap = null;
102         labelsMap = null;
103         itemHyperlinks = null;
104     }
105
106     protected void customEvaluate(JRCalculator calculator)
107             throws JRExpressionEvalException
108     {
109         if (timePeriodSeries != null && timePeriodSeries.length > 0)
110         {
111             for (int i = 0; i < timePeriodSeries.length; i++)
112             {
113                 timePeriodSeries[i].evaluate(calculator);
114             }
115         }
116     }
117
118     protected void customIncrement()
119     {
120         if (timePeriodSeries != null && timePeriodSeries.length > 0)
121         {
122             if (seriesNames == null)
123             {
124                 seriesNames = new ArrayList JavaDoc();
125                 seriesMap = new HashMap JavaDoc();
126                 labelsMap = new HashMap JavaDoc();
127                 itemHyperlinks = new HashMap JavaDoc();
128             }
129
130             for (int i = 0; i < timePeriodSeries.length; i++)
131             {
132                 JRFillTimePeriodSeries crtTimePeriodSeries = timePeriodSeries[i];
133
134                 Comparable JavaDoc seriesName = crtTimePeriodSeries.getSeries();
135                 if (seriesName == null)
136                 {
137                     throw new JRRuntimeException("Time period series name is null.");
138                 }
139
140                 TimePeriodValues timePeriodValues = (TimePeriodValues)seriesMap.get(seriesName);
141                 if (timePeriodValues == null)
142                 {
143                     timePeriodValues = new TimePeriodValues(seriesName.toString());
144                     seriesNames.add(seriesName);
145                     seriesMap.put(seriesName, timePeriodValues);
146                 }
147
148                 SimpleTimePeriod stp =
149                     new SimpleTimePeriod(
150                         crtTimePeriodSeries.getStartDate(),
151                         crtTimePeriodSeries.getEndDate()
152                         );
153                 
154                 timePeriodValues.add(stp, crtTimePeriodSeries.getValue());
155                 
156                 if (crtTimePeriodSeries.getLabelExpression() != null)
157                 {
158                     Map JavaDoc seriesLabels = (Map JavaDoc)labelsMap.get(seriesName);
159                     if (seriesLabels == null)
160                     {
161                         seriesLabels = new HashMap JavaDoc();
162                         labelsMap.put(seriesName, seriesLabels);
163                     }
164                     
165                     seriesLabels.put(stp, crtTimePeriodSeries.getLabel());
166                 }
167                 
168                 if (crtTimePeriodSeries.hasItemHyperlink())
169                 {
170                     Map JavaDoc seriesLinks = (Map JavaDoc) itemHyperlinks.get(seriesName);
171                     if (seriesLinks == null)
172                     {
173                         seriesLinks = new HashMap JavaDoc();
174                         itemHyperlinks.put(seriesName, seriesLinks);
175                     }
176                     
177                     seriesLinks.put(stp, crtTimePeriodSeries.getPrintItemHyperlink());
178                 }
179             }
180         }
181     }
182
183     public Dataset getCustomDataset()
184     {
185         TimePeriodValuesCollection dataset = new TimePeriodValuesCollection();
186         if (seriesNames != null)
187         {
188             for(int i = 0; i < seriesNames.size(); i++)
189             {
190                 Comparable JavaDoc seriesName = (Comparable JavaDoc)seriesNames.get(i);
191                 dataset.addSeries((TimePeriodValues)seriesMap.get(seriesName));
192             }
193         }
194         return dataset;
195     }
196
197     /**
198      *
199      */

200     public byte getDatasetType()
201     {
202         return JRChartDataset.TIMEPERIOD_DATASET;
203     }
204
205     /**
206      *
207      */

208     public TimePeriodDatasetLabelGenerator getLabelGenerator()
209     {
210         return new TimePeriodDatasetLabelGenerator(labelsMap);
211     }
212
213     /**
214      *
215      */

216     public void collectExpressions(JRExpressionCollector collector)
217     {
218         collector.collect(this);
219     }
220     
221     
222     public boolean hasItemHyperlinks()
223     {
224         boolean foundLinks = false;
225         if (timePeriodSeries != null && timePeriodSeries.length > 0)
226         {
227             for (int i = 0; i < timePeriodSeries.length && !foundLinks; i++)
228             {
229                 foundLinks = timePeriodSeries[i].hasItemHyperlink();
230             }
231         }
232         return foundLinks;
233     }
234     
235     
236     public Map JavaDoc getItemHyperlinks()
237     {
238         return itemHyperlinks;
239     }
240
241
242     public void validate(JRVerifier verifier)
243     {
244         verifier.verify(this);
245     }
246
247 }
248
Popular Tags