Source for org.jfree.chart.imagemap.ImageMapUtilities

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ----------------------
  28:  * ImageMapUtilities.java
  29:  * ----------------------
  30:  * (C) Copyright 2004-2007, by Richard Atkinson and Contributors.
  31:  *
  32:  * Original Author:  Richard Atkinson;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: ImageMapUtilities.java,v 1.3.2.3 2007/02/06 11:28:53 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 02-Aug-2004 : Initial version (RA);
  40:  * 13-Jan-2005 : Renamed ImageMapUtilities (DG);
  41:  * 19-Jan-2005 : Reversed order of tags for chart entities to get correct
  42:  *               layering (DG);
  43:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  44:  * 06-Feb-2006 : API doc updates (DG);
  45:  * 
  46:  */
  47: 
  48: package org.jfree.chart.imagemap;
  49: 
  50: import java.io.IOException;
  51: import java.io.PrintWriter;
  52: 
  53: import org.jfree.chart.ChartRenderingInfo;
  54: import org.jfree.chart.entity.ChartEntity;
  55: import org.jfree.chart.entity.EntityCollection;
  56: import org.jfree.util.StringUtils;
  57: 
  58: /**
  59:  * Collection of utility methods related to producing image maps.  
  60:  * Functionality was originally in {@link org.jfree.chart.ChartUtilities}.
  61:  */
  62: public class ImageMapUtilities {
  63: 
  64:     /**
  65:      * Writes an image map to an output stream.
  66:      *
  67:      * @param writer  the writer (<code>null</code> not permitted).
  68:      * @param name  the map name (<code>null</code> not permitted).
  69:      * @param info  the chart rendering info (<code>null</code> not permitted).
  70:      *
  71:      * @throws java.io.IOException if there are any I/O errors.
  72:      */
  73:     public static void writeImageMap(PrintWriter writer, String name, 
  74:                                      ChartRenderingInfo info)
  75:         throws IOException {
  76: 
  77:         // defer argument checking...
  78:         ImageMapUtilities.writeImageMap(writer, name, info,
  79:                 new StandardToolTipTagFragmentGenerator(),
  80:                 new StandardURLTagFragmentGenerator());
  81: 
  82:     }
  83: 
  84:     /**
  85:      * Writes an image map to an output stream.
  86:      *
  87:      * @param writer  the writer (<code>null</code> not permitted).
  88:      * @param name  the map name (<code>null</code> not permitted).
  89:      * @param info  the chart rendering info (<code>null</code> not permitted).
  90:      * @param useOverLibForToolTips  whether to use OverLIB for tooltips
  91:      *                               (http://www.bosrup.com/web/overlib/).
  92:      *
  93:      * @throws java.io.IOException if there are any I/O errors.
  94:      */
  95:     public static void writeImageMap(PrintWriter writer,
  96:                                      String name,
  97:                                      ChartRenderingInfo info,
  98:                                      boolean useOverLibForToolTips) 
  99:         throws IOException {
 100: 
 101:         ToolTipTagFragmentGenerator toolTipTagFragmentGenerator = null;
 102:         if (useOverLibForToolTips) {
 103:             toolTipTagFragmentGenerator 
 104:                     = new OverLIBToolTipTagFragmentGenerator();
 105:         }
 106:         else {
 107:             toolTipTagFragmentGenerator 
 108:                     = new StandardToolTipTagFragmentGenerator();
 109:         }
 110:         ImageMapUtilities.writeImageMap(writer, name, info, 
 111:                 toolTipTagFragmentGenerator, 
 112:                 new StandardURLTagFragmentGenerator());
 113: 
 114:     }
 115: 
 116:     /**
 117:      * Writes an image map to an output stream.
 118:      *
 119:      * @param writer  the writer (<code>null</code> not permitted).
 120:      * @param name  the map name (<code>null</code> not permitted).
 121:      * @param info  the chart rendering info (<code>null</code> not permitted).
 122:      * @param toolTipTagFragmentGenerator  a generator for the HTML fragment
 123:      *     that will contain the tooltip text (<code>null</code> not permitted 
 124:      *     if <code>info</code> contains tooltip information).
 125:      * @param urlTagFragmentGenerator  a generator for the HTML fragment that
 126:      *     will contain the URL reference (<code>null</code> not permitted if 
 127:      *     <code>info</code> contains URLs).
 128:      *
 129:      * @throws java.io.IOException if there are any I/O errors.
 130:      */
 131:     public static void writeImageMap(PrintWriter writer, String name, 
 132:             ChartRenderingInfo info,
 133:             ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
 134:             URLTagFragmentGenerator urlTagFragmentGenerator) 
 135:         throws IOException {
 136: 
 137:         writer.println(ImageMapUtilities.getImageMap(name, info, 
 138:                 toolTipTagFragmentGenerator, urlTagFragmentGenerator));
 139:     }
 140: 
 141:     /**
 142:      * Creates an image map element that complies with the XHTML 1.0
 143:      * specification.
 144:      *
 145:      * @param name  the map name (<code>null</code> not permitted).
 146:      * @param info  the chart rendering info (<code>null</code> not permitted).
 147:      *
 148:      * @return The map element.
 149:      */
 150:     public static String getImageMap(String name, ChartRenderingInfo info) {
 151:         return ImageMapUtilities.getImageMap(name, info,
 152:                 new StandardToolTipTagFragmentGenerator(),
 153:                 new StandardURLTagFragmentGenerator());
 154:     }
 155: 
 156:     /**
 157:      * Creates an image map element that complies with the XHTML 1.0
 158:      * specification.
 159:      *
 160:      * @param name  the map name (<code>null</code> not permitted).
 161:      * @param info  the chart rendering info (<code>null</code> not permitted).
 162:      * @param toolTipTagFragmentGenerator  a generator for the HTML fragment
 163:      *     that will contain the tooltip text (<code>null</code> not permitted 
 164:      *     if <code>info</code> contains tooltip information).
 165:      * @param urlTagFragmentGenerator  a generator for the HTML fragment that
 166:      *     will contain the URL reference (<code>null</code> not permitted if 
 167:      *     <code>info</code> contains URLs).
 168:      *
 169:      * @return The map tag.
 170:      */
 171:     public static String getImageMap(String name, ChartRenderingInfo info,
 172:             ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
 173:             URLTagFragmentGenerator urlTagFragmentGenerator) {
 174: 
 175:         StringBuffer sb = new StringBuffer();
 176:         sb.append("<map id=\"" + name + "\" name=\"" + name + "\">");
 177:         sb.append(StringUtils.getLineSeparator());
 178:         EntityCollection entities = info.getEntityCollection();
 179:         if (entities != null) {
 180:             int count = entities.getEntityCount();
 181:             for (int i = count - 1; i >= 0; i--) {
 182:                 ChartEntity entity = entities.getEntity(i);
 183:                 if (entity.getToolTipText() != null 
 184:                         || entity.getURLText() != null) {
 185:                     String area = entity.getImageMapAreaTag(
 186:                             toolTipTagFragmentGenerator, 
 187:                             urlTagFragmentGenerator);
 188:                     if (area.length() > 0) {
 189:                         sb.append(area);
 190:                         sb.append(StringUtils.getLineSeparator());
 191:                     }
 192:                 }
 193:             }
 194:         }
 195:         sb.append("</map>");
 196:         return sb.toString();
 197:         
 198:     }
 199: 
 200: }