001: /* ================================================================
002: * Cewolf : Chart enabling Web Objects Framework
003: * ================================================================
004: *
005: * Project Info: http://cewolf.sourceforge.net
006: * Project Lead: Guido Laures (guido@laures.de);
007: *
008: * (C) Copyright 2002, by Guido Laures
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of
013: * the License, or (at your option) any later version.
014: *
015: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
016: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
017: * See the GNU Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public License along with this
020: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
021: * Boston, MA 02111-1307, USA.
022: */
023:
024: package de.laures.cewolf.taglib.tags;
025:
026: import java.io.IOException;
027:
028: import javax.servlet.jsp.JspException;
029:
030: import de.laures.cewolf.CewolfException;
031: import de.laures.cewolf.ChartHolder;
032: import de.laures.cewolf.ChartImage;
033: import de.laures.cewolf.Configuration;
034: import de.laures.cewolf.Storage;
035: import de.laures.cewolf.WebConstants;
036: import de.laures.cewolf.taglib.ChartImageDefinition;
037: import de.laures.cewolf.taglib.TaglibConstants;
038: import de.laures.cewolf.taglib.html.HTMLImgTag;
039: import de.laures.cewolf.taglib.util.PageUtils;
040:
041: /**
042: * Tag <legend> which can be used to render a chart's legend
043: * separately somewhere in the page.
044: * @author Guido Laures
045: */
046: public class LegendTag extends HTMLImgTag implements CewolfRootTag,
047: TaglibConstants, WebConstants {
048:
049: private static final String DEFAULT_MIME_TYPE = MIME_PNG;
050: private static final int DEFAULT_TIMEOUT = 300;
051: private int timeout = DEFAULT_TIMEOUT;
052: private transient String sessionKey;
053: private transient String renderer;
054: private String mimeType = DEFAULT_MIME_TYPE;
055:
056: public int doStartTag() throws JspException {
057: ChartHolder cd = PageUtils.getChartHolder(getChartId(),
058: pageContext);
059: ChartImage cid = new ChartImageDefinition(cd, width, height,
060: ChartImage.IMG_TYPE_LEGEND, mimeType, timeout);
061: Storage storage = Configuration.getInstance(
062: pageContext.getServletContext()).getStorage();
063: try {
064: this .sessionKey = storage.storeChartImage(cid, pageContext);
065: } catch (CewolfException cwex) {
066: log.error(cwex);
067: throw new JspException(cwex.getMessage());
068: }
069: return SKIP_BODY;
070: }
071:
072: public int doEndTag() throws JspException {
073: super .doStartTag();
074: final StringBuffer buffer = new StringBuffer(" src=\"");
075: buffer.append(getImgURL());
076: buffer.append("\"");
077: try {
078: pageContext.getOut().write(buffer.toString());
079: } catch (IOException ioex) {
080: reset();
081: log.error(ioex);
082: throw new JspException(ioex.getMessage());
083: }
084: return super .doEndTag();
085: }
086:
087: /**
088: * To enable further server side scriptings on JSP output the session ID is always
089: * encoded into the image URL even if cookies are enabled on the client side.
090: */
091: protected String getImgURL() {
092: return ChartImgTag.buildImgURL(renderer, pageContext,
093: sessionKey, width, height, mimeType, forceSessionId,
094: removeAfterRender);
095: }
096:
097: protected void reset() {
098: // as of a weird JSP compiler in resin
099: // a reused tag's attribute is only set if
100: // it changes. So width an height may not
101: // be unset to ensure correct values.
102: int lHeight = this .height;
103: int lWidth = this .width;
104: int lTimeout = this .timeout;
105: super .reset();
106: this .height = lHeight;
107: this .width = lWidth;
108: this .timeout = lTimeout;
109: }
110:
111: public String getChartId() {
112: return getId();
113: }
114:
115: public void setRenderer(String rend) {
116: this .renderer = rend;
117: }
118:
119: /**
120: * Sets the mimeType.
121: * @param mimeType The mimeType to set
122: */
123: public void setMime(String mimeType) {
124: this .mimeType = mimeType;
125: }
126:
127: /**
128: * @return Returns the timeout.
129: */
130: public int getTimeout() {
131: return timeout;
132: }
133:
134: /**
135: * @param timeout The timeout to set.
136: */
137: public void setTimeout(int timeout) {
138: this.timeout = timeout;
139: }
140: }
|