Переглянути джерело

Enables replacing per Drag&Drop on MainCanvas

Andreas T. Meyer-Berg 6 роки тому
батько
коміт
af59f63808

+ 25 - 0
src/ui/controller/CanvasController.java

@@ -105,6 +105,31 @@ public class CanvasController {
 		notifyObjListeners();
 	}
 
+	/**
+     * Replaces {@code toBeReplaced} by {@code by} on the canvas
+     * @param toBeReplaced the object that will be replaced
+     * @param by the object that will replace it
+     */
+	public void replaceObjectOnCanvas(AbstractCpsObject toBeReplaced, AbstractCpsObject by) {
+		/** let all edges of 'toBeReplaced' connect to 'by' */
+		for(CpsEdge e: toBeReplaced.getConnections()){
+			if(e.getA() == toBeReplaced){
+				e.setA(by);
+			}else if(e.getB() == toBeReplaced){
+				e.setB(by);
+			}
+			
+			/** if edge from an object to itself -> remove it */
+			if(e.getA() == e.getB())
+				removeEdgesOnCanvas(e);
+			else/** else add edge to 'by' */
+				by.addConnection(e);
+		}
+		/** delete 'toBeReplaced' new empty connections, to prevent Nullpointer*/
+		toBeReplaced.setConnections(new ArrayList<CpsEdge>(1));
+		deleteObjectOnCanvas(toBeReplaced);
+	}
+	
 	/**
 	 * Add an edge to the Canvas.
 	 * 

+ 11 - 0
src/ui/controller/Control.java

@@ -312,6 +312,16 @@ public class Control {
             }
     }
 
+    /**
+     * Replaces {@code toBeReplaced} by {@code by} on the canvas
+     * @param toBeReplaced the object that will be replaced
+     * @param by the object that will replace it
+     */
+    public void replaceCanvasObject(AbstractCpsObject toBeReplaced, AbstractCpsObject by) {
+    	canvasController.replaceObjectOnCanvas(toBeReplaced, by);
+    	
+    }
+    
     /**
      * Add an edge to the Canvas.
      *
@@ -1003,4 +1013,5 @@ public class Control {
 		globalController.setFairnessModel(fairnessModel);
 	}
 
+
 }

+ 1 - 2
src/ui/view/MyCanvas.java

@@ -635,8 +635,7 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 				}
 				/** if replacement of exactly one object possible */
 				if(replaceCounter == 1 && toBeReplaced != null){
-					System.out.println(toBeReplaced.getName() + " should be replaced by " + tempCps.getName());
-					//TODO: replace
+					controller.replaceCanvasObject(toBeReplaced, tempCps);
 				}
 				
 				controller.autoSave();