/*
 *    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;

// J2SE dependencies
import java.awt.Shape;
import java.awt.geom.Rectangle2D;

import org.geotools.geometry.jts.TransformedShape;
import org.geotools.resources.Utilities;


/**
 * Style to represent points as small filled and stroked shapes
 *
 * @author Andrea Aime
 * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/render/src/main/java/org/geotools/renderer/style/MarkStyle2D.java $
 * @version $Id: MarkStyle2D.java 24458 2007-02-16 17:05:44Z simboss $
 */
public class MarkStyle2D extends PolygonStyle2D {
    Shape shape;
    int size;
    float rotation;

    /**
     * Returns the shape rotation, in radians
     *
     */
    public float getRotation() {
        return rotation;
    }

    /**
     * Returns the shape to be used to render the mark
     *
     */
    public Shape getShape() {
        return shape;
    }

    /**
     * Returns a shape that can be used to draw the mark at the x, y coordinates
     * with appropriated rotation and size (according to the current style)
     *
     * @param x the x coordinate where the mark will be drawn
     * @param y the y coordinate where the mark will be drawn
     *
     * @return a shape that can be used to draw the mark 
     */
    public Shape getTransformedShape(float x, float y) {
        if (shape != null) {
            Rectangle2D bounds = shape.getBounds2D();
            double shapeSize = Math.max(bounds.getWidth(), bounds.getHeight());
            double scale = size / shapeSize;
            TransformedShape ts = new TransformedShape();
            ts.shape = shape;
            ts.translate(x, y);
            ts.rotate(rotation);
            ts.scale(scale, -scale);

            return ts;
        } else {
            return null;
        }
    }

    /**
     * Returns the size of the shape, in pixels
     *
     */
    public int getSize() {
        return size;
    }

    /**
     * Sets the shape rotation, in radians
     *
     * @param f
     */
    public void setRotation(float f) {
        rotation = f;
    }

    /**
     * Sets the shape to be used to render the mark
     *
     * @param shape
     */
    public void setShape(Shape shape) {
        this.shape = shape;
    }

    /**
     * Sets the size of the shape, in pixels
     *
     * @param i
     */
    public void setSize(int i) {
        size = i;
    }

    /**
     * Returns a string representation of this style.
     */
    public String toString() {
        return Utilities.getShortClassName(this) + '[' + shape + ']';
    }
}
