1 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 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 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 82 public int getHeight() { 83 return height; 84 } 85 86 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 110 public String getMimeType() { 111 return mimeType; 112 } 113 114 117 protected void finalize() throws Throwable { 118 super.finalize(); 119 log.debug(this + " finalized."); 120 } 121 122 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 145 public int getSize() throws CewolfException { 146 ensureRendered(); 147 return renderedImage.data.length; 148 } 149 150 153 public Date getTimeoutTime() { 154 return timeoutTime; 155 } 156 157 162 private void onImageRendered(RenderedImage renderedImage) { 163 if (chartHolder instanceof ChartImageRenderListener) { 164 ((ChartImageRenderListener) chartHolder).onImageRendered(renderedImage); 166 } 167 } 168 169 } 170 | Popular Tags |