Sfoglia il codice sorgente

Adds RightClickMenu to the VisualisationPanel, no functionality yet

Andreas T. Meyer-Berg 6 anni fa
parent
commit
4caa12cb00

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

@@ -1,10 +1,13 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
 
+import java.awt.Point;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseMotionListener;
 
+import javax.swing.JMenuItem;
+import javax.swing.JPopupMenu;
 import javax.swing.event.MouseInputListener;
 
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
@@ -34,7 +37,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
 	 */
@@ -51,7 +69,7 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 	public int dragged_y;
 
 	/**
-	 * 
+	 * Creates a new VisualisationInteractor
 	 * @param model
 	 * @param controller
 	 */
@@ -59,14 +77,62 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 		this.model = model;
 		this.control = controller;
 		this.panel = panel;
+		this.rightClickMenu = new JPopupMenu();
+		itemCreate = new JMenuItem("Create SmartDevice");
+		itemDelete = new JMenuItem("Delete");
+		rightClickMenu.add(itemCreate);
+		rightClickMenu.add(itemDelete);
+		this.panel.add(rightClickMenu);
 	}
 
 	@Override
 	public void mouseClicked(MouseEvent e) {
-		//if(e.getButton()==MouseEvent.BUTTON3)//rechtsklick menü etc
+		/**
+		 * SmartDevice that was clicked
+		 */
+		SmartDevice clickedOn = 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()) {
+				clickedOn = d;
+			}
+		}
+		//If Rightclick
+		if(e.getButton()==MouseEvent.BUTTON3) {
+			//Show the RightClickMenu
+			showRightClickMenu(clickedOn);	
+		}
 			
 	}
 
+	/**
+	 * 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){		
+				itemCreate.setEnabled(true);
+				itemDelete.setEnabled(false);
+			}
+			else{
+				itemCreate.setEnabled(false);
+				itemDelete.setEnabled(true);
+			}
+			rightClickMenu.show(panel, mousePos.x, mousePos.y);
+			rightClickMenu.setEnabled(true);
+			rightClickMenu.setVisible(true);
+		}
+	}
+
 	@Override
 	public void mousePressed(MouseEvent e) {
 		// Check if SmartDevice was clicked

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

@@ -47,7 +47,7 @@ public class VisualisationPanel extends JPanel {
 
 		this.model = model;
 		this.control = control;
-
+		
 		this.interactor = new VisualisationInteractor(model, control, this);
 
 		this.addMouseMotionListener(interactor);