Sfoglia il codice sorgente

Fixes new/invalid Links&Connection on creation abort

Andreas T. Meyer-Berg 6 anni fa
parent
commit
69efeccad1

+ 5 - 6
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationInteractor.java

@@ -881,14 +881,13 @@ public class VisualisationInteractor implements MouseInputListener,
 			for(SmartDevice d: selectedDevices)
 				ports.add(new Port(d, (short) 12));
 			Link link = null;
+			/**
+			 * Find common link -> else null -> create new link
+			 */
 			for(Link l:network.getLinks()){
-				if(l.getDevices().containsAll(selectedDevices))
+				if(l.getDevices().containsAll(selectedDevices)){
 					link = l;
-			}
-			if(link == null){
-				link = new SimpleLink("New Link");
-				for(SmartDevice device: selectedDevices){
-					controller.getNetworkController().addLinkToDevice(link, device);
+					break;
 				}
 			}
 			new ConnectionCreationDialog(ports,link, controller, panel);

+ 2 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/ConnectionCreationDialog.java

@@ -17,20 +17,19 @@ Component parent;
 
 	public ConnectionCreationDialog(Connection connection, Controller controller, Component panel) {
 		super();
-		content = new ConnectionCreationPanel(connection, controller);
+		content = new ConnectionCreationPanel(connection, controller, this);
 		parent = panel;
 		initialize();
 	}
 	
 	public ConnectionCreationDialog(Collection<Port> devices, Link link, Controller controller, Component panel){
 		super();
-		content = new ConnectionCreationPanel(devices, link, controller);
+		content = new ConnectionCreationPanel(devices, link, controller, this);
 		parent = panel;
 		initialize();
 	}
 
 	private void initialize() {
-		content.setFrame(this);
 		this.setModal(true);
 		this.setContentPane(content);
 		this.pack();

+ 35 - 20
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/ConnectionCreationPanel.java

@@ -5,6 +5,8 @@ import java.awt.Dimension;
 import java.awt.Window;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -98,6 +100,11 @@ public class ConnectionCreationPanel extends JScrollPane {
 	 * being created
 	 */
 	private boolean edit;
+	
+	/**
+	 * True if a new Link was created for this connection
+	 */
+	private boolean newLink = false;
 
 	/**
 	 * Last index which was selected in the protocolType ComboBox, to allow
@@ -153,10 +160,11 @@ public class ConnectionCreationPanel extends JScrollPane {
 	/**
 	 * @wbp.parser.constructor
 	 */
-	public ConnectionCreationPanel(Connection connection, Controller controller) {
+	public ConnectionCreationPanel(Connection connection, Controller controller, Window frame) {
 		this.controller = controller;
 		this.connection = connection;
 		this.network = controller.getNetworkController();
+		this.frame = frame;
 		ports = new Port[connection.getParticipants().size()];
 		int i = 0;
 		for (Port d : connection.getParticipants()) {
@@ -167,9 +175,20 @@ public class ConnectionCreationPanel extends JScrollPane {
 		initializePanel();
 	}
 
-	public ConnectionCreationPanel(Collection<Port> ports, Link l, Controller controller) {
+	public ConnectionCreationPanel(Collection<Port> ports, Link l, Controller controller, Window frame) {
 		this.controller = controller;
 		this.network = controller.getNetworkController();
+		this.frame = frame;
+		/**
+		 * if link null -> create new link
+		 */
+		if(l == null){
+			l = new SimpleLink("New Link");
+			for(Port device: ports){
+				controller.getNetworkController().addLinkToDevice(l, device.getOwner());
+			}
+			newLink = true;
+		}
 		connection = new ConnectionPerformance(l, new MQTT_protocol());
 		this.ports = new Port[ports.size()];
 		int i = 0;
@@ -537,6 +556,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 					return;
 				} else if (!connection.getLink().getConnections().contains(connection)){
 					network.addConnectionToLink(connection, connection.getLink());
+					
 				}
 				
 				if(tfName.getText()!=null)
@@ -581,7 +601,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 				if(!network.getLinks().contains(connection.getLink())){
 					network.addLink(connection.getLink());
 				}
-				
+				edit = true;
 				/**
 				 * Close Window
 				 */
@@ -675,6 +695,17 @@ public class ConnectionCreationPanel extends JScrollPane {
 			});
 			currentHeight += 20;
 		}
+		frame.addWindowListener(new WindowAdapter() {
+			@Override
+			public void windowClosing(WindowEvent e) {
+				if(!edit){
+					if(newLink){
+						network.deleteLink(connection.getLink());
+					}
+					network.deleteConnection(connection);
+				}
+			};
+		});
 		refreshGUI();
 	}
 
@@ -774,8 +805,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 		m.addConnectionNetwork(testNet);
 		m.addConnectionNetwork(new SimpleLink("Wifi"));
 		m.addConnectionNetwork(new SimpleLink("Ethernet"));
-		panel = new ConnectionCreationPanel(ports, testNet, new Controller(m));
-		panel.setFrame(frame);
+		panel = new ConnectionCreationPanel(ports, testNet, new Controller(m),frame);
 		frame.setContentPane(panel);
 		frame.pack();
 		frame.setVisible(true);
@@ -788,19 +818,4 @@ public class ConnectionCreationPanel extends JScrollPane {
 			}
 		});
 	}
-
-	/**
-	 * @return the frame
-	 */
-	public Window getFrame() {
-		return frame;
-	}
-
-	/**
-	 * @param frame
-	 *            the frame to set
-	 */
-	public void setFrame(Window frame) {
-		this.frame = frame;
-	}
 }

+ 31 - 6
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/LinkCreationDialog.java

@@ -9,27 +9,52 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 
+/**
+ * Simple Window which contains the {@link LinkCreationPanel}
+ *
+ * @author Andreas T. Meyer-Berg
+ */
 @SuppressWarnings("serial")
 public class LinkCreationDialog extends JDialog {
-LinkCreationPanel content;
-Component parent;
+	/**
+	 * Link Creation Panel which is displayed
+	 */
+	private LinkCreationPanel content;
+	/**
+	 * Parent Frame for relative location
+	 */
+	private Component parent;
 
+	/**
+	 * Create a Dialog for link editing
+	 * @param link link to be edited
+	 * @param controller controller to be used
+	 * @param panel which is the parent
+	 */
 	public LinkCreationDialog(Link link, Controller controller, Component panel) {
 		super();
-		content = new LinkCreationPanel(link, controller);
+		content = new LinkCreationPanel(link, controller, this);
 		parent = panel;
 		initialize();
 	}
 	
+	/**
+	 * Creates a Dialog for creation of a new connections
+	 * @param devices devices to be linked
+	 * @param controller controller to manipulate the model
+	 * @param panel panel which is the parent
+	 */
 	public LinkCreationDialog(Collection<SmartDevice> devices, Controller controller, Component panel){
 		super();
-		content = new LinkCreationPanel(devices, controller);
+		content = new LinkCreationPanel(devices, controller, this);
 		parent = panel;
 		initialize();
 	}
-
+	
+	/**
+	 * Initialize the panel
+	 */
 	private void initialize() {
-		content.setFrame(this);
 		this.setModal(true);
 		this.setContentPane(content);
 		this.pack();

+ 69 - 25
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/LinkCreationPanel.java

@@ -4,6 +4,8 @@ import java.awt.Dimension;
 import java.awt.Window;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
 import java.io.File;
 import java.util.Collection;
 import java.util.LinkedList;
@@ -29,26 +31,60 @@ import javax.swing.SwingConstants;
 import javax.swing.JComboBox;
 import javax.swing.JButton;
 
+/**
+ * Link Creation Panels allows creation and editing of Links
+ *
+ * @author Andreas T. Meyer-Berg
+ */
 @SuppressWarnings("serial")
 public class LinkCreationPanel extends JScrollPane{
 	
-	JPanel content;
+	/**
+	 * Content of the LinkCreation Panel, e.g. all cmbBoxes, Buttons and Textfields
+	 */
+	private JPanel content;
+	/**
+	 * Textfield for the name
+	 */
 	private JTextField tfName;
-	
+	/**
+	 * Parent window, which is needed for closing operation etc.
+	 */
 	private Window frame;
-	
+	/**
+	 * Link which is being created or edited
+	 */
 	private Link newLink;
+	/**
+	 * Array of devices which are edited
+	 */
 	private SmartDevice[] devices;
-	
-	@SuppressWarnings("unused")
+	/**
+	 * True if an existing link is being edited
+	 */
 	private boolean edit;
+	/**
+	 * Last SelectedIndex of the LinkType ComboBox
+	 */
 	private int lastIndex = -1;
-	
+	/**
+	 * Controller for manipulation of the model
+	 */
 	private Controller controller;
+	/**
+	 * Controller for manipulating the network model
+	 */
 	private NetworkController network;
-	public LinkCreationPanel(Link link, Controller controller) {
+	/**
+	 * Creates a new LinkCreation Panel for editing existing links
+	 * @param link link to be edited
+	 * @param controller controller to manipulate the link
+	 * @param frame parent frame
+	 */
+	public LinkCreationPanel(Link link, Controller controller, Window frame) {
 		this.controller = controller;
 		this.network = controller.getNetworkController();
+		this.frame = frame;
 		newLink = link;
 		this.devices = new SmartDevice[link.getDevices().size()];
 		int i=0;
@@ -59,9 +95,16 @@ public class LinkCreationPanel extends JScrollPane{
 		initializePanel();
 	}
 	
-	public LinkCreationPanel(Collection<SmartDevice> devices, Controller controller) {
+	/**
+	 * Create a new Link from the given Devices
+	 * @param devices devices which should be combined to a link
+	 * @param controller controller which should be used
+	 * @param frame parent frame
+	 */
+	public LinkCreationPanel(Collection<SmartDevice> devices, Controller controller, Window frame) {
 		this.controller = controller;
 		this.network = controller.getNetworkController();
+		this.frame = frame;
 		newLink = new SimpleLink("LinkName");
 		this.devices = new SmartDevice[devices.size()];
 		int i=0;
@@ -73,8 +116,22 @@ public class LinkCreationPanel extends JScrollPane{
 		initializePanel();
 	}
 	
+	/**
+	 * Initialize the Panel, add all components
+	 */
 	@SuppressWarnings("unchecked")
 	private void initializePanel() {
+		frame.addWindowListener(new WindowAdapter() {
+			@Override
+			public void windowClosing(WindowEvent e) {
+				if(!edit){
+					/**
+					 * Delete the link, if it is not being edited (And edit wasn't set to true by the createButton action
+					 */
+					network.deleteLink(newLink);
+				}
+			}
+		});
 		LinkedList<Class<? extends Link>> availableLinks = controller.getImportController().getLinks();
 		
 		setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
@@ -225,7 +282,8 @@ public class LinkCreationPanel extends JScrollPane{
 			public void actionPerformed(ActionEvent e) {
 				//network.addLink(newLink);
 				newLink.setName(tfName.getText());
-				
+				network.addLink(newLink);
+				edit = true;
 				content.setVisible(false);
 				setVisible(false);
 				controller.notifyObservers();
@@ -277,14 +335,14 @@ public class LinkCreationPanel extends JScrollPane{
         LinkedList<SmartDevice> devicesOfLink = new LinkedList<SmartDevice>();
         for(int i = 0; i<5; i++)
         	devicesOfLink.add(new SmartDevice("Device" + i));
-        panel = new LinkCreationPanel(devicesOfLink, new Controller(new Model()));
+        panel = new LinkCreationPanel(devicesOfLink, new Controller(new Model()),frame);
         /*
         Link testNet = new SimpleLink("Test Network");
         for(int i = 0; i<5; i++)
         	testNet.addDevice(new SmartDevice("Device" + i));
         panel = new LinkCreationPanel(testNet);
         */
-        panel.setFrame(frame);
+       
         frame.setContentPane(panel);
         frame.pack();
         frame.setVisible(true);
@@ -299,18 +357,4 @@ public class LinkCreationPanel extends JScrollPane{
             }
         });
     }
-
-	/**
-	 * @return the frame
-	 */
-	public Window getFrame() {
-		return frame;
-	}
-
-	/**
-	 * @param frame the frame to set
-	 */
-	public void setFrame(Window frame) {
-		this.frame = frame;
-	}
 }