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.HashMap;
031: import java.util.Map;
032:
033: import net.sf.jasperreports.charts.JRPieDataset;
034: import net.sf.jasperreports.charts.util.PieLabelGenerator;
035: import net.sf.jasperreports.engine.JRChartDataset;
036: import net.sf.jasperreports.engine.JRException;
037: import net.sf.jasperreports.engine.JRExpression;
038: import net.sf.jasperreports.engine.JRExpressionCollector;
039: import net.sf.jasperreports.engine.JRHyperlink;
040: import net.sf.jasperreports.engine.JRHyperlinkHelper;
041: import net.sf.jasperreports.engine.JRPrintHyperlink;
042: import net.sf.jasperreports.engine.JRRuntimeException;
043: import net.sf.jasperreports.engine.design.JRVerifier;
044: import net.sf.jasperreports.engine.fill.JRCalculator;
045: import net.sf.jasperreports.engine.fill.JRExpressionEvalException;
046: import net.sf.jasperreports.engine.fill.JRFillChartDataset;
047: import net.sf.jasperreports.engine.fill.JRFillHyperlinkHelper;
048: import net.sf.jasperreports.engine.fill.JRFillObjectFactory;
049:
050: import org.jfree.data.general.Dataset;
051: import org.jfree.data.general.DefaultPieDataset;
052:
053: /**
054: * @author Teodor Danciu (teodord@users.sourceforge.net)
055: * @version $Id: JRFillPieDataset.java 1720 2007-05-07 10:02:56Z lucianc $
056: */
057: public class JRFillPieDataset extends JRFillChartDataset implements
058: JRPieDataset {
059:
060: /**
061: *
062: */
063: private DefaultPieDataset dataset = new DefaultPieDataset();
064: private Map labels = null;
065:
066: private Comparable key = null;
067: private Number value = null;
068: private String label = null;
069:
070: private Map sectionHyperlinks;
071: private JRPrintHyperlink sectionHyperlink;
072:
073: /**
074: *
075: */
076: public JRFillPieDataset(JRPieDataset pieDataset,
077: JRFillObjectFactory factory) {
078: super (pieDataset, factory);
079: }
080:
081: /**
082: *
083: */
084: public JRExpression getKeyExpression() {
085: return ((JRPieDataset) parent).getKeyExpression();
086: }
087:
088: /**
089: *
090: */
091: public JRExpression getValueExpression() {
092: return ((JRPieDataset) parent).getValueExpression();
093: }
094:
095: /**
096: *
097: */
098: public JRExpression getLabelExpression() {
099: return ((JRPieDataset) parent).getLabelExpression();
100: }
101:
102: /**
103: *
104: */
105: protected void customInitialize() {
106: dataset = new DefaultPieDataset();
107: labels = new HashMap();
108: sectionHyperlinks = new HashMap();
109: }
110:
111: /**
112: *
113: */
114: protected void customEvaluate(JRCalculator calculator)
115: throws JRExpressionEvalException {
116: key = (Comparable) calculator.evaluate(getKeyExpression());
117: value = (Number) calculator.evaluate(getValueExpression());
118: label = (String) calculator.evaluate(getLabelExpression());
119:
120: if (hasSectionHyperlinks()) {
121: evaluateSectionHyperlink(calculator);
122: }
123: }
124:
125: protected void evaluateSectionHyperlink(JRCalculator calculator)
126: throws JRExpressionEvalException {
127: try {
128: sectionHyperlink = JRFillHyperlinkHelper.evaluateHyperlink(
129: getSectionHyperlink(), calculator,
130: JRExpression.EVALUATION_DEFAULT);
131: } catch (JRExpressionEvalException e) {
132: throw e;
133: } catch (JRException e) {
134: throw new JRRuntimeException(e);
135: }
136: }
137:
138: /**
139: *
140: */
141: protected void customIncrement() {
142: dataset.setValue(key, value);
143: labels.put(key, label);
144:
145: if (hasSectionHyperlinks()) {
146: sectionHyperlinks.put(key, sectionHyperlink);
147: }
148: }
149:
150: /**
151: *
152: */
153: public Dataset getCustomDataset() {
154: return dataset;
155: }
156:
157: /* (non-Javadoc)
158: * @see net.sf.jasperreports.engine.JRChartDataset#getDatasetType()
159: */
160: public byte getDatasetType() {
161: return JRChartDataset.PIE_DATASET;
162: }
163:
164: public PieLabelGenerator getLabelGenerator() {
165: return (getLabelExpression() == null) ? null
166: : new PieLabelGenerator(labels);
167: }
168:
169: /**
170: *
171: */
172: public void collectExpressions(JRExpressionCollector collector) {
173: collector.collect(this );
174: }
175:
176: public JRHyperlink getSectionHyperlink() {
177: return ((JRPieDataset) parent).getSectionHyperlink();
178: }
179:
180: public boolean hasSectionHyperlinks() {
181: return !JRHyperlinkHelper.isEmpty(getSectionHyperlink());
182: }
183:
184: public Map getSectionHyperlinks() {
185: return sectionHyperlinks;
186: }
187:
188: public void validate(JRVerifier verifier) {
189: verifier.verify(this);
190: }
191:
192: }
|