001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2005, 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, 2005, by Richard Atkinson and Contributors.
031     *
032     * Original Author:  Richard Atkinson;
033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
034     *
035     * $Id: ImageMapUtilities.java,v 1.3.2.1 2005/10/25 20:42:39 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 02-Aug-2004 : Initial version (RA);
040     * 13-Jan-2005 : Renamed ImageMapUtilities (DG);
041     * 19-Jan-2005 : Reversed order of tags for chart entities to get correct
042     *               layering (DG);
043     * 
044     */
045    
046    package org.jfree.chart.imagemap;
047    
048    import java.io.IOException;
049    import java.io.PrintWriter;
050    
051    import org.jfree.chart.ChartRenderingInfo;
052    import org.jfree.chart.entity.ChartEntity;
053    import org.jfree.chart.entity.EntityCollection;
054    import 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     *
060     * @author Richard Atkinson
061     */
062    public class ImageMapUtilities {
063    
064        /**
065         * Writes an image map to an output stream.
066         *
067         * @param writer  the writer (<code>null</code> not permitted).
068         * @param name  the map name (<code>null</code> not permitted).
069         * @param info  the chart rendering info (<code>null</code> not permitted).
070         *
071         * @throws java.io.IOException if there are any I/O errors.
072         */
073        public static void writeImageMap(PrintWriter writer, String name, 
074                                         ChartRenderingInfo info)
075            throws IOException {
076    
077            // defer argument checking...
078            ImageMapUtilities.writeImageMap(
079                writer, name, info,
080                new StandardToolTipTagFragmentGenerator(),
081                new StandardURLTagFragmentGenerator()
082            );
083    
084        }
085    
086        /**
087         * Writes an image map to an output stream.
088         *
089         * @param writer  the writer (<code>null</code> not permitted).
090         * @param name  the map name (<code>null</code> not permitted).
091         * @param info  the chart rendering info (<code>null</code> not permitted).
092         * @param useOverLibForToolTips  whether to use OverLIB for tooltips
093         *                               (http://www.bosrup.com/web/overlib/).
094         *
095         * @throws java.io.IOException if there are any I/O errors.
096         */
097        public static void writeImageMap(PrintWriter writer,
098                                         String name,
099                                         ChartRenderingInfo info,
100                                         boolean useOverLibForToolTips) 
101            throws IOException {
102    
103            ToolTipTagFragmentGenerator toolTipTagFragmentGenerator = null;
104            if (useOverLibForToolTips) {
105                toolTipTagFragmentGenerator 
106                    = new OverLIBToolTipTagFragmentGenerator();
107            }
108            else {
109                toolTipTagFragmentGenerator 
110                    = new StandardToolTipTagFragmentGenerator();
111            }
112            ImageMapUtilities.writeImageMap(
113                writer, name, info, toolTipTagFragmentGenerator, 
114                new StandardURLTagFragmentGenerator()
115            );
116    
117        }
118    
119        /**
120         * Writes an image map to an output stream.
121         *
122         * @param writer  the writer (<code>null</code> not permitted).
123         * @param name  the map name (<code>null</code> not permitted).
124         * @param info  the chart rendering info (<code>null</code> not permitted).
125         * @param toolTipTagFragmentGenerator  the tool tip generator.
126         * @param urlTagFragmentGenerator  the url generator.
127         *
128         * @throws java.io.IOException if there are any I/O errors.
129         */
130        public static void writeImageMap(PrintWriter writer, String name, 
131                ChartRenderingInfo info,
132                ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
133                URLTagFragmentGenerator urlTagFragmentGenerator) 
134            throws IOException {
135    
136            writer.println(
137                ImageMapUtilities.getImageMap(
138                    name, info, toolTipTagFragmentGenerator, urlTagFragmentGenerator
139                )
140            );
141        }
142    
143        /**
144         * Creates an image map element that complies with the XHTML 1.0
145         * specification.
146         *
147         * @param name  the map name (<code>null</code> not permitted).
148         * @param info  the chart rendering info (<code>null</code> not permitted).
149         *
150         * @return The map element.
151         */
152        public static String getImageMap(String name, ChartRenderingInfo info) {
153            return ImageMapUtilities.getImageMap(
154                name,
155                info,
156                new StandardToolTipTagFragmentGenerator(),
157                new StandardURLTagFragmentGenerator()
158            );
159        }
160    
161        /**
162         * Creates an image map element that complies with the XHTML 1.0
163         * specification.
164         *
165         * @param name  the map name (<code>null</code> not permitted).
166         * @param info  the chart rendering info (<code>null</code> not permitted).
167         * @param toolTipTagFragmentGenerator  the tool tip generator.
168         * @param urlTagFragmentGenerator  the url generator.
169         *
170         * @return The map tag.
171         */
172        public static String getImageMap(String name, ChartRenderingInfo info,
173                ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
174                URLTagFragmentGenerator urlTagFragmentGenerator) {
175    
176            StringBuffer sb = new StringBuffer();
177            sb.append("<map id=\"" + name + "\" name=\"" + name + "\">");
178            sb.append(StringUtils.getLineSeparator());
179            EntityCollection entities = info.getEntityCollection();
180            if (entities != null) {
181                int count = entities.getEntityCount();
182                for (int i = count - 1; i >= 0; i--) {
183                    ChartEntity entity = entities.getEntity(i);
184                    if (entity.getToolTipText() != null 
185                            || entity.getURLText() != null) {
186                        String area = entity.getImageMapAreaTag(
187                            toolTipTagFragmentGenerator, urlTagFragmentGenerator
188                        );
189                        if (area.length() > 0) {
190                            sb.append(area);
191                            sb.append(StringUtils.getLineSeparator());
192                        }
193                    }
194                }
195            }
196            sb.append("</map>");
197            return sb.toString();
198            
199        }
200    
201    }