/**
 * 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.ui.editor.policies;

import java.util.Collection;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.diagram.core.util.ViewType;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CanonicalConnectionEditPolicy;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.View;
import org.yakindu.sct.model.sgraph.Region;
import org.yakindu.sct.model.sgraph.Transition;
import org.yakindu.sct.model.sgraph.Vertex;
import org.yakindu.sct.ui.editor.providers.SemanticHints;
import org.yakindu.sct.ui.editor.utils.SemanticHintUtil;

import com.google.common.collect.Lists;

/**
 * 
 * @author andreas muelder - Initial contribution and API
 * 
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class RegionCompartmentCanonicalEditPolicy extends
		CanonicalConnectionEditPolicy {

	@Override
	protected List getSemanticChildrenList() {
		return getSemanticHost().getVertices();
	}

	@Override
	protected List getSemanticConnectionsList() {
		List<Transition> transitions = Lists.newArrayList();
		EList<Vertex> vertices = getSemanticHost().getVertices();
		for (Vertex vertex : vertices) {
			transitions.addAll(vertex.getOutgoingTransitions());
		}
		return transitions;
	}

	protected boolean shouldIncludeConnection(Edge connection,
			Collection<View> children) {
		// Connections should only be included, when the source vertex is
		// contained in the region this edit policy belongs to
		EObject element = (EObject) connection.getElement();
		if (element instanceof Transition) {
			Vertex source = ((Transition) element).getSource();
			if (!getSemanticHost().getVertices().contains(source)) {
				return false;
			}
		}
		return super.shouldIncludeConnection(connection, children);
	}


	@Override
	protected EObject getSourceElement(EObject relationship) {
		Assert.isTrue(relationship instanceof Transition);
		Transition transition = (Transition) relationship;
		return transition.getSource();
	}

	@Override
	protected EObject getTargetElement(EObject relationship) {
		Assert.isTrue(relationship instanceof Transition);
		Transition transition = (Transition) relationship;
		return transition.getTarget();
	}

	@Override
	public Region getSemanticHost() {
		return (Region) super.getSemanticHost();
	}

	@Override
	public IGraphicalEditPart getHost() {
		return (IGraphicalEditPart) super.getHost();
	}

	@Override
	protected String getFactoryHint(IAdaptable elementAdapter) {
		EObject modelElement = (EObject) elementAdapter
				.getAdapter(EObject.class);
		String factoryHint = SemanticHintUtil.getSemanticHint(modelElement);
		return factoryHint;
	}

	protected boolean shouldDeleteView(View view) {
		//#Bug 349119 
		if (ViewType.NOTE.equals(view.getType())
				|| ViewType.NOTEATTACHMENT.equals(view.getType())
				|| ViewType.TEXT.equals(view.getType())) {
			return false;
		}
		return true;
	}

	@Override
	protected String getDefaultFactoryHint() {
		return SemanticHints.TRANSITION;
	}
}

@@ -436,24 +436,53 @@
 olicies;%0D%0A%0D%0A
+import java.util.ArrayList;%0D%0A
 import java.
@@ -495,24 +495,24 @@
 ollection;%0D%0A
-
 import java.
@@ -1492,20 +1492,17 @@
  extends
-%0D%0A%09%09
+ 
 Canonica
@@ -1997,21 +1997,17 @@
 nection,
-%0D%0A%09%09%09
+ 
 Collecti
@@ -2467,18 +2467,16 @@
 %0D%0A%09%7D%0D%0A%0D%0A
-%0D%0A
 %09@Overri
@@ -3237,22 +3237,16 @@
 tAdapter
-%0D%0A%09%09%09%09
 .getAdap
@@ -3421,16 +3421,17 @@
  %7B%0D%0A%09%09//
+ 
 #Bug 349
@@ -3433,17 +3433,16 @@
 g 349119
- 
 %0D%0A%09%09if (
@@ -3469,38 +3469,33 @@
 (view.getType())
-%0D%0A%09%09%09%09
+ 
 %7C%7C ViewType.NOTE
@@ -3675,24 +3675,24 @@
 ryHint() %7B%0D%0A
-
 %09%09return Sem
@@ -3718,12 +3718,530 @@
 ON;%0D%0A%09%7D%0D
+%0A%0D%0A%09/**%0D%0A%09 * This method is overridden to prevent the execution of%0D%0A%09 * DeferredLayoutCommand%0D%0A%09 */%0D%0A%09@Override%0D%0A%09protected void refreshSemantic() %7B%0D%0A%09%09List%3CIAdaptable%3E createdViews = super.refreshSemanticChildren();%0D%0A%09%09List%3CIAdaptable%3E createdConnectionViews = refreshSemanticConnections();%0D%0A%09%09List%3CIAdaptable%3E allViews = new ArrayList%3CIAdaptable%3E(createdConnectionViews.size() + createdViews.size());%0D%0A%09%09allViews.addAll(createdViews);%0D%0A%09%09allViews.addAll(createdConnectionViews);%0D%0A%09%09makeViewsImmutable(allViews);%0D%0A%09%7D%0D
 %0A%7D%0D%0A

Disable execution of DeferredLayoutCommand for CannonicalConnectionEditPolicy