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:  * Changes
  36:  * -------
  37:  * 02-Aug-2004 : Initial version (RA);
  38:  * 13-Jan-2005 : Renamed ImageMapUtilities (DG);
  39:  * 19-Jan-2005 : Reversed order of tags for chart entities to get correct
  40:  *               layering (DG);
  41:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  42:  * 06-Feb-2006 : API doc updates (DG);
  43:  * 
  44:  */
  45: 
  46: package org.jfree.chart.imagemap;
  47: 
  48: import java.io.IOException;
  49: import java.io.PrintWriter;
  50: 
  51: import org.jfree.chart.ChartRenderingInfo;
  52: import org.jfree.chart.entity.ChartEntity;
  53: import org.jfree.chart.entity.EntityCollection;
  54: import org.jfree.util.StringUtils;
  55: 
  56: /**
  57:  * Collection of utility methods related to producing image maps.  
  58:  * Functionality was originally in {@link org.jfree.chart.ChartUtilities}.
  59:  */
  60: public class ImageMapUtilities {
  61: 
  62:     /**
  63:      * Writes an image map to an output stream.
  64:      *
  65:      * @param writer  the writer (<code>null</code> not permitted).
  66:      * @param name  the map name (<code>null</code> not permitted).
  67:      * @param info  the chart rendering info (<code>null</code> not permitted).
  68:      *
  69:      * @throws java.io.IOException if there are any I/O errors.
  70:      */
  71:     public static void writeImageMap(PrintWriter writer, String name, 
  72:                                      ChartRenderingInfo info)
  73:         throws IOException {
  74: 
  75:         // defer argument checking...
  76:         ImageMapUtilities.writeImageMap(writer, name, info,
  77:                 new StandardToolTipTagFragmentGenerator(),
  78:                 new StandardURLTagFragmentGenerator());
  79: 
  80:     }
  81: 
  82:     /**
  83:      * Writes an image map to an output stream.
  84:      *
  85:      * @param writer  the writer (<code>null</code> not permitted).
  86:      * @param name  the map name (<code>null</code> not permitted).
  87:      * @param info  the chart rendering info (<code>null</code> not permitted).
  88:      * @param useOverLibForToolTips  whether to use OverLIB for tooltips
  89:      *                               (http://www.bosrup.com/web/overlib/).
  90:      *
  91:      * @throws java.io.IOException if there are any I/O errors.
  92:      */
  93:     public static void writeImageMap(PrintWriter writer,
  94:                                      String name,
  95:                                      ChartRenderingInfo info,
  96:                                      boolean useOverLibForToolTips) 
  97:         throws IOException {
  98: 
  99:         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: }