Commits

Anonymous committed 4f26a8f

It is refined for component layout, but still it does not look as if it is working

Comments (0)

Files changed (1)

src/nl/tudelft/lime/layout/LimeDiagramDirectedGraphLayoutVisitor.java

 package nl.tudelft.lime.layout;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
 import nl.tudelft.lime.edit.editpart.ArcEditPart;
 import nl.tudelft.lime.edit.editpart.ComponentEditPart;
 import nl.tudelft.lime.edit.editpart.LimeContainerEditPart;
 import org.eclipse.draw2d.graph.Edge;
 import org.eclipse.draw2d.graph.Node;
 import org.eclipse.draw2d.graph.NodeList;
-
 import org.eclipse.gef.EditPart;
 import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
 
 /**
  * Visitor with support for populating nodes and edges of DirectedGraph
      */
     @SuppressWarnings("unchecked")
     public void layoutDiagram(LimeDiagramEditPart diagram) {
-//        List<LimeEditPart> children = (List<LimeEditPart>) diagram.getChildren();
-//
-//        for (LimeEditPart child : children) {
-//            if (child instanceof ComponentEditPart) {
-//                layoutComponentContents(child);
-//            }
-//        }
+        List<LimeEditPart> children = (List<LimeEditPart>) diagram.getChildren();
+
+        for (LimeEditPart child : children) {
+            if (child instanceof ComponentEditPart) {
+                layoutComponentContents(child, child);
+            }
+        }
 
         Map<AbstractGraphicalEditPart, Object> partToNodesMap = new HashMap<AbstractGraphicalEditPart, Object>();
         Map<Node, Set<Node>> processedEdges = new HashMap<Node, Set<Node>>();
         addNodesToTopmostDiagram(diagram, graph, partToNodesMap);
 
         if (graph.nodes.size() > 0) {
-            addEdges(diagram, getPorts(diagram), true, graph, partToNodesMap, processedEdges);
+            addEdges(diagram, getPorts(diagram), graph, partToNodesMap, processedEdges);
             new NodeJoiningDirectedGraphLayout().visit(graph);
             applyResults(diagram, partToNodesMap);
         }
      * @param component
      */
     @SuppressWarnings("unchecked")
-    private void layoutComponentContents(LimeEditPart item) {
-    	LimeDiagramEditPart component = (LimeDiagramEditPart) item;
+    private void layoutComponentContents(LimeEditPart mainParent, LimeEditPart item) {
+    	ComponentEditPart component = (ComponentEditPart) item;
     	List<LimeEditPart> children = (List<LimeEditPart>) component.getChildren();
 
         //this is the recursive part
         for (LimeEditPart child : children) {
             if (child instanceof ComponentEditPart) {
-                layoutComponentContents(child);
+                layoutComponentContents(mainParent, child);
             }
         }
 
             componentGraph.nodes.add(node);
 
             //if the graph is created
-            if (componentGraph.nodes.size() > 0) {
-                //then draw the edges for the 
-                addEdges(component, getPorts(component), false, componentGraph, componentPartsMap, processedEdges);
-                new NodeJoiningDirectedGraphLayout().visit(componentGraph);
-                applyResults(component, componentPartsMap);
-            }
+        }
+        if (componentGraph.nodes.size() > 0) {
+            //then draw the edges for the 
+            addEdges(mainParent, getPorts(component), componentGraph, componentPartsMap, processedEdges);
+            new NodeJoiningDirectedGraphLayout().visit(componentGraph);
+            applyResults(component, componentPartsMap);
         }
     }
 
      * if there is a port to port connection between them, otherwise they are not connected together
      * @param ports
      */
-    private void addEdgesForComponent(LimeDiagramEditPart parent, ArcEditPart arcEditPart, DirectedGraph graph, Map<AbstractGraphicalEditPart, Object> componentPartMap, Map<Node, Set<Node>> processedEdges) {
+    private void addEdgesForComponent(ComponentEditPart parent, ArcEditPart arcEditPart, DirectedGraph graph, Map<AbstractGraphicalEditPart, Object> componentPartMap, Map<Node, Set<Node>> processedEdges) {
 		GraphAnimation.recordInitialState((Connection) arcEditPart.getFigure());
 		EditPart fromEditPart = findTopmostParent(parent, arcEditPart.getSource());
 		EditPart toEditPart = findTopmostParent(parent, arcEditPart.getTarget());
+		//if the arc is not inside this component, disregard it
+		if (fromEditPart == null || toEditPart == null)
+			return;
 		//if it is a port, then use itself
 		Node source, target;
 		if (fromEditPart.equals(parent)){
      * @param currentNode
      * @return
      */
-    private EditPart findTopmostParent(LimeDiagramEditPart whichIsAChildOf, EditPart currentNode) {
+    private EditPart findTopmostParent(LimeEditPart whichIsAChildOf, EditPart currentNode) {
         //go up
         while (!currentNode.getParent().equals(whichIsAChildOf)) {
+        	if (currentNode.getParent().getClass().equals(LimeDiagramEditPart.class))
+        		return null;
             currentNode = currentNode.getParent();
         }
 
      * @param processedEdges 
      */
     @SuppressWarnings("unchecked")
-    protected void addEdges(LimeDiagramEditPart parent, List<PortEditPart> nodes, boolean isDiagram,DirectedGraph graph, Map<AbstractGraphicalEditPart, Object> partToNodesMap, Map<Node, Set<Node>> processedEdges) {
+    protected void addEdges(LimeEditPart parent, List<PortEditPart> nodes, DirectedGraph graph, Map<AbstractGraphicalEditPart, Object> partToNodesMap, Map<Node, Set<Node>> processedEdges) {
         for (PortEditPart part : nodes) {
             List<ArcEditPart> outgoings = (List<ArcEditPart>) part.getSourceConnections();
 
             for (Object outgoing : outgoings) {
-                if (isDiagram) {
-                    addEdges(parent, (ArcEditPart) outgoing, graph, partToNodesMap, processedEdges);
+            	if (parent instanceof ComponentEditPart){
+                    addEdgesForComponent((ComponentEditPart)parent, (ArcEditPart) outgoing, graph, partToNodesMap, processedEdges);
                 } else {
-                    addEdgesForComponent(parent, (ArcEditPart) outgoing, graph, partToNodesMap, processedEdges);
+                    addEdges((LimeDiagramEditPart)parent, (ArcEditPart) outgoing, graph, partToNodesMap, processedEdges);
                 }
             }
         }