KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import net.sf.jasperreports.charts.JRCategoryDataset;
34 import net.sf.jasperreports.charts.JRCategorySeries;
35 import net.sf.jasperreports.charts.util.CategoryLabelGenerator;
36 import net.sf.jasperreports.engine.JRChartDataset;
37 import net.sf.jasperreports.engine.JRExpressionCollector;
38 import net.sf.jasperreports.engine.JRRuntimeException;
39 import net.sf.jasperreports.engine.design.JRVerifier;
40 import net.sf.jasperreports.engine.fill.JRCalculator;
41 import net.sf.jasperreports.engine.fill.JRExpressionEvalException;
42 import net.sf.jasperreports.engine.fill.JRFillChartDataset;
43 import net.sf.jasperreports.engine.fill.JRFillObjectFactory;
44
45 import org.jfree.data.category.DefaultCategoryDataset;
46 import org.jfree.data.general.Dataset;
47
48
49 /**
50  * @author Teodor Danciu (teodord@users.sourceforge.net)
51  * @version $Id: JRFillCategoryDataset.java 1531 2006-12-21 19:38:30 +0200 (Thu, 21 Dec 2006) teodord $
52  */

53 public class JRFillCategoryDataset extends JRFillChartDataset implements JRCategoryDataset
54 {
55
56     /**
57      *
58      */

59     protected JRFillCategorySeries[] categorySeries = null;
60
61     private DefaultCategoryDataset dataset = null;
62     private Map JavaDoc labelsMap = null;
63     
64     private Map JavaDoc itemHyperlinks;
65
66     
67     /**
68      *
69      */

70     public JRFillCategoryDataset(
71         JRCategoryDataset categoryDataset,
72         JRFillObjectFactory factory
73         )
74     {
75         super(categoryDataset, factory);
76
77         /* */
78         JRCategorySeries[] srcCategorySeries = categoryDataset.getSeries();
79         if (srcCategorySeries != null && srcCategorySeries.length > 0)
80         {
81             categorySeries = new JRFillCategorySeries[srcCategorySeries.length];
82             for(int i = 0; i < categorySeries.length; i++)
83             {
84                 categorySeries[i] = (JRFillCategorySeries)factory.getCategorySeries(srcCategorySeries[i]);
85             }
86         }
87     }
88     
89     
90     /**
91      *
92      */

93     public JRCategorySeries[] getSeries()
94     {
95         return categorySeries;
96     }
97
98
99     /**
100      *
101      */

102     protected void customInitialize()
103     {
104         dataset = null;
105         labelsMap = null;
106         itemHyperlinks = null;
107     }
108
109     /**
110      *
111      */

112     protected void customEvaluate(JRCalculator calculator) throws JRExpressionEvalException
113     {
114         if (categorySeries != null && categorySeries.length > 0)
115         {
116             for(int i = 0; i < categorySeries.length; i++)
117             {
118                 categorySeries[i].evaluate(calculator);
119             }
120         }
121     }
122
123     /**
124      *
125      */

126     protected void customIncrement()
127     {
128         if (categorySeries != null && categorySeries.length > 0)
129         {
130             if (dataset == null)
131             {
132                 dataset = new DefaultCategoryDataset();
133                 labelsMap = new HashMap JavaDoc();
134                 itemHyperlinks = new HashMap JavaDoc();
135             }
136             
137             for(int i = 0; i < categorySeries.length; i++)
138             {
139                 JRFillCategorySeries crtCategorySeries = categorySeries[i];
140                 
141                 Comparable JavaDoc seriesName = crtCategorySeries.getSeries();
142                 if (seriesName == null)
143                 {
144                     throw new JRRuntimeException("Category series name is null.");
145                 }
146
147                 dataset.addValue(
148                     crtCategorySeries.getValue(),
149                     crtCategorySeries.getSeries(),
150                     crtCategorySeries.getCategory()
151                     );
152
153                 if (crtCategorySeries.getLabelExpression() != null)
154                 {
155                     Map JavaDoc seriesLabels = (Map JavaDoc)labelsMap.get(seriesName);
156                     if (seriesLabels == null)
157                     {
158                         seriesLabels = new HashMap JavaDoc();
159                         labelsMap.put(seriesName, seriesLabels);
160                     }
161                     
162                     seriesLabels.put(crtCategorySeries.getCategory(), crtCategorySeries.getLabel());
163                 }
164                 
165                 if (crtCategorySeries.hasItemHyperlinks())
166                 {
167                     Map JavaDoc seriesLinks = (Map JavaDoc) itemHyperlinks.get(seriesName);
168                     if (seriesLinks == null)
169                     {
170                         seriesLinks = new HashMap JavaDoc();
171                         itemHyperlinks.put(seriesName, seriesLinks);
172                     }
173                     seriesLinks.put(crtCategorySeries.getCategory(), crtCategorySeries.getPrintItemHyperlink());
174                 }
175             }
176         }
177     }
178
179     /**
180      *
181      */

182     public Dataset getCustomDataset()
183     {
184         return dataset;
185     }
186
187
188     /**
189      *
190      */

191     public byte getDatasetType() {
192         return JRChartDataset.CATEGORY_DATASET;
193     }
194
195     
196     /**
197      *
198      */

199     public CategoryLabelGenerator getLabelGenerator()
200     {
201         return new CategoryLabelGenerator(labelsMap);
202     }
203
204
205     /**
206      *
207      */

208     public void collectExpressions(JRExpressionCollector collector)
209     {
210         collector.collect(this);
211     }
212
213     
214     public Map JavaDoc getItemHyperlinks()
215     {
216         return itemHyperlinks;
217     }
218     
219     
220     public boolean hasItemHyperlinks()
221     {
222         boolean foundLinks = false;
223         if (categorySeries != null && categorySeries.length > 0)
224         {
225             for (int i = 0; i < categorySeries.length && !foundLinks; i++)
226             {
227                 JRFillCategorySeries serie = categorySeries[i];
228                 foundLinks = serie.hasItemHyperlinks();
229             }
230         }
231         return foundLinks;
232     }
233
234
235     public void validate(JRVerifier verifier)
236     {
237         verifier.verify(this);
238     }
239
240 }
241
Popular Tags