franta-hg@1: /* ================================================================ franta-hg@1: * Cewolf : Chart enabling Web Objects Framework franta-hg@1: * ================================================================ franta-hg@1: * franta-hg@1: * Project Info: http://cewolf.sourceforge.net franta-hg@1: * Project Lead: Guido Laures (guido@laures.de); franta-hg@1: * franta-hg@1: * (C) Copyright 2002, by Guido Laures franta-hg@1: * franta-hg@1: * This library is free software; you can redistribute it and/or modify it under the terms franta-hg@1: * of the GNU Lesser General Public License as published by the Free Software Foundation; franta-hg@1: * either version 2.1 of the License, or (at your option) any later version. franta-hg@1: * franta-hg@1: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; franta-hg@1: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. franta-hg@1: * See the GNU Lesser General Public License for more details. franta-hg@1: * franta-hg@1: * You should have received a copy of the GNU Lesser General Public License along with this franta-hg@1: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, franta-hg@1: * Boston, MA 02111-1307, USA. franta-hg@1: */ franta-hg@1: franta-hg@1: package de.laures.cewolf.taglib.tags; franta-hg@1: franta-hg@1: import java.io.IOException; franta-hg@1: import java.io.Writer; franta-hg@1: import java.util.Iterator; franta-hg@1: franta-hg@1: import javax.servlet.http.HttpServletRequest; franta-hg@1: import javax.servlet.http.HttpServletResponse; franta-hg@1: import javax.servlet.jsp.JspException; franta-hg@1: franta-hg@1: import org.apache.commons.logging.Log; franta-hg@1: import org.apache.commons.logging.LogFactory; franta-hg@1: import org.jfree.chart.ChartRenderingInfo; franta-hg@1: import org.jfree.chart.entity.CategoryItemEntity; franta-hg@1: import org.jfree.chart.entity.ChartEntity; franta-hg@1: import org.jfree.chart.entity.LegendItemEntity; franta-hg@1: import org.jfree.chart.entity.PieSectionEntity; franta-hg@1: import org.jfree.chart.entity.XYItemEntity; franta-hg@1: import org.jfree.data.category.CategoryDataset; franta-hg@1: import org.jfree.data.general.Dataset; franta-hg@1: import org.jfree.data.general.PieDataset; franta-hg@1: import org.jfree.data.xy.XYDataset; franta-hg@1: franta-hg@1: import de.laures.cewolf.CewolfException; franta-hg@1: import de.laures.cewolf.Configuration; franta-hg@1: import de.laures.cewolf.links.CategoryItemLinkGenerator; franta-hg@1: import de.laures.cewolf.links.LinkGenerator; franta-hg@1: import de.laures.cewolf.links.PieSectionLinkGenerator; franta-hg@1: import de.laures.cewolf.links.XYItemLinkGenerator; franta-hg@1: import de.laures.cewolf.taglib.util.BrowserDetection; franta-hg@1: import de.laures.cewolf.taglib.util.PageUtils; franta-hg@1: import de.laures.cewolf.tooltips.CategoryToolTipGenerator; franta-hg@1: import de.laures.cewolf.tooltips.PieToolTipGenerator; franta-hg@1: import de.laures.cewolf.tooltips.ToolTipGenerator; franta-hg@1: import de.laures.cewolf.tooltips.XYToolTipGenerator; franta-hg@1: franta-hg@1: /** franta-hg@1: * Tag <map> which defines the tooltip and link tags. franta-hg@1: * @see DataTag franta-hg@1: * @author Guido Laures franta-hg@1: */ franta-hg@1: public class ChartMapTag extends CewolfTag { franta-hg@1: franta-hg@1: private static final long serialVersionUID = -3742340487378471159L; franta-hg@1: franta-hg@1: private static final Log LOG = LogFactory.getLog(ChartMapTag.class); franta-hg@1: franta-hg@1: ToolTipGenerator toolTipGenerator = null; franta-hg@1: LinkGenerator linkGenerator = null; franta-hg@1: franta-hg@1: // If the links provided by the JFreeChart renderer should be used. franta-hg@1: boolean useJFreeChartLinkGenerator = false; franta-hg@1: // If the tooltips provided by the JFreeChart renderer should be used. franta-hg@1: boolean useJFreeChartTooltipGenerator = false; franta-hg@1: franta-hg@1: public int doStartTag() throws JspException { franta-hg@1: // Object linkGenerator = getLinkGenerator(); franta-hg@1: Mapped root = (Mapped) PageUtils.findRoot(this, pageContext); franta-hg@1: root.enableMapping(); franta-hg@1: String chartId = ((CewolfRootTag) root).getChartId(); franta-hg@1: try { franta-hg@1: Dataset dataset = PageUtils.getDataset(chartId, pageContext); franta-hg@1: Writer out = pageContext.getOut(); franta-hg@1: final boolean isIE = BrowserDetection.isIE((HttpServletRequest) pageContext.getRequest()); franta-hg@1: if (hasToolTips()) { franta-hg@1: enableToolTips(out, isIE); franta-hg@1: } franta-hg@1: out.write("\n"); franta-hg@1: ChartRenderingInfo info = (ChartRenderingInfo) root.getRenderingInfo(); franta-hg@1: Iterator entities = info.getEntityCollection().iterator(); franta-hg@1: while (entities.hasNext()) { franta-hg@1: ChartEntity ce = (ChartEntity) entities.next(); franta-hg@1: out.write("\n"); franta-hg@1: } franta-hg@1: } catch (IOException ioex) { franta-hg@1: log.error(ioex); franta-hg@1: throw new JspException(ioex.getMessage()); franta-hg@1: } catch (CewolfException cwex) { franta-hg@1: log.error(cwex); franta-hg@1: throw new JspException(cwex.getMessage()); franta-hg@1: } franta-hg@1: return EVAL_PAGE; franta-hg@1: } franta-hg@1: franta-hg@1: public int doEndTag() throws JspException { franta-hg@1: // print out image map end franta-hg@1: Writer out = pageContext.getOut(); franta-hg@1: try { franta-hg@1: out.write(""); franta-hg@1: } catch (IOException ioex) { franta-hg@1: log.error(ioex); franta-hg@1: throw new JspException(ioex.getMessage()); franta-hg@1: } franta-hg@1: return doAfterEndTag(EVAL_PAGE); franta-hg@1: } franta-hg@1: franta-hg@1: public void reset() { franta-hg@1: this.toolTipGenerator = null; franta-hg@1: this.linkGenerator = null; franta-hg@1: } franta-hg@1: franta-hg@1: public void writeOutLink(Object linkGen, Dataset dataset, Writer out, ChartEntity ce) throws IOException { franta-hg@1: final String link = generateLink(dataset, ce); franta-hg@1: franta-hg@1: if (null != link) { franta-hg@1: final String href = ((HttpServletResponse) pageContext.getResponse()).encodeURL(link); franta-hg@1: out.write("HREF=\"" + href + "\""); franta-hg@1: } franta-hg@1: } franta-hg@1: franta-hg@1: private void writeOutToolTip(Dataset dataset, Writer out, final boolean isIE, ChartEntity ce) throws IOException, JspException { franta-hg@1: String toolTip = generateToolTip(dataset, ce); franta-hg@1: if (null != toolTip) { franta-hg@1: if (!isIE) { franta-hg@1: out.write("ONMOUSEOVER=\"return overlib('" franta-hg@1: + toolTip + "', WIDTH, '20');\" ONMOUSEOUT=\"return nd();\" "); franta-hg@1: } else { franta-hg@1: out.write("ALT=\"" + toolTip + "\" "); franta-hg@1: } franta-hg@1: } franta-hg@1: } franta-hg@1: franta-hg@1: public void enableToolTips(Writer out, final boolean isIE) throws IOException { franta-hg@1: if (!PageUtils.isToolTipsEnabled(pageContext) && !isIE) { franta-hg@1: Configuration config = Configuration.getInstance(pageContext.getServletContext()); franta-hg@1: String overLibURL = ChartImgTag.fixAbsolutURL(config.getOverlibURL(), pageContext); franta-hg@1: out.write("\n"); franta-hg@1: out.write("
\n"); franta-hg@1: PageUtils.setToolTipsEnabled(pageContext); franta-hg@1: } franta-hg@1: } franta-hg@1: franta-hg@1: private String generateLink(Dataset dataset, ChartEntity ce) { franta-hg@1: String link = null; franta-hg@1: if (useJFreeChartLinkGenerator) { franta-hg@1: link = ce.getURLText(); franta-hg@1: } else if (linkGenerator instanceof CategoryItemLinkGenerator || linkGenerator instanceof XYItemLinkGenerator || linkGenerator instanceof PieSectionLinkGenerator) { franta-hg@1: if (linkGenerator instanceof CategoryItemLinkGenerator) { franta-hg@1: if (ce instanceof CategoryItemEntity) { franta-hg@1: CategoryItemEntity catEnt = (CategoryItemEntity) ce; franta-hg@1: link = ((CategoryItemLinkGenerator) linkGenerator).generateLink(dataset, catEnt.getSeries(), catEnt.getCategory()); franta-hg@1: } franta-hg@1: } franta-hg@1: if (linkGenerator instanceof XYItemLinkGenerator) { franta-hg@1: if (ce instanceof XYItemEntity) { franta-hg@1: XYItemEntity xyEnt = (XYItemEntity) ce; franta-hg@1: link = ((XYItemLinkGenerator) linkGenerator).generateLink(dataset, xyEnt.getSeriesIndex(), xyEnt.getItem()); franta-hg@1: } else { franta-hg@1: // Note; there is a simple ChartEntity also passed since Jfreechart 1.0rc1, that is ignored franta-hg@1: LOG.debug("Link entity skipped, not XYItemEntity.class:" + ce); franta-hg@1: } franta-hg@1: } franta-hg@1: if (linkGenerator instanceof PieSectionLinkGenerator) { franta-hg@1: if (ce instanceof PieSectionEntity) { franta-hg@1: PieSectionEntity pieEnt = (PieSectionEntity) ce; franta-hg@1: link = ((PieSectionLinkGenerator) linkGenerator).generateLink(dataset, pieEnt.getSectionKey()); franta-hg@1: } franta-hg@1: } franta-hg@1: } franta-hg@1: return link; franta-hg@1: } franta-hg@1: franta-hg@1: private String generateToolTip(Dataset dataset, ChartEntity ce) throws JspException { franta-hg@1: String tooltip = null; franta-hg@1: if (useJFreeChartTooltipGenerator) { franta-hg@1: tooltip = ce.getToolTipText(); franta-hg@1: } else if (toolTipGenerator instanceof CategoryToolTipGenerator || toolTipGenerator instanceof XYToolTipGenerator || toolTipGenerator instanceof PieToolTipGenerator) { franta-hg@1: if (toolTipGenerator instanceof CategoryToolTipGenerator) { franta-hg@1: if (ce instanceof CategoryItemEntity) { franta-hg@1: CategoryItemEntity catEnt = (CategoryItemEntity) ce; franta-hg@1: tooltip = ((CategoryToolTipGenerator) toolTipGenerator).generateToolTip((CategoryDataset) dataset, catEnt.getSeries(), catEnt.getCategoryIndex()); franta-hg@1: } franta-hg@1: } franta-hg@1: franta-hg@1: if (toolTipGenerator instanceof XYToolTipGenerator) { franta-hg@1: if (ce instanceof XYItemEntity) { franta-hg@1: XYItemEntity xyEnt = (XYItemEntity) ce; franta-hg@1: tooltip = ((XYToolTipGenerator) toolTipGenerator).generateToolTip((XYDataset) dataset, xyEnt.getSeriesIndex(), xyEnt.getItem()); franta-hg@1: } franta-hg@1: } franta-hg@1: franta-hg@1: if (toolTipGenerator instanceof PieToolTipGenerator) { franta-hg@1: if (ce instanceof PieSectionEntity) { franta-hg@1: PieSectionEntity pieEnt = (PieSectionEntity) ce; franta-hg@1: PieDataset ds = (PieDataset) dataset; franta-hg@1: final int index = pieEnt.getSectionIndex(); franta-hg@1: tooltip = ((PieToolTipGenerator) toolTipGenerator).generateToolTip(ds, ds.getKey(index), index); franta-hg@1: } franta-hg@1: } franta-hg@1: } else { franta-hg@1: // throw because category is unknown franta-hg@1: throw new JspException( franta-hg@1: "TooltipgGenerator of class " + toolTipGenerator.getClass().getName() + " does not implement the appropriate TooltipGenerator interface for entity type " + ce.getClass().getName()); franta-hg@1: } franta-hg@1: return tooltip; franta-hg@1: } franta-hg@1: franta-hg@1: private boolean hasToolTips() throws JspException { franta-hg@1: if (toolTipGenerator!=null && useJFreeChartTooltipGenerator) { franta-hg@1: throw new JspException("Can't have both tooltipGenerator and useJFreeChartTooltipGenerator parameters specified!"); franta-hg@1: } franta-hg@1: return toolTipGenerator != null || useJFreeChartTooltipGenerator; franta-hg@1: } franta-hg@1: franta-hg@1: public void setTooltipgeneratorid(String id) { franta-hg@1: this.toolTipGenerator = (ToolTipGenerator) pageContext.findAttribute(id); franta-hg@1: } franta-hg@1: franta-hg@1: private boolean hasLinks() throws JspException { franta-hg@1: if (linkGenerator!=null && useJFreeChartLinkGenerator) { franta-hg@1: throw new JspException("Can't have both linkGenerator and useJFreeChartLinkGenerator parameters specified!"); franta-hg@1: } franta-hg@1: return linkGenerator != null || useJFreeChartLinkGenerator; franta-hg@1: } franta-hg@1: franta-hg@1: public void setLinkgeneratorid(String id) { franta-hg@1: this.linkGenerator = (LinkGenerator) pageContext.findAttribute(id); franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * Setter of the useJFreeChartLinkGenerator field. franta-hg@1: * @param useJFreeChartLinkGenerator the useJFreeChartLinkGenerator to set. franta-hg@1: */ franta-hg@1: public void setUseJFreeChartLinkGenerator(boolean useJFreeChartLinkGenerator) { franta-hg@1: this.useJFreeChartLinkGenerator = useJFreeChartLinkGenerator; franta-hg@1: } franta-hg@1: /** franta-hg@1: * Setter of the useJFreeChartTooltipGenerator field. franta-hg@1: * @param useJFreeChartTooltipGenerator the useJFreeChartTooltipGenerator to set. franta-hg@1: */ franta-hg@1: public void setUseJFreeChartTooltipGenerator(boolean useJFreeChartTooltipGenerator) { franta-hg@1: this.useJFreeChartTooltipGenerator = useJFreeChartTooltipGenerator; franta-hg@1: } franta-hg@1: franta-hg@1: }