/*
 * File:     SVGAlignableAnnotation.java
 * Project:  MPI Linguistic Application
 * Date:     03 April 2006
 *
 * Copyright (C) 2001-2006  Max Planck Institute for Psycholinguistics
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package mpi.eudico.server.corpora.clomimpl.abstr;

import mpi.eudico.server.corpora.clom.SVGAnnotation;
import mpi.eudico.server.corpora.clom.Tier;
import mpi.eudico.server.corpora.clom.TimeSlot;

import mpi.eudico.server.corpora.util.ACMEditEvent;

import java.awt.Shape;


/**
 * Extends AlignableAnnotation by adding a field for an id of a referenced svg
 * element and a field for a corresponding Shape object.
 *
 * @author Alexander Klassmann
 * @version 14/08/2003
 */
public class SVGAlignableAnnotation extends AlignableAnnotation
    implements SVGAnnotation {
    private String SVGElementId = "";
    private Shape shape;

    /**
     * Creates a new SVGAlignableAnnotation instance
     *
     * @param bts the begin time time slot
     * @param ets the end time time slot
     * @param theTier the parent tier
     */
    public SVGAlignableAnnotation(TimeSlot bts, TimeSlot ets, Tier theTier) {
        super(bts, ets, theTier);
    }

    /**
     * Creates a new SVGAlignableAnnotation instance
     *
     * @param bts the begin time time slot
     * @param ets the end time time slot
     * @param theTier the parent tier
     * @param id the id of the referenced svg element
     */
    public SVGAlignableAnnotation(TimeSlot bts, TimeSlot ets, Tier theTier,
        String id) {
        super(bts, ets, theTier);
        setSVGElementID(id);
    }

    /**
     * Perform some checks on the passed String and send an ACMEditEvent when
     * necessary.
     *
     * @param id the id of the referenced svg element
     */
    public void setSVGElementID(String id) {
        if (id != null) {
            if (id.equals(SVGElementId)) { /* ignore case?? */

                return;
            }

            SVGElementId = id;
        } else {
            // treat passing null as removing the svg reference...
            if (SVGElementId.equals("")) {
                return; // avoid unnecessary ACMEditEvents 
            }

            SVGElementId = "";
        }

        //svgElementChanged();
    }

    /**
     * Returns the id of the referenced svg element.
     *
     * @return the id of the referenced svg element
     */
    public String getSVGElementID() {
        return SVGElementId;
    }

    /**
     * A parse-time method to insert a shape.<br>
     * This method does not create a ACMEditEvent and should therefore only be
     * used on the initialization of the svg objects.<br>
     *
     * @param shape the shape
     */
    public void insertShape(Shape shape) {
        this.shape = shape;
    }

    /**
     * Sets the (new) shape for this annotation. Creates a ACMEditEvent to
     * notify listeners of a change in the graphic annotation.
     *
     * @param shape the shape
     */
    public void setShape(Shape shape) {
        if (this.shape == shape) {
            return;
        }

        this.shape = shape;
        svgElementChanged();
    }

    /**
     * Returns the current shape/graphic annotation of this annotation.<br>
     *
     * @return the current shape
     */
    public Shape getShape() {
        return shape;
    }

    /**
     * If the referenced graphics object has been changed / edited send an
     * ACMEditEvent.
     */
    public void svgElementChanged() {
        modified(ACMEditEvent.CHANGE_ANNOTATION_GRAPHICS, this);
    }
}
