001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ---------------------- 028 * ImageMapUtilities.java 029 * ---------------------- 030 * (C) Copyright 2004-2007, by Richard Atkinson and Contributors. 031 * 032 * Original Author: Richard Atkinson; 033 * Contributor(s): David Gilbert (for Object Refinery Limited); 034 * 035 * Changes 036 * ------- 037 * 02-Aug-2004 : Initial version (RA); 038 * 13-Jan-2005 : Renamed ImageMapUtilities (DG); 039 * 19-Jan-2005 : Reversed order of tags for chart entities to get correct 040 * layering (DG); 041 * ------------- JFREECHART 1.0.x --------------------------------------------- 042 * 06-Feb-2006 : API doc updates (DG); 043 * 044 */ 045 046package org.jfree.chart.imagemap; 047 048import java.io.IOException; 049import java.io.PrintWriter; 050 051import org.jfree.chart.ChartRenderingInfo; 052import org.jfree.chart.entity.ChartEntity; 053import org.jfree.chart.entity.EntityCollection; 054import org.jfree.util.StringUtils; 055 056/** 057 * Collection of utility methods related to producing image maps. 058 * Functionality was originally in {@link org.jfree.chart.ChartUtilities}. 059 */ 060public class ImageMapUtilities { 061 062 /** 063 * Writes an image map to an output stream. 064 * 065 * @param writer the writer (<code>null</code> not permitted). 066 * @param name the map name (<code>null</code> not permitted). 067 * @param info the chart rendering info (<code>null</code> not permitted). 068 * 069 * @throws java.io.IOException if there are any I/O errors. 070 */ 071 public static void writeImageMap(PrintWriter writer, String name, 072 ChartRenderingInfo info) 073 throws IOException { 074 075 // defer argument checking... 076 ImageMapUtilities.writeImageMap(writer, name, info, 077 new StandardToolTipTagFragmentGenerator(), 078 new StandardURLTagFragmentGenerator()); 079 080 } 081 082 /** 083 * Writes an image map to an output stream. 084 * 085 * @param writer the writer (<code>null</code> not permitted). 086 * @param name the map name (<code>null</code> not permitted). 087 * @param info the chart rendering info (<code>null</code> not permitted). 088 * @param useOverLibForToolTips whether to use OverLIB for tooltips 089 * (http://www.bosrup.com/web/overlib/). 090 * 091 * @throws java.io.IOException if there are any I/O errors. 092 */ 093 public static void writeImageMap(PrintWriter writer, 094 String name, 095 ChartRenderingInfo info, 096 boolean useOverLibForToolTips) 097 throws IOException { 098 099 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator = null; 100 if (useOverLibForToolTips) { 101 toolTipTagFragmentGenerator 102 = new OverLIBToolTipTagFragmentGenerator(); 103 } 104 else { 105 toolTipTagFragmentGenerator 106 = new StandardToolTipTagFragmentGenerator(); 107 } 108 ImageMapUtilities.writeImageMap(writer, name, info, 109 toolTipTagFragmentGenerator, 110 new StandardURLTagFragmentGenerator()); 111 112 } 113 114 /** 115 * Writes an image map to an output stream. 116 * 117 * @param writer the writer (<code>null</code> not permitted). 118 * @param name the map name (<code>null</code> not permitted). 119 * @param info the chart rendering info (<code>null</code> not permitted). 120 * @param toolTipTagFragmentGenerator a generator for the HTML fragment 121 * that will contain the tooltip text (<code>null</code> not permitted 122 * if <code>info</code> contains tooltip information). 123 * @param urlTagFragmentGenerator a generator for the HTML fragment that 124 * will contain the URL reference (<code>null</code> not permitted if 125 * <code>info</code> contains URLs). 126 * 127 * @throws java.io.IOException if there are any I/O errors. 128 */ 129 public static void writeImageMap(PrintWriter writer, String name, 130 ChartRenderingInfo info, 131 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, 132 URLTagFragmentGenerator urlTagFragmentGenerator) 133 throws IOException { 134 135 writer.println(ImageMapUtilities.getImageMap(name, info, 136 toolTipTagFragmentGenerator, urlTagFragmentGenerator)); 137 } 138 139 /** 140 * Creates an image map element that complies with the XHTML 1.0 141 * specification. 142 * 143 * @param name the map name (<code>null</code> not permitted). 144 * @param info the chart rendering info (<code>null</code> not permitted). 145 * 146 * @return The map element. 147 */ 148 public static String getImageMap(String name, ChartRenderingInfo info) { 149 return ImageMapUtilities.getImageMap(name, info, 150 new StandardToolTipTagFragmentGenerator(), 151 new StandardURLTagFragmentGenerator()); 152 } 153 154 /** 155 * Creates an image map element that complies with the XHTML 1.0 156 * specification. 157 * 158 * @param name the map name (<code>null</code> not permitted). 159 * @param info the chart rendering info (<code>null</code> not permitted). 160 * @param toolTipTagFragmentGenerator a generator for the HTML fragment 161 * that will contain the tooltip text (<code>null</code> not permitted 162 * if <code>info</code> contains tooltip information). 163 * @param urlTagFragmentGenerator a generator for the HTML fragment that 164 * will contain the URL reference (<code>null</code> not permitted if 165 * <code>info</code> contains URLs). 166 * 167 * @return The map tag. 168 */ 169 public static String getImageMap(String name, ChartRenderingInfo info, 170 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, 171 URLTagFragmentGenerator urlTagFragmentGenerator) { 172 173 StringBuffer sb = new StringBuffer(); 174 sb.append("<map id=\"" + name + "\" name=\"" + name + "\">"); 175 sb.append(StringUtils.getLineSeparator()); 176 EntityCollection entities = info.getEntityCollection(); 177 if (entities != null) { 178 int count = entities.getEntityCount(); 179 for (int i = count - 1; i >= 0; i--) { 180 ChartEntity entity = entities.getEntity(i); 181 if (entity.getToolTipText() != null 182 || entity.getURLText() != null) { 183 String area = entity.getImageMapAreaTag( 184 toolTipTagFragmentGenerator, 185 urlTagFragmentGenerator); 186 if (area.length() > 0) { 187 sb.append(area); 188 sb.append(StringUtils.getLineSeparator()); 189 } 190 } 191 } 192 } 193 sb.append("</map>"); 194 return sb.toString(); 195 196 } 197 198}