java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/tags/LegendTag.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
franta-hg@1
    13
 * the License, or (at your option) any later version.
franta-hg@1
    14
 *
franta-hg@1
    15
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
franta-hg@1
    16
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
franta-hg@1
    17
 * See the GNU Lesser General Public License for more details.
franta-hg@1
    18
 *
franta-hg@1
    19
 * You should have received a copy of the GNU Lesser General Public License along with this
franta-hg@1
    20
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
franta-hg@1
    21
 * Boston, MA 02111-1307, USA.
franta-hg@1
    22
 */
franta-hg@1
    23
franta-hg@1
    24
package de.laures.cewolf.taglib.tags;
franta-hg@1
    25
franta-hg@1
    26
import java.io.IOException;
franta-hg@1
    27
franta-hg@1
    28
import javax.servlet.jsp.JspException;
franta-hg@1
    29
franta-hg@1
    30
import de.laures.cewolf.CewolfException;
franta-hg@1
    31
import de.laures.cewolf.ChartHolder;
franta-hg@1
    32
import de.laures.cewolf.ChartImage;
franta-hg@1
    33
import de.laures.cewolf.Configuration;
franta-hg@1
    34
import de.laures.cewolf.Storage;
franta-hg@1
    35
import de.laures.cewolf.WebConstants;
franta-hg@1
    36
import de.laures.cewolf.taglib.ChartImageDefinition;
franta-hg@1
    37
import de.laures.cewolf.taglib.TaglibConstants;
franta-hg@1
    38
import de.laures.cewolf.taglib.html.HTMLImgTag;
franta-hg@1
    39
import de.laures.cewolf.taglib.util.PageUtils;
franta-hg@1
    40
franta-hg@1
    41
/**
franta-hg@1
    42
 * Tag &lt;legend&gt; which can be used to render a chart's legend
franta-hg@1
    43
 * separately somewhere in the page.
franta-hg@1
    44
 * @author  Guido Laures
franta-hg@1
    45
 */
franta-hg@1
    46
public class LegendTag extends HTMLImgTag implements CewolfRootTag, TaglibConstants, WebConstants {
franta-hg@1
    47
franta-hg@1
    48
    private static final String DEFAULT_MIME_TYPE = MIME_PNG;
franta-hg@1
    49
    private static final int DEFAULT_TIMEOUT = 300;
franta-hg@1
    50
    private int timeout = DEFAULT_TIMEOUT;
franta-hg@1
    51
    private transient String sessionKey;
franta-hg@1
    52
    private transient String renderer;
franta-hg@1
    53
    private String mimeType = DEFAULT_MIME_TYPE;
franta-hg@1
    54
franta-hg@1
    55
    public int doStartTag() throws JspException {
franta-hg@1
    56
        ChartHolder cd = PageUtils.getChartHolder(getChartId(), pageContext);
franta-hg@1
    57
        ChartImage cid = new ChartImageDefinition(cd, width, height, ChartImage.IMG_TYPE_LEGEND, mimeType,timeout);
franta-hg@1
    58
        Storage storage = Configuration.getInstance(pageContext.getServletContext()).getStorage();
franta-hg@1
    59
        try {
franta-hg@1
    60
        	this.sessionKey = storage.storeChartImage(cid, pageContext);
franta-hg@1
    61
        } catch(CewolfException cwex){
franta-hg@1
    62
        	log.error(cwex);
franta-hg@1
    63
        	throw new JspException(cwex.getMessage());
franta-hg@1
    64
        }
franta-hg@1
    65
        return SKIP_BODY;
franta-hg@1
    66
    }
franta-hg@1
    67
franta-hg@1
    68
    public int doEndTag() throws JspException {
franta-hg@1
    69
		super.doStartTag();
franta-hg@1
    70
		final StringBuffer buffer = new StringBuffer(" src=\"");
franta-hg@1
    71
		buffer.append(getImgURL());
franta-hg@1
    72
		buffer.append("\"");
franta-hg@1
    73
		try {
franta-hg@1
    74
			pageContext.getOut().write(buffer.toString());
franta-hg@1
    75
		} catch (IOException ioex) {
franta-hg@1
    76
			reset();
franta-hg@1
    77
			log.error(ioex);
franta-hg@1
    78
			throw new JspException(ioex.getMessage());
franta-hg@1
    79
		}
franta-hg@1
    80
		return super.doEndTag();
franta-hg@1
    81
    }
franta-hg@1
    82
franta-hg@1
    83
	/**
franta-hg@1
    84
	* To enable further server side scriptings on JSP output the session ID is always
franta-hg@1
    85
	* encoded into the image URL even if cookies are enabled on the client side.
franta-hg@1
    86
	*/
franta-hg@1
    87
	protected String getImgURL() {
franta-hg@1
    88
		return ChartImgTag.buildImgURL(renderer, pageContext, sessionKey, width, height, mimeType, forceSessionId,removeAfterRender);
franta-hg@1
    89
	}
franta-hg@1
    90
franta-hg@1
    91
    protected void reset() {
franta-hg@1
    92
        // as of a weird JSP compiler in resin
franta-hg@1
    93
        // a reused tag's attribute is only set if 
franta-hg@1
    94
        // it changes. So width an height may not
franta-hg@1
    95
        // be unset to ensure correct values.
franta-hg@1
    96
        int lHeight = this.height;
franta-hg@1
    97
        int lWidth = this.width;
franta-hg@1
    98
        int lTimeout = this.timeout;
franta-hg@1
    99
        super.reset();
franta-hg@1
   100
        this.height = lHeight;
franta-hg@1
   101
        this.width = lWidth;
franta-hg@1
   102
        this.timeout = lTimeout;
franta-hg@1
   103
    }
franta-hg@1
   104
franta-hg@1
   105
    public String getChartId() {
franta-hg@1
   106
        return getId();
franta-hg@1
   107
    }
franta-hg@1
   108
franta-hg@1
   109
    public void setRenderer(String rend) {
franta-hg@1
   110
        this.renderer = rend;
franta-hg@1
   111
    }
franta-hg@1
   112
franta-hg@1
   113
    /**
franta-hg@1
   114
     * Sets the mimeType.
franta-hg@1
   115
     * @param mimeType The mimeType to set
franta-hg@1
   116
     */
franta-hg@1
   117
    public void setMime(String mimeType) {
franta-hg@1
   118
        this.mimeType = mimeType;
franta-hg@1
   119
    }
franta-hg@1
   120
franta-hg@1
   121
    /**
franta-hg@1
   122
     * @return Returns the timeout.
franta-hg@1
   123
     */
franta-hg@1
   124
    public int getTimeout()
franta-hg@1
   125
    {
franta-hg@1
   126
      return timeout;
franta-hg@1
   127
    }
franta-hg@1
   128
    /**
franta-hg@1
   129
     * @param timeout The timeout to set.
franta-hg@1
   130
     */
franta-hg@1
   131
    public void setTimeout( int timeout )
franta-hg@1
   132
    {
franta-hg@1
   133
      this.timeout = timeout;
franta-hg@1
   134
    }
franta-hg@1
   135
}