Browse Source

Enables creation of SimpleConnections by rightClickDragging

Fixes Connections to itself and nullpointer in terminated connections
Andreas T. Meyer-Berg 5 years ago
parent
commit
15c3ed2a9e

+ 3 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/Controller.java

@@ -192,6 +192,8 @@ public class Controller {
 		// Remove from Links
 		for (Link link : toDelete.getLinks())
 			removeSmartDeviceFromLink(toDelete, link);
+		// Remove Links from Device
+		toDelete.getLinks().clear();
 
 		// Delete Connections
 		for (Connection c : toDelete.getConnections()) {
@@ -214,6 +216,6 @@ public class Controller {
 	 * @param link the Link, which contains the SmartDevice
 	 */
 	public void removeSmartDeviceFromLink(SmartDevice toRemove, Link link) {
-		toRemove.removeLink(link);
+		link.removeDevice(toRemove);
 	}
 }

+ 1 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SmartDevice.java

@@ -43,7 +43,7 @@ public class SmartDevice {
 	private int x;
 
 	/**
-	 * Positon on the y-Axis
+	 * Position on the y-Axis
 	 */
 	private int y;
 
@@ -100,7 +100,6 @@ public class SmartDevice {
 		if (link == null)
 			return;
 		links.remove(link);
-		link.removeDevice(this);
 	}
 
 	/**

+ 3 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/SimpleConnection.java

@@ -75,6 +75,8 @@ public class SimpleConnection implements Connection {
 	}
 	@Override
 	public void removeSmartDevice(SmartDevice sd) {
+		//of source == null - connection was already terminated
+		if(source == null)return;
 		srcOfTermination = sd;
 		other = sd == source? destination : source;
 		source = null;
@@ -83,7 +85,7 @@ public class SimpleConnection implements Connection {
 	
 	@Override
 	public Collection<Packet> getTerminationPackages(long startTime) {
-		return new LinkedList<Packet>(Arrays.asList((Packet)new SimplePacket(startTime, srcOfTermination, other)));
+		return new LinkedList<Packet>(Arrays.asList((Packet)new SimplePacket(startTime, srcOfTermination, other,"Terminated")));
 	}
 
 }

+ 11 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/SimplePacket.java

@@ -44,7 +44,7 @@ public class SimplePacket extends Packet {
 		this.time = time;
 		this.source = source;
 		this.destination = destination;
-		this.payload = null;
+		this.payload = "";
 	}
 
 	/**
@@ -74,8 +74,16 @@ public class SimplePacket extends Packet {
 
 	@Override
 	public String getTextualRepresentation() {
-		return "[SimplePacket:" + payload + " time-" + time + ";source:" + source.getName() + ";dest:"
-				+ destination.getName() + "]";
+		return this.toString();
+	}
+	
+	@Override
+	public String toString() {
+		String destName = destination == null ? "null" : destination.getName();
+		String srcName = source == null ? "null" : source.getName();
+			
+		return "[SimplePacket:" + payload + " time-" + time + ";source:" + srcName + ";dest:"
+				+ destName + "]";
 	}
 
 	@Override

+ 69 - 15
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationInteractor.java

@@ -16,11 +16,14 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleConnection;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
 
 /**
  * Listener which detects User Interaction with the {@link VisualisationPanel},
  * stores interaction information, triggers the controller and helps
- * visualisation of the interaction
+ * visualization of the interaction
  * 
  * @author Andreas T. Meyer-Berg
  */
@@ -77,6 +80,7 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 	 */
 	private SmartDevice clicked = null;
 
+	public SmartDevice connectionFrom = null;
 	
 	/**
 	 * Creates a new VisualisationInteractor
@@ -130,9 +134,12 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 
 	@Override
 	public void mouseClicked(MouseEvent e) {
+		//Finish operations
 		if(dragged!=null){
 			finishDrag();
 		}
+		connectionFrom =null;
+		
 		/*
 		 * FindSmartDevice that was clicked
 		 */
@@ -153,7 +160,7 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 	}
 
 	/**
-	 * Shows the RightClick Menu on the Visualisation Panel, with Options for the given SmartDevice clickedOn, 
+	 * Shows the RightClick Menu on the Visualization Panel, with Options for the given SmartDevice clickedOn, 
 	 * if clickedOn is null, the RightClick Menu contains Options for creation of new SmartDevices.
 	 * The Menu will be shown at the Mouse position on the VisualisationPanel. 
 	 * @param clickedOn Device which was clicked, null if no
@@ -183,24 +190,27 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 	@Override
 	public void mousePressed(MouseEvent e) {
 		// Check if SmartDevice was clicked
-		for (SmartDevice d : model.getDevices()) {
-			if (Math.abs(d.getX() - e.getX()) < panel.getVisualisationRadius()
-					&& Math.abs(d.getY() - e.getY()) < panel.getVisualisationRadius()) {
-				// Recognize possible drag operation
-				if (e.getButton() == MouseEvent.BUTTON1) {
-					dragged = d;
-					dragged_x = d.getX();
-					dragged_y = d.getY();
-				}
-			}
+		SmartDevice pressedOn = getSmartDeviceAtPosition(e.getX(), e.getY());
+		if(pressedOn == null)return;
+		
+		// Recognize possible drag operation
+		if (e.getButton() == MouseEvent.BUTTON1 && connectionFrom == null) {
+			dragged = pressedOn;
+			dragged_x = pressedOn.getX();
+			dragged_y = pressedOn.getY();
+		} else if(e.getButton() == MouseEvent.BUTTON3 && dragged == null){
+			connectionFrom = pressedOn;
 		}
 	}
 
 	@Override
 	public void mouseReleased(MouseEvent e) {
 		// Finish drag operation
-		if (e.getButton() == MouseEvent.BUTTON1)
+		//if (e.getButton() == MouseEvent.BUTTON1)
 			finishDrag();
+		//else if(e.getButton() == MouseEvent.BUTTON3){
+			finishConnectionCreation();
+		//}
 	}
 
 	@Override
@@ -234,8 +244,8 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 			dragged_y = model.getHeight() - panel.getVisualisationRadius();
 		else
 			dragged_y = e.getY();
-		// repaint the dragged smartDevice
-		if (dragged != null)
+		// repaint the dragged smartDevice or new connection
+		if (dragged != null || connectionFrom != null)
 			panel.repaint();
 	}
 
@@ -254,4 +264,48 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 			dragged = null;
 		}
 	}
+	
+	/**
+	 * Finishes the create connection operation and open a PopUp for Link/Connection creation
+	 */
+	private void finishConnectionCreation() {
+		/**
+		 * SmartDevice the connection was 
+		 */
+		SmartDevice connectionTo = getSmartDeviceAtPosition(dragged_x, dragged_y);
+		if(connectionFrom != null && connectionTo != null && connectionFrom != connectionTo){
+			//Create new Connection
+			Link l = new SimpleLink("Ethernet: "+connectionFrom.getName()+" to "+connectionTo.getName());
+			l.addDevice(connectionFrom);
+			l.addDevice(connectionTo);
+			Connection c = new SimpleConnection(connectionFrom, connectionTo, l, new SimpleProtocol(connectionFrom, connectionTo));
+			connectionTo.addLink(l);
+			connectionTo.addConnection(c);
+			connectionFrom.addLink(l);
+			connectionFrom.addConnection(c);
+			connectionFrom = null;
+		}else{
+			connectionFrom = null;
+		}
+		panel.repaint();
+	}
+	
+	/**
+	 * Returns SmartDevice which is at position (x,y) or within its visualization radius.
+	 * Returns null, if no SmartDevice is within the range.
+	 * 
+	 * @param x x-position which should be checked
+	 * @param y y-position which should be checked
+	 * @return {@link SmartDevice} at position (x,y) or null if there is no
+	 */
+	private SmartDevice getSmartDeviceAtPosition(int x, int y) {
+		//Check is device is inside visualization radius
+		for (SmartDevice d : model.getDevices()) {
+			if (Math.abs(d.getX() - x) < panel.getVisualisationRadius()
+					&& Math.abs(d.getY() - y) < panel.getVisualisationRadius()) {
+				return d;
+			}
+		}
+		return null;
+	}
 }

+ 4 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationPanel.java

@@ -90,7 +90,7 @@ public class VisualisationPanel extends JPanel {
 		g.fillRect(0, 0, this.getWidth(), this.getHeight());
 
 		paintConnections(g);
-
+		
 		paintDevices(g);
 	}
 
@@ -171,6 +171,9 @@ public class VisualisationPanel extends JPanel {
 					}
 				}
 			}
+		//paint new in progress connection, if a connection or link is in creation
+		if(interactor.connectionFrom != null)
+			g.drawLine(interactor.connectionFrom.getX(), interactor.connectionFrom.getY(), interactor.dragged_x, interactor.dragged_y);
 	}
 
 	/**