java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/tags/ChartMapTag.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Feb 2009 21:31:02 +0100
changeset 1 639991d0808a
permissions -rw-r--r--
Rozbalená knihovna verze 1.0
franta-hg@1
     1
/* ================================================================
franta-hg@1
     2
 * Cewolf : Chart enabling Web Objects Framework
franta-hg@1
     3
 * ================================================================
franta-hg@1
     4
 *
franta-hg@1
     5
 * Project Info:  http://cewolf.sourceforge.net
franta-hg@1
     6
 * Project Lead:  Guido Laures (guido@laures.de);
franta-hg@1
     7
 *
franta-hg@1
     8
 * (C) Copyright 2002, by Guido Laures
franta-hg@1
     9
 *
franta-hg@1
    10
 * This library is free software; you can redistribute it and/or modify it under the terms
franta-hg@1
    11
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
franta-hg@1
    12
 * either version 2.1 of the License, or (at your option) any later version.
franta-hg@1
    13
 *
franta-hg@1
    14
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
franta-hg@1
    15
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
franta-hg@1
    16
 * See the GNU Lesser General Public License for more details.
franta-hg@1
    17
 *
franta-hg@1
    18
 * You should have received a copy of the GNU Lesser General Public License along with this
franta-hg@1
    19
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
franta-hg@1
    20
 * Boston, MA 02111-1307, USA.
franta-hg@1
    21
 */
franta-hg@1
    22
franta-hg@1
    23
package de.laures.cewolf.taglib.tags;
franta-hg@1
    24
franta-hg@1
    25
import java.io.IOException;
franta-hg@1
    26
import java.io.Writer;
franta-hg@1
    27
import java.util.Iterator;
franta-hg@1
    28
franta-hg@1
    29
import javax.servlet.http.HttpServletRequest;
franta-hg@1
    30
import javax.servlet.http.HttpServletResponse;
franta-hg@1
    31
import javax.servlet.jsp.JspException;
franta-hg@1
    32
franta-hg@1
    33
import org.apache.commons.logging.Log;
franta-hg@1
    34
import org.apache.commons.logging.LogFactory;
franta-hg@1
    35
import org.jfree.chart.ChartRenderingInfo;
franta-hg@1
    36
import org.jfree.chart.entity.CategoryItemEntity;
franta-hg@1
    37
import org.jfree.chart.entity.ChartEntity;
franta-hg@1
    38
import org.jfree.chart.entity.LegendItemEntity;
franta-hg@1
    39
import org.jfree.chart.entity.PieSectionEntity;
franta-hg@1
    40
import org.jfree.chart.entity.XYItemEntity;
franta-hg@1
    41
import org.jfree.data.category.CategoryDataset;
franta-hg@1
    42
import org.jfree.data.general.Dataset;
franta-hg@1
    43
import org.jfree.data.general.PieDataset;
franta-hg@1
    44
import org.jfree.data.xy.XYDataset;
franta-hg@1
    45
franta-hg@1
    46
import de.laures.cewolf.CewolfException;
franta-hg@1
    47
import de.laures.cewolf.Configuration;
franta-hg@1
    48
import de.laures.cewolf.links.CategoryItemLinkGenerator;
franta-hg@1
    49
import de.laures.cewolf.links.LinkGenerator;
franta-hg@1
    50
import de.laures.cewolf.links.PieSectionLinkGenerator;
franta-hg@1
    51
import de.laures.cewolf.links.XYItemLinkGenerator;
franta-hg@1
    52
import de.laures.cewolf.taglib.util.BrowserDetection;
franta-hg@1
    53
import de.laures.cewolf.taglib.util.PageUtils;
franta-hg@1
    54
import de.laures.cewolf.tooltips.CategoryToolTipGenerator;
franta-hg@1
    55
import de.laures.cewolf.tooltips.PieToolTipGenerator;
franta-hg@1
    56
import de.laures.cewolf.tooltips.ToolTipGenerator;
franta-hg@1
    57
import de.laures.cewolf.tooltips.XYToolTipGenerator;
franta-hg@1
    58
franta-hg@1
    59
/**
franta-hg@1
    60
 * Tag &lt;map&gt; which defines the tooltip and link tags.
franta-hg@1
    61
 * @see DataTag
franta-hg@1
    62
 * @author  Guido Laures
franta-hg@1
    63
 */
franta-hg@1
    64
public class ChartMapTag extends CewolfTag {
franta-hg@1
    65
franta-hg@1
    66
	private static final long serialVersionUID = -3742340487378471159L;
franta-hg@1
    67
	
franta-hg@1
    68
	private static final Log LOG = LogFactory.getLog(ChartMapTag.class);
franta-hg@1
    69
		
franta-hg@1
    70
	ToolTipGenerator toolTipGenerator = null;
franta-hg@1
    71
	LinkGenerator linkGenerator = null;
franta-hg@1
    72
	
franta-hg@1
    73
	// If the links provided by the JFreeChart renderer should be used.
franta-hg@1
    74
	boolean useJFreeChartLinkGenerator = false;
franta-hg@1
    75
	// If the tooltips provided by the JFreeChart renderer should be used.
franta-hg@1
    76
	boolean useJFreeChartTooltipGenerator = false;
franta-hg@1
    77
franta-hg@1
    78
	public int doStartTag() throws JspException {
franta-hg@1
    79
		// Object linkGenerator = getLinkGenerator();
franta-hg@1
    80
		Mapped root = (Mapped) PageUtils.findRoot(this, pageContext);
franta-hg@1
    81
		root.enableMapping();
franta-hg@1
    82
		String chartId = ((CewolfRootTag) root).getChartId();
franta-hg@1
    83
		try {
franta-hg@1
    84
			Dataset dataset = PageUtils.getDataset(chartId, pageContext);
franta-hg@1
    85
			Writer out = pageContext.getOut();
franta-hg@1
    86
			final boolean isIE = BrowserDetection.isIE((HttpServletRequest) pageContext.getRequest());
franta-hg@1
    87
			if (hasToolTips()) {
franta-hg@1
    88
				enableToolTips(out, isIE);
franta-hg@1
    89
			}
franta-hg@1
    90
			out.write("<MAP name=\"" + chartId + "\">\n");
franta-hg@1
    91
			ChartRenderingInfo info = (ChartRenderingInfo) root.getRenderingInfo();
franta-hg@1
    92
			Iterator entities = info.getEntityCollection().iterator();
franta-hg@1
    93
			while (entities.hasNext()) {
franta-hg@1
    94
				ChartEntity ce = (ChartEntity) entities.next();
franta-hg@1
    95
				out.write("\n<AREA shape=\"" + ce.getShapeType() + "\" ");
franta-hg@1
    96
				out.write("COORDS=\"" + ce.getShapeCoords() + "\" ");
franta-hg@1
    97
		        if (ce instanceof XYItemEntity)
franta-hg@1
    98
		        {
franta-hg@1
    99
		          dataset = ((XYItemEntity)ce).getDataset();
franta-hg@1
   100
		        }
franta-hg@1
   101
				if (!(ce instanceof LegendItemEntity)) {
franta-hg@1
   102
					if (hasToolTips()) {
franta-hg@1
   103
						writeOutToolTip(dataset, out, isIE, ce);
franta-hg@1
   104
					}
franta-hg@1
   105
					if (hasLinks()) {
franta-hg@1
   106
						writeOutLink(linkGenerator, dataset, out, ce);
franta-hg@1
   107
					}
franta-hg@1
   108
				}
franta-hg@1
   109
				out.write(">");
franta-hg@1
   110
			}
franta-hg@1
   111
		} catch (IOException ioex) {
franta-hg@1
   112
			log.error(ioex);
franta-hg@1
   113
			throw new JspException(ioex.getMessage());
franta-hg@1
   114
		} catch (CewolfException cwex) {
franta-hg@1
   115
			log.error(cwex);
franta-hg@1
   116
			throw new JspException(cwex.getMessage());
franta-hg@1
   117
		}
franta-hg@1
   118
		return EVAL_PAGE;
franta-hg@1
   119
	}
franta-hg@1
   120
franta-hg@1
   121
	public int doEndTag() throws JspException {
franta-hg@1
   122
		// print out image map end
franta-hg@1
   123
		Writer out = pageContext.getOut();
franta-hg@1
   124
		try {
franta-hg@1
   125
			out.write("</MAP>");
franta-hg@1
   126
		} catch (IOException ioex) {
franta-hg@1
   127
			log.error(ioex);
franta-hg@1
   128
			throw new JspException(ioex.getMessage());
franta-hg@1
   129
		}
franta-hg@1
   130
		return doAfterEndTag(EVAL_PAGE);
franta-hg@1
   131
	}
franta-hg@1
   132
franta-hg@1
   133
	public void reset() {
franta-hg@1
   134
		this.toolTipGenerator = null;
franta-hg@1
   135
		this.linkGenerator = null;
franta-hg@1
   136
	}
franta-hg@1
   137
franta-hg@1
   138
	public void writeOutLink(Object linkGen, Dataset dataset, Writer out, ChartEntity ce) throws IOException {
franta-hg@1
   139
		final String link = generateLink(dataset, ce);
franta-hg@1
   140
		
franta-hg@1
   141
		if (null != link) {
franta-hg@1
   142
			final String href = ((HttpServletResponse) pageContext.getResponse()).encodeURL(link);
franta-hg@1
   143
			out.write("HREF=\"" + href + "\"");
franta-hg@1
   144
		}
franta-hg@1
   145
	}
franta-hg@1
   146
franta-hg@1
   147
	private void writeOutToolTip(Dataset dataset, Writer out, final boolean isIE, ChartEntity ce) throws IOException, JspException {
franta-hg@1
   148
		String toolTip = generateToolTip(dataset, ce);
franta-hg@1
   149
		if (null != toolTip) {
franta-hg@1
   150
			if (!isIE) {
franta-hg@1
   151
				out.write("ONMOUSEOVER=\"return overlib('"
franta-hg@1
   152
						+ toolTip + "', WIDTH, '20');\" ONMOUSEOUT=\"return nd();\" ");
franta-hg@1
   153
			} else {
franta-hg@1
   154
				out.write("ALT=\"" + toolTip + "\" ");
franta-hg@1
   155
			}
franta-hg@1
   156
		}
franta-hg@1
   157
	}
franta-hg@1
   158
franta-hg@1
   159
	public void enableToolTips(Writer out, final boolean isIE) throws IOException {
franta-hg@1
   160
		if (!PageUtils.isToolTipsEnabled(pageContext) && !isIE) {
franta-hg@1
   161
			Configuration config = Configuration.getInstance(pageContext.getServletContext());
franta-hg@1
   162
			String overLibURL = ChartImgTag.fixAbsolutURL(config.getOverlibURL(), pageContext);
franta-hg@1
   163
			out.write("<script language=\"JavaScript\" src=\"");
franta-hg@1
   164
			out.write(overLibURL + "\"><!-- overLIB (c) Erik Bosrup --></script>\n");
franta-hg@1
   165
			out.write("<div id=\"overDiv\" style=\"position:absolute; visibility:hidden; z-index:1000;\"></div>\n");
franta-hg@1
   166
			PageUtils.setToolTipsEnabled(pageContext);
franta-hg@1
   167
		}
franta-hg@1
   168
	}
franta-hg@1
   169
franta-hg@1
   170
	private String generateLink(Dataset dataset, ChartEntity ce) {
franta-hg@1
   171
		String link = null;
franta-hg@1
   172
		if (useJFreeChartLinkGenerator) {
franta-hg@1
   173
			link = ce.getURLText();
franta-hg@1
   174
		} else if (linkGenerator instanceof CategoryItemLinkGenerator || linkGenerator instanceof XYItemLinkGenerator || linkGenerator instanceof PieSectionLinkGenerator) {
franta-hg@1
   175
    		if (linkGenerator instanceof CategoryItemLinkGenerator) {
franta-hg@1
   176
    			if (ce instanceof CategoryItemEntity) {
franta-hg@1
   177
    				CategoryItemEntity catEnt = (CategoryItemEntity) ce;
franta-hg@1
   178
    				link = ((CategoryItemLinkGenerator) linkGenerator).generateLink(dataset, catEnt.getSeries(), catEnt.getCategory());
franta-hg@1
   179
    			}
franta-hg@1
   180
    		}
franta-hg@1
   181
    		if (linkGenerator instanceof XYItemLinkGenerator) {
franta-hg@1
   182
  		    if (ce instanceof XYItemEntity) {
franta-hg@1
   183
    				XYItemEntity xyEnt = (XYItemEntity) ce;
franta-hg@1
   184
    				link = ((XYItemLinkGenerator) linkGenerator).generateLink(dataset, xyEnt.getSeriesIndex(), xyEnt.getItem());
franta-hg@1
   185
  		    } else {
franta-hg@1
   186
  		        // Note; there is a simple ChartEntity also passed since Jfreechart 1.0rc1, that is ignored
franta-hg@1
   187
  		        LOG.debug("Link entity skipped, not XYItemEntity.class:" + ce);
franta-hg@1
   188
  		    }
franta-hg@1
   189
    		}
franta-hg@1
   190
    		if (linkGenerator instanceof PieSectionLinkGenerator) {
franta-hg@1
   191
  		    if (ce instanceof PieSectionEntity) {
franta-hg@1
   192
    				PieSectionEntity pieEnt = (PieSectionEntity) ce;
franta-hg@1
   193
    				link = ((PieSectionLinkGenerator) linkGenerator).generateLink(dataset, pieEnt.getSectionKey());
franta-hg@1
   194
  		    }
franta-hg@1
   195
    		}
franta-hg@1
   196
  	}
franta-hg@1
   197
		return link;
franta-hg@1
   198
	}
franta-hg@1
   199
franta-hg@1
   200
	private String generateToolTip(Dataset dataset, ChartEntity ce) throws JspException {
franta-hg@1
   201
		String tooltip = null;
franta-hg@1
   202
		if (useJFreeChartTooltipGenerator) {
franta-hg@1
   203
			tooltip = ce.getToolTipText();
franta-hg@1
   204
		} else if (toolTipGenerator instanceof CategoryToolTipGenerator || toolTipGenerator instanceof XYToolTipGenerator || toolTipGenerator instanceof PieToolTipGenerator) {
franta-hg@1
   205
		  if (toolTipGenerator instanceof CategoryToolTipGenerator) {
franta-hg@1
   206
		    if (ce instanceof CategoryItemEntity) {
franta-hg@1
   207
				CategoryItemEntity catEnt = (CategoryItemEntity) ce;
franta-hg@1
   208
				tooltip = ((CategoryToolTipGenerator) toolTipGenerator).generateToolTip((CategoryDataset) dataset, catEnt.getSeries(), catEnt.getCategoryIndex());
franta-hg@1
   209
		    }
franta-hg@1
   210
		  }
franta-hg@1
   211
		    
franta-hg@1
   212
		  if (toolTipGenerator instanceof XYToolTipGenerator) {
franta-hg@1
   213
		    if (ce instanceof XYItemEntity) {
franta-hg@1
   214
				XYItemEntity xyEnt = (XYItemEntity) ce;
franta-hg@1
   215
				tooltip = ((XYToolTipGenerator) toolTipGenerator).generateToolTip((XYDataset) dataset, xyEnt.getSeriesIndex(), xyEnt.getItem());
franta-hg@1
   216
		    }
franta-hg@1
   217
		  }
franta-hg@1
   218
franta-hg@1
   219
		  if (toolTipGenerator instanceof PieToolTipGenerator) {
franta-hg@1
   220
		    if (ce instanceof PieSectionEntity) {
franta-hg@1
   221
				PieSectionEntity pieEnt = (PieSectionEntity) ce;
franta-hg@1
   222
				PieDataset ds = (PieDataset) dataset;
franta-hg@1
   223
				final int index = pieEnt.getSectionIndex();
franta-hg@1
   224
				tooltip = ((PieToolTipGenerator) toolTipGenerator).generateToolTip(ds, ds.getKey(index), index);
franta-hg@1
   225
		    }
franta-hg@1
   226
		  }
franta-hg@1
   227
		} else {
franta-hg@1
   228
			// throw because category is unknown
franta-hg@1
   229
		    throw new JspException(
franta-hg@1
   230
				"TooltipgGenerator of class " + toolTipGenerator.getClass().getName() + " does not implement the appropriate TooltipGenerator interface for entity type " + ce.getClass().getName());
franta-hg@1
   231
		}
franta-hg@1
   232
		return tooltip;
franta-hg@1
   233
	}
franta-hg@1
   234
franta-hg@1
   235
	private boolean hasToolTips() throws JspException {
franta-hg@1
   236
		if (toolTipGenerator!=null && useJFreeChartTooltipGenerator) {
franta-hg@1
   237
			throw new JspException("Can't have both tooltipGenerator and useJFreeChartTooltipGenerator parameters specified!");
franta-hg@1
   238
	}
franta-hg@1
   239
		return toolTipGenerator != null || useJFreeChartTooltipGenerator;
franta-hg@1
   240
	}
franta-hg@1
   241
franta-hg@1
   242
	public void setTooltipgeneratorid(String id) {
franta-hg@1
   243
		this.toolTipGenerator = (ToolTipGenerator) pageContext.findAttribute(id);
franta-hg@1
   244
	}
franta-hg@1
   245
franta-hg@1
   246
	private boolean hasLinks() throws JspException {
franta-hg@1
   247
		if (linkGenerator!=null && useJFreeChartLinkGenerator) {
franta-hg@1
   248
			throw new JspException("Can't have both linkGenerator and useJFreeChartLinkGenerator parameters specified!");
franta-hg@1
   249
	}
franta-hg@1
   250
		return linkGenerator != null || useJFreeChartLinkGenerator;
franta-hg@1
   251
	}
franta-hg@1
   252
franta-hg@1
   253
	public void setLinkgeneratorid(String id) {
franta-hg@1
   254
		this.linkGenerator = (LinkGenerator) pageContext.findAttribute(id);
franta-hg@1
   255
	}
franta-hg@1
   256
	
franta-hg@1
   257
	/**
franta-hg@1
   258
	 * Setter of the useJFreeChartLinkGenerator field.
franta-hg@1
   259
	 * @param useJFreeChartLinkGenerator the useJFreeChartLinkGenerator to set.
franta-hg@1
   260
	 */
franta-hg@1
   261
	public void setUseJFreeChartLinkGenerator(boolean useJFreeChartLinkGenerator) {
franta-hg@1
   262
		this.useJFreeChartLinkGenerator = useJFreeChartLinkGenerator;
franta-hg@1
   263
	}
franta-hg@1
   264
	/**
franta-hg@1
   265
	 * Setter of the useJFreeChartTooltipGenerator field.
franta-hg@1
   266
	 * @param useJFreeChartTooltipGenerator the useJFreeChartTooltipGenerator to set.
franta-hg@1
   267
	 */
franta-hg@1
   268
	public void setUseJFreeChartTooltipGenerator(boolean useJFreeChartTooltipGenerator) {
franta-hg@1
   269
		this.useJFreeChartTooltipGenerator = useJFreeChartTooltipGenerator;
franta-hg@1
   270
	}
franta-hg@1
   271
	
franta-hg@1
   272
}