view flys-artifacts/src/main/java/de/intevation/flys/exports/ChartInfoGenerator.java @ 655:913b52064449

Refactored version of "Berechnung 4" flys-artifacts/trunk@2053 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 05 Jun 2011 18:24:46 +0000
parents 67c7020f4ed3
children b22f21b173a7
line wrap: on
line source
package de.intevation.flys.exports;

import java.awt.Transparency;
import java.io.IOException;
import java.io.OutputStream;

import org.w3c.dom.Document;

import org.apache.log4j.Logger;

import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.CallContext;

import de.intevation.artifacts.common.utils.XMLUtils;


/**
 * An OutGenerator that generates meta information for charts. A concrete
 * ChartInfoGenerator need to instantiate a concrete ChartGenerator and dispatch
 * the methods to that instance. The only thing this ChartInfoGenerator needs
 * to, is to overrite the generate() method which doesn't write the chart image
 * to the OutputStream but a Document that contains some meta information of the
 * created chart.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public abstract class ChartInfoGenerator implements OutGenerator {

    /** The logger used in this generator.*/
    private static Logger logger =
        Logger.getLogger(ChartInfoGenerator.class);


    /** The OutGenerator that creates the charts.*/
    protected XYChartGenerator generator;

    protected OutputStream out;



    public ChartInfoGenerator(XYChartGenerator generator) {
        this.generator = generator;
    }


    /**
     * Dispatches the operation to the instantiated generator.
     *
     * @param request
     * @param out
     * @param context
     */
    public void init(Document request, OutputStream out, CallContext context) {
        this.out = out;

        generator.init(request, out, context);
    }


    /**
     * Dispatches the operation to the instantiated generator.
     *
     * @param master
     */
    public void setMasterArtifact(Artifact master) {
        generator.setMasterArtifact(master);
    }


    /**
     * Dispatches the operation to the instantiated generator.
     *
     * @param artifacts
     * @param facet
     * @param attr
     */
    public void doOut(Artifact artifact, String facet, Document attr) {
        generator.doOut(artifact, facet, attr);
    }


    /**
     * This method generates the chart using a concrete ChartGenerator but
     * doesn't write the chart itself to the OutputStream but a Document that
     * contains meta information of the created chart.
     */
    @Override
    public void generate()
    throws IOException
    {
        logger.debug("ChartInfoGenerator.generate");

        JFreeChart chart = generator.generateChart();

        int[] size = generator.getSize();

        ChartRenderingInfo info = new ChartRenderingInfo();

        chart.createBufferedImage(size[0], size[1], Transparency.BITMASK, info);

        Document doc = InfoGeneratorHelper.createInfoDocument(chart, info);

        XMLUtils.toStream(doc, out);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org