// File: ModeCreateConstEdge.java
// Classes: ModeCreateConstEdge
// Author: Kedar Patankar

package edu.neu.ccs.demeter.tools.apstudio.graphedit;

import java.awt.event.MouseEvent;
import java.awt.Cursor;
import java.awt.Point;

public class ModeCreateConstEdge extends ModeCreate 
{
	private UVertex sourceNode;
	private Editor _editor;
	private int count; // this variable is used to store no of mouseup events. When this becomes 2 time to add

	public ModeCreateConstEdge(Document par, Editor editor)
	{
		super(par);
		_editor=editor;
		editor.setMessage("In mode Create Construction Edge. Click on the source node and drag it to the destination node");
	}
	public void setMessage(Editor ed)
	{
		ed.setMessage("In mode Create Construction Edge. Click on the source node and drag it to the destination node");
	}
	public void setCursorType()
	{
		parent.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
	}

	public DiagramElement createNewItem(MouseEvent e) 
	{
		return new FigBentLine(e.getX(), e.getY());
	}
	
	public void mouseDown(MouseEvent event)
	{
		if(count==1) // This is inbetween mousedown
			return;

		DiagramElement underMouse = parent.pick(event.getX(),event.getY());
		parent.deselect();

		if (underMouse != null)
		{
			if(underMouse instanceof Perspective) 
			{
				sourceNode=(UVertex)((Perspective)underMouse).owner();
				if(sourceNode instanceof UConstVertex)
				{
					if( ((UConstVertex)sourceNode).isBreakingRepetition() )
					{
						done();
						_editor.setMessage("Repetition class can't have parts");
						return;
					}
				}
				// Set the superclass variables so that the later events can be passed to newItem
				newItem = createNewItem(event);
				parent.selectItem(underMouse);
			}
		}
		else
			done(); 
	}

	public void mouseUp(MouseEvent event) 
	{
		count++;

		if(sourceNode == null)
		{ 
			newItem.damagedIn(parent);
			newItem = null;
			done();
			return;
		}

		DiagramElement de = parent.pick(event.getX(),event.getY());
// if the mouse up happens on the blank surface it means user wants to go for one more iteration of drawing
		if(de==null && count<2) 
		{
			((FigBentLine)newItem).setBendPoint(event.getX(),event.getY());
			return;
		}
		Point midPoint = ((FigBentLine)newItem).getBendPoint();
		newItem.damagedIn(parent);
		newItem = null;
		done();

// Checking for illegal conditions

		if(de==null) return;
		if(!(de instanceof Perspective)) return;
		UVertex destNode = (UVertex)((Perspective) de).owner();

// if source and destination are same then there has to be a bend point and it has to be outside the vertex bounding
// area in order to build the edge.
		if(destNode == sourceNode)
		{
			if(midPoint==null)
				return;
			else
			if(de.inside(midPoint))
				return;
		}

// Legality checked. No problem go on make edge.

		Class arcClass;
		UEdge newArc;
		try { arcClass = Class.forName("edu.neu.ccs.demeter.tools.apstudio.graphedit.UConstEdge"); }
		catch (java.lang.ClassNotFoundException ignore) { return;}
		try { newArc = (UEdge)arcClass.newInstance(); }
		catch (java.lang.IllegalAccessException ignore) { return; }
		catch (java.lang.InstantiationException ignore) { return; }
		
		Action addConstructionEdge = new ActionCreateConstEdge
				(_editor,newArc,sourceNode,destNode,midPoint);
		addConstructionEdge.doIt();
	}

} /* end class ModeCreateConstEdge */

