java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/ChartImageDefinition.java
changeset 1 639991d0808a
     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/ChartImageDefinition.java	Sat Feb 28 21:31:02 2009 +0100
     1.3 @@ -0,0 +1,169 @@
     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;
    1.27 +
    1.28 +import java.io.Serializable;
    1.29 +import java.util.Calendar;
    1.30 +import java.util.Date;
    1.31 +import java.util.GregorianCalendar;
    1.32 +
    1.33 +import org.apache.commons.logging.Log;
    1.34 +import org.apache.commons.logging.LogFactory;
    1.35 +
    1.36 +import de.laures.cewolf.CewolfException;
    1.37 +import de.laures.cewolf.ChartHolder;
    1.38 +import de.laures.cewolf.ChartImage;
    1.39 +import de.laures.cewolf.ChartValidationException;
    1.40 +import de.laures.cewolf.DatasetProduceException;
    1.41 +import de.laures.cewolf.PostProcessingException;
    1.42 +import de.laures.cewolf.event.ChartImageRenderListener;
    1.43 +import de.laures.cewolf.util.RenderedImage;
    1.44 +import de.laures.cewolf.util.Renderer;
    1.45 +
    1.46 +/**
    1.47 + * Serializable implementaton of a ChartImage.
    1.48 + * @author glaures
    1.49 + * @see de.laures.cewolf.ChartImage
    1.50 + */
    1.51 +public class ChartImageDefinition implements ChartImage, ChartHolder, Serializable {
    1.52 +	
    1.53 +    private static final Log log = LogFactory.getLog(ChartImageDefinition.class);
    1.54 +
    1.55 +	private final ChartHolder chartHolder;
    1.56 +	private final int height;
    1.57 +	private final int width;
    1.58 +	private final int type;
    1.59 +	private final String mimeType;
    1.60 +	transient private final Date timeoutTime;
    1.61 +	
    1.62 +	private RenderedImage renderedImage;
    1.63 +
    1.64 +	/**
    1.65 +	 * Constructor for ChartImage
    1.66 +	 */
    1.67 +	public ChartImageDefinition(ChartHolder ch, int width, int height, int type, String mimeType, int timeout) {
    1.68 +		if(width <= 0 || height <= 0){
    1.69 +			throw new IllegalArgumentException("ChartImage with height or width <= 0 is illegal");
    1.70 +		}
    1.71 +		this.chartHolder = ch;
    1.72 +		this.width = width;
    1.73 +		this.height = height;
    1.74 +		this.type = type;
    1.75 +		this.mimeType = mimeType;
    1.76 +    Calendar cal = new GregorianCalendar();
    1.77 +    cal.add(Calendar.SECOND,timeout);
    1.78 +    this.timeoutTime = cal.getTime();
    1.79 +	}
    1.80 +		
    1.81 +	/**
    1.82 +	 * Returns the height.
    1.83 +	 * @return int
    1.84 +	 */
    1.85 +	public int getHeight() {
    1.86 +		return height;
    1.87 +	}
    1.88 +
    1.89 +	/**
    1.90 +	 * Returns the width.
    1.91 +	 * @return int
    1.92 +	 */
    1.93 +	public int getWidth() {
    1.94 +		return width;
    1.95 +	}
    1.96 +
    1.97 +	public int getType() {
    1.98 +		return type;
    1.99 +	}
   1.100 +
   1.101 +	public Object getChart() throws DatasetProduceException, ChartValidationException, PostProcessingException {
   1.102 +		return chartHolder.getChart();
   1.103 +	}
   1.104 +
   1.105 +	public Object getDataset() throws DatasetProduceException {
   1.106 +		return chartHolder.getDataset();
   1.107 +	}
   1.108 +
   1.109 +	/**
   1.110 +	 * Returns the mimeType.
   1.111 +	 * @return String
   1.112 +	 */
   1.113 +	public String getMimeType() {
   1.114 +		return mimeType;
   1.115 +	}
   1.116 +	
   1.117 +	/**
   1.118 +	 * @see java.lang.Object#finalize()
   1.119 +	 */
   1.120 +	protected void finalize() throws Throwable {
   1.121 +		super.finalize();
   1.122 +		log.debug(this + " finalized.");
   1.123 +	}
   1.124 +	
   1.125 +	/**
   1.126 +	 * @see de.laures.cewolf.ChartImage#getRenderingInfo()
   1.127 +	 */
   1.128 +	public Object getRenderingInfo() throws CewolfException {
   1.129 +		ensureRendered();
   1.130 +		return renderedImage.renderingInfo;
   1.131 +	}
   1.132 +	
   1.133 +	public byte[] getBytes() throws CewolfException{
   1.134 +		ensureRendered();
   1.135 +		return renderedImage.data;
   1.136 +	}
   1.137 +	
   1.138 +	private void ensureRendered() throws CewolfException{
   1.139 +		if(renderedImage == null){
   1.140 +			renderedImage = Renderer.render(this, chartHolder.getChart());
   1.141 +			onImageRendered(renderedImage);
   1.142 +		}
   1.143 +	}
   1.144 +
   1.145 +	/**
   1.146 +	 * @see de.laures.cewolf.ChartImage#getSize()
   1.147 +	 */
   1.148 +	public int getSize() throws CewolfException {
   1.149 +		ensureRendered();
   1.150 +		return renderedImage.data.length;
   1.151 +	}
   1.152 +
   1.153 +	/* (non-Javadoc)
   1.154 +	 * @see de.laures.cewolf.ChartImage#getTimeoutTime()
   1.155 +	 */
   1.156 +	public Date getTimeoutTime() {
   1.157 +		return timeoutTime;
   1.158 +	}
   1.159 +
   1.160 +	/**
   1.161 +	 * Implemented onImageRendered method.
   1.162 +	 * @see de.laures.cewolf.ChartHolder#onImageRendered(de.laures.cewolf.util.RenderedImage).
   1.163 +	 * @param renderedImage The image
   1.164 +	 */
   1.165 +	private void onImageRendered(RenderedImage renderedImage) {
   1.166 +		if (chartHolder instanceof ChartImageRenderListener) {
   1.167 +			// delegate to chartHolder if it's interested...
   1.168 +			((ChartImageRenderListener) chartHolder).onImageRendered(renderedImage);
   1.169 +		}
   1.170 +	}
   1.171 +
   1.172 +}