view gnv-artifacts/src/main/java/de/intevation/gnv/chart/XMLChartTheme.java @ 622:89aca25642d6

Implemented method stubs of MapfileGenerator. Mapfiles are successfully created corresponding meta.xml files. gnv-artifacts/trunk@693 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 19 Feb 2010 13:28:34 +0000
parents 3d13fa281a7e
children 79401c871da4
line wrap: on
line source
package de.intevation.gnv.chart;

import java.awt.Font;
import java.awt.Color;
import java.awt.Paint;
import java.awt.geom.Ellipse2D;
import java.lang.NumberFormatException;

import org.apache.log4j.Logger;

import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.AbstractXYItemRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.ui.RectangleInsets;
import org.jfree.util.UnitType;


import org.w3c.dom.Document;

import de.intevation.artifactdatabase.Config;

/**
 * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
 */
public class XMLChartTheme
extends      StandardChartTheme
{
    private static final Color DEFAULT_COLOR = Color.BLACK;

    private Logger log = Logger.getLogger(XMLChartTheme.class);

    protected boolean domainCrosshairVisible;
    protected boolean rangeCrosshairVisible;
    protected boolean renderLines;
    protected boolean renderShapes;

    protected int pointWidth;
    protected int pointHeight;

    protected Paint histogramBasePaint;


    public XMLChartTheme(String name) {
        super(name);
    }


    public void setDomainCrosshairVisible(boolean visible) {
        this.domainCrosshairVisible = visible;
    }


    public boolean getDomainCrosshairVisible() {
        return domainCrosshairVisible;
    }


    public boolean getRangeCrosshairVisible() {
        return rangeCrosshairVisible;
    }


    public void setRangeCrosshairVisible(boolean visible) {
        this.rangeCrosshairVisible = visible;
    }


    public void setHistogramBasePaint(Color c) {
        this.histogramBasePaint = c;
    }


    public Paint getHistogramBasePaint() {
        return histogramBasePaint;
    }


    public void applyXMLConfiguration(Document document) {
        log.debug("create XMLChartTheme");

        init(document);
    }


    private void init(Document document) {
        log.debug("init XMLChartTheme parameters");

        initChartParameters(document);
        initTitleParameters(document);
        initSubtitleParameters(document);
        initPlotParameters(document);
        initAxisParameters(document);
        initLegendParameters(document);
        initRenderer(document);
        initHistogramColor(document);
    }


    private void initTitleParameters(Document document) {
        log.debug("init title parameters.");

        int     size  = getInt(document, "theme/title/font/size/@value");
        String  type  = getString(document, "theme/title/font/type/@value");
        boolean bold  = getBool(document, "theme/title/font/bold/@value");

        setExtraLargeFont(createFont(type, size, bold));

        String color = getString(document, "theme/title/font/color/@value");
        Color  c     = decodeColor(color);
        if (c != null)
            setTitlePaint(c);
    }


    private void initSubtitleParameters(Document document) {
        log.debug("init title parameters.");

        int     size  = getInt(document, "theme/subtitle/font/size/@value");
        String  type  = getString(document, "theme/subtitle/font/type/@value");
        boolean bold  = getBool(document, "theme/subtitle/font/bold/@value");

        setLargeFont(createFont(type, size, bold));

        String col  = getString(document, "theme/subtitle/font/color/@value");
        setSubtitlePaint(Color.decode(col));
    }


    private void initChartParameters(Document document) {
        log.debug("init chart parameters.");

        String bg = getString(document, "theme/chart/background/color/@value");
        Color  c  = decodeColor(bg);
        if (c != null)
            setChartBackgroundPaint(c);
    }


    private void initPlotParameters(Document document) {
        log.debug("init plot parameters.");

        String tmp = null;
        tmp        = getString(document, "theme/plot/background/color/@value");
        Color  c   = decodeColor(tmp);
        if (c != null)
            setPlotBackgroundPaint(c);

        tmp = getString(document, "theme/plot/outline/color/@value");
        c   = decodeColor(tmp);
        if (c != null)
            setPlotOutlinePaint(c);

        tmp = getString(document, "theme/plot/domaingridline/color/@value");
        c   = decodeColor(tmp);
        if (c != null)
            setDomainGridlinePaint(c);

        tmp = getString(document, "theme/plot/rangegridline/color/@value");
        c   = decodeColor(tmp);
        if (c != null)
            setRangeGridlinePaint(c);

        boolean rangeCrosshairVisible = getBool(
            document, "theme/plot/rangecrosshair/visible/@value");
        setRangeCrosshairVisible(rangeCrosshairVisible);

        boolean domainCrosshairVisible = getBool(
            document, "theme/plot/domaincrosshair/visible/@value");
        setDomainCrosshairVisible(domainCrosshairVisible);

        int top    = getInt(document, "theme/plot/offset/top/@value");
        int bottom = getInt(document, "theme/plot/offset/bottom/@value");
        int left   = getInt(document, "theme/plot/offset/left/@value");
        int right  = getInt(document, "theme/plot/offset/right/@value");
        setAxisOffset(new RectangleInsets(
            UnitType.RELATIVE,
            top, left, bottom, right)
        );
    }


    private void initAxisParameters(Document document) {
        log.debug("init axis parameters.");

        String tmp = null;
        tmp        = getString(document, "theme/axis/label/color/@value");
        Color c    = decodeColor(tmp);
        if (c != null)
            setAxisLabelPaint(c);

        tmp = getString(document, "theme/axis/ticklabel/color/@value");
        c   = decodeColor(tmp);
        if (c != null)
            setTickLabelPaint(c);
    }


    private void initLegendParameters(Document document) {
        log.debug("init legend parameters.");

        String tmp = null;
        tmp        = getString(document, "theme/legend/font/color/@value");
        Color c    = decodeColor(tmp);
        if (c != null)
            setLegendItemPaint(c);

        tmp = getString(document, "theme/legend/background/color/@value");
        c   = decodeColor(tmp);
        if (c != null)
            setLegendBackgroundPaint(c);
    }


    private void initRenderer(Document document) {
        log.debug("init renderer parameters.");

        pointWidth  = getInt(document, "theme/plot/itemrenderer/width/@value");
        log.debug("Read point width of " + pointWidth);
        pointHeight = getInt(document, "theme/plot/itemrenderer/height/@value");
        log.debug("Read point height of " + pointHeight);
        renderLines = getBool(
            document, "theme/plot/itemrenderer/renderLines/@value"
        );
        renderShapes = getBool(
            document, "theme/plot/itemrenderer/renderPoints/@value"
        );
    }


    private void initHistogramColor(Document document) {
        log.debug("init histogram color");
        String tmp = getString(document, "theme/histogram/bar/color/@value");
        Color c = decodeColor(tmp);

        if (c != null)
            setHistogramBasePaint(c);
    }


    private static String getString(Document document, String xpath) {
        return Config.getStringXPath(document, xpath);
    }


    private static int getInt(Document document, String xpath) {
        String tmp = getString(document, xpath);

        if (tmp != null)
            return Integer.parseInt(tmp);
        else
            return 0;
    }


    private static boolean getBool(Document document, String xpath) {
        String tmp = getString(document, xpath);

        if (tmp != null)
            return Boolean.parseBoolean(tmp);
        else
            return false;
    }


    protected Color decodeColor(String color) {
        try {
            if (color == null)
                return null;

            return Color.decode(color);
        }
        catch (NumberFormatException nfe) {
            log.warn("Error while parsing color: " + color, nfe);
        }

        return null;
    }


    protected Font createFont(String type, int size, boolean bold) {
        Font font = null;
        if (bold)
            font = new Font(type, Font.BOLD, size);
        else
            font = new Font(type, Font.PLAIN, size);

        return font;
    }


    protected void applyToXYPlot(XYPlot plot) {
        log.debug("apply theme parameter to XYPlot");

        super.applyToXYPlot(plot);
        plot.setDomainCrosshairVisible(this.domainCrosshairVisible);
        plot.setRangeCrosshairVisible(this.rangeCrosshairVisible);

        AbstractXYItemRenderer renderer = (AbstractXYItemRenderer) 
            plot.getRenderer();

        if (renderer instanceof XYLineAndShapeRenderer)
            applyToXYLineAndShapeRenderer(plot);

        if (renderer instanceof XYBarRenderer)
            applyToXYBarRenderer(plot);
    }


    protected void applyToXYLineAndShapeRenderer(XYPlot plot) {
        if (plot == null)
            return;

        XYLineAndShapeRenderer renderer =
            (XYLineAndShapeRenderer) plot.getRenderer();

        Ellipse2D.Double point = new Ellipse2D.Double(
            -(pointWidth/2), -(pointHeight/2), pointWidth, pointHeight
        );

        renderer.setSeriesShape(0, point);
        renderer.setSeriesShapesVisible(0, renderShapes);
        renderer.setSeriesLinesVisible(0, renderLines);

        plot.setRenderer(renderer);
    }


    protected void applyToXYBarRenderer(XYPlot plot) {
        if (plot == null)
            return;

        XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();

        renderer.setSeriesPaint(0, histogramBasePaint);
    }
}

http://dive4elements.wald.intevation.org