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.renderers;
029:
030: import java.awt.geom.Rectangle2D;
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.StringTokenizer;
035:
036: import net.sf.jasperreports.engine.JRConstants;
037: import net.sf.jasperreports.engine.JRException;
038: import net.sf.jasperreports.engine.JRImageMapRenderer;
039: import net.sf.jasperreports.engine.JRPrintHyperlink;
040: import net.sf.jasperreports.engine.JRPrintImageArea;
041: import net.sf.jasperreports.engine.JRPrintImageAreaHyperlink;
042:
043: import org.jfree.chart.ChartRenderingInfo;
044: import org.jfree.chart.JFreeChart;
045: import org.jfree.chart.entity.ChartEntity;
046: import org.jfree.chart.entity.EntityCollection;
047:
048: /**
049: * Abstract image map renderer for charts.
050: *
051: * @author Lucian Chirita (lucianc@users.sourceforge.net)
052: * @version $Id: JRAbstractChartImageMapRenderer.java 1364 2006-08-31 15:13:20Z lucianc $
053: */
054: public abstract class JRAbstractChartImageMapRenderer extends
055: JFreeChartRenderer implements JRImageMapRenderer {
056:
057: private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;
058:
059: public JRAbstractChartImageMapRenderer(JFreeChart chart) {
060: super (chart);
061: }
062:
063: public List getImageAreaHyperlinks(Rectangle2D renderingArea)
064: throws JRException {
065: //FIXME don't render twice
066: ChartRenderingInfo renderingInfo = new ChartRenderingInfo();
067: getChart().createBufferedImage((int) renderingArea.getWidth(),
068: (int) renderingArea.getHeight(), renderingInfo);
069:
070: EntityCollection entityCollection = renderingInfo
071: .getEntityCollection();
072: List areaHyperlinks = null;
073: if (entityCollection != null
074: && entityCollection.getEntityCount() > 0) {
075: areaHyperlinks = new ArrayList(entityCollection
076: .getEntityCount());
077:
078: for (Iterator it = entityCollection.iterator(); it
079: .hasNext();) {
080: ChartEntity entity = (ChartEntity) it.next();
081: JRPrintHyperlink printHyperlink = getEntityHyperlink(entity);
082: if (printHyperlink != null) {
083: JRPrintImageArea area = getImageArea(entity);
084:
085: JRPrintImageAreaHyperlink areaHyperlink = new JRPrintImageAreaHyperlink();
086: areaHyperlink.setArea(area);
087: areaHyperlink.setHyperlink(printHyperlink);
088: areaHyperlinks.add(areaHyperlink);
089: }
090: }
091: }
092:
093: return areaHyperlinks;
094: }
095:
096: protected JRPrintImageArea getImageArea(ChartEntity entity) {
097: JRPrintImageArea area = new JRPrintImageArea();
098: area.setShape(JRPrintImageArea.getShape(entity.getShapeType()));
099:
100: int[] coordinates = getCoordinates(entity);
101: if (coordinates != null) {
102: area.setCoordinates(coordinates);
103: }
104: return area;
105: }
106:
107: protected int[] getCoordinates(ChartEntity entity) {
108: int[] coordinates = null;
109: String shapeCoords = entity.getShapeCoords();
110: if (shapeCoords != null && shapeCoords.length() > 0) {
111: StringTokenizer tokens = new StringTokenizer(shapeCoords,
112: ",");
113: coordinates = new int[tokens.countTokens()];
114: int idx = 0;
115: while (tokens.hasMoreTokens()) {
116: String coord = tokens.nextToken();
117: coordinates[idx] = Integer.parseInt(coord);
118: ++idx;
119: }
120: }
121: return coordinates;
122: }
123:
124: protected abstract JRPrintHyperlink getEntityHyperlink(
125: ChartEntity entity);
126: }
|