package de.renew.gui.fs;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import de.uni_hamburg.fs.Node;

import de.renew.draw.ui.ontology.DrawingView;
import de.renew.gui.ClickHandle;


/**
 * The handle associated with shutters.
 */
public class ShutterHandle extends ClickHandle implements de.renew.gui.TokenHandle {
    Node _node;
    boolean _isClosed;

    /**
     * Constructor of the ShutterHandle.
     * This defines a default yellow and black style.
     *
     * @param owner owner of the handle
     * @param box box of the handle
     * @param node node of the handle
     * @param isClosed whether the handle should work on a closed or open state
     */
    public ShutterHandle(FSFigure owner, Rectangle box, Node node, boolean isClosed) {
        super(owner, Color.yellow, Color.black, box);
        this._node = node;
        this._isClosed = isClosed;
    }

    @Override
    protected void drawInner(Graphics g) {
        int ym = box.y + box.height / 2;


        // draw minus:
        g.drawLine(box.x + box.width / 4, ym, box.x + box.width * 3 / 4, ym);
        if (_isClosed) {
            // draw plus:
            int xm = box.x + box.width / 2;
            g.drawLine(xm, box.y + box.height / 4, xm, box.y + box.height * 3 / 4);
        }
    }

    @Override
    public void invokeStart(MouseEvent e, int x, int y, DrawingView view) {
        super.invokeStart(e, x, y, view);
        if (e.getClickCount() == 1) {
            _isClosed = !_isClosed;
        }
        ((FSFigure) owner()).setNodeShutState(_node, _isClosed, e.getClickCount() > 1);

        // Despite the fact that the appearance of the FSFigure changes
        // when nodes are opened or closed, the semantics of the figure do
        // not change. More important, the FSFigure does currently not
        // serialize information about open nodes in its undo snapshot.
        // So we inform the AbstractTool that nothing happened.
        noChangesMade();
    }
}