/**
 * Copyright (c) 2010 committers of YAKINDU and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * Contributors:
 * 	committers of YAKINDU - initial API and implementation
 * 
 */
package org.yakindu.sct.statechart.diagram.commands;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
import org.yakindu.model.sct.statechart.FinalState;
import org.yakindu.model.sct.statechart.HistoryState;
import org.yakindu.model.sct.statechart.InitialState;
import org.yakindu.model.sct.statechart.Region;
import org.yakindu.model.sct.statechart.StatechartFactory;
import org.yakindu.model.sct.statechart.Transition;
import org.yakindu.model.sct.statechart.Vertex;

/**
 * 
 * @author muelder
 *
 */
public class CreateTransitionCommand extends EditElementCommand {

	private final Vertex source;

	private final Vertex target;

	private final Region container;

	public CreateTransitionCommand(CreateRelationshipRequest request) {
		super(request.getLabel(), null, request);
		this.source = (Vertex) request.getSource();
		this.target = (Vertex) request.getTarget();
		container = setContainer(source, target);
	}

	@Override
	public boolean canExecute() {
		if (source == null && target == null) {
			return false;
		}
		if (source != null && !(source instanceof Vertex))
			return false;
		if (target != null && !(target instanceof Vertex))
			return false;
		return checkConstraints();
	}

	private boolean checkConstraints() {
		if (source instanceof FinalState)
			return false;
		if (target instanceof InitialState)
			return false;
		if (target instanceof HistoryState)
			return false;
		if (source instanceof InitialState && ((InitialState) source).getOutgoingTransitions().size() > 0)
			return false;
		if (target instanceof HistoryState && ((HistoryState) target).getOutgoingTransitions().size() > 0)
			return false;
		if (target instanceof FinalState && ((FinalState) target).getIncomingTransitions().size() > 0)
			return false;

		return true;
	}

	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		if (!canExecute()) {
			throw new ExecutionException("Invalid arguments in create link command");
		}
		if (getSource() != null && getTarget() != null) {
			Transition transition = StatechartFactory.eINSTANCE.createTransition();
			source.getOutgoingTransitions().add(transition);
			transition.setSource(source);
			transition.setTarget(target);
			source.getOutgoingTransitions().add(transition);
			target.getIncomingTransitions().add(transition);
			((CreateElementRequest) getRequest()).setNewElement(transition);
		}
		return CommandResult.newOKCommandResult();

	}

	private static Region setContainer(Vertex source, Vertex target) {
		if (source == null || target == null)
			return null;
		Assert.isTrue(source.getParentRegion().equals(target.getParentRegion()));
		return source.getParentRegion();
	}

	public Region getContainer() {
		return container;
	}

	public Vertex getSource() {
		return source;
	}

	public Vertex getTarget() {
		return target;
	}

}

@@ -19,16 +19,22 @@
 (c) 2010
+-21011
  committ
@@ -61,16 +61,16 @@
 thers.%0D%0A
-
  * All r
@@ -513,49 +513,8 @@
 n;%0D%0A
-import org.eclipse.core.runtime.Assert;%0D%0A
 impo
@@ -1073,57 +1073,8 @@
 e;%0D%0A
-import org.yakindu.model.sct.statechart.Region;%0D%0A
 impo
@@ -1263,16 +1263,37 @@
 uelder%0D%0A
+ * @author terfloth%0D%0A
  *%0D%0A */%0D
@@ -1432,44 +1432,8 @@
 %0D%0A%0D%0A
-%09private final Region container;%0D%0A%0D%0A
 %09pub
@@ -1641,53 +1641,8 @@
 );%0D%0A
-%09%09container = setContainer(source, target);%0D%0A
 %09%7D%0D%0A
@@ -2082,38 +2082,38 @@
 n false;%0D%0A%09%09if (
-target
+source
  instanceof Hist
@@ -2264,244 +2264,8 @@
 se;%0D
-%0A%09%09if (target instanceof HistoryState && ((HistoryState) target).getOutgoingTransitions().size() %3E 0)%0D%0A%09%09%09return false;%0D%0A%09%09if (target instanceof FinalState && ((FinalState) target).getIncomingTransitions().size() %3E 0)%0D%0A%09%09%09return false;%0D
 %0A%0D%0A%09
@@ -3009,314 +3009,8 @@
 %0D%0A%0D%0A
-%09private static Region setContainer(Vertex source, Vertex target) %7B%0D%0A%09%09if (source == null %7C%7C target == null)%0D%0A%09%09%09return null;%0D%0A%09%09Assert.isTrue(source.getParentRegion().equals(target.getParentRegion()));%0D%0A%09%09return source.getParentRegion();%0D%0A%09%7D%0D%0A%0D%0A%09public Region getContainer() %7B%0D%0A%09%09return container;%0D%0A%09%7D%0D%0A%0D%0A
 %09pub

Complete - # 230: relax editing constraints for transitions