From 74a4199f6a1a2d6d6c44a41da3713b35ff46c655 Mon Sep 17 00:00:00 2001
From: Simon Barner <barner@fortiss.org>
Date: Fri, 26 May 2017 11:59:59 +0000
Subject: [PATCH] - Enable automatic layouter only for diagrams without rounded
 edges, which is the case if none of the positions of the connected connectors
 is specified using an angle (i.e., in particular also if the model does not
 contain any connectors, or none of the connectors is connected to a
 connection). - getContributedItems(): Use shortcut style when checking
 conditions, i.e. return dedicated value at as soon as possible, instead of
 using nested ifs refs 2898

---
 .../base/ui/layout/auto/AutoLayoutMenu.java   | 66 ++++++++++++++-----
 1 file changed, 51 insertions(+), 15 deletions(-)

diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/layout/auto/AutoLayoutMenu.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/layout/auto/AutoLayoutMenu.java
index f77c031ba..2c00f94ee 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/layout/auto/AutoLayoutMenu.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/layout/auto/AutoLayoutMenu.java
@@ -17,6 +17,8 @@ $Id$
 +--------------------------------------------------------------------------*/
 package org.fortiss.tooling.base.ui.layout.auto;
 
+import static org.fortiss.tooling.base.layout.LayoutKeyConstants.CONNECTOR_ANGLE;
+import static org.fortiss.tooling.base.utils.AngleUtils.getAngle;
 import static org.fortiss.tooling.kernel.utils.EcoreUtils.pickInstanceOf;
 
 import java.util.ArrayList;
@@ -28,6 +30,8 @@ import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.ActionContributionItem;
 import org.eclipse.jface.action.IContributionItem;
 import org.eclipse.jface.resource.ImageDescriptor;
+import org.fortiss.tooling.base.model.element.IConnection;
+import org.fortiss.tooling.base.model.element.IConnector;
 import org.fortiss.tooling.base.model.element.IHierarchicElement;
 import org.fortiss.tooling.base.model.layout.ILayoutedModelElement;
 import org.fortiss.tooling.base.ui.ToolingBaseUIActivator;
@@ -41,7 +45,7 @@ import org.fortiss.tooling.kernel.ui.extension.data.ContextMenuContextProvider;
  * @author barner
  * @author $Author$
  * @version $Rev$
- * @ConQAT.Rating YELLOW Hash: 2EC00DB461A5106CEB42DF8819159504
+ * @ConQAT.Rating YELLOW Hash: 76FF1ADFC034DE69815575B04BA534F0
  */
 public class AutoLayoutMenu implements IContextMenuContributor {
 
@@ -76,26 +80,58 @@ public class AutoLayoutMenu implements IContextMenuContributor {
 		}
 	}
 
+	/**
+	 * Predicate if the given {@link IHierarchicElement} contains an {@link IConnector} whose
+	 * position is specified using an angle, and that is connected to at least one incoming or
+	 * outgoing {@link IConnection}.
+	 */
+	private boolean hasConnectedConnectorWithAngle(IHierarchicElement element) {
+		for(IConnector connector : element.getConnectors()) {
+			if(getAngle((ILayoutedModelElement)connector, CONNECTOR_ANGLE) != null) {
+				return !connector.getIncoming().isEmpty() || !connector.getOutgoing().isEmpty();
+			}
+		}
+
+		return false;
+	}
+
 	/** {@inheritDoc} */
 	@Override
 	public List<IContributionItem> getContributedItems(EObject selection,
 			ContextMenuContextProvider contextProvider) {
-		if(selection instanceof IHierarchicElement) {
-			IHierarchicElement element = (IHierarchicElement)selection;
-			// Skip single top-level model element (e.g., AF3 component architecture)
-			if(element instanceof IProjectRootElement && element.getContainedElements().size() == 1) {
-				// There is exactly one child element
-				element = element.getContainedElements().get(0);
-			}
-			if(!pickInstanceOf(ILayoutedModelElement.class, element.getContainedElements())
-					.isEmpty()) {
-				List<IContributionItem> contributionItems = new ArrayList<IContributionItem>();
-				contributionItems.add(new ActionContributionItem(
-						new LaunchAutoLayoutAction(element)));
-				return contributionItems;
+
+		if(!(selection instanceof IHierarchicElement)) {
+			return Collections.emptyList();
+		}
+
+		IHierarchicElement element = (IHierarchicElement)selection;
+		// Skip single top-level model element (e.g., AF3 component architecture)
+		if(element instanceof IProjectRootElement && element.getContainedElements().size() == 1) {
+			// There is exactly one child element
+			element = element.getContainedElements().get(0);
+		}
+
+		// Check if model is empty
+		if(pickInstanceOf(ILayoutedModelElement.class, element.getContainedElements()).isEmpty()) {
+			return Collections.emptyList();
+		}
+
+		// Enable automatic layouter only for diagrams without rounded edges, which is the case if
+		// none of the positions of the connected connectors is specified using an angle (i.e., in
+		// particular also if the model does not contain any connectors, or none of the connectors
+		// is connected to a connection).
+		if(hasConnectedConnectorWithAngle(element)) {
+			return Collections.emptyList();
+		}
+		for(IHierarchicElement childElement : element.getContainedElements()) {
+			if(hasConnectedConnectorWithAngle(childElement)) {
+				return Collections.emptyList();
 			}
 		}
-		return Collections.emptyList();
+
+		List<IContributionItem> contributionItems = new ArrayList<IContributionItem>();
+		contributionItems.add(new ActionContributionItem(new LaunchAutoLayoutAction(element)));
+		return contributionItems;
 	}
 
 	/** {@inheritDoc} */
-- 
GitLab