KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > xml > JRChartFactory


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.engine.xml;
29
30 import java.awt.Color JavaDoc;
31 import java.util.Collection JavaDoc;
32
33 import net.sf.jasperreports.charts.util.JRAxisFormat;
34 import net.sf.jasperreports.engine.JRException;
35 import net.sf.jasperreports.engine.JRExpression;
36 import net.sf.jasperreports.engine.design.JRDesignChart;
37 import net.sf.jasperreports.engine.design.JRDesignGroup;
38
39 import org.xml.sax.Attributes JavaDoc;
40
41
42 /**
43  * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
44  * @version $Id: JRChartFactory.java 1393 2006-09-06 01:04:21 +0300 (Wed, 06 Sep 2006) bklawans $
45  */

46 public class JRChartFactory extends JRBaseFactory
47 {
48
49     private static final String JavaDoc ATTRIBUTE_isShowLegend = "isShowLegend";
50     private static final String JavaDoc ATTRIBUTE_evaluationTime = "evaluationTime";
51     private static final String JavaDoc ATTRIBUTE_evaluationGroup = "evaluationGroup";
52     private static final String JavaDoc ATTRIBUTE_hyperlinkType = "hyperlinkType";
53     private static final String JavaDoc ATTRIBUTE_hyperlinkTarget = "hyperlinkTarget";
54     private static final String JavaDoc ATTRIBUTE_bookmarkLevel = "bookmarkLevel";
55     private static final String JavaDoc ATTRIBUTE_customizerClass = "customizerClass";
56
57
58     /**
59      *
60      */

61     public Object JavaDoc createObject(Attributes JavaDoc atts)
62     {
63         JRXmlLoader xmlLoader = (JRXmlLoader)digester.peek(digester.getCount() - 1);
64         Collection JavaDoc groupEvaluatedCharts = xmlLoader.getGroupEvaluatedCharts();
65
66         JRDesignChart chart = (JRDesignChart) digester.peek();
67
68         String JavaDoc isShowLegend = atts.getValue(ATTRIBUTE_isShowLegend);
69         if (isShowLegend != null && isShowLegend.length() > 0)
70             chart.setShowLegend(Boolean.valueOf(isShowLegend).booleanValue());
71
72         Byte JavaDoc evaluationTime = (Byte JavaDoc)JRXmlConstants.getEvaluationTimeMap().get(atts.getValue(ATTRIBUTE_evaluationTime));
73         if (evaluationTime != null)
74         {
75             chart.setEvaluationTime(evaluationTime.byteValue());
76         }
77         if (chart.getEvaluationTime() == JRExpression.EVALUATION_TIME_GROUP)
78         {
79             groupEvaluatedCharts.add(chart);
80
81             String JavaDoc groupName = atts.getValue(ATTRIBUTE_evaluationGroup);
82             if (groupName != null)
83             {
84                 JRDesignGroup group = new JRDesignGroup();
85                 group.setName(groupName);
86                 chart.setEvaluationGroup(group);
87             }
88         }
89
90         String JavaDoc hyperlinkType = atts.getValue(ATTRIBUTE_hyperlinkType);
91         if (hyperlinkType != null)
92         {
93             chart.setLinkType(hyperlinkType);
94         }
95
96         Byte JavaDoc hyperlinkTarget = (Byte JavaDoc)JRXmlConstants.getHyperlinkTargetMap().get(atts.getValue(ATTRIBUTE_hyperlinkTarget));
97         if (hyperlinkTarget != null)
98         {
99             chart.setHyperlinkTarget(hyperlinkTarget.byteValue());
100         }
101         
102         String JavaDoc bookmarkLevelAttr = atts.getValue(ATTRIBUTE_bookmarkLevel);
103         if (bookmarkLevelAttr != null)
104         {
105             chart.setBookmarkLevel(Integer.parseInt(bookmarkLevelAttr));
106         }
107
108         String JavaDoc chartCustomizerClass = atts.getValue( ATTRIBUTE_customizerClass );
109         if( chartCustomizerClass != null && chartCustomizerClass.length() > 0 ){
110             chart.setCustomizerClass(chartCustomizerClass);
111         }
112
113         return chart;
114     }
115
116
117     /**
118      *
119      */

120     public static class JRChartTitleFactory extends JRBaseFactory
121     {
122         private static final String JavaDoc ATTRIBUTE_position = "position";
123         private static final String JavaDoc ATTRIBUTE_color = "color";
124
125
126         public Object JavaDoc createObject(Attributes JavaDoc atts)
127         {
128             JRDesignChart chart = (JRDesignChart) digester.peek();
129
130             String JavaDoc position = atts.getValue(ATTRIBUTE_position);
131             if (position != null && position.length() > 0)
132                 chart.setTitlePosition(((Byte JavaDoc)JRXmlConstants.getChartTitlePositionMap().get(position)).byteValue());
133
134
135             Color JavaDoc color = JRXmlConstants.getColor(atts.getValue(ATTRIBUTE_color), Color.black);
136             if (color != null)
137             {
138                 chart.setTitleColor(color);
139             }
140
141             return chart;
142         }
143     }
144
145
146     /**
147      *
148      */

149     public static class JRChartSubtitleFactory extends JRBaseFactory
150     {
151         private static final String JavaDoc ATTRIBUTE_color = "color";
152
153
154         public Object JavaDoc createObject(Attributes JavaDoc atts)
155         {
156             JRDesignChart chart = (JRDesignChart) digester.peek();
157
158             Color JavaDoc color = JRXmlConstants.getColor(atts.getValue(ATTRIBUTE_color), Color.black);
159             if (color != null)
160             {
161                 chart.setSubtitleColor(color);
162             }
163
164             return chart;
165         }
166     }
167
168
169     /**
170      * A factory responsible for creating new chart legend formatting objects.
171      *
172      * @author Barry Klawans (bklawans@users.sourceforge.net)
173      */

174     public static class JRChartLegendFactory extends JRBaseFactory
175     {
176         /**
177          * Attribute used to specify the font color in the legend.
178          */

179         public static final String JavaDoc ATTRIBUTE_textColor = "textColor";
180         
181         /**
182          * Attribute used to specify the background color of the legend.
183          */

184         public static final String JavaDoc ATTRIBUTE_backgroundColor = "backgroundColor";
185         
186         /**
187          *
188          */

189         public Object JavaDoc createObject(Attributes JavaDoc atts) throws JRException
190         {
191             // Grab the chart from the object stack.
192
JRDesignChart chart = (JRDesignChart)digester.peek();
193             
194             // Set the text color
195
String JavaDoc attrValue = atts.getValue(ATTRIBUTE_textColor);
196             if (attrValue != null && attrValue.length() > 0)
197             {
198                 Color JavaDoc color = JRXmlConstants.getColor(attrValue, null);
199                 chart.setLegendColor(color);
200             }
201
202             // Set the background color
203
attrValue = atts.getValue(ATTRIBUTE_backgroundColor);
204             if (attrValue != null && attrValue.length() > 0)
205             {
206                 Color JavaDoc color = JRXmlConstants.getColor(attrValue, null);
207                 chart.setLegendBackgroundColor(color);
208             }
209             
210             // Any font set will be put in the chart directly by the digester
211

212             return chart;
213         }
214     }
215
216
217     /**
218      * A factory responsible for creating new chart axis formatting objects.
219      *
220      * @author Barry Klawans (bklawans@users.sourceforge.net)
221      */

222     public static class JRChartAxisFormatFactory extends JRBaseFactory
223     {
224         /**
225          * Attribute to use to specify the color of the axis label.
226          */

227         public static final String JavaDoc ATTRIBUTE_labelColor = "labelColor";
228         
229         /**
230          * Attribute to use to specify the color of each tick mark label.
231          */

232         public static final String JavaDoc ATTRIBUTE_tickLabelColor = "tickLabelColor";
233         
234         /**
235          * Attribute to use to specify a formatting mask for each tick mark label.
236          */

237         public static final String JavaDoc ATTRIBUTE_tickLabelMask = "tickLabelMask";
238         
239         /**
240          * Attribute to use to specify the color of the axis line and any tick marks.
241          */

242         public static final String JavaDoc ATTRIBUTE_axisLineColor = "axisLineColor";
243         
244         /**
245          *
246          */

247         public Object JavaDoc createObject(Attributes JavaDoc atts) throws JRException
248         {
249             // Create an empty axis formatting object
250
JRAxisFormat axisLabel = new JRAxisFormat();
251             
252             // Set the label color
253
String JavaDoc attrValue = atts.getValue(ATTRIBUTE_labelColor);
254             if (attrValue != null && attrValue.length() > 0)
255             {
256                 Color JavaDoc color = JRXmlConstants.getColor(attrValue, null);
257                 axisLabel.setLabelColor(color);
258             }
259             
260             // Set the tick label color
261
attrValue = atts.getValue(ATTRIBUTE_tickLabelColor);
262             if (attrValue != null && attrValue.length() > 0)
263             {
264                 Color JavaDoc color = JRXmlConstants.getColor(attrValue, null);
265                 axisLabel.setTickLabelColor(color);
266             }
267             
268             // Set the tick mask
269
attrValue = atts.getValue(ATTRIBUTE_tickLabelMask);
270             if (attrValue != null && attrValue.length() > 0)
271             {
272                 axisLabel.setTickLabelMask(attrValue);
273             }
274             
275             // And finally set the axis line color
276
attrValue = atts.getValue(ATTRIBUTE_axisLineColor);
277             if (attrValue != null && attrValue.length() > 0)
278             {
279                 Color JavaDoc color = JRXmlConstants.getColor(attrValue, null);
280                 axisLabel.setLineColor(color);
281             }
282
283             // Any fonts set will be put in the axis format object by the digester.
284

285             return axisLabel;
286         }
287     }
288 }
289
Popular Tags