/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2003-2006, Geotools Project Managment Committee (PMC)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.renderer.style;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Paint;

import org.geotools.feature.Feature;
import org.geotools.styling.Fill;
import org.geotools.styling.PolygonSymbolizer;


/**
 * A dynamic polygon style, that will compute its parameters each time they are requested instead
 * of caching them
 *
 * @author jamesm
 * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/render/src/main/java/org/geotools/renderer/style/DynamicPolygonStyle2D.java $
 */
public class DynamicPolygonStyle2D extends org.geotools.renderer.style.PolygonStyle2D {
    Feature feature;
    PolygonSymbolizer ps;

    /**
     * Creates a new instance of DynamicPolygonStyle2D
     */
    public DynamicPolygonStyle2D(Feature f, PolygonSymbolizer sym) {
        feature = f;
        ps = sym;
    }

    /**
     * Computes and returns the fill based on the feature and the symbolizer
     */
    public java.awt.Paint getFill() {
        Fill fill = ps.getFill();

        if (fill == null) {
            return null;
        }

        Paint fillPaint = (Color) fill.getColor().evaluate(feature, Color.class);

        // if a graphic fill is to be used, prepare the paint accordingly....
        org.geotools.styling.Graphic gr = fill.getGraphicFill();

        if (gr != null) {
            SLDStyleFactory fac = new SLDStyleFactory();
            fillPaint = fac.getTexturePaint(gr, feature);
        }

        return fillPaint;
    }

    /**
     * Computes and returns the fill composite based on the feature and the symbolizer
     */
    public Composite getFillComposite() {
        Fill fill = ps.getFill();

        if (fill == null) {
            return null;
        }

        // get the opacity and prepare the composite
        float opacity = ((Float) fill.getOpacity().evaluate(feature,Float.class)).floatValue();

        if (opacity == 1) {
            return null;
        }

        return AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity);
    }
}
