/*
 * @(#)FeatureConnectionHandle.java 5.1
 *
 */

package de.renew.gui.fs;

import java.awt.Dimension;
import java.awt.Point;

import de.uni_hamburg.fs.Feature;
import de.uni_hamburg.fs.FeatureStructure;

import CH.ifa.draw.standard.ConnectionHandle;
import CH.ifa.draw.standard.RelativeLocator;
import CH.ifa.draw.util.ColorMap;
import de.renew.draw.storables.ontology.ConnectionFigure;
import de.renew.draw.storables.ontology.Connector;
import de.renew.draw.storables.ontology.Figure;
import de.renew.draw.storables.ontology.Locator;
import de.renew.draw.ui.ontology.DrawingView;
import de.renew.gui.CPNTextFigure;


/**
 * A handle to connect FSNodes to one another.
 *
 * @see ConnectionFigure
 */
public class FeatureConnectionHandle extends ConnectionHandle {

    /**
     * Constructs a handle for the given owner
     *
     * @param owner owner of the handle
     */
    public FeatureConnectionHandle(Figure owner) {
        super(owner, RelativeLocator.center(), FeatureConnection.NORMAL_ARC);
    }

    /**
     * Constructs a handle for the given owner
     *
     * @param owner owner of the handle
     * @param locator locator of the handle holding the handles location
     */
    public FeatureConnectionHandle(Figure owner, Locator locator) {
        super(owner, locator, FeatureConnection.NORMAL_ARC);
    }

    /**
     * Connects the figures if the mouse is released over another
     * FSNode figure;
     * otherwise, a new figure is created!
     */
    @Override
    public void invokeEnd(int x, int y, int anchorX, int anchorY, DrawingView view) {
        Connector target = findConnectionTarget(x, y, view.drawing());
        if (target == null) {
            Figure owner = owner();
            Feature feature = null;
            FeatureStructure fs = null;
            if (owner instanceof FSFigure) {
                fs = ((FSFigure) owner).getFeatureStructure();
                if (fs != null) {
                    feature = fs.getFirstMissingAssociation();
                    //logger.debug("Found missing feature "+feature.feature+" of type "+feature.type);
                }
            }
            if (owner.findFigureInside(x, y) == null) {
                Figure endFigure;
                if (owner instanceof FSFigure) {
                    FSFigure fsEndFigure = new FSFigure();
                    endFigure = fsEndFigure;
                    if (feature != null) {
                        fsEndFigure.setText(new FeatureStructure(feature._type).toString());
                        if (fs != null && fsEndFigure.getFeatureStructure() != null) {
                            fs.getRoot().setFeature(
                                feature._feature, fsEndFigure.getFeatureStructure().getRoot());
                        }
                    }
                } else {
                    endFigure = new FSNodeFigure("Type");
                }
                Dimension d = endFigure.displayBox().getSize();
                int w2 = d.width / 2;
                int h2 = d.height / 2;
                endFigure.displayBox(
                    new Point(x - w2, y - h2), new Point(x - w2 + d.width, y - h2 + d.height));
                view.drawing().add(endFigure);
                String featureStr = "feat";
                if (feature != null) {
                    featureStr = feature._feature.toString();
                }
                CPNTextFigure featureFigure = new CPNTextFigure(CPNTextFigure.INSCRIPTION);
                featureFigure.setAttribute("FillColor", ColorMap.BACKGROUND);
                featureFigure.setText(featureStr);
                featureFigure.setParent((FeatureConnection) getConnection());
                view.drawing().add(featureFigure);
            }
        }
        super.invokeEnd(x, y, anchorX, anchorY, view);
    }
}