java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/ChartImageDefinition.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 the License, or (at your option) any later version.
    13  *
    14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    16  * See the GNU Lesser General Public License for more details.
    17  *
    18  * You should have received a copy of the GNU Lesser General Public License along with this
    19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    20  * Boston, MA 02111-1307, USA.
    21  */
    22 
    23 package de.laures.cewolf.taglib;
    24 
    25 import java.io.Serializable;
    26 import java.util.Calendar;
    27 import java.util.Date;
    28 import java.util.GregorianCalendar;
    29 
    30 import org.apache.commons.logging.Log;
    31 import org.apache.commons.logging.LogFactory;
    32 
    33 import de.laures.cewolf.CewolfException;
    34 import de.laures.cewolf.ChartHolder;
    35 import de.laures.cewolf.ChartImage;
    36 import de.laures.cewolf.ChartValidationException;
    37 import de.laures.cewolf.DatasetProduceException;
    38 import de.laures.cewolf.PostProcessingException;
    39 import de.laures.cewolf.event.ChartImageRenderListener;
    40 import de.laures.cewolf.util.RenderedImage;
    41 import de.laures.cewolf.util.Renderer;
    42 
    43 /**
    44  * Serializable implementaton of a ChartImage.
    45  * @author glaures
    46  * @see de.laures.cewolf.ChartImage
    47  */
    48 public class ChartImageDefinition implements ChartImage, ChartHolder, Serializable {
    49 	
    50     private static final Log log = LogFactory.getLog(ChartImageDefinition.class);
    51 
    52 	private final ChartHolder chartHolder;
    53 	private final int height;
    54 	private final int width;
    55 	private final int type;
    56 	private final String mimeType;
    57 	transient private final Date timeoutTime;
    58 	
    59 	private RenderedImage renderedImage;
    60 
    61 	/**
    62 	 * Constructor for ChartImage
    63 	 */
    64 	public ChartImageDefinition(ChartHolder ch, int width, int height, int type, String mimeType, int timeout) {
    65 		if(width <= 0 || height <= 0){
    66 			throw new IllegalArgumentException("ChartImage with height or width <= 0 is illegal");
    67 		}
    68 		this.chartHolder = ch;
    69 		this.width = width;
    70 		this.height = height;
    71 		this.type = type;
    72 		this.mimeType = mimeType;
    73     Calendar cal = new GregorianCalendar();
    74     cal.add(Calendar.SECOND,timeout);
    75     this.timeoutTime = cal.getTime();
    76 	}
    77 		
    78 	/**
    79 	 * Returns the height.
    80 	 * @return int
    81 	 */
    82 	public int getHeight() {
    83 		return height;
    84 	}
    85 
    86 	/**
    87 	 * Returns the width.
    88 	 * @return int
    89 	 */
    90 	public int getWidth() {
    91 		return width;
    92 	}
    93 
    94 	public int getType() {
    95 		return type;
    96 	}
    97 
    98 	public Object getChart() throws DatasetProduceException, ChartValidationException, PostProcessingException {
    99 		return chartHolder.getChart();
   100 	}
   101 
   102 	public Object getDataset() throws DatasetProduceException {
   103 		return chartHolder.getDataset();
   104 	}
   105 
   106 	/**
   107 	 * Returns the mimeType.
   108 	 * @return String
   109 	 */
   110 	public String getMimeType() {
   111 		return mimeType;
   112 	}
   113 	
   114 	/**
   115 	 * @see java.lang.Object#finalize()
   116 	 */
   117 	protected void finalize() throws Throwable {
   118 		super.finalize();
   119 		log.debug(this + " finalized.");
   120 	}
   121 	
   122 	/**
   123 	 * @see de.laures.cewolf.ChartImage#getRenderingInfo()
   124 	 */
   125 	public Object getRenderingInfo() throws CewolfException {
   126 		ensureRendered();
   127 		return renderedImage.renderingInfo;
   128 	}
   129 	
   130 	public byte[] getBytes() throws CewolfException{
   131 		ensureRendered();
   132 		return renderedImage.data;
   133 	}
   134 	
   135 	private void ensureRendered() throws CewolfException{
   136 		if(renderedImage == null){
   137 			renderedImage = Renderer.render(this, chartHolder.getChart());
   138 			onImageRendered(renderedImage);
   139 		}
   140 	}
   141 
   142 	/**
   143 	 * @see de.laures.cewolf.ChartImage#getSize()
   144 	 */
   145 	public int getSize() throws CewolfException {
   146 		ensureRendered();
   147 		return renderedImage.data.length;
   148 	}
   149 
   150 	/* (non-Javadoc)
   151 	 * @see de.laures.cewolf.ChartImage#getTimeoutTime()
   152 	 */
   153 	public Date getTimeoutTime() {
   154 		return timeoutTime;
   155 	}
   156 
   157 	/**
   158 	 * Implemented onImageRendered method.
   159 	 * @see de.laures.cewolf.ChartHolder#onImageRendered(de.laures.cewolf.util.RenderedImage).
   160 	 * @param renderedImage The image
   161 	 */
   162 	private void onImageRendered(RenderedImage renderedImage) {
   163 		if (chartHolder instanceof ChartImageRenderListener) {
   164 			// delegate to chartHolder if it's interested...
   165 			((ChartImageRenderListener) chartHolder).onImageRendered(renderedImage);
   166 		}
   167 	}
   168 
   169 }