Explorar el Código

Refactors RightClickMenu Code, Fixes some small visualisation glitches

Andreas T. Meyer-Berg hace 6 años
padre
commit
60cd457fa9

+ 135 - 121
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationInteractor.java

@@ -42,7 +42,7 @@ public class VisualisationInteractor implements MouseInputListener,
 	/**
 	 * Controller to notify when the model changes
 	 */
-	private Controller control;
+	private Controller controller;
 
 	/**
 	 * Panel which is observed
@@ -142,115 +142,10 @@ public class VisualisationInteractor implements MouseInputListener,
 			VisualisationPanel panel) {
 		// Initialize the values
 		this.model = model;
-		this.control = controller;
+		this.controller = controller;
 		this.panel = panel;
 
-		this.rightClickMenu = new JPopupMenu();
-
-		// Create device option
-		itemCreate = new JMenuItem("Create SmartDevice");
-
-		itemCreate.addActionListener(e -> {
-			SmartDevice toEdit;
-			if(clicked == null){
-				toEdit = new SmartDevice("DeviceName");
-				toEdit.setX(dragged_x);
-				toEdit.setY(dragged_y);
-			}else{
-				toEdit = clicked;
-			}
-			SmartDeviceCreationPopUp popUp = new SmartDeviceCreationPopUp(toEdit, toEdit==clicked,control);
-			popUp.setLocationRelativeTo(panel);
-			popUp.setEnabled(true);
-			popUp.setVisible(true);
-			mode = NOTHING;
-
-		});
-
-		rightClickMenu.add(itemCreate);
-
-		// Delete device option
-		itemDelete = new JMenuItem("Delete");
-
-		itemDelete.addActionListener(e -> {
-			// Delete the clicked object
-				controller.deleteSmartDevice(clicked);
-				clicked = null;
-				controller.notifyObservers();
-				mode = NOTHING;
-			});
-
-		rightClickMenu.add(itemDelete);
-		
-		// Delete device option
-		itemDeleteSelected = new JMenuItem("Delete Selected");
-		
-		itemDeleteSelected.addActionListener(e -> {
-			// Delete the clicked object
-			for(SmartDevice c: selectedDevices)
-				controller.deleteSmartDevice(c);
-			selectedDevices.clear();
-			clicked = null;
-			controller.notifyObservers();
-			mode = NOTHING;
-		});
-		itemDeleteSelected.setEnabled(false);
-		rightClickMenu.add(itemDeleteSelected);
-				
-		/*
-		 * Add PrintDebug option
-		 */
-		itemDebug = new JMenuItem("Print NetworkState");
-		itemDebug.addActionListener(e -> {
-			// Print Links, Devices and Connections
-				for (Link l : model.getConnectionNetworks())
-					System.out.println("Link: " + l.getName());
-				for (SmartDevice d : model.getDevices()) {
-					System.out.println("Device: " + d.getName());
-					for (Port p : d.getPorts()) {
-						if(p==null){
-							System.out.println("Port: null");
-							continue;
-						}
-						if(p.getConnection()==null){
-							System.out.println("Connection: null");
-							continue;
-						}
-						switch (p.getConnection().getStatus()) {
-						case Connection.ACTIVE:
-							System.out.println("Connection: active");
-							break;
-						case Connection.FINISHED:
-						case Connection.TERMINATED:
-							System.out.println("Connection: terminated");
-
-							break;
-
-						case Connection.HALTED:
-						default:
-							break;
-						}
-					}
-				}
-				// Print Terminating Packages
-				LinkedList<Connection> terminated = new LinkedList<Connection>();
-
-				model.getConnections()
-						.stream()
-						.forEach(c -> {
-							if(c.getStatus() == Connection.FINISHED || c
-								.getStatus() == Connection.TERMINATED)
-								terminated.add(c);
-							for (Packet p : c.getTerminationPackages(1000))
-								System.out.println(p.getTextualRepresentation());
-						});
-				model.getConnections().removeAll(terminated);
-				controller.notifyObservers();
-				mode = NOTHING;
-			});
-		rightClickMenu.add(itemDebug);
-
-		this.panel.add(rightClickMenu);
+		initializeRightClickMenu();
 	}
 
 	@Override
@@ -416,14 +311,14 @@ public class VisualisationInteractor implements MouseInputListener,
 			int y_offset = dragged_y - dragged.getY();
 			//Validate for all moved devices, that they are within the model, if not change offset
 			for(SmartDevice d:selectedDevices){
-				if (d.getX() + x_offset <= control.getDevice_visualization_radius())
-					x_offset = control.getDevice_visualization_radius()-d.getX();
-				else if (d.getX() + x_offset >= model.getWidth() - control.getDevice_visualization_radius())
-					x_offset = model.getWidth() - control.getDevice_visualization_radius()-d.getX();
-				if (d.getY() + y_offset <= control.getDevice_visualization_radius())
-					y_offset = control.getDevice_visualization_radius()-d.getY();
-				else if (d.getY() + y_offset >= model.getHeight() - control.getDevice_visualization_radius())
-					y_offset = model.getHeight() - control.getDevice_visualization_radius()-d.getY();
+				if (d.getX() + x_offset <= controller.getDevice_visualization_radius())
+					x_offset = controller.getDevice_visualization_radius()-d.getX();
+				else if (d.getX() + x_offset >= model.getWidth() - controller.getDevice_visualization_radius())
+					x_offset = model.getWidth() - controller.getDevice_visualization_radius()-d.getX();
+				if (d.getY() + y_offset <= controller.getDevice_visualization_radius())
+					y_offset = controller.getDevice_visualization_radius()-d.getY();
+				else if (d.getY() + y_offset >= model.getHeight() - controller.getDevice_visualization_radius())
+					y_offset = model.getHeight() - controller.getDevice_visualization_radius()-d.getY();
 			}
 			//update the dragged position, if the offset changed
 			dragged_x =dragged.getX() + x_offset;
@@ -488,7 +383,7 @@ public class VisualisationInteractor implements MouseInputListener,
 				int x_offset = dragged_x-dragged.getX();
 				int y_offset = dragged_y-dragged.getY();
 				for(SmartDevice d: selectedDevices)
-					control.moveSmartDevice(d, d.getX()+x_offset, d.getY()+y_offset, d.getZ());
+					controller.moveSmartDevice(d, d.getX()+x_offset, d.getY()+y_offset, d.getZ());
 			}
 			dragged = null;
 			if(selectedDevices.isEmpty())
@@ -550,9 +445,12 @@ public class VisualisationInteractor implements MouseInputListener,
 			
 		}
 		connectionFrom = null;
-		mode = NOTHING;
+		if(selectedDevices.isEmpty())
+			mode = NOTHING;
+		else
+			mode = SELECTED;
 		panel.repaint();
-		control.notifyObservers();
+		controller.notifyObservers();
 	}
 
 	/**
@@ -569,8 +467,8 @@ public class VisualisationInteractor implements MouseInputListener,
 	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) < control.getDevice_visualization_radius()
-					&& Math.abs(d.getY() - y) < control.getDevice_visualization_radius()) {
+			if (Math.abs(d.getX() - x) < controller.getDevice_visualization_radius()
+					&& Math.abs(d.getY() - y) < controller.getDevice_visualization_radius()) {
 				return d;
 			}
 		}
@@ -618,4 +516,120 @@ public class VisualisationInteractor implements MouseInputListener,
 
 	@Override
 	public void actionPerformed(ActionEvent e) {}
+	
+
+	/**
+	 * Creates the RightClickMenu, adds the different Labels and functionalities to the menu and adds it to the panel.
+	 */
+	private void initializeRightClickMenu() {
+		this.rightClickMenu = new JPopupMenu();
+
+		// Create device option
+		itemCreate = new JMenuItem("Create SmartDevice");
+
+		itemCreate.addActionListener(e -> {
+			SmartDevice toEdit;
+			if(clicked == null){
+				toEdit = new SmartDevice("DeviceName");
+				toEdit.setX(dragged_x);
+				toEdit.setY(dragged_y);
+			}else{
+				toEdit = clicked;
+			}
+			SmartDeviceCreationPopUp popUp = new SmartDeviceCreationPopUp(toEdit, toEdit==clicked,controller);
+			popUp.setLocationRelativeTo(panel);
+			popUp.setEnabled(true);
+			popUp.setVisible(true);
+			mode = NOTHING;
+
+		});
+
+		rightClickMenu.add(itemCreate);
+
+		// Delete device option
+		itemDelete = new JMenuItem("Delete");
+
+		itemDelete.addActionListener(e -> {
+			// Delete the clicked object
+				controller.deleteSmartDevice(clicked);
+				clicked = null;
+				controller.notifyObservers();
+				mode = NOTHING;
+			});
+
+		rightClickMenu.add(itemDelete);
+		
+		// Delete device option
+		itemDeleteSelected = new JMenuItem("Delete Selected");
+		
+		itemDeleteSelected.addActionListener(e -> {
+			// Delete the clicked object
+			for(SmartDevice c: selectedDevices)
+				controller.deleteSmartDevice(c);
+			selectedDevices.clear();
+			clicked = null;
+			controller.notifyObservers();
+			mode = NOTHING;
+		});
+		itemDeleteSelected.setEnabled(false);
+		rightClickMenu.add(itemDeleteSelected);
+				
+		/*
+		 * Add PrintDebug option
+		 */
+		itemDebug = new JMenuItem("Print NetworkState");
+		itemDebug.addActionListener(e -> {
+			// Print Links, Devices and Connections
+				for (Link l : model.getConnectionNetworks())
+					System.out.println("Link: " + l.getName());
+				for (SmartDevice d : model.getDevices()) {
+					System.out.println("Device: " + d.getName());
+					for (Port p : d.getPorts()) {
+						if(p==null){
+							System.out.println("Port: null");
+							continue;
+						}
+						if(p.getConnection()==null){
+							System.out.println("Connection: null");
+							continue;
+						}
+						switch (p.getConnection().getStatus()) {
+						case Connection.ACTIVE:
+							System.out.println("Connection: active");
+							break;
+						case Connection.FINISHED:
+						case Connection.TERMINATED:
+							System.out.println("Connection: terminated");
+
+							break;
+
+						case Connection.HALTED:
+						default:
+							break;
+						}
+					}
+				}
+				// Print Terminating Packages
+				LinkedList<Connection> terminated = new LinkedList<Connection>();
+
+				model.getConnections()
+						.stream()
+						.forEach(c -> {
+							if(c.getStatus() == Connection.FINISHED || c
+								.getStatus() == Connection.TERMINATED)
+								terminated.add(c);
+							for (Packet p : c.getTerminationPackages(1000))
+								System.out.println(p.getTextualRepresentation());
+						});
+				model.getConnections().removeAll(terminated);
+				if(selectedDevices.isEmpty())
+					mode = NOTHING;
+				else
+					mode = SELECTED;
+				controller.notifyObservers();
+			});
+		rightClickMenu.add(itemDebug);
+
+		this.panel.add(rightClickMenu);
+	}
 }