Browse Source

Adds Edit->DeleteNetwork to delete all Devices, Connections & Links

Andreas T. Meyer-Berg 5 years ago
parent
commit
950e250709

+ 73 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/Controller.java

@@ -2,6 +2,7 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim.control;
 
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.Observer;
 
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
@@ -546,4 +547,76 @@ public class Controller {
 		
 		return newCon;
 	}
+	
+	/**
+	 * Deletes the network model, removes all Devices, Connections and Links
+	 */
+	public void deleteNetworkModel(){
+		/**
+		 * Devices which should be deleted
+		 */
+		LinkedList<SmartDevice> devicesToDelete = new LinkedList<SmartDevice>(model.getDevices());
+		for(SmartDevice d: devicesToDelete)
+			deleteSmartDevice(d);
+		devicesToDelete.clear();
+		/**
+		 * Connections which should be deleted
+		 */
+		LinkedList<Connection> connectionsToDelete = new LinkedList<Connection>(model.getConnections());
+		for(Connection c: connectionsToDelete)
+			deleteConnection(c);
+		connectionsToDelete.clear();
+		/**
+		 * Links which should be delted
+		 */
+		LinkedList<Link> linksToDelete = new LinkedList<Link>(model.getConnectionNetworks());
+		for(Link l: model.getConnectionNetworks())
+			deleteLink(l);
+		linksToDelete.clear();
+		/**
+		 * Update the GUI
+		 */
+		notifyObservers();
+	}
+
+	/**
+	 * Deletes the Connection c and all references
+	 * @param c Connection to be deleted
+	 */
+	public void deleteConnection(Connection c) {
+		if(c == null)return;
+		for(Port p:c.getParticipants())
+			removeDeviceFromConnection(p, c);
+		removeConnectionFromLink(c, c.getLink());
+		c.setStatus(Connection.TERMINATED);
+		removeConnection(c);
+	}
+
+	/**
+	 * Deletes Link l and all references
+	 * @param l Link to be deleted
+	 */
+	public void deleteLink(Link l) {
+		if(l==null)return;
+		for(SmartDevice d : l.getDevices())
+			removeLinkFromDevice(l, d);
+		for(Connection c:l.getConnections())
+			removeConnectionFromLink(c,l);
+		l.getPackets().clear();
+		removeLink(l);
+	}
+	
+	/**
+	 * Removes Connection from Link
+	 * @param c Connection to be removed
+	 * @param l Link to be removed
+	 */
+	private void removeConnectionFromLink(Connection c, Link l) {
+		if(c!=null && c.getLink()==l){
+			c.setLink(null);
+		}
+		if(l!=null){
+			l.removeConnection(c);		
+		}
+	}
 }

+ 13 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/MenuBar.java

@@ -3,11 +3,14 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
 import java.awt.FlowLayout;
 
 
+
 import javax.swing.JMenu;
 import javax.swing.JMenuBar;
 import javax.swing.JMenuItem;
 
 
+import javax.swing.JOptionPane;
+
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups.AboutPopUp;
@@ -89,6 +92,16 @@ public class MenuBar extends JMenuBar {
 	 */
 	private void initializeEditMenu() {
 		mnEdit = new JMenu("Edit");
+		
+		JMenuItem mntmDeleteModel = new JMenuItem("Delete Network");
+		mntmDeleteModel.addActionListener(a -> {
+			int dialogResult = JOptionPane.showConfirmDialog(this.getParent(), "Do you really want do delete all Devices, Ports, Links & Connections?");
+			if(dialogResult == JOptionPane.YES_OPTION){
+			  controller.deleteNetworkModel();
+			}
+		});
+		mnEdit.add(mntmDeleteModel);
+		
 		JMenuItem mntmOption = new JMenuItem("Settings");
 		mntmOption.addActionListener(a -> {
 			SettingsPopUp settings = new SettingsPopUp(model, controller);