1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/tags/ChartMapTag.java Sat Feb 28 21:31:02 2009 +0100
1.3 @@ -0,0 +1,272 @@
1.4 +/* ================================================================
1.5 + * Cewolf : Chart enabling Web Objects Framework
1.6 + * ================================================================
1.7 + *
1.8 + * Project Info: http://cewolf.sourceforge.net
1.9 + * Project Lead: Guido Laures (guido@laures.de);
1.10 + *
1.11 + * (C) Copyright 2002, by Guido Laures
1.12 + *
1.13 + * This library is free software; you can redistribute it and/or modify it under the terms
1.14 + * of the GNU Lesser General Public License as published by the Free Software Foundation;
1.15 + * either version 2.1 of the License, or (at your option) any later version.
1.16 + *
1.17 + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
1.18 + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1.19 + * See the GNU Lesser General Public License for more details.
1.20 + *
1.21 + * You should have received a copy of the GNU Lesser General Public License along with this
1.22 + * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1.23 + * Boston, MA 02111-1307, USA.
1.24 + */
1.25 +
1.26 +package de.laures.cewolf.taglib.tags;
1.27 +
1.28 +import java.io.IOException;
1.29 +import java.io.Writer;
1.30 +import java.util.Iterator;
1.31 +
1.32 +import javax.servlet.http.HttpServletRequest;
1.33 +import javax.servlet.http.HttpServletResponse;
1.34 +import javax.servlet.jsp.JspException;
1.35 +
1.36 +import org.apache.commons.logging.Log;
1.37 +import org.apache.commons.logging.LogFactory;
1.38 +import org.jfree.chart.ChartRenderingInfo;
1.39 +import org.jfree.chart.entity.CategoryItemEntity;
1.40 +import org.jfree.chart.entity.ChartEntity;
1.41 +import org.jfree.chart.entity.LegendItemEntity;
1.42 +import org.jfree.chart.entity.PieSectionEntity;
1.43 +import org.jfree.chart.entity.XYItemEntity;
1.44 +import org.jfree.data.category.CategoryDataset;
1.45 +import org.jfree.data.general.Dataset;
1.46 +import org.jfree.data.general.PieDataset;
1.47 +import org.jfree.data.xy.XYDataset;
1.48 +
1.49 +import de.laures.cewolf.CewolfException;
1.50 +import de.laures.cewolf.Configuration;
1.51 +import de.laures.cewolf.links.CategoryItemLinkGenerator;
1.52 +import de.laures.cewolf.links.LinkGenerator;
1.53 +import de.laures.cewolf.links.PieSectionLinkGenerator;
1.54 +import de.laures.cewolf.links.XYItemLinkGenerator;
1.55 +import de.laures.cewolf.taglib.util.BrowserDetection;
1.56 +import de.laures.cewolf.taglib.util.PageUtils;
1.57 +import de.laures.cewolf.tooltips.CategoryToolTipGenerator;
1.58 +import de.laures.cewolf.tooltips.PieToolTipGenerator;
1.59 +import de.laures.cewolf.tooltips.ToolTipGenerator;
1.60 +import de.laures.cewolf.tooltips.XYToolTipGenerator;
1.61 +
1.62 +/**
1.63 + * Tag <map> which defines the tooltip and link tags.
1.64 + * @see DataTag
1.65 + * @author Guido Laures
1.66 + */
1.67 +public class ChartMapTag extends CewolfTag {
1.68 +
1.69 + private static final long serialVersionUID = -3742340487378471159L;
1.70 +
1.71 + private static final Log LOG = LogFactory.getLog(ChartMapTag.class);
1.72 +
1.73 + ToolTipGenerator toolTipGenerator = null;
1.74 + LinkGenerator linkGenerator = null;
1.75 +
1.76 + // If the links provided by the JFreeChart renderer should be used.
1.77 + boolean useJFreeChartLinkGenerator = false;
1.78 + // If the tooltips provided by the JFreeChart renderer should be used.
1.79 + boolean useJFreeChartTooltipGenerator = false;
1.80 +
1.81 + public int doStartTag() throws JspException {
1.82 + // Object linkGenerator = getLinkGenerator();
1.83 + Mapped root = (Mapped) PageUtils.findRoot(this, pageContext);
1.84 + root.enableMapping();
1.85 + String chartId = ((CewolfRootTag) root).getChartId();
1.86 + try {
1.87 + Dataset dataset = PageUtils.getDataset(chartId, pageContext);
1.88 + Writer out = pageContext.getOut();
1.89 + final boolean isIE = BrowserDetection.isIE((HttpServletRequest) pageContext.getRequest());
1.90 + if (hasToolTips()) {
1.91 + enableToolTips(out, isIE);
1.92 + }
1.93 + out.write("<MAP name=\"" + chartId + "\">\n");
1.94 + ChartRenderingInfo info = (ChartRenderingInfo) root.getRenderingInfo();
1.95 + Iterator entities = info.getEntityCollection().iterator();
1.96 + while (entities.hasNext()) {
1.97 + ChartEntity ce = (ChartEntity) entities.next();
1.98 + out.write("\n<AREA shape=\"" + ce.getShapeType() + "\" ");
1.99 + out.write("COORDS=\"" + ce.getShapeCoords() + "\" ");
1.100 + if (ce instanceof XYItemEntity)
1.101 + {
1.102 + dataset = ((XYItemEntity)ce).getDataset();
1.103 + }
1.104 + if (!(ce instanceof LegendItemEntity)) {
1.105 + if (hasToolTips()) {
1.106 + writeOutToolTip(dataset, out, isIE, ce);
1.107 + }
1.108 + if (hasLinks()) {
1.109 + writeOutLink(linkGenerator, dataset, out, ce);
1.110 + }
1.111 + }
1.112 + out.write(">");
1.113 + }
1.114 + } catch (IOException ioex) {
1.115 + log.error(ioex);
1.116 + throw new JspException(ioex.getMessage());
1.117 + } catch (CewolfException cwex) {
1.118 + log.error(cwex);
1.119 + throw new JspException(cwex.getMessage());
1.120 + }
1.121 + return EVAL_PAGE;
1.122 + }
1.123 +
1.124 + public int doEndTag() throws JspException {
1.125 + // print out image map end
1.126 + Writer out = pageContext.getOut();
1.127 + try {
1.128 + out.write("</MAP>");
1.129 + } catch (IOException ioex) {
1.130 + log.error(ioex);
1.131 + throw new JspException(ioex.getMessage());
1.132 + }
1.133 + return doAfterEndTag(EVAL_PAGE);
1.134 + }
1.135 +
1.136 + public void reset() {
1.137 + this.toolTipGenerator = null;
1.138 + this.linkGenerator = null;
1.139 + }
1.140 +
1.141 + public void writeOutLink(Object linkGen, Dataset dataset, Writer out, ChartEntity ce) throws IOException {
1.142 + final String link = generateLink(dataset, ce);
1.143 +
1.144 + if (null != link) {
1.145 + final String href = ((HttpServletResponse) pageContext.getResponse()).encodeURL(link);
1.146 + out.write("HREF=\"" + href + "\"");
1.147 + }
1.148 + }
1.149 +
1.150 + private void writeOutToolTip(Dataset dataset, Writer out, final boolean isIE, ChartEntity ce) throws IOException, JspException {
1.151 + String toolTip = generateToolTip(dataset, ce);
1.152 + if (null != toolTip) {
1.153 + if (!isIE) {
1.154 + out.write("ONMOUSEOVER=\"return overlib('"
1.155 + + toolTip + "', WIDTH, '20');\" ONMOUSEOUT=\"return nd();\" ");
1.156 + } else {
1.157 + out.write("ALT=\"" + toolTip + "\" ");
1.158 + }
1.159 + }
1.160 + }
1.161 +
1.162 + public void enableToolTips(Writer out, final boolean isIE) throws IOException {
1.163 + if (!PageUtils.isToolTipsEnabled(pageContext) && !isIE) {
1.164 + Configuration config = Configuration.getInstance(pageContext.getServletContext());
1.165 + String overLibURL = ChartImgTag.fixAbsolutURL(config.getOverlibURL(), pageContext);
1.166 + out.write("<script language=\"JavaScript\" src=\"");
1.167 + out.write(overLibURL + "\"><!-- overLIB (c) Erik Bosrup --></script>\n");
1.168 + out.write("<div id=\"overDiv\" style=\"position:absolute; visibility:hidden; z-index:1000;\"></div>\n");
1.169 + PageUtils.setToolTipsEnabled(pageContext);
1.170 + }
1.171 + }
1.172 +
1.173 + private String generateLink(Dataset dataset, ChartEntity ce) {
1.174 + String link = null;
1.175 + if (useJFreeChartLinkGenerator) {
1.176 + link = ce.getURLText();
1.177 + } else if (linkGenerator instanceof CategoryItemLinkGenerator || linkGenerator instanceof XYItemLinkGenerator || linkGenerator instanceof PieSectionLinkGenerator) {
1.178 + if (linkGenerator instanceof CategoryItemLinkGenerator) {
1.179 + if (ce instanceof CategoryItemEntity) {
1.180 + CategoryItemEntity catEnt = (CategoryItemEntity) ce;
1.181 + link = ((CategoryItemLinkGenerator) linkGenerator).generateLink(dataset, catEnt.getSeries(), catEnt.getCategory());
1.182 + }
1.183 + }
1.184 + if (linkGenerator instanceof XYItemLinkGenerator) {
1.185 + if (ce instanceof XYItemEntity) {
1.186 + XYItemEntity xyEnt = (XYItemEntity) ce;
1.187 + link = ((XYItemLinkGenerator) linkGenerator).generateLink(dataset, xyEnt.getSeriesIndex(), xyEnt.getItem());
1.188 + } else {
1.189 + // Note; there is a simple ChartEntity also passed since Jfreechart 1.0rc1, that is ignored
1.190 + LOG.debug("Link entity skipped, not XYItemEntity.class:" + ce);
1.191 + }
1.192 + }
1.193 + if (linkGenerator instanceof PieSectionLinkGenerator) {
1.194 + if (ce instanceof PieSectionEntity) {
1.195 + PieSectionEntity pieEnt = (PieSectionEntity) ce;
1.196 + link = ((PieSectionLinkGenerator) linkGenerator).generateLink(dataset, pieEnt.getSectionKey());
1.197 + }
1.198 + }
1.199 + }
1.200 + return link;
1.201 + }
1.202 +
1.203 + private String generateToolTip(Dataset dataset, ChartEntity ce) throws JspException {
1.204 + String tooltip = null;
1.205 + if (useJFreeChartTooltipGenerator) {
1.206 + tooltip = ce.getToolTipText();
1.207 + } else if (toolTipGenerator instanceof CategoryToolTipGenerator || toolTipGenerator instanceof XYToolTipGenerator || toolTipGenerator instanceof PieToolTipGenerator) {
1.208 + if (toolTipGenerator instanceof CategoryToolTipGenerator) {
1.209 + if (ce instanceof CategoryItemEntity) {
1.210 + CategoryItemEntity catEnt = (CategoryItemEntity) ce;
1.211 + tooltip = ((CategoryToolTipGenerator) toolTipGenerator).generateToolTip((CategoryDataset) dataset, catEnt.getSeries(), catEnt.getCategoryIndex());
1.212 + }
1.213 + }
1.214 +
1.215 + if (toolTipGenerator instanceof XYToolTipGenerator) {
1.216 + if (ce instanceof XYItemEntity) {
1.217 + XYItemEntity xyEnt = (XYItemEntity) ce;
1.218 + tooltip = ((XYToolTipGenerator) toolTipGenerator).generateToolTip((XYDataset) dataset, xyEnt.getSeriesIndex(), xyEnt.getItem());
1.219 + }
1.220 + }
1.221 +
1.222 + if (toolTipGenerator instanceof PieToolTipGenerator) {
1.223 + if (ce instanceof PieSectionEntity) {
1.224 + PieSectionEntity pieEnt = (PieSectionEntity) ce;
1.225 + PieDataset ds = (PieDataset) dataset;
1.226 + final int index = pieEnt.getSectionIndex();
1.227 + tooltip = ((PieToolTipGenerator) toolTipGenerator).generateToolTip(ds, ds.getKey(index), index);
1.228 + }
1.229 + }
1.230 + } else {
1.231 + // throw because category is unknown
1.232 + throw new JspException(
1.233 + "TooltipgGenerator of class " + toolTipGenerator.getClass().getName() + " does not implement the appropriate TooltipGenerator interface for entity type " + ce.getClass().getName());
1.234 + }
1.235 + return tooltip;
1.236 + }
1.237 +
1.238 + private boolean hasToolTips() throws JspException {
1.239 + if (toolTipGenerator!=null && useJFreeChartTooltipGenerator) {
1.240 + throw new JspException("Can't have both tooltipGenerator and useJFreeChartTooltipGenerator parameters specified!");
1.241 + }
1.242 + return toolTipGenerator != null || useJFreeChartTooltipGenerator;
1.243 + }
1.244 +
1.245 + public void setTooltipgeneratorid(String id) {
1.246 + this.toolTipGenerator = (ToolTipGenerator) pageContext.findAttribute(id);
1.247 + }
1.248 +
1.249 + private boolean hasLinks() throws JspException {
1.250 + if (linkGenerator!=null && useJFreeChartLinkGenerator) {
1.251 + throw new JspException("Can't have both linkGenerator and useJFreeChartLinkGenerator parameters specified!");
1.252 + }
1.253 + return linkGenerator != null || useJFreeChartLinkGenerator;
1.254 + }
1.255 +
1.256 + public void setLinkgeneratorid(String id) {
1.257 + this.linkGenerator = (LinkGenerator) pageContext.findAttribute(id);
1.258 + }
1.259 +
1.260 + /**
1.261 + * Setter of the useJFreeChartLinkGenerator field.
1.262 + * @param useJFreeChartLinkGenerator the useJFreeChartLinkGenerator to set.
1.263 + */
1.264 + public void setUseJFreeChartLinkGenerator(boolean useJFreeChartLinkGenerator) {
1.265 + this.useJFreeChartLinkGenerator = useJFreeChartLinkGenerator;
1.266 + }
1.267 + /**
1.268 + * Setter of the useJFreeChartTooltipGenerator field.
1.269 + * @param useJFreeChartTooltipGenerator the useJFreeChartTooltipGenerator to set.
1.270 + */
1.271 + public void setUseJFreeChartTooltipGenerator(boolean useJFreeChartTooltipGenerator) {
1.272 + this.useJFreeChartTooltipGenerator = useJFreeChartTooltipGenerator;
1.273 + }
1.274 +
1.275 +}