From 2153ffa92f696ca3f7f9db4743ebf6ddac5055d5 Mon Sep 17 00:00:00 2001
From: Dongyue Mou <mou@fortiss.org>
Date: Tue, 17 Apr 2012 11:22:33 +0000
Subject: [PATCH] improved refinement testing

---
 .../ConnectorConnectionCompositorBase.java    | 138 ++++++++----------
 1 file changed, 60 insertions(+), 78 deletions(-)

diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/compose/ConnectorConnectionCompositorBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/compose/ConnectorConnectionCompositorBase.java
index b688343e4..78cb10bd2 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/compose/ConnectorConnectionCompositorBase.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/compose/ConnectorConnectionCompositorBase.java
@@ -18,6 +18,7 @@ $Id$
 package org.fortiss.tooling.base.ui.compose;
 
 import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.util.EcoreUtil;
 import org.fortiss.tooling.base.model.base.ConnectionSegmentBase;
 import org.fortiss.tooling.base.model.base.ConnectorBase;
 import org.fortiss.tooling.base.model.base.EntryConnectorBase;
@@ -46,47 +47,41 @@ public abstract class ConnectorConnectionCompositorBase<HE extends IHierarchicEl
 
 	/** {@inheritDoc} */
 	@Override
-	public boolean canConnect(S source, T target, HE parent,
-			IConnectionCompositionContext context) {
+	public boolean canConnect(S source, T target, HE parent, IConnectionCompositionContext context) {
 		// Deny creation of feedback connections in internal view
-		if (!allowInternalFeedback()
-				&& context instanceof ConnectionDragContext) {
-			ConnectionDragContext dc = (ConnectionDragContext) context;
+		if(!allowInternalFeedback() && context instanceof ConnectionDragContext) {
+			ConnectionDragContext dc = (ConnectionDragContext)context;
 			//
-			if (dc.getSource() instanceof FreeConnectorEditPartBase
-					&& dc.getTarget() instanceof FreeConnectorEditPartBase) {
+			if(dc.getSource() instanceof FreeConnectorEditPartBase &&
+					dc.getTarget() instanceof FreeConnectorEditPartBase) {
 				return false;
 			}
 		}
 
-		if (source instanceof EntryConnectorBase) {
-			if (target instanceof EntryConnectorBase) {
-				return checkEntryEntry((EntryConnectorBase) source,
-						(EntryConnectorBase) target, null);
-			} else if (target instanceof ExitConnectorBase) {
+		if(source instanceof EntryConnectorBase) {
+			if(target instanceof EntryConnectorBase) {
+				return checkEntryEntry((EntryConnectorBase)source, (EntryConnectorBase)target, null);
+			} else if(target instanceof ExitConnectorBase) {
 				// use swapping here
-				return checkExitEntry((ExitConnectorBase) target,
-						(EntryConnectorBase) source, null);
+				return checkExitEntry((ExitConnectorBase)target, (EntryConnectorBase)source, null);
 			}
-		} else if (source instanceof ExitConnectorBase) {
-			if (target instanceof EntryConnectorBase) {
-				return checkExitEntry((ExitConnectorBase) source,
-						(EntryConnectorBase) target, null);
-			} else if (target instanceof ExitConnectorBase) {
-				return checkExitExit((ExitConnectorBase) source,
-						(ExitConnectorBase) target, null);
+		} else if(source instanceof ExitConnectorBase) {
+			if(target instanceof EntryConnectorBase) {
+				return checkExitEntry((ExitConnectorBase)source, (EntryConnectorBase)target, null);
+			} else if(target instanceof ExitConnectorBase) {
+				return checkExitExit((ExitConnectorBase)source, (ExitConnectorBase)target, null);
 			}
 		}
 		return false;
 	}
 
 	/** Checks entry to entry connection. */
-	private boolean checkEntryEntry(EntryConnectorBase source,
-			EntryConnectorBase target, EObject connection) {
+	private boolean checkEntryEntry(EntryConnectorBase source, EntryConnectorBase target,
+			EObject connection) {
 		boolean swapped = false;
 
 		// child input to super input => swap input
-		if (source.eContainer().eContainer() == target.eContainer()) {
+		if(source.eContainer().eContainer() == target.eContainer()) {
 			EntryConnectorBase temp = source;
 			source = target;
 			target = temp;
@@ -94,20 +89,17 @@ public abstract class ConnectorConnectionCompositorBase<HE extends IHierarchicEl
 		}
 
 		// super input to child input
-		if (swapped || source.eContainer() == target.eContainer().eContainer()) {
-			return checkSourceOut(source, connection)
-					&& checkTargetIn(target, connection);
+		if(swapped || source.eContainer() == target.eContainer().eContainer()) {
+			return checkSourceOut(source, connection) && checkTargetIn(target, connection);
 		}
 		return false;
 	}
 
 	/** Checks exit to entry connection. */
-	private boolean checkExitEntry(ExitConnectorBase source,
-			EntryConnectorBase target, EObject connection) {
-		if (source.eContainer().eContainer() == target.eContainer()
-				.eContainer()) {
-			return checkSourceOut(source, connection)
-					&& checkTargetIn(target, connection);
+	private boolean checkExitEntry(ExitConnectorBase source, EntryConnectorBase target,
+			EObject connection) {
+		if(source.eContainer().eContainer() == target.eContainer().eContainer()) {
+			return checkSourceOut(source, connection) && checkTargetIn(target, connection);
 		}
 		return false;
 	}
@@ -115,18 +107,17 @@ public abstract class ConnectorConnectionCompositorBase<HE extends IHierarchicEl
 	/**
 	 * Checks exit to exit connection.
 	 */
-	private boolean checkExitExit(ExitConnectorBase source,
-			ExitConnectorBase target, EObject connection) {
+	private boolean checkExitExit(ExitConnectorBase source, ExitConnectorBase target,
+			EObject connection) {
 		boolean swapped = false;
-		if (source.eContainer() == target.eContainer().eContainer()) {
+		if(source.eContainer() == target.eContainer().eContainer()) {
 			ExitConnectorBase temp = source;
 			source = target;
 			target = temp;
 			swapped = true;
 		}
-		if (swapped || source.eContainer().eContainer() == target.eContainer()) {
-			return checkSourceOut(source, connection)
-					&& checkTargetIn(target, connection);
+		if(swapped || source.eContainer().eContainer() == target.eContainer()) {
+			return checkSourceOut(source, connection) && checkTargetIn(target, connection);
 		}
 		return false;
 	}
@@ -137,8 +128,8 @@ public abstract class ConnectorConnectionCompositorBase<HE extends IHierarchicEl
 	private boolean checkSourceOut(ConnectorBase source, EObject connection) {
 		// source has no outgoing connection, is already connected, or allows
 		// multiple connections
-		return source.getOutgoingLength() == 0
-				|| (connection instanceof ConnectionSegmentBase && ((ConnectionSegmentBase) connection)
+		return source.getOutgoingLength() == 0 ||
+				(connection instanceof ConnectionSegmentBase && ((ConnectionSegmentBase)connection)
 						.getSource() == source) || allowOneToMany();
 	}
 
@@ -148,8 +139,8 @@ public abstract class ConnectorConnectionCompositorBase<HE extends IHierarchicEl
 	private boolean checkTargetIn(ConnectorBase target, EObject connection) {
 		// target has no incoming connection, is already connected, or allows
 		// multiple connections.
-		return target.getIncomingLength() == 0
-				|| (connection instanceof ConnectionSegmentBase && ((ConnectionSegmentBase) connection)
+		return target.getIncomingLength() == 0 ||
+				(connection instanceof ConnectionSegmentBase && ((ConnectionSegmentBase)connection)
 						.getTarget() == target) || allowManyToOne();
 	}
 
@@ -157,56 +148,50 @@ public abstract class ConnectorConnectionCompositorBase<HE extends IHierarchicEl
 	 * {@inheritDoc}
 	 */
 	@Override
-	public boolean connect(S source, T target, HE parent,
-			IConnectionCompositionContext context) {
+	public boolean connect(S source, T target, HE parent, IConnectionCompositionContext context) {
 
 		ConnectionSegmentBase conn = createConnection(source, target, context);
 
 		// If we are working in a connected model, deal with IDs
-		ITopLevelElement modelContext = IPersistencyService.INSTANCE
-				.getTopLevelElementFor(source);
-		if (modelContext != null) {
+		ITopLevelElement modelContext = IPersistencyService.INSTANCE.getTopLevelElementFor(source);
+		if(modelContext != null) {
 			modelContext.prepareIDs(conn);
 		}
 
 		boolean swap = false;
 
-		if (source instanceof EntryConnectorBase) {
-			if (target instanceof EntryConnectorBase) {
-				if (source.eContainer() == target.eContainer().eContainer()) {
-					((HierarchicElementBase) source.eContainer())
-							.getConnectionsList().add(conn);
+		if(source instanceof EntryConnectorBase) {
+			if(target instanceof EntryConnectorBase) {
+				if(source.eContainer() == target.eContainer().eContainer()) {
+					((HierarchicElementBase)source.eContainer()).getConnectionsList().add(conn);
 				} else {
 					// source.eContainer().eContainer() ==
 					// target.eContainer()
-					((HierarchicElementBase) target.eContainer())
-							.getConnectionsList().add(conn);
+					((HierarchicElementBase)target.eContainer()).getConnectionsList().add(conn);
 					swap = true;
 				}
-			} else if (target instanceof ExitConnectorBase) {
-				((HierarchicElementBase) source.eContainer().eContainer())
-						.getConnectionsList().add(conn);
+			} else if(target instanceof ExitConnectorBase) {
+				((HierarchicElementBase)source.eContainer().eContainer()).getConnectionsList().add(
+						conn);
 				swap = true;
 			}
-		} else if (source instanceof ExitConnectorBase) {
-			if (target instanceof EntryConnectorBase) {
-				((HierarchicElementBase) source.eContainer().eContainer())
-						.getConnectionsList().add(conn);
-			} else if (target instanceof ExitConnectorBase) {
-				if (source.eContainer() == target.eContainer().eContainer()) {
-					((HierarchicElementBase) source.eContainer())
-							.getConnectionsList().add(conn);
+		} else if(source instanceof ExitConnectorBase) {
+			if(target instanceof EntryConnectorBase) {
+				((HierarchicElementBase)source.eContainer().eContainer()).getConnectionsList().add(
+						conn);
+			} else if(target instanceof ExitConnectorBase) {
+				if(source.eContainer() == target.eContainer().eContainer()) {
+					((HierarchicElementBase)source.eContainer()).getConnectionsList().add(conn);
 					swap = true;
 				} else {
 					// source.eContainer().eContainer() ==
 					// target.eContainer()
-					((HierarchicElementBase) target.eContainer())
-							.getConnectionsList().add(conn);
+					((HierarchicElementBase)target.eContainer()).getConnectionsList().add(conn);
 				}
 			}
 		}
 
-		if (swap) {
+		if(swap) {
 			conn.setSource(target);
 			conn.setTarget(source);
 		} else {
@@ -221,8 +206,8 @@ public abstract class ConnectorConnectionCompositorBase<HE extends IHierarchicEl
 	 * provided to make use of their names when naming the connection. However
 	 * they should not be used for connecting.
 	 */
-	protected abstract ConnectionSegmentBase createConnection(S source,
-			T target, IConnectionCompositionContext context);
+	protected abstract ConnectionSegmentBase createConnection(S source, T target,
+			IConnectionCompositionContext context);
 
 	/**
 	 * Configures multiple connection behavior for 1-to-many connections. The
@@ -250,7 +235,7 @@ public abstract class ConnectorConnectionCompositorBase<HE extends IHierarchicEl
 	/** {@inheritDoc} */
 	@Override
 	public boolean canDisconnect(EObject connection) {
-		if (!canDisconnectSpecific(connection)) {
+		if(!canDisconnectSpecific(connection)) {
 			return false;
 		}
 		return true;
@@ -262,12 +247,9 @@ public abstract class ConnectorConnectionCompositorBase<HE extends IHierarchicEl
 	/** {@inheritDoc} */
 	@Override
 	public boolean disconnect(EObject connection) {
-		if (connection instanceof IConnection) {
-			IConnection conn = (IConnection) connection;
-			// this order is important !
-			conn.setOwner(null);
-			conn.setSource(null);
-			conn.setTarget(null);
+		if(connection instanceof IConnection) {
+			// since an IConnection object contains no element, it can be safe deleted directly.
+			EcoreUtil.delete(connection);
 			return true;
 		}
 		return false;
-- 
GitLab