java/cewolf-1.0/src/main/java/de/laures/cewolf/taglib/AbstractChartDefinition.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;
    25 
    26 import java.awt.Image;
    27 import java.awt.Paint;
    28 import java.io.Serializable;
    29 import java.util.ArrayList;
    30 import java.util.Iterator;
    31 import java.util.List;
    32 import java.util.Map;
    33 
    34 import org.apache.commons.logging.Log;
    35 import org.apache.commons.logging.LogFactory;
    36 import org.jfree.chart.JFreeChart;
    37 import org.jfree.chart.title.LegendTitle;
    38 import org.jfree.ui.RectangleEdge;
    39 
    40 import de.laures.cewolf.ChartHolder;
    41 import de.laures.cewolf.ChartPostProcessor;
    42 import de.laures.cewolf.ChartValidationException;
    43 import de.laures.cewolf.DatasetProduceException;
    44 import de.laures.cewolf.PostProcessingException;
    45 import de.laures.cewolf.event.ChartImageRenderListener;
    46 import de.laures.cewolf.util.ImageHelper;
    47 import de.laures.cewolf.util.RenderedImage;
    48 
    49 /**
    50  * Serializable implementaton of a ChartDefinition.
    51  * @author glaures
    52  * @see de.laures.cewolf.ChartHolder
    53  */
    54 public abstract class AbstractChartDefinition implements ChartHolder, Serializable, TaglibConstants, ChartImageRenderListener {
    55     
    56     protected static Log log = LogFactory.getLog(AbstractChartDefinition.class);
    57 
    58     protected String title;
    59 	protected String xAxisLabel;
    60 	protected String yAxisLabel;
    61 	protected String type;
    62 
    63     private boolean antialias = true;
    64     private String background;
    65     private float backgroundImageAlpha = 1.0f;
    66     private Paint paint;
    67 
    68     private int legendAnchor = ANCHOR_SOUTH;
    69     private boolean showLegend = true;
    70 
    71     private transient List postProcessors = new ArrayList();
    72     private List postProcessorsParams = new ArrayList();
    73 
    74     private transient JFreeChart chart;
    75 	
    76 	protected abstract JFreeChart produceChart() throws DatasetProduceException, ChartValidationException;
    77 	
    78   
    79     //gets first legend in the list
    80     public LegendTitle getLegend()
    81     {
    82       //i need to find the legend now.
    83       LegendTitle legend = null;
    84       List subTitles = chart.getSubtitles();
    85       Iterator iter = subTitles.iterator();
    86       while (iter.hasNext())
    87       {
    88         Object o = iter.next();
    89         if (o instanceof LegendTitle)
    90         {
    91           legend = (LegendTitle) o;
    92           break;
    93         }
    94       }
    95       return legend;
    96     }
    97     
    98     //removes first legend in the list
    99     public void removeLegend()
   100     {
   101       List subTitles = chart.getSubtitles();
   102       Iterator iter = subTitles.iterator();
   103       while (iter.hasNext())
   104       {
   105         Object o = iter.next();
   106         if (o instanceof LegendTitle)
   107         {
   108           iter.remove();
   109           break;
   110         }
   111       }
   112     }
   113   
   114     /**
   115      * This method triggers the dataset and chart production. It is only
   116      * from outside if there is no cached image available in the the
   117      * image cache.
   118      */
   119     public Object getChart() throws DatasetProduceException, ChartValidationException, PostProcessingException {
   120         if (chart == null) {
   121             chart = produceChart();
   122             chart.setAntiAlias(antialias);
   123             if (background != null) {
   124                 Image image = ImageHelper.loadImage(background);
   125                 chart.setBackgroundImage(image);
   126                 chart.setBackgroundImageAlpha(backgroundImageAlpha);
   127             }
   128             if (paint != null) {
   129                 chart.setBackgroundPaint(paint);
   130             }
   131             if (showLegend) 
   132             {
   133 
   134                 LegendTitle legend = this.getLegend();
   135                 switch (legendAnchor) 
   136                 {
   137                     case ANCHOR_NORTH :
   138                         legend.setPosition(RectangleEdge.TOP);
   139                         break;
   140                     case ANCHOR_WEST :
   141                       legend.setPosition(RectangleEdge.RIGHT);
   142                         break;
   143                     case ANCHOR_EAST :
   144                       legend.setPosition(RectangleEdge.LEFT);
   145                         break;
   146                     default :
   147                       legend.setPosition(RectangleEdge.BOTTOM);
   148                 }
   149             } 
   150             else 
   151             {
   152               this.removeLegend();
   153             }
   154             // postProcessing
   155             for (int i = 0; i < postProcessors.size(); i++) {
   156                 ChartPostProcessor pp = (ChartPostProcessor)postProcessors.get(i);
   157                 try {
   158                     pp.processChart(chart, (Map)postProcessorsParams.get(i));
   159                 } catch (Throwable t) {
   160                 	log.error(t);
   161                     throw new PostProcessingException(t.getClass().getName() + " raised by post processor '" +
   162                     		pp + "'.\nPost processing of this post processor " + "has been ignored.");
   163                 }
   164             }
   165         }
   166         return chart;
   167     }
   168 
   169     /**
   170      * Sets the antialias.
   171      * @param antialias The antialias to set
   172      */
   173     public void setAntialias(boolean antialias) {
   174         this.antialias = antialias;
   175     }
   176 
   177     /**
   178      * Sets the background.
   179      * @param background The background to set
   180      */
   181     public void setBackground(String background) {
   182         this.background = background;
   183     }
   184 
   185     /**
   186      * Sets the backgroundImageAlpha.
   187      * @param backgroundImageAlpha The backgroundImageAlpha to set
   188      */
   189     public void setBackgroundImageAlpha(float backgroundImageAlpha) {
   190         this.backgroundImageAlpha = backgroundImageAlpha;
   191     }
   192 
   193     /**
   194      * Sets the legendAnchor.
   195      * @param legendAnchor The legendAnchor to set
   196      */
   197     public void setLegendAnchor(int legendAnchor) {
   198         this.legendAnchor = legendAnchor;
   199     }
   200 
   201     /**
   202      * Sets the paint.
   203      * @param paint The paint to set
   204      */
   205     public void setPaint(Paint paint) {
   206         this.paint = paint;
   207     }
   208 
   209     /**
   210      * Sets the showLegend.
   211      * @param showLegend The showLegend to set
   212      */
   213     public void setShowLegend(boolean showLegend) {
   214         this.showLegend = showLegend;
   215     }
   216 
   217     /**
   218      * Sets the title.
   219      * @param title The title to set
   220      */
   221     public void setTitle(String title) {
   222         this.title = title;
   223     }
   224 
   225     /**
   226      * Sets the type.
   227      * @param type The type to set
   228      */
   229     public void setType(String type) {
   230         this.type = type;
   231     }
   232 
   233     /**
   234      * Sets the xAxisLabel.
   235      * @param xAxisLabel The xAxisLabel to set
   236      */
   237     public void setXAxisLabel(String xAxisLabel) {
   238         this.xAxisLabel = xAxisLabel;
   239     }
   240 
   241     /**
   242      * Sets the yAxisLabel.
   243      * @param yAxisLabel The yAxisLabel to set
   244      */
   245     public void setYAxisLabel(String yAxisLabel) {
   246         this.yAxisLabel = yAxisLabel;
   247     }
   248 
   249     public void addPostProcessor(ChartPostProcessor pp) {
   250         postProcessors.add(pp);
   251     }
   252 
   253     public void addPostProcessorParams(Map params) {
   254         postProcessorsParams.add(params);
   255     }
   256     
   257 	/**
   258 	 * Callback right after a new image gets rendered.
   259 	 * Implemented, so if postprocessors implement the ImageRenderListener interface
   260 	 * then they will be called back also
   261 	 * 
   262 	 * @param renderedImage The fresh image just got rendered
   263 	 */
   264 	public void onImageRendered (RenderedImage renderedImage) {
   265 		// if the postprocessor implements ImageRenderListener interface call it!
   266         for (int i = 0; i < postProcessors.size(); i++) {
   267             ChartPostProcessor pp = (ChartPostProcessor)postProcessors.get(i);
   268             if (pp instanceof ChartImageRenderListener) {
   269             	((ChartImageRenderListener) pp).onImageRendered(renderedImage);
   270             }
   271         }		
   272 	}
   273 
   274 }