Browse Source

Separates RightClick and Dragging

Andreas T. Meyer-Berg 6 years ago
parent
commit
18a2da7b5f

+ 57 - 38
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationInteractor.java

@@ -40,22 +40,22 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 	 * Panel which is observed
 	 */
 	private VisualisationPanel panel;
-	
+
 	/**
 	 * Menu which is shown on right clicks
 	 */
 	private JPopupMenu rightClickMenu;
-	
+
 	/**
 	 * RightClick MenuItem for SmartDevice creation
 	 */
 	private JMenuItem itemCreate;
-	
+
 	/**
 	 * RightClick MenuItem for SmartDevice deletion
 	 */
 	private JMenuItem itemDelete;
-	
+
 	/**
 	 * SmartDevice that is dragged on Screen
 	 */
@@ -71,8 +71,14 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 	 */
 	public int dragged_y;
 
+	/**
+	 * SmartDevice that was clicked
+	 */
+	private SmartDevice clicked = null;
+
 	/**
 	 * Creates a new VisualisationInteractor
+	 * 
 	 * @param model
 	 * @param controller
 	 */
@@ -84,23 +90,24 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 		itemCreate = new JMenuItem("Create SmartDevice");
 		itemDelete = new JMenuItem("Delete");
 		itemDelete.addActionListener(e -> {
-			controller.deleteSmartDevice(dragged);
+			controller.deleteSmartDevice(clicked);
+			clicked = null;
 			panel.repaint();
-			});
-		itemCreate.addActionListener(e->{
-			for(Link l:model.getConnectionNetworks())
-				System.out.println("Link: "+l.getName());
-			for(SmartDevice d: model.getDevices()){
-				System.out.println("Device: "+d.getName());
-				for(Connection c: d.getConnections()){
-					if(c.getSource()==null)
+		});
+		itemCreate.addActionListener(e -> {
+			for (Link l : model.getConnectionNetworks())
+				System.out.println("Link: " + l.getName());
+			for (SmartDevice d : model.getDevices()) {
+				System.out.println("Device: " + d.getName());
+				for (Connection c : d.getConnections()) {
+					if (c.getSource() == null)
 						System.out.println("Connection: terminated");
 					else
-						System.out.println("Connection: "+c.getSource().getName());
+						System.out.println("Connection: " + c.getSource().getName());
 				}
 			}
-			for(Connection c: model.getTerminatingConnections()){
-				for(Packet p:c.getTerminationPackages(1000))
+			for (Connection c : model.getTerminatingConnections()) {
+				for (Packet p : c.getTerminationPackages(1000))
 					System.out.println(p.toString());
 			}
 		});
@@ -111,43 +118,48 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 
 	@Override
 	public void mouseClicked(MouseEvent e) {
+		if (dragged != null) {
+			finishDrag();
+		}
 		/*
 		 * FindSmartDevice that was clicked
 		 */
-		dragged = null;
+		clicked = null;
 		// Find the clicked SmartDevice
 		for (SmartDevice d : model.getDevices()) {
 			if (Math.abs(d.getX() - e.getX()) < panel.getVisualisationRadius()
 					&& Math.abs(d.getY() - e.getY()) < panel.getVisualisationRadius()) {
-				dragged = d;
+				clicked = d;
 			}
 		}
-		//If Rightclick
-		if(e.getButton()==MouseEvent.BUTTON3) {
-			//Show the RightClickMenu
-			showRightClickMenu(dragged);	
+		// If Rightclick
+		if (e.getButton() == MouseEvent.BUTTON3) {
+			// Show the RightClickMenu
+			showRightClickMenu(clicked);
 		}
-			
+
 	}
 
 	/**
-	 * Shows the RightClick Menu on the Visualisation 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
+	 * Shows the RightClick Menu on the Visualisation 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
 	 */
 	protected void showRightClickMenu(SmartDevice clickedOn) {
 		/**
 		 * Mouse Position on the VisualisationPanel
 		 */
 		Point mousePos = panel.getMousePosition();
-		//Just execute if Mouse Position is on the Panel
-		if(mousePos != null){
-			if(clickedOn == null){		
+		// Just execute if Mouse Position is on the Panel
+		if (mousePos != null) {
+			if (clickedOn == null) {
 				itemCreate.setEnabled(true);
 				itemDelete.setEnabled(false);
-			}
-			else{
+			} else {
 				itemCreate.setEnabled(false);
 				itemDelete.setEnabled(true);
 			}
@@ -176,12 +188,8 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 	@Override
 	public void mouseReleased(MouseEvent e) {
 		// Finish drag operation
-		if (dragged != null && e.getButton() == MouseEvent.BUTTON1
-				&& (dragged.getX() != dragged_x || dragged.getY() != dragged_y)) {
-			control.moveSmartDevice(dragged, dragged_x, dragged_y, dragged.getZ());
-			panel.repaint();
-		}
-		dragged = null;
+		if (e.getButton() == MouseEvent.BUTTON1)
+			finishDrag();
 	}
 
 	@Override
@@ -224,4 +232,15 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 	public void mouseMoved(MouseEvent e) {
 	}
 
+	/**
+	 * Finishes the drag operation, if a SmartDevice was dragged, repaint the
+	 * panel and set dragged==null
+	 */
+	public void finishDrag() {
+		if (dragged != null && (dragged.getX() != dragged_x || dragged.getY() != dragged_y)) {
+			control.moveSmartDevice(dragged, dragged_x, dragged_y, dragged.getZ());
+			panel.repaint();
+			dragged = null;
+		}
+	}
 }