From 3589c82988fce1e531739f4fd712a2db3e5cb412 Mon Sep 17 00:00:00 2001
From: Johannes Eder <eder@fortiss.org>
Date: Mon, 30 Nov 2015 16:36:25 +0000
Subject: [PATCH] v0.8 working sub diagrams refs 2446

---
 .../AllocationDiagramEditPartBase.java        | 73 +--------------
 .../AllocationElementEditPartBase.java        | 88 ++++++++++---------
 .../AllocationFreeConnectorEditPartBase.java  | 15 ++--
 ...AllocationStickyConnectorEditPartBase.java | 15 ++--
 .../AllocationSubDiagramEditPartBase.java     | 18 ++--
 .../base/ui/editpart/IAllocationPosition.java | 15 +---
 6 files changed, 76 insertions(+), 148 deletions(-)

diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationDiagramEditPartBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationDiagramEditPartBase.java
index 377455a53..6cbf81f53 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationDiagramEditPartBase.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationDiagramEditPartBase.java
@@ -48,10 +48,7 @@ import org.fortiss.tooling.base.ui.layout.RectangleGridLayoutDiagramConfiguratio
  * @ConQAT.Rating RED Hash:
  */
 public abstract class AllocationDiagramEditPartBase<T extends EObject> extends
-		GraphicalEditPartBase<T> implements NodeEditPart/*
-														 * , IHierarchicElementBehavior<S,
-														 * V>
-														 */{
+		GraphicalEditPartBase<T> implements NodeEditPart {
 
 	/** Stores the diagram layout configuration. */
 	protected final IDiagramLayoutConfiguration layoutConfiguration;
@@ -59,45 +56,13 @@ public abstract class AllocationDiagramEditPartBase<T extends EObject> extends
 	/** Stores the connection router. */
 	private final ConnectionRouter connectionRouter;
 
-	// /** Displayed model children of allocation element 1 */
-	// private List<Object> modelChildren1;
-	// /** Displayed model children of allocation element 2 */
-	// private List<Object> modelChildren2;
-
-	// /** Class of S */
-	// private Class<S> clazzS;
-	//
-	// /** Class of V */
-	// private Class<V> clazzV;
-
 	/** Constructor. */
 	protected AllocationDiagramEditPartBase(T modelElement) {
 		super(modelElement);
 		this.layoutConfiguration = RectangleGridLayoutDiagramConfiguration.INSTANCE;
 		this.connectionRouter = new BendpointConnectionRouter();
-		// this.clazzS = clazzS;
-		// this.clazzV = clazzV;
-		// modelChildren1 = new ArrayList<>();
-		// modelChildren2 = new ArrayList<>();
-		// modelChildren1.addAll(getInitialModelChildren1());
-		// modelChildren2.addAll(getInitialModelChildren2());
 	}
 
-	// /** {@inheritDoc} */
-	// @Override
-	// final List<?> getModelChildren() {
-	// List<Object> result = new ArrayList<>();
-	// result.addAll(modelChildren1);
-	// result.addAll(modelChildren2);
-	// return result;
-	// }
-
-	// /** Returns all the initially displayed elements of allocation element 1 */
-	// abstract protected List<?> getInitialModelChildren1();
-	//
-	// /** Returns all the initially displayed elements of allocation element 2 */
-	// abstract protected List<?> getInitialModelChildren2();
-
 	/** {@inheritDoc} */
 	@Override
 	public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) {
@@ -125,7 +90,7 @@ public abstract class AllocationDiagramEditPartBase<T extends EObject> extends
 	/** {@inheritDoc} */
 	@Override
 	protected void createEditPolicies() {
-		// nothing to do yet
+		// nothing to do
 	}
 
 	/** {@inheritDoc} */
@@ -160,14 +125,7 @@ public abstract class AllocationDiagramEditPartBase<T extends EObject> extends
 	protected IFigure createFigure() {
 		setConnectionRouter();
 
-		FreeformLayer f = new FreeformLayer() {
-			/** {@inheritDoc} */
-			@Override
-			protected boolean useLocalCoordinates() {
-				// TODO Auto-generated method stub
-				return true;
-			}
-		};
+		FreeformLayer f = new FreeformLayer();
 		f.setOpaque(false);
 		f.setLayoutManager(new XYLayout());
 		f.setBackgroundColor(ColorConstants.white);
@@ -183,29 +141,4 @@ public abstract class AllocationDiagramEditPartBase<T extends EObject> extends
 		}
 	}
 
-	// /** {@inheritDoc} */
-	// @SuppressWarnings("unchecked")
-	// @Override
-	// public void performRequest(Request req) {
-	// if(req.getType() == RequestConstants.REQ_OPEN) {
-	// Set<?> keySet = req.getExtendedData().keySet();
-	// Object object = keySet.stream().findFirst().get();
-	// if(clazzS.isInstance(object)) {
-	// List<Object> subElements1 = (List<Object>)getSubElements1((S)object);
-	// if(!subElements1.isEmpty()) {
-	// modelChildren1 = subElements1;
-	// refresh();
-	// }
-	// } else if(clazzV.isInstance(object)) {
-	// List<Object> subElements2 = (List<Object>)getSubElements2((V)object);
-	// if(!subElements2.isEmpty()) {
-	// modelChildren2 = subElements2;
-	// refresh();
-	// }
-	// }
-	// }
-	//
-	// super.performRequest(req);
-	// }
-
 }
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationElementEditPartBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationElementEditPartBase.java
index 802f8b945..5c65e450a 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationElementEditPartBase.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationElementEditPartBase.java
@@ -18,6 +18,10 @@ $Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
 package org.fortiss.tooling.base.ui.editpart;
 
 import static org.fortiss.tooling.base.layout.DefaultLayoutConstants.DEFAULT_GRID_SIZE;
+import static org.fortiss.tooling.base.ui.editpart.IAllocationPosition.minSourcePos;
+import static org.fortiss.tooling.base.ui.editpart.IAllocationPosition.minTargetPos;
+import static org.fortiss.tooling.base.ui.editpart.IAllocationPosition.sourceAllocationBounds;
+import static org.fortiss.tooling.base.ui.editpart.IAllocationPosition.targetAllocationBounds;
 import static org.fortiss.tooling.base.ui.utils.LayoutDataUIUtils.getNodeBounds;
 
 import java.util.List;
@@ -63,7 +67,7 @@ import org.fortiss.tooling.kernel.utils.KernelModelElementUtils;
  * @ConQAT.Rating RED Hash:
  */
 public abstract class AllocationElementEditPartBase<T extends ILayoutedModelElement & INamedCommentedElement>
-		extends PositionedEditPartBase<T> implements IAllocationPosition {
+		extends PositionedEditPartBase<T> {
 
 	/** The default font for titles. */
 	public static Font DEFAULT_TITLE_FONT = FontUtils.derive(JFaceResources.getDefaultFont(), 0,
@@ -101,9 +105,10 @@ public abstract class AllocationElementEditPartBase<T extends ILayoutedModelElem
 	@Override
 	protected Rectangle determineLabelFigureBounds() {
 		Rectangle textBounds = labelFigure.getTextBounds();
+		Rectangle parentBounds = baseFigure.getParent().getBounds();
 		Rectangle labelBounds = determineBaseFigureBounds();
-		labelBounds.y += (labelBounds.height - textBounds.height) / 2;
-		labelBounds.x += 2 * DefaultLayoutConstants.DEFAULT_SHAPE_INSETS;
+		labelBounds.y += parentBounds.y + (labelBounds.height - textBounds.height) / 2;
+		labelBounds.x += parentBounds.x + 2 * DefaultLayoutConstants.DEFAULT_SHAPE_INSETS;
 		labelBounds.width -= 4 * DefaultLayoutConstants.DEFAULT_SHAPE_INSETS;
 		labelBounds.height = textBounds.height * 2;
 		return labelBounds;
@@ -127,51 +132,54 @@ public abstract class AllocationElementEditPartBase<T extends ILayoutedModelElem
 	protected Rectangle determineBaseFigureBounds() {
 		T model2 = getModel();
 		Rectangle r = getNodeBounds(model2);
-		int modelElementLevel = EcoreUtils.getModelElementLevel(getModel());
-		if(modelElementLevel >= 2) {
-			EObject parentElement =
-					KernelModelElementUtils.getParentElement(getModel(), EObject.class, false);
-			EList<? extends ILayoutedModelElement> layoutElements =
-					EcoreUtils.getChildrenWithType(parentElement, getModel().getClass());
-			Rectangle reduce =
-					layoutElements.stream().map(m -> getNodeBounds(m))
-							.reduce(new Rectangle(), (a, b) -> a.union(b));
-
-			int minX = Integer.MAX_VALUE, minY = Integer.MAX_VALUE;
-			for(ILayoutedModelElement ml : layoutElements) {
-				org.fortiss.tooling.base.model.layout.Rectangle nodePosition =
-						LayoutDataUtils.getNodeBounds(ml);
-				if(nodePosition.getX() < minX) {
-					minX = nodePosition.getX();
-				}
-				if(nodePosition.getY() < minY) {
-					minY = nodePosition.getY();
-				}
-			}
 
-			if(isAllocationTargetElement()) {
-				r = IAllocationPosition.allocationTargetElementBounds(r);
-				minTargetPos.x = minX;
-				minTargetPos.y = minY;
-				targetAllocationBounds.x = reduce.x;
-				targetAllocationBounds.y = reduce.y;
-				targetAllocationBounds.width = reduce.width;
-				targetAllocationBounds.height = reduce.height;
-			} else {
-				minSourcePos.x = minX;
-				minSourcePos.y = minY;
-				sourceAllocationBounds.x = reduce.x;
-				sourceAllocationBounds.y = reduce.y;
-				sourceAllocationBounds.height = reduce.height;
-				sourceAllocationBounds.width = reduce.width;
-				sourceAllocationBounds.resize(DEFAULT_GRID_SIZE, DEFAULT_GRID_SIZE);
+		// Calculations for sub diagram figures
+		// TODO(JE): move to sub diagrams
+		EObject parentElement =
+				KernelModelElementUtils.getParentElement(getModel(), EObject.class, false);
+		EList<? extends ILayoutedModelElement> layoutElements =
+				EcoreUtils.getChildrenWithType(parentElement, getModel().getClass());
+		Rectangle reduce =
+				layoutElements.stream().map(m -> getNodeBounds(m))
+						.reduce(new Rectangle(), (a, b) -> a.union(b));
+
+		int minX = Integer.MAX_VALUE, minY = Integer.MAX_VALUE;
+		for(ILayoutedModelElement ml : layoutElements) {
+			org.fortiss.tooling.base.model.layout.Rectangle nodePosition =
+					LayoutDataUtils.getNodeBounds(ml);
+			if(nodePosition.getX() < minX) {
+				minX = nodePosition.getX();
+			}
+			if(nodePosition.getY() < minY) {
+				minY = nodePosition.getY();
 			}
+		}
 
+		if(isAllocationTargetElement()) {
+			minTargetPos.x = minX;
+			minTargetPos.y = minY;
+			targetAllocationBounds.x = reduce.x;
+			targetAllocationBounds.y = reduce.y;
+			targetAllocationBounds.width = reduce.width;
+			targetAllocationBounds.height = reduce.height;
+		} else {
+			minSourcePos.x = minX;
+			minSourcePos.y = minY;
+			sourceAllocationBounds.x = reduce.x;
+			sourceAllocationBounds.y = reduce.y;
+			sourceAllocationBounds.height = reduce.height;
+			sourceAllocationBounds.width = reduce.width;
+			sourceAllocationBounds.resize(DEFAULT_GRID_SIZE, DEFAULT_GRID_SIZE);
 		}
 
+		// }
+
 		return r;
 	}
 
+	/** Returns true if this element is the target of an allocation. */
+	public abstract boolean isAllocationTargetElement();
+
 	/** {@inheritDoc} */
 	@Override
 	protected ConnectionAnchor defaultConnectionAnchor() {
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationFreeConnectorEditPartBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationFreeConnectorEditPartBase.java
index 5f5c28c59..e552c252d 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationFreeConnectorEditPartBase.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationFreeConnectorEditPartBase.java
@@ -32,7 +32,7 @@ import org.fortiss.tooling.kernel.model.INamedCommentedElement;
  * @ConQAT.Rating RED Hash:
  */
 public abstract class AllocationFreeConnectorEditPartBase<T extends ILayoutedModelElement & IConnector & INamedCommentedElement>
-		extends FreeConnectorEditPartBase<T> implements IAllocationPosition {
+		extends FreeConnectorEditPartBase<T> {
 
 	/**
 	 * @param modelElement
@@ -44,13 +44,14 @@ public abstract class AllocationFreeConnectorEditPartBase<T extends ILayoutedMod
 
 	/** {@inheritDoc} */
 	@Override
-	protected Rectangle determineBaseFigureBounds() {
-		Rectangle r = super.determineBaseFigureBounds();
-		if(isAllocationTargetElement()) {
-			return IAllocationPosition.allocationTargetElementBounds(r);
-		}
-		return r;
+	protected Rectangle determineLabelFigureBounds() {
+		// position label right of base figure
+		Rectangle textBounds = labelFigure.getTextBounds();
+		Rectangle baseBounds = baseFigure.getBounds();
+		Rectangle parentBounds = baseFigure.getParent().getParent().getBounds();
 
+		return new Rectangle(parentBounds.x + baseBounds.x + baseBounds.width, parentBounds.y +
+				baseBounds.y, textBounds.width, baseBounds.height);
 	}
 
 }
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationStickyConnectorEditPartBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationStickyConnectorEditPartBase.java
index e14cfda06..dae3bc031 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationStickyConnectorEditPartBase.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationStickyConnectorEditPartBase.java
@@ -31,7 +31,7 @@ import org.fortiss.tooling.kernel.model.INamedCommentedElement;
  * @ConQAT.Rating RED Hash:
  */
 public abstract class AllocationStickyConnectorEditPartBase<T extends ILayoutedModelElement & IConnector & INamedCommentedElement>
-		extends StickyConnectorEditPartBase<T> implements IAllocationPosition {
+		extends StickyConnectorEditPartBase<T> {
 
 	/**
 	 * @param modelElement
@@ -43,13 +43,14 @@ public abstract class AllocationStickyConnectorEditPartBase<T extends ILayoutedM
 
 	/** {@inheritDoc} */
 	@Override
-	protected Rectangle determineBaseFigureBounds() {
-		Rectangle r = super.determineBaseFigureBounds();
-		if(isAllocationTargetElement()) {
-			return IAllocationPosition.allocationTargetElementBounds(r);
-		}
-		return r;
+	protected Rectangle determineLabelFigureBounds() {
+		// position label right of base figure
+		Rectangle textBounds = labelFigure.getTextBounds();
+		Rectangle baseBounds = baseFigure.getBounds();
+		Rectangle parentBounds = baseFigure.getParent().getParent().getBounds();
 
+		return new Rectangle(parentBounds.x + baseBounds.x + baseBounds.width, parentBounds.y +
+				baseBounds.y, textBounds.width, baseBounds.height);
 	}
 
 }
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationSubDiagramEditPartBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationSubDiagramEditPartBase.java
index ff79c633a..b80fa0df7 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationSubDiagramEditPartBase.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/AllocationSubDiagramEditPartBase.java
@@ -56,6 +56,7 @@ public abstract class AllocationSubDiagramEditPartBase<T extends EObject> extend
 	/** The decoration figure. */
 	protected Label decorationFigure;
 
+	/** The layout configuration. */
 	private IDiagramLayoutConfiguration layoutConfiguration;
 
 	/**
@@ -84,23 +85,16 @@ public abstract class AllocationSubDiagramEditPartBase<T extends EObject> extend
 	}
 
 	/**
-	 * Returns the position and size of the decoration figure. The computation
-	 * may use the bounds of the base figure or the label figure, since both
-	 * bounds were determined first.
+	 * Returns the position and size of the background figure of this sub diagram.
 	 */
 	protected Rectangle determineBaseFigureBounds() {
 		Rectangle sourceBounds = IAllocationPosition.sourceAllocationBounds;
 		Rectangle targetBounds = IAllocationPosition.targetAllocationBounds;
-		Point sourcePos = IAllocationPosition.minSourcePos;
-		Point targetPos = IAllocationPosition.minTargetPos;
+		Point minTargetPos = IAllocationPosition.minTargetPos;
 		if(isTargetDiagram()) {
-			System.out.println("Source: " + sourceBounds.toString());
-			System.out.println("Target: " + targetBounds.toString());
-
 			return new Rectangle(targetBounds.x, sourceBounds.y + sourceBounds.height,
-					targetBounds.width, targetBounds.height);
+					targetBounds.width + minTargetPos.x, targetBounds.height + 20);
 		}
-
 		return new Rectangle(sourceBounds.x, sourceBounds.y, sourceBounds.width,
 				sourceBounds.height);
 	}
@@ -130,8 +124,7 @@ public abstract class AllocationSubDiagramEditPartBase<T extends EObject> extend
 	/** {@inheritDoc} */
 	@Override
 	protected void createEditPolicies() {
-		// TODO Auto-generated method stub
-
+		// nothing to do
 	}
 
 	/** Returns the foreground color used. */
@@ -142,7 +135,6 @@ public abstract class AllocationSubDiagramEditPartBase<T extends EObject> extend
 
 	/**
 	 * Returns if this edit part is the target edit part of the allocation.
-	 * TODO: move to interface {@link IAllocationPosition} and change it to sub diagrams
 	 */
 	abstract public boolean isTargetDiagram();
 
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/IAllocationPosition.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/IAllocationPosition.java
index 99b863147..6e8da7c78 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/IAllocationPosition.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editpart/IAllocationPosition.java
@@ -22,6 +22,7 @@ import org.eclipse.draw2d.geometry.Rectangle;
 
 /**
  * Interface for allocation target elements.
+ * TODO: make this interface a singleton
  * 
  * @author eder
  * @author $Author: hoelzl $
@@ -36,18 +37,10 @@ public interface IAllocationPosition {
 	/** Stores the bounds of the target of the allocation in the editor. */
 	static Rectangle targetAllocationBounds = new Rectangle();
 
+	/** Stores the position of the top left most element of all source elements */
 	static Point minSourcePos = new Point();
-	static Point minTargetPos = new Point();
-
-	/** Returns true if T is the target of the allocation, otherwise false. */
-	public boolean isAllocationTargetElement();
 
-	/** Returns the bounds for allocation target elements. */
-	public static Rectangle allocationTargetElementBounds(Rectangle r) {
+	/** Stores the position of the top left most element of all target elements */
+	static Point minTargetPos = new Point();
 
-		// int sheight = sourceAllocationBounds.height;
-		// int sy = sourceAllocationBounds.y;
-		// r.y = r.y + sheight;// - sy;// + 20;
-		return r;
-	}
 }
-- 
GitLab