Browse Source

Adds simple selection detection/visualization

Andreas T. Meyer-Berg 5 years ago
parent
commit
c93d11a278

+ 88 - 20
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationInteractor.java

@@ -81,7 +81,21 @@ public class VisualisationInteractor implements MouseInputListener,
 	 * Current y Position of the dragged device
 	 */
 	public int dragged_y;
-
+	
+	public int dragged_x_start;
+	
+	public int dragged_y_start;
+	
+	public LinkedList<SmartDevice> selectedDevices = new LinkedList<SmartDevice>();
+	
+	public static final byte NOTHING = 0; 
+	public static final byte RIGHTCLICK_MENU = 1; 
+	public static final byte DRAG_DEVICE = 2;
+	public static final byte DRAG_CONNECTION = 3;
+	public static final byte DRAG_SELECT = 4; 
+	public static final byte SELECTED = 5;
+	public byte mode = NOTHING;
+	
 	/**
 	 * SmartDevice that was clicked
 	 */
@@ -124,6 +138,7 @@ public class VisualisationInteractor implements MouseInputListener,
 			popUp.setLocationRelativeTo(panel);
 			popUp.setEnabled(true);
 			popUp.setVisible(true);
+			mode = NOTHING;
 
 		});
 
@@ -137,6 +152,7 @@ public class VisualisationInteractor implements MouseInputListener,
 				controller.deleteSmartDevice(clicked);
 				clicked = null;
 				controller.notifyObservers();
+				mode = NOTHING;
 			});
 
 		rightClickMenu.add(itemDelete);
@@ -186,11 +202,11 @@ public class VisualisationInteractor implements MouseInputListener,
 								.getStatus() == Connection.TERMINATED)
 								terminated.add(c);
 							for (Packet p : c.getTerminationPackages(1000))
-								System.out.println(p.toString());
+								System.out.println(p.getTextualRepresentation());
 						});
 				model.getConnections().removeAll(terminated);
 				controller.notifyObservers();
-			
+				mode = NOTHING;
 			});
 		rightClickMenu.add(itemDebug);
 
@@ -253,6 +269,7 @@ public class VisualisationInteractor implements MouseInputListener,
 			rightClickMenu.show(panel, mousePos.x, mousePos.y);
 			rightClickMenu.setEnabled(true);
 			rightClickMenu.setVisible(true);
+			mode = RIGHTCLICK_MENU;
 		}
 	}
 
@@ -263,16 +280,24 @@ public class VisualisationInteractor implements MouseInputListener,
 		dragged_y = e.getY();
 		// Check if SmartDevice was clicked
 		SmartDevice pressedOn = getSmartDeviceAtPosition(e.getX(), e.getY());
-		if (pressedOn == null)
+		if (pressedOn == null){
+			//Click drag - multi select
+			dragged_x_start = e.getX();
+			dragged_y_start = e.getY();
+			mode = DRAG_SELECT;
+			selectedDevices.clear();
 			return;
+		}
 
 		// Recognize possible drag operation
 		if (e.getButton() == MouseEvent.BUTTON1 && connectionFrom == null) {
 			dragged = pressedOn;
 			dragged_x = pressedOn.getX();
 			dragged_y = pressedOn.getY();
+			mode = DRAG_DEVICE;
 		} else if (e.getButton() == MouseEvent.BUTTON3 && dragged == null) {
 			connectionFrom = pressedOn;
+			mode = DRAG_CONNECTION;
 		}
 	}
 
@@ -280,9 +305,15 @@ public class VisualisationInteractor implements MouseInputListener,
 	public void mouseReleased(MouseEvent e) {
 		// Finish drag operation
 		// if (e.getButton() == MouseEvent.BUTTON1)
-		finishDrag();
+		if(mode == DRAG_SELECT){
+			mode = SELECTED;
+			panel.repaint();
+		}
+		if(mode == DRAG_DEVICE)
+			finishDrag();
 		// else if(e.getButton() == MouseEvent.BUTTON3){
-		finishConnectionCreation();
+		if(mode == DRAG_CONNECTION)
+			finishConnectionCreation();
 		// }
 	}
 
@@ -304,21 +335,56 @@ public class VisualisationInteractor implements MouseInputListener,
 	@Override
 	public void mouseDragged(MouseEvent e) {
 		// move dragged object on screen along y Axis
-		if (e.getX() <= control.getDevice_visualization_radius())
-			dragged_x = control.getDevice_visualization_radius();
-		else if (e.getX() >= model.getWidth() - control.getDevice_visualization_radius())
-			dragged_x = model.getWidth() - control.getDevice_visualization_radius();
-		else
-			dragged_x = e.getX();
-		// move dragged object on screen along y Axis
-		if (e.getY() <= control.getDevice_visualization_radius())
-			dragged_y = control.getDevice_visualization_radius();
-		else if (e.getY() >= model.getHeight() - control.getDevice_visualization_radius())
-			dragged_y = model.getHeight() - control.getDevice_visualization_radius();
-		else
-			dragged_y = e.getY();
+		switch (mode) {
+		case DRAG_SELECT:
+		case DRAG_CONNECTION:
+			if (e.getX() < 0)
+				dragged_x = 0;
+			else if (e.getX() >= model.getWidth())
+				dragged_x = model.getWidth()-1;
+			else
+				dragged_x = e.getX();
+			// move dragged object on screen along y Axis
+			if (e.getY() < 0)
+				dragged_y = 0;
+			else if (e.getY() >= model.getHeight())
+				dragged_y = model.getHeight()-1;
+			else
+				dragged_y = e.getY();
+			if(mode == DRAG_CONNECTION)break;
+				int min_x = Math.min(dragged_x, dragged_x_start);
+				int min_y = Math.min(dragged_y, dragged_y_start);
+				int max_x = Math.max(dragged_x, dragged_x_start);
+				int max_y = Math.max(dragged_y, dragged_y_start);
+				selectedDevices.removeIf(s->(s.getX()<min_x||s.getX()>max_x||s.getY()<min_y||s.getY()>max_y));
+				for(SmartDevice sel:model.getDevices()){
+					if(sel.getX()>=min_x&&sel.getX()<=max_x&&sel.getY()>=min_y&&sel.getY()<=max_y&&!selectedDevices.contains(sel)){
+						selectedDevices.add(sel);
+					}
+				}
+			break;
+		case DRAG_DEVICE:
+			if (e.getX() <= control.getDevice_visualization_radius())
+				dragged_x = control.getDevice_visualization_radius();
+			else if (e.getX() >= model.getWidth() - control.getDevice_visualization_radius())
+				dragged_x = model.getWidth() - control.getDevice_visualization_radius();
+			else
+				dragged_x = e.getX();
+			// move dragged object on screen along y Axis
+			if (e.getY() <= control.getDevice_visualization_radius())
+				dragged_y = control.getDevice_visualization_radius();
+			else if (e.getY() >= model.getHeight() - control.getDevice_visualization_radius())
+				dragged_y = model.getHeight() - control.getDevice_visualization_radius();
+			else
+				dragged_y = e.getY();
+			break;
+
+		default:
+			break;
+		}
+		
 		// repaint the dragged smartDevice or new connection
-		if (dragged != null || connectionFrom != null)
+		if (dragged != null || connectionFrom != null||mode==DRAG_SELECT)
 			panel.repaint();
 	}
 
@@ -338,6 +404,7 @@ public class VisualisationInteractor implements MouseInputListener,
 					dragged.getZ());
 			dragged = null;
 			model.notifyObservers();
+			mode = NOTHING;
 		}
 	}
 
@@ -376,6 +443,7 @@ public class VisualisationInteractor implements MouseInputListener,
 			connectionFrom = null;
 		}
 		control.notifyObservers();
+		mode = NOTHING;
 	}
 
 	/**

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

@@ -5,7 +5,6 @@ import java.awt.Graphics;
 import java.awt.event.ComponentEvent;
 import java.awt.event.ComponentListener;
 import java.util.Collection;
-import java.util.Iterator;
 import java.util.Observable;
 import java.util.Observer;
 
@@ -17,7 +16,14 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.VisualisationInteractor;
 
+/**
+ * Panel which visualizes the SmartHome Network, it's Devices and Connections
+ * 
+ *
+ * @author Andreas T. Meyer-Berg
+ */
 @SuppressWarnings("serial")
 public class VisualisationPanel extends JPanel implements Observer {
 
@@ -35,7 +41,7 @@ public class VisualisationPanel extends JPanel implements Observer {
 	 * Listener which processes the GUI Interactions
 	 */
 	private VisualisationInteractor interactor;
-
+	
 	/**
 	 * Initializes the Visualization Panel
 	 * 
@@ -96,6 +102,8 @@ public class VisualisationPanel extends JPanel implements Observer {
 		paintConnections(g);
 
 		paintDevices(g);
+		
+		paintDrag(g);
 	}
 
 	/**
@@ -106,13 +114,7 @@ public class VisualisationPanel extends JPanel implements Observer {
 	 */
 	public void paintDevices(Graphics g) {
 
-		for (Iterator<SmartDevice> it = model.getDevices().iterator();it.hasNext();) {
-			SmartDevice s;
-			try {
-				s = it.next();
-			} catch (Exception e) {
-				continue;
-			}
+		for (SmartDevice s: model.getDevices()) {
 			int x = s.getX();
 			int y = s.getY();
 			if (s == interactor.dragged) {
@@ -120,7 +122,11 @@ public class VisualisationPanel extends JPanel implements Observer {
 				x = interactor.dragged_x;
 				y = interactor.dragged_y;
 			}
-			g.setColor(Color.WHITE);
+			if((interactor.mode==VisualisationInteractor.SELECTED||interactor.mode==VisualisationInteractor.DRAG_SELECT)&&interactor.selectedDevices.contains(s)){
+				//HighlightSelected Devices
+				g.setColor(new Color(135,206,235));
+			}else
+				g.setColor(Color.WHITE);
 			g.fillOval(x - control.getDevice_visualization_radius(), y - control.getDevice_visualization_radius(),
 					2 * control.getDevice_visualization_radius() - 1, 2 * control.getDevice_visualization_radius() - 1);
 			g.setColor(Color.BLACK);
@@ -270,7 +276,23 @@ public class VisualisationPanel extends JPanel implements Observer {
 					interactor.connectionFrom.getY(), interactor.dragged_x,
 					interactor.dragged_y);
 	}
+	
+	/**
+	 * Paints the 
+	 * @param g
+	 */
+	public void paintDrag(Graphics g){
+		if(interactor.mode!=VisualisationInteractor.DRAG_SELECT)return;
+		//interactor.dragged_x_start
+		
+		int low_x = interactor.dragged_x<interactor.dragged_x_start?interactor.dragged_x:interactor.dragged_x_start;
+		int low_y = interactor.dragged_y<interactor.dragged_y_start?interactor.dragged_y:interactor.dragged_y_start;
+		int high_x = interactor.dragged_x>=interactor.dragged_x_start?interactor.dragged_x:interactor.dragged_x_start;
+		int high_y = interactor.dragged_y>=interactor.dragged_y_start?interactor.dragged_y:interactor.dragged_y_start;
+		g.setColor(Color.BLUE);
+		g.drawRect(low_x, low_y, high_x-low_x, high_y-low_y);
 
+	}
 	@Override
 	public void update(Observable o, Object arg) {
 		repaint();