/**
 * 
 */
package de.uni_jena.iaa.linktype.atomic.editors.grapheditor.commands;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.NotificationImpl;
import org.eclipse.gef.commands.Command;

import de.hu_berlin.german.korpling.saltnpepper.salt.graph.Graph;
import de.hu_berlin.german.korpling.saltnpepper.salt.graph.Node;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SDominanceRelation;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SSpan;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SSpanningRelation;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SStructure;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SToken;
import de.hu_berlin.german.korpling.saltnpepper.salt.saltCore.SRelation;

/**
 * @author Stephan Druskat
 *
 */
public class RelationCreateCommand extends Command {
	
	// FIXME TODO Do check on whether executable in Policy and return null for command when check fails 

	private Node target;
	private Node source;
	private SRelation relation;
	private Graph graph;
	
	@Override
	public boolean canExecute() {
		if (getRelation() instanceof SDominanceRelation) {
			return source != null && source instanceof SStructure && target != null && getRelation() != null;
		}
		else if (getRelation() instanceof SSpanningRelation) {
			return source != null && source instanceof SSpan && (target instanceof SToken || target instanceof SSpan) && target != null && getRelation() != null;
		}
		return false;
	}
			   
	@Override 
	public void execute() {
		getRelation().setSource(getSource());
		getRelation().setTarget(getTarget());
		getRelation().setGraph(getGraph());
		source.eNotify(new NotificationImpl(Notification.ADD, null, getRelation()));
		target.eNotify(new NotificationImpl(Notification.ADD, null, getRelation()));
	}
			 
	@Override 
	public void undo() {
		graph.getOutEdges(getRelation().getSTarget().getSId()).remove(getRelation());
		graph.getInEdges(getRelation().getSTarget().getSId()).remove(getRelation());
		getRelation().setSGraph(null);
	}


	public void setTarget(Node node) {
		this.target = node;
	}

	/**
	 * @return the target
	 */
	private Node getTarget() {
		return target;
	}

	public void setSource(Node model) {
		this.source = model;
		
	}

	/**
	 * @return the source
	 */
	private Node getSource() {
		return source;
	}

	public void setRelation(SRelation newObject) {
		this.relation = newObject;
	}

	/**
	 * @return the relation
	 */
	public SRelation getRelation() {
		return relation;
	}

	public void setGraph(Graph graph) {
		this.graph = graph;
	}

	/**
	 * @return the graph
	 */
	private Graph getGraph() {
		return graph;
	}

}

@@ -423,1236 +423,315 @@
 ltCo
-mmon.sDocumentStructure.SDominanceRelation;%0Aimport de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SSpan;%0Aimport de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SSpanningRelation;%0Aimport de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SStructure;%0Aimport de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SToken;%0Aimport de.hu_berlin.german.korpling.saltnpepper.salt.saltCore.SRelation;%0A%0A/**%0A * @author Stephan Druskat%0A *%0A */%0Apublic class RelationCreateCommand extends Command %7B%0A%09%0A%09// FIXME TODO Do check on whether executable in Policy and return null for command when check fails %0A%0A%09private Node target;%0A%09private Node source;%0A%09private SRelation relation;%0A%09private Graph graph;%0A%09%0A%09@Override%0A%09public boolean canExecute() %7B%0A%09%09if (getRelation() instanceof SDominanceRelation) %7B%0A%09%09%09return source != null && source instanceof SStructure && target != null && getRelation() != null;%0A%09%09%7D%0A%09%09else if (getRelation() instanceof SSpanningRelation) %7B%0A%09%09%09return source != null && source instanceof SSpan && (target instanceof SToken %7C%7C target instanceof SSpan) && target != null && getRelation() != null;%0A%09%09%7D%0A%09%09return false;%0A%09%7D%0A%09%09%09   
+re.SRelation;%0A%0A/**%0A * @author Stephan Druskat%0A *%0A */%0Apublic class RelationCreateCommand extends Command %7B%0A%09%0A%09// FIXME TODO Do check on whether executable in Policy and return null for command when check fails %0A%0A%09private Node target;%0A%09private Node source;%0A%09private SRelation relation;%0A%09private Graph graph;%0A%09
 %0A%09@O

Remove unnecessary canExecute() override