Browse Source

Deletes NetworkTreeSettings on Object delete, Keeps Color on LinkChange

Andreas T. Meyer-Berg 5 years ago
parent
commit
61b77432de

+ 35 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/NetworkController.java

@@ -10,6 +10,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.configuration.NetworkTreeNodeStatus;
 
 /**
  * Controller for manipulation of the network model with methods like  
@@ -27,7 +28,10 @@ public class NetworkController {
 	 * Controller which can be use
 	 */
 	private Controller controller;
-	
+	/**
+	 * NetworkTreeSettings Controller
+	 */
+	private NetworkTreeSettingsController networkTreeSettings;
 	/**
 	 * Creates a new NetworkController, which may manipulate the given model and use the controller
 	 * @param model
@@ -36,6 +40,7 @@ public class NetworkController {
 	public NetworkController(Model model, Controller controller) {
 		this.model = model;
 		this.controller = controller;
+		networkTreeSettings = controller.getSettingsController().getNetworkTreeSettingsController();
 	}
 
 	/**
@@ -117,6 +122,9 @@ public class NetworkController {
 				
 		//Remove all ports from the device
 		toDelete.getPorts().clear();
+		
+		//Remove from network Tree
+		networkTreeSettings.removeStatusOfObject(toDelete);
 		model.getDevices().remove(toDelete);
 	}
 	/**
@@ -443,6 +451,7 @@ public class NetworkController {
 		Connection newCon = null;
 		try{
 			newCon = newConnectionClass.newInstance();
+			copyNetworkTreeStatus(connection, newCon);
 			newCon.setProtocol(connection.getProtocol());
 			newCon.setStatus(connection.getStatus());
 			newCon.setPacketLossProbability(connection.getPacketLossProbability());
@@ -540,6 +549,7 @@ public class NetworkController {
 		ports.clear();
 		removeConnectionFromLink(c, c.getLink());
 		c.setStatus(Connection.TERMINATED);
+		networkTreeSettings.removeStatusOfObject(c);
 		removeConnection(c);
 	}
 
@@ -558,6 +568,14 @@ public class NetworkController {
 			removeConnectionFromLink(c,l);
 		connections.clear();
 		l.getPackets().clear();
+		/**
+		 * Remove Link Color
+		 */
+		controller.getSettingsController().getLinkColors().removeLink(l);
+		networkTreeSettings.removeStatusOfObject(l);
+		/**
+		 * Remove Link from model
+		 */
 		removeLink(l);
 	}
 
@@ -596,6 +614,9 @@ public class NetworkController {
 		if (newLink == null || !(newLink instanceof Link)) {
 			return null;
 		}else {
+			LinkColorController linkColor = controller.getSettingsController().getLinkColors();
+			linkColor.setColorOfLink(newLink, linkColor.getColorOfLink(oldLink).getRight());
+			copyNetworkTreeStatus(oldLink, newLink);
 			// Set old Name
 			newLink.setName(oldLink.getName());
 			
@@ -618,4 +639,17 @@ public class NetworkController {
 			return newLink;
 		}
 	}
+	
+	/**
+	 * Adds status of the old Object to the new Object
+	 * @param oldObject old object, whose status should be copied
+	 * @param newObject new object, which should get the new status
+	 */
+	private void copyNetworkTreeStatus(Object oldObject, Object newObject){
+		NetworkTreeNodeStatus oldStatus = networkTreeSettings.getStatusOfObject(oldObject);
+		NetworkTreeNodeStatus newStatus = new NetworkTreeNodeStatus(newObject);
+		newStatus.setExpanded(oldStatus.isExpanded());
+		newStatus.setVisible(oldStatus.isVisible());
+		networkTreeSettings.addStatusOfObject(newObject, newStatus);
+	}
 }

+ 8 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/NetworkTreeSettingsController.java

@@ -197,5 +197,13 @@ public class NetworkTreeSettingsController {
 	public boolean isVisible(Object o){
 		return getStatusOfObject(o).isVisible();
 	}
+	
+	/**
+	 * Removes the status of the object o
+	 * @param o Object, which status should be removed
+	 */
+	public void removeStatusOfObject(Object o){
+		this.networkTreeSettings.removeStatusOfObject(o);
+	}
 
 }

+ 8 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/configuration/NetworkTreeSettings.java

@@ -56,4 +56,12 @@ public class NetworkTreeSettings {
 	public void addStatusOfObject(Object o, NetworkTreeNodeStatus status){
 		map.put(o, status);
 	}
+	
+	/**
+	 * Removes the status of the object o
+	 * @param o Object, which status should be removed
+	 */
+	public void removeStatusOfObject(Object o){
+		map.remove(o);
+	}
 }