KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > renderers > JRAbstractChartImageMapRenderer


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.renderers;
29
30 import java.awt.geom.Rectangle2D JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35
36 import net.sf.jasperreports.engine.JRConstants;
37 import net.sf.jasperreports.engine.JRException;
38 import net.sf.jasperreports.engine.JRImageMapRenderer;
39 import net.sf.jasperreports.engine.JRPrintHyperlink;
40 import net.sf.jasperreports.engine.JRPrintImageArea;
41 import net.sf.jasperreports.engine.JRPrintImageAreaHyperlink;
42
43 import org.jfree.chart.ChartRenderingInfo;
44 import org.jfree.chart.JFreeChart;
45 import org.jfree.chart.entity.ChartEntity;
46 import org.jfree.chart.entity.EntityCollection;
47
48
49 /**
50  * Abstract image map renderer for charts.
51  *
52  * @author Lucian Chirita (lucianc@users.sourceforge.net)
53  * @version $Id: JRAbstractChartImageMapRenderer.java 1364 2006-08-31 18:13:20 +0300 (Thu, 31 Aug 2006) lucianc $
54  */

55 public abstract class JRAbstractChartImageMapRenderer extends JFreeChartRenderer implements JRImageMapRenderer
56 {
57     
58     private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
59     
60     public JRAbstractChartImageMapRenderer(JFreeChart chart)
61     {
62         super(chart);
63     }
64
65
66     public List JavaDoc getImageAreaHyperlinks(Rectangle2D JavaDoc renderingArea) throws JRException
67     {
68         //FIXME don't render twice
69
ChartRenderingInfo renderingInfo = new ChartRenderingInfo();
70         getChart().createBufferedImage((int) renderingArea.getWidth(), (int) renderingArea.getHeight(), renderingInfo);
71         
72         EntityCollection entityCollection = renderingInfo.getEntityCollection();
73         List JavaDoc areaHyperlinks = null;
74         if (entityCollection != null && entityCollection.getEntityCount() > 0)
75         {
76             areaHyperlinks = new ArrayList JavaDoc(entityCollection.getEntityCount());
77             
78             for (Iterator JavaDoc it = entityCollection.iterator(); it.hasNext();)
79             {
80                 ChartEntity entity = (ChartEntity) it.next();
81                 JRPrintHyperlink printHyperlink = getEntityHyperlink(entity);
82                 if (printHyperlink != null)
83                 {
84                     JRPrintImageArea area = getImageArea(entity);
85
86                     JRPrintImageAreaHyperlink areaHyperlink = new JRPrintImageAreaHyperlink();
87                     areaHyperlink.setArea(area);
88                     areaHyperlink.setHyperlink(printHyperlink);
89                     areaHyperlinks.add(areaHyperlink);
90                 }
91             }
92         }
93         
94         return areaHyperlinks;
95     }
96
97     protected JRPrintImageArea getImageArea(ChartEntity entity)
98     {
99         JRPrintImageArea area = new JRPrintImageArea();
100         area.setShape(JRPrintImageArea.getShape(entity.getShapeType()));
101         
102         int[] coordinates = getCoordinates(entity);
103         if (coordinates != null)
104         {
105             area.setCoordinates(coordinates);
106         }
107         return area;
108     }
109     
110     protected int[] getCoordinates(ChartEntity entity)
111     {
112         int[] coordinates = null;
113         String JavaDoc shapeCoords = entity.getShapeCoords();
114         if (shapeCoords != null && shapeCoords.length() > 0)
115         {
116             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(shapeCoords, ",");
117             coordinates = new int[tokens.countTokens()];
118             int idx = 0;
119             while (tokens.hasMoreTokens())
120             {
121                 String JavaDoc coord = tokens.nextToken();
122                 coordinates[idx] = Integer.parseInt(coord);
123                 ++idx;
124             }
125         }
126         return coordinates;
127     }
128
129     protected abstract JRPrintHyperlink getEntityHyperlink(ChartEntity entity);
130 }
131
Popular Tags