Pārlūkot izejas kodu

Fixes various bugs

* NullPointer on different occasions
* Link change not moving connections
* Improves ConnectionPanel code
Andreas T. Meyer-Berg 6 gadi atpakaļ
vecāks
revīzija
013071337d

+ 80 - 41
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/NetworkController.java

@@ -102,27 +102,9 @@ public class NetworkController {
 		// Delete Connections
 		for (Port p : toDelete.getPorts()) {
 			//Skip ports that are not connected
-			if(p.getConnection()==null)continue;
+			if(p.getConnection()==null)
+				continue;
 			removeDeviceFromConnection(p, p.getConnection());
-			
-			/**
-			 * Connection of the current port
-			 *
-			Connection c = p.getConnection();
-			
-			//remove from protocol
-			if(c.getProtocol()!=null)
-				c.getProtocol().removeDevice(p);
-			
-			//remove from connection
-			c.removeSmartDevice(p);
-			if (c.getStatus()==Connection.FINISHED||c.getStatus()==Connection.TERMINATED) {
-				//if connection terminated - remove rest of ports
-				for (Port sd : c.getParticipants()) {
-					if (sd != null && sd != p)
-						sd.getOwner().removePort(sd);
-				}
-			}*/
 		}
 		
 		// Remove from Links
@@ -178,7 +160,8 @@ public class NetworkController {
 	 * @param link link to add
 	 */
 	public void addLink(Link link){
-		model.addConnectionNetwork(link);
+		if(link!=null)
+			model.addConnectionNetwork(link);
 	}
 
 	/**
@@ -189,30 +172,62 @@ public class NetworkController {
 		model.getConnectionNetworks().remove(link);
 	}
 
+	/**
+	 * Return links of the model
+	 * @return link
+	 */
 	public Collection<Link> getLinks(){
 		return model.getConnectionNetworks();
 	}
 
+	/**
+	 * Add Connection to the model
+	 * @param connection connection to be added
+	 */
 	public void addConnection(Connection connection){
-		model.addConnection(connection);
+		if(connection!=null)
+			model.addConnection(connection);
 	}
 
+	/**
+	 * Remove the connection from the model
+	 * @param connection connection to be removed
+	 */
 	public void removeConnection(Connection connection){
 		model.getConnections().remove(connection);
 	}
 
+	/**
+	 * Returns connections of the model
+	 * @return connections
+	 */
 	public Collection<Connection> getConnections(){
 		return model.getConnections();
 	}
-
-	public void addLinkToDevice(Link newLink, SmartDevice smartDevice) {
-		if(!newLink.getDevices().contains(smartDevice)){
-			newLink.addDevice(smartDevice);
-			smartDevice.addLink(newLink);
+	
+	/**
+	 * Adds the smartDevice to the link
+	 * @param link link to be added to the device
+	 * @param smartDevice device to be added to the link
+	 */
+	public void addLinkToDevice(Link link, SmartDevice smartDevice) {
+		if(link == null || smartDevice == null)return;
+		
+		if(!link.getDevices().contains(smartDevice)){
+			link.addDevice(smartDevice);
+		}
+		
+		if(!smartDevice.getLinks().contains(smartDevice)){
+			smartDevice.addLink(link);
 		}
 		
 	}
 
+	/**
+	 * Remove the smartDevice from the link
+	 * @param link link, the device should be removed from
+	 * @param smartDevice device which should be removed
+	 */
 	public void removeLinkFromDevice(Link link, SmartDevice smartDevice) {
 		if(smartDevice!=null)
 			smartDevice.removeLink(link);
@@ -312,27 +327,46 @@ public class NetworkController {
 
 	/**
 	 * Changes the link of the given connection to the new link. Returns true if it was successfully changed, false if it could not be changed.
-	 * @param connection
-	 * @param newLink
+	 * @param connection connection which should be edited
+	 * @param link new link
 	 * @return true on successful change, false on failure & restore
 	 */
-	public boolean changeLinkOfConnection(Connection connection, Link newLink) {
-		if(newLink != null){
-			connection.getLink().removeConnection(connection);
-			connection.setLink(newLink);
-			for(Port p:connection.getParticipants()){
-				if(p.getOwner()!=null&& !newLink.getDevices().contains(p.getOwner())){
-					addLinkToDevice(newLink, p.getOwner());
-				}
-			}
-			connection.setLink(newLink);
-			newLink.addConnection(connection);
+	public boolean changeLinkOfConnection(Connection connection, Link link) {
+		if(connection !=null && link != null){
+			addConnectionToLink(connection, link);
 			return true;
 		}else{
 			return false;
 		}
 	}
 
+	/**
+	 * Adds the given connection to the link
+	 * @param connection connection to be added
+	 * @param link link the connection should be added to
+	 */
+	public void addConnectionToLink(Connection connection, Link link) {
+		if(connection == null || link == null)return;
+		/**
+		 * Remove connection from Old Link
+		 */
+		if(connection.getLink()!=null)
+			removeConnectionFromLink(connection, connection.getLink());
+		/**
+		 * Add Connection to new Link
+		 */
+		connection.setLink(link);
+		link.addConnection(connection);
+		/**
+		 * 
+		 */
+		for(Port p:connection.getParticipants()){
+			if(p!=null && !link.getDevices().contains(p.getOwner())){
+				addLinkToDevice(link, p.getOwner());
+			}
+		}
+	}
+
 	/**
 	 * Changes the type of the connection to the new Type and updates all references
 	 * @param connection connection to be updated
@@ -494,7 +528,7 @@ public class NetworkController {
 		}catch(Exception e){
 			return null;
 		}
-		if (newLink == null) {
+		if (newLink == null || !(newLink instanceof Link)) {
 			return null;
 		}else {
 			// Set old Name
@@ -510,6 +544,11 @@ public class NetworkController {
 				removeLinkFromDevice(oldLink, device);
 				addLinkToDevice(newLink, device);
 			}
+			//Connection to the new Link
+			for(Connection c: new LinkedList<Connection>(oldLink.getConnections())){
+				removeConnectionFromLink(c, oldLink);
+				addConnectionToLink(c, newLink);
+			}
 		return newLink;
 		}
 	}

+ 195 - 184
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/ConnectionCreationPanel.java

@@ -59,7 +59,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 	private Connection connection;
 
 	/**
-	 * Index of the disconnected option in the boxes Array
+	 * Index of the disconnected option in the cmbPortRoles Array
 	 */
 	private int disconnectedIndex;
 
@@ -86,12 +86,12 @@ public class ConnectionCreationPanel extends JScrollPane {
 	/**
 	 * ComboBoxes to allow role changes of the different Ports
 	 */
-	private JComboBox<String>[] boxes;
+	private JComboBox<String>[] cmbPortRoles;
 
 	/**
 	 * ComboBox for the status
 	 */
-	JComboBox<String> cmbStatus;
+	private JComboBox<String> cmbStatus;
 
 	/**
 	 * True if an existing connection is being edited, or false if a new one is
@@ -120,7 +120,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 	/**
 	 * Mutex for the protocol change, so the different Devices don't change
 	 * their role, while their comboBoxes are updated. True if the protocol
-	 * change is in progress and the boxes should not update their role.
+	 * change is in progress and the cmbPortRoles should not update their role.
 	 */
 	private boolean protocolChange = false;
 	/**
@@ -167,8 +167,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 		initializePanel();
 	}
 
-	public ConnectionCreationPanel(Collection<Port> ports, Link l,
-			Controller controller) {
+	public ConnectionCreationPanel(Collection<Port> ports, Link l, Controller controller) {
 		this.controller = controller;
 		this.network = controller.getNetworkController();
 		connection = new ConnectionPerformance(l, new MQTT_protocol());
@@ -228,50 +227,45 @@ public class ConnectionCreationPanel extends JScrollPane {
 		content.add(lblSelectedLink);
 
 		// Check if model contains link
-		if (connection.getLink() != null
-				&& !network.getLinks().contains(connection.getLink()))
+		if (connection.getLink() != null && !network.getLinks().contains(connection.getLink()))
 			network.addLink(connection.getLink());
 
 		cmbSelectedLink = new JComboBox<String>();
 		cmbSelectedLink.setBounds(155, 40, 125, 20);
 		content.add(cmbSelectedLink);
 
-		cmbSelectedLink
-				.addActionListener(a -> {
-					if (mutex) {
-						return;
-					}
-					int cmbSelectedLinkIndex = cmbSelectedLink
-							.getSelectedIndex();
-					if (cmbSelectedLinkIndex == -1)
-						return;
-					Link newLink = null;
-					Iterator<Link> linkIterator = network.getLinks()
-							.iterator();
-					for (int i = 0; i < cmbSelectedLinkIndex
-							&& linkIterator.hasNext(); i++) {
-						linkIterator.next();
-					}
-					if (linkIterator.hasNext())
-						newLink = linkIterator.next();
-					if (!network.changeLinkOfConnection(connection, newLink)) {
-						// if LinkChange failed: Restore
-						cmbSelectedLink.setSelectedIndex(lastLinkIndex);
-					} else {
-						lastLinkIndex = cmbSelectedLinkIndex;
-					}
+		cmbSelectedLink.addActionListener(a -> {
+			if (mutex) {
+				return;
+			}
+			int cmbSelectedLinkIndex = cmbSelectedLink.getSelectedIndex();
+			if (cmbSelectedLinkIndex == -1)
+				return;
+			Link newLink = null;
+			Iterator<Link> linkIterator = network.getLinks().iterator();
+			for (int i = 0; i < cmbSelectedLinkIndex && linkIterator.hasNext(); i++) {
+				linkIterator.next();
+			}
+			if (linkIterator.hasNext())
+				newLink = linkIterator.next();
+			if (!network.changeLinkOfConnection(connection, newLink)) {
+				// if LinkChange failed: Restore
+				cmbSelectedLink.setSelectedIndex(lastLinkIndex);
+			} else {
+				lastLinkIndex = cmbSelectedLinkIndex;
+			}
 
-				});
+		});
 
 		JButton btnCreateLink = new JButton("Create Link");
 		btnCreateLink.setBounds(290, 40, 155, 20);
 		content.add(btnCreateLink);
 		btnCreateLink.addActionListener(a -> {
-			new LinkCreationDialog(connection.getParticipants().stream()
-					.map(lElem -> lElem.getOwner())
-					.collect(Collectors.toList()), controller, content);
+			new LinkCreationDialog(
+					connection.getParticipants().stream().map(lElem -> lElem.getOwner()).collect(Collectors.toList()),
+					controller, content);
 			// TODO: Refresh
-			});
+		});
 
 		JLabel lblConnectionType = new JLabel("Connection type:");
 		lblConnectionType.setHorizontalAlignment(SwingConstants.RIGHT);
@@ -289,8 +283,8 @@ public class ConnectionCreationPanel extends JScrollPane {
 			cmbConnection.getSelectedIndex();
 			int selectedIndex = cmbConnection.getSelectedIndex();
 			if (lastConnectionIndex != selectedIndex && selectedIndex != -1) {
-				Connection newConnection = network.changeConnectionType(
-						connection, availableConnection.get(selectedIndex));
+				Connection newConnection = network.changeConnectionType(connection,
+						availableConnection.get(selectedIndex));
 				if (newConnection == null) {
 					System.out.println("Warning invalidConnection changed");
 					cmbConnection.setSelectedIndex(lastConnectionIndex);
@@ -308,8 +302,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 		btnImportConnection.setBounds(290, 70, 155, 20);
 		content.add(btnImportConnection);
 		btnImportConnection.addActionListener(a -> {
-			ImportPopUp<Connection> popUp = new ImportPopUp<Connection>(this,
-					Connection.class);
+			ImportPopUp<Connection> popUp = new ImportPopUp<Connection>(this, Connection.class);
 			try {
 				Class<? extends Connection> imported = popUp.showPopUp();
 				if (imported == null)
@@ -317,12 +310,10 @@ public class ConnectionCreationPanel extends JScrollPane {
 				if (controller.getImportController().addConnection(imported)) {
 					refreshGUI();
 				} else {
-					JOptionPane.showMessageDialog(frame,
-							"Import failed: Invalid Connection");
+					JOptionPane.showMessageDialog(frame, "Import failed: Invalid Connection");
 				}
 			} catch (Exception e1) {
-				JOptionPane.showMessageDialog(frame,
-						"Import failed: " + e1.getMessage());
+				JOptionPane.showMessageDialog(frame, "Import failed: " + e1.getMessage());
 			}
 		});
 
@@ -339,9 +330,9 @@ public class ConnectionCreationPanel extends JScrollPane {
 		cmbProtocolType.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				if(mutex)
+				if (mutex)
 					return;
-				
+
 				/**
 				 * Old Protocol
 				 */
@@ -355,16 +346,14 @@ public class ConnectionCreationPanel extends JScrollPane {
 				Protocol newProtocol = null;
 				try {
 					// Create new Instance of the protocol
-					newProtocol = controller.getImportController().getProtocols().get(
-							cmbProtocolType.getSelectedIndex()).newInstance();
+					newProtocol = controller.getImportController().getProtocols()
+							.get(cmbProtocolType.getSelectedIndex()).newInstance();
 				} catch (InstantiationException | IllegalAccessException e1) {
-					System.out
-							.println("WARNING: Protocol could not be initialized");
+					System.out.println("WARNING: Protocol could not be initialized");
 				}
 				if (newProtocol == null) {
 					cmbProtocolType.setSelectedIndex(lastProtocolIndex);
-					System.out
-							.println("WARNING: Invalid Protocol Selected - restore last index");
+					System.out.println("WARNING: Invalid Protocol Selected - restore last index");
 				} else if (connection.setProtocol(newProtocol)) {
 					/**
 					 * New Roles as Strings
@@ -381,33 +370,29 @@ public class ConnectionCreationPanel extends JScrollPane {
 					// Update lastProtocolIndex to new Index
 					lastProtocolIndex = cmbProtocolType.getSelectedIndex();
 					// Update Boxes
-					for (int i = 0; i < boxes.length; i++) {
+					for (int i = 0; i < cmbPortRoles.length; i++) {
 						/**
 						 * The previous selected Index
 						 */
-						int oldSelected = boxes[i].getSelectedIndex();
+						int oldSelected = cmbPortRoles[i].getSelectedIndex();
 						// Update Labels of the current Box
-						boxes[i].removeAllItems();
+						cmbPortRoles[i].removeAllItems();
 						for (int j = 0; j < newRoles.length; j++)
-							boxes[i].addItem(newRoles[j]);
-						boxes[i].addItem("Disconnected");
+							cmbPortRoles[i].addItem(newRoles[j]);
+						cmbPortRoles[i].addItem("Disconnected");
 						// Try to update the Roles
-						if (oldSelected < 0 || oldSelected >= disconnectedIndex
-								|| oldSelected == oldDisconnected) {
+						if (oldSelected < 0 || oldSelected >= disconnectedIndex || oldSelected == oldDisconnected) {
 							// Invalid Index -> Disconnected
-							network.removeDeviceFromConnection(ports[i],
-									connection);
-							boxes[i].setSelectedIndex(disconnectedIndex);
+							network.removeDeviceFromConnection(ports[i], connection);
+							cmbPortRoles[i].setSelectedIndex(disconnectedIndex);
 						} else {
-							if (network.addDeviceToConnection(ports[i],
-									connection, oldSelected)) {
+							if (network.addDeviceToConnection(ports[i], connection, oldSelected)) {
 								// Set to Box to display role
-								boxes[i].setSelectedIndex(oldSelected);
+								cmbPortRoles[i].setSelectedIndex(oldSelected);
 							} else {
 								// Could not be added -> Disconnected
-								network.removeDeviceFromConnection(ports[i],
-										connection);
-								boxes[i].setSelectedIndex(disconnectedIndex);
+								network.removeDeviceFromConnection(ports[i], connection);
+								cmbPortRoles[i].setSelectedIndex(disconnectedIndex);
 							}
 						}
 					}
@@ -423,19 +408,17 @@ public class ConnectionCreationPanel extends JScrollPane {
 		btnImportProtocol.setBounds(290, 100, 155, 20);
 		content.add(btnImportProtocol);
 		btnImportProtocol.addActionListener(a -> {
-				ImportPopUp<Protocol> popUp = new ImportPopUp<Protocol>(this,
-						Protocol.class);
+			ImportPopUp<Protocol> popUp = new ImportPopUp<Protocol>(this, Protocol.class);
 
-				Class<? extends Protocol> imported = null;
-				try {
-					imported = popUp.showPopUp();
-				} catch (Exception e1) {
-					JOptionPane.showMessageDialog(frame,
-							"Import failed: " + e1.getMessage());
-					return;
-				}
-				if (imported == null) {
-					// Import cancelled
+			Class<? extends Protocol> imported = null;
+			try {
+				imported = popUp.showPopUp();
+			} catch (Exception e1) {
+				JOptionPane.showMessageDialog(frame, "Import failed: " + e1.getMessage());
+				return;
+			}
+			if (imported == null) {
+				// Import cancelled
 				return;
 			}
 			// Add Protocol
@@ -443,8 +426,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 				// Update GUI
 				refreshGUI();
 			} else {
-				JOptionPane.showMessageDialog(frame,
-						"Import failed: Invalid Protocol");
+				JOptionPane.showMessageDialog(frame, "Import failed: Invalid Protocol");
 				return;
 			}
 		});
@@ -452,47 +434,44 @@ public class ConnectionCreationPanel extends JScrollPane {
 		JLabel lblPacketlossrate = new JLabel("PacketLossRate:");
 		lblPacketlossrate.setHorizontalAlignment(SwingConstants.RIGHT);
 		lblPacketlossrate.setBounds(10, 130, 140, 20);
-		lblPacketlossrate
-				.setToolTipText("Probability of Packet loss, should be a Double in [0.0,1.0]");
+		lblPacketlossrate.setToolTipText("Probability of Packet loss, should be a Double in [0.0,1.0]");
 		content.add(lblPacketlossrate);
 
 		tfPacketLossRate = new JTextField();
 		tfPacketLossRate.setBounds(155, 130, 90, 20);
 		content.add(tfPacketLossRate);
 		tfPacketLossRate.setColumns(10);
-		tfPacketLossRate.getDocument().addDocumentListener(
-				new DocumentListener() {
+		tfPacketLossRate.getDocument().addDocumentListener(new DocumentListener() {
 
-					@Override
-					public void removeUpdate(DocumentEvent e) {
-						updateConnectionAndField();
-					}
+			@Override
+			public void removeUpdate(DocumentEvent e) {
+				updateConnectionAndField();
+			}
 
-					@Override
-					public void insertUpdate(DocumentEvent e) {
-						updateConnectionAndField();
-					}
+			@Override
+			public void insertUpdate(DocumentEvent e) {
+				updateConnectionAndField();
+			}
 
-					@Override
-					public void changedUpdate(DocumentEvent e) {
-						updateConnectionAndField();
-					}
+			@Override
+			public void changedUpdate(DocumentEvent e) {
+				updateConnectionAndField();
+			}
 
-					private void updateConnectionAndField() {
-						try {
-							double newRate = Double
-									.parseDouble(tfPacketLossRate.getText());
-							if (newRate >= 0.0 && newRate <= 1.0) {
-								connection.setPacketLossProbability(newRate);
-								tfPacketLossRate.setBackground(Color.WHITE);
-							} else {
-								tfPacketLossRate.setBackground(Color.RED);
-							}
-						} catch (Exception exception) {
-							tfPacketLossRate.setBackground(Color.RED);
-						}
+			private void updateConnectionAndField() {
+				try {
+					double newRate = Double.parseDouble(tfPacketLossRate.getText());
+					if (newRate >= 0.0 && newRate <= 1.0) {
+						connection.setPacketLossProbability(newRate);
+						tfPacketLossRate.setBackground(Color.WHITE);
+					} else {
+						tfPacketLossRate.setBackground(Color.RED);
 					}
-				});
+				} catch (Exception exception) {
+					tfPacketLossRate.setBackground(Color.RED);
+				}
+			}
+		});
 
 		JLabel lblStatus = new JLabel("Status:");
 		lblStatus.setHorizontalAlignment(SwingConstants.RIGHT);
@@ -511,8 +490,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 				if (mutex) {
 					cmbStatus.setSelectedIndex(connection.getStatus());
 				}
-				if (cmbStatus.getSelectedIndex() < 0
-						|| cmbStatus.getSelectedIndex() > 4)
+				if (cmbStatus.getSelectedIndex() < 0 || cmbStatus.getSelectedIndex() > 4)
 					cmbStatus.setSelectedIndex(connection.getStatus());
 				else
 					connection.setStatus((byte) cmbStatus.getSelectedIndex());
@@ -527,43 +505,86 @@ public class ConnectionCreationPanel extends JScrollPane {
 		btnCreate.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
+				/**
+				 * Validate Packet loss rate
+				 */
+				try{
+					double d = Double.parseDouble(tfPacketLossRate.getText());
+					if(d < 0.0 && 1.0 < d)
+						throw new Exception("Packet loss rate out of bounds");
+					connection.setPacketLossProbability(d);
+				}catch(Exception e1){
+					JOptionPane.showMessageDialog(frame, "Invalid PacketsLossProbability, should be double between 0.0 and 1.0", "Invalid Packetloss Rate",
+							JOptionPane.WARNING_MESSAGE);
+					return;
+				}
+				/**
+				 * Validate Status
+				 */
+				if(cmbStatus.getSelectedIndex()<0){
+					JOptionPane.showMessageDialog(frame, "Status not set", "Status invalid",
+							JOptionPane.WARNING_MESSAGE);
+					return;
+				}
+				connection.setStatus((byte)cmbStatus.getSelectedIndex());
+				
+				/**
+				 * Link should be set and contain connection
+				 */
+				if (connection.getLink() == null) {
+					JOptionPane.showMessageDialog(frame, "Link should not be null.", "Invalid Link Selected",
+							JOptionPane.WARNING_MESSAGE);
+					return;
+				} else if (!connection.getLink().getConnections().contains(connection)){
+					network.addConnectionToLink(connection, connection.getLink());
+				}
+				
+				if(tfName.getText()!=null)
+					connection.setName(tfName.getText());
+				
+				/**
+				 * Check each Port/Device
+				 */
+				for (int i = 0; i < ports.length; i++) {
+					//Add Device to Link
+					if (connection.getProtocol().getDevices().contains(ports[i])) {
+						/**
+						 * If Link part of protocol & connection
+						 */
+						if (!ports[i].getOwner().getLinks().contains(connection.getLink()))
+							network.addLinkToDevice(connection.getLink(), ports[i].getOwner());
+						if (!connection.getParticipants().contains(ports[i]))
+							connection.addSmartDevice(ports[i]);
+						if (ports[i].getConnection() != connection)
+							ports[i].setConnection(connection);
+						if (!ports[i].getOwner().getPorts().contains(ports[i]))
+							ports[i].getOwner().addPort(ports[i]);
+					} else {
+						/**
+						 * Else remove from COnnection
+						 */
+						network.removeDeviceFromConnection(ports[i], connection);
+						if (ports[i].getOwner().getPorts().contains(ports[i]))
+							ports[i].getOwner().removePort(ports[i]);
 
-				if (!edit) {
-					if (connection.getLink()!=null && !connection.getLink().getConnections()
-							.contains(connection))
-						connection.getLink().addConnection(connection);
-					for (int i = 0; i < ports.length; i++) {
-						if (!ports[i].getOwner().getLinks()
-								.contains(connection.getLink()))
-							ports[i].getOwner().addLink(connection.getLink());
-						if (connection.getProtocol().getDevices()
-								.contains(ports[i])) {
-							if (!connection.getParticipants()
-									.contains(ports[i]))
-								connection.addSmartDevice(ports[i]);
-							if (ports[i].getConnection() != connection)
-								ports[i].setConnection(connection);
-							if (!ports[i].getOwner().getPorts()
-									.contains(ports[i]))
-								ports[i].getOwner().addPort(ports[i]);
-						} else {
-							if (connection.getParticipants().contains(ports[i]))
-								connection.removeSmartDevice(ports[i]);
-							if (ports[i].getConnection() != null)
-								ports[i].setConnection(null);
-							if (ports[i].getOwner().getPorts()
-									.contains(ports[i]))
-								ports[i].getOwner().removePort(ports[i]);
-
-						}
 					}
-					if (!network.getLinks().contains(connection.getLink()))
-						network.addLink(connection.getLink());
-					if (!network.getConnections().contains(connection))
-						network.addConnection(connection);
 				}
-
-				connection.setName(tfName.getText());
+				
+				/**
+				 * Connection should be in the model
+				 */
+				if(!network.getConnections().contains(connection))
+					network.addConnection(connection);
+				/**
+				 * Link should be in the model
+				 */
+				if(!network.getLinks().contains(connection.getLink())){
+					network.addLink(connection.getLink());
+				}
+				
+				/**
+				 * Close Window
+				 */
 				content.setVisible(false);
 				setVisible(false);
 				if (frame != null) {
@@ -579,32 +600,30 @@ public class ConnectionCreationPanel extends JScrollPane {
 		 */
 		int currentHeight = 190;
 
-		boxes = new JComboBox[ports.length];
+		cmbPortRoles = new JComboBox[ports.length];
 		for (int i = 0; i < ports.length; i++) {
 			// Effectively final variables for the action listener
 			int pos = i;
 			Port currentPort = ports[i];
 			SmartDevice currentDevice = currentPort.getOwner();
 
-			JLabel lblDevice = new JLabel(currentDevice.getName() + ":"
-					+ currentPort.getPortNumber() + ":");
+			JLabel lblDevice = new JLabel(currentDevice.getName() + ":" + currentPort.getPortNumber() + ":");
 			lblDevice.setHorizontalAlignment(SwingConstants.RIGHT);
 			lblDevice.setBounds(10, currentHeight, 230, 18);
 			content.add(lblDevice);
 
-			boxes[pos] = new JComboBox<String>();
+			cmbPortRoles[pos] = new JComboBox<String>();
 			for (String role : connection.getProtocol().getRoles())
-				boxes[pos].addItem(role);
-			boxes[pos].addItem("Disconnected");
-			boxes[pos].setBounds(250, currentHeight, 240, 18);
-			content.add(boxes[pos]);
+				cmbPortRoles[pos].addItem(role);
+			cmbPortRoles[pos].addItem("Disconnected");
+			cmbPortRoles[pos].setBounds(250, currentHeight, 240, 18);
+			content.add(cmbPortRoles[pos]);
 			if (edit) {
-				int roleOfDevice = connection.getProtocol().getRoleOfDevice(
-						currentPort);
+				int roleOfDevice = connection.getProtocol().getRoleOfDevice(currentPort);
 				if (roleOfDevice != -1)
-					boxes[pos].setSelectedIndex(roleOfDevice);
+					cmbPortRoles[pos].setSelectedIndex(roleOfDevice);
 				else
-					boxes[pos].setSelectedIndex(disconnectedIndex);
+					cmbPortRoles[pos].setSelectedIndex(disconnectedIndex);
 			} else {
 				/**
 				 * Number of tries to add the Port
@@ -612,28 +631,24 @@ public class ConnectionCreationPanel extends JScrollPane {
 				int n = 5;
 				for (int j = 0; j <= n; j++) {
 					if (j == 0) {// Try to add Router first
-						if (network.addDeviceToConnection(currentPort,
-								connection, 0)) {
-							boxes[pos].setSelectedIndex(0);
+						if (network.addDeviceToConnection(currentPort, connection, 0)) {
+							cmbPortRoles[pos].setSelectedIndex(0);
 							break;
 						}
 					} else if (j == n) {// If it could not be added
-						boxes[pos].setSelectedIndex(disconnectedIndex);
-						network.removeDeviceFromConnection(currentPort,
-								connection);
+						cmbPortRoles[pos].setSelectedIndex(disconnectedIndex);
+						network.removeDeviceFromConnection(currentPort, connection);
 						break;
 					} else {
-						int randomRole = ThreadLocalRandom.current().nextInt(0,
-								disconnectedIndex);
-						if (network.addDeviceToConnection(currentPort,
-								connection, randomRole)) {
-							boxes[pos].setSelectedIndex(randomRole);
+						int randomRole = ThreadLocalRandom.current().nextInt(0, disconnectedIndex);
+						if (network.addDeviceToConnection(currentPort, connection, randomRole)) {
+							cmbPortRoles[pos].setSelectedIndex(randomRole);
 							break;
 						}
 					}
 				}
 			}
-			boxes[pos].addActionListener(new ActionListener() {
+			cmbPortRoles[pos].addActionListener(new ActionListener() {
 
 				@Override
 				public void actionPerformed(ActionEvent e) {
@@ -644,19 +659,17 @@ public class ConnectionCreationPanel extends JScrollPane {
 					 * Selected Index of the ComboBox, which represents the new
 					 * role (or disconnected)
 					 */
-					int selected = boxes[pos].getSelectedIndex();
+					int selected = cmbPortRoles[pos].getSelectedIndex();
 					/**
 					 * True if role was successfully changed
 					 */
-					boolean successfullChange = network.changeRoleOfDevice(
-							connection.getProtocol(), connection, ports[pos],
-							selected);
+					boolean successfullChange = network.changeRoleOfDevice(connection.getProtocol(), connection,
+							ports[pos], selected);
 					/**
 					 * Set to Disconnected, if role could not be changed
 					 */
 					if (!successfullChange) {
-						boxes[pos].setSelectedIndex(connection.getProtocol()
-								.getNumberOfRoles());
+						cmbPortRoles[pos].setSelectedIndex(connection.getProtocol().getNumberOfRoles());
 					}
 				}
 			});
@@ -720,17 +733,15 @@ public class ConnectionCreationPanel extends JScrollPane {
 		cmbProtocolType.removeAllItems();
 		for (int i = 0; i < availableProtocols.size(); i++)
 			try {
-				cmbProtocolType.addItem(availableProtocols.get(i).newInstance()
-						.getName());
+				cmbProtocolType.addItem(availableProtocols.get(i).newInstance().getName());
 			} catch (InstantiationException | IllegalAccessException e1) {
 				System.out.println("Protocol " + i + " is invalid");
 				cmbProtocolType.addItem("unknown");
 			}
 		// Set Index to selected Protocol
 		lastProtocolIndex = -1;
-		for (int i = 0; i < availableProtocols.size(); i++){
-			if (connection.getProtocol().getClass()
-					.equals(availableProtocols.get(i))) {
+		for (int i = 0; i < availableProtocols.size(); i++) {
+			if (connection.getProtocol().getClass().equals(availableProtocols.get(i))) {
 				// Select the right protocol and save last index
 				lastProtocolIndex = i;
 			}

+ 0 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/NetworkTreeWindow.java

@@ -14,7 +14,6 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  */
 public class NetworkTreeWindow extends JFrame {
 
-	
 	/**
 	 * Serial Version number
 	 */