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