|
@@ -3,6 +3,8 @@ 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.KeyEvent;
|
|
|
+import java.awt.event.KeyListener;
|
|
|
import java.awt.event.MouseEvent;
|
|
|
import java.awt.event.MouseMotionListener;
|
|
|
import java.util.LinkedList;
|
|
@@ -30,7 +32,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimplePr
|
|
|
* @author Andreas T. Meyer-Berg
|
|
|
*/
|
|
|
public class VisualisationInteractor implements MouseInputListener,
|
|
|
- MouseMotionListener, ActionListener {
|
|
|
+ MouseMotionListener, ActionListener, KeyListener {
|
|
|
|
|
|
/**
|
|
|
* Smart Home model which should be interacted with
|
|
@@ -82,11 +84,21 @@ public class VisualisationInteractor implements MouseInputListener,
|
|
|
*/
|
|
|
public int dragged_y;
|
|
|
|
|
|
+ /**
|
|
|
+ * x Position where the drage begun
|
|
|
+ */
|
|
|
public int dragged_x_start;
|
|
|
|
|
|
+ /**
|
|
|
+ * y Position where the drag begun
|
|
|
+ */
|
|
|
public int dragged_y_start;
|
|
|
|
|
|
+ /**
|
|
|
+ * Devices which are currently selected
|
|
|
+ */
|
|
|
public LinkedList<SmartDevice> selectedDevices = new LinkedList<SmartDevice>();
|
|
|
+ public LinkedList<SmartDevice> selectedDevicesDrag = new LinkedList<SmartDevice>();
|
|
|
|
|
|
public static final byte NOTHING = 0;
|
|
|
public static final byte RIGHTCLICK_MENU = 1;
|
|
@@ -94,6 +106,9 @@ public class VisualisationInteractor implements MouseInputListener,
|
|
|
public static final byte DRAG_CONNECTION = 3;
|
|
|
public static final byte DRAG_SELECT = 4;
|
|
|
public static final byte SELECTED = 5;
|
|
|
+ /**
|
|
|
+ * Mode, which action is currently executed
|
|
|
+ */
|
|
|
public byte mode = NOTHING;
|
|
|
|
|
|
/**
|
|
@@ -103,6 +118,10 @@ public class VisualisationInteractor implements MouseInputListener,
|
|
|
|
|
|
public SmartDevice connectionFrom = null;
|
|
|
|
|
|
+ /**
|
|
|
+ * True if the ControlKey is currently pressed
|
|
|
+ */
|
|
|
+ public boolean controlDown = false;
|
|
|
/**
|
|
|
* Creates a new VisualisationInteractor
|
|
|
*
|
|
@@ -218,14 +237,7 @@ public class VisualisationInteractor implements MouseInputListener,
|
|
|
/*
|
|
|
* FindSmartDevice that was clicked
|
|
|
*/
|
|
|
- clicked = null;
|
|
|
- // Find the clicked SmartDevice
|
|
|
- for (SmartDevice d : model.getDevices()) {
|
|
|
- if (Math.abs(d.getX() - e.getX()) < control.getDevice_visualization_radius()
|
|
|
- && Math.abs(d.getY() - e.getY()) < control.getDevice_visualization_radius()) {
|
|
|
- clicked = d;
|
|
|
- }
|
|
|
- }
|
|
|
+ clicked = getSmartDeviceAtPosition(e.getX(), e.getY());
|
|
|
|
|
|
switch (mode) {
|
|
|
case DRAG_DEVICE:
|
|
@@ -235,6 +247,26 @@ public class VisualisationInteractor implements MouseInputListener,
|
|
|
case DRAG_CONNECTION:
|
|
|
finishConnectionCreation();
|
|
|
break;
|
|
|
+ case SELECTED:
|
|
|
+ case NOTHING:
|
|
|
+ if(!selectedDevices.isEmpty()&&!controlDown){
|
|
|
+ selectedDevices.clear();
|
|
|
+ if (clicked == null) {
|
|
|
+ panel.repaint();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(clicked!=null){
|
|
|
+ if(!controlDown || clicked != null && !selectedDevices.remove(clicked))
|
|
|
+ selectedDevices.add(clicked);
|
|
|
+ mode = SELECTED;
|
|
|
+ panel.repaint();
|
|
|
+ }else{
|
|
|
+ if(!controlDown)
|
|
|
+ mode = NOTHING;
|
|
|
+ else if(!selectedDevices.isEmpty())
|
|
|
+ mode = SELECTED;
|
|
|
+ }
|
|
|
+ break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
@@ -290,14 +322,16 @@ public class VisualisationInteractor implements MouseInputListener,
|
|
|
switch (mode) {
|
|
|
case SELECTED:
|
|
|
case RIGHTCLICK_MENU:
|
|
|
- mode = NOTHING;
|
|
|
+ if(!controlDown)
|
|
|
+ mode = NOTHING;
|
|
|
case NOTHING:
|
|
|
if (pressedOn == null){
|
|
|
//Click drag - multi select
|
|
|
dragged_x_start = e.getX();
|
|
|
dragged_y_start = e.getY();
|
|
|
mode = DRAG_SELECT;
|
|
|
- selectedDevices.clear();
|
|
|
+ if(!controlDown)
|
|
|
+ selectedDevices.clear();
|
|
|
return;
|
|
|
} else if (e.getButton() == MouseEvent.BUTTON1){// && connectionFrom == null) {
|
|
|
// Recognize possible drag operation
|
|
@@ -325,7 +359,20 @@ public class VisualisationInteractor implements MouseInputListener,
|
|
|
// Finish operation
|
|
|
switch (mode) {
|
|
|
case DRAG_SELECT:
|
|
|
- mode = SELECTED;
|
|
|
+ LinkedList<SmartDevice> merged = new LinkedList<SmartDevice>();
|
|
|
+ //XOR of selectedDevices and selectedDevices Drag
|
|
|
+ merged.addAll(selectedDevices);
|
|
|
+ merged.removeAll(selectedDevicesDrag);
|
|
|
+ selectedDevicesDrag.removeAll(selectedDevices);
|
|
|
+ merged.addAll(selectedDevicesDrag);
|
|
|
+ //clear sets
|
|
|
+ selectedDevices.clear();
|
|
|
+ selectedDevicesDrag.clear();
|
|
|
+ selectedDevices = merged;
|
|
|
+ if(selectedDevices.isEmpty())
|
|
|
+ mode = NOTHING;
|
|
|
+ else
|
|
|
+ mode = SELECTED;
|
|
|
panel.repaint();
|
|
|
break;
|
|
|
case DRAG_DEVICE:
|
|
@@ -356,17 +403,17 @@ public class VisualisationInteractor implements MouseInputListener,
|
|
|
|
|
|
@Override
|
|
|
public void mouseDragged(MouseEvent e) {
|
|
|
- // move dragged object on screen along y Axis
|
|
|
+ // move dragged object/objecte selection on screen
|
|
|
switch (mode) {
|
|
|
case DRAG_SELECT:
|
|
|
case DRAG_CONNECTION:
|
|
|
+ //Update position
|
|
|
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())
|
|
@@ -375,14 +422,17 @@ public class VisualisationInteractor implements MouseInputListener,
|
|
|
dragged_y = e.getY();
|
|
|
if(mode == DRAG_CONNECTION)
|
|
|
break;
|
|
|
+ //Update Selected objects, if DragSelect mode is active
|
|
|
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));
|
|
|
+ //Remove unselected
|
|
|
+ selectedDevicesDrag.removeIf(s->(s.getX()<min_x||s.getX()>max_x||s.getY()<min_y||s.getY()>max_y));
|
|
|
+ //Add selected devices
|
|
|
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);
|
|
|
+ if(sel.getX()>=min_x&&sel.getX()<=max_x&&sel.getY()>=min_y&&sel.getY()<=max_y&&!selectedDevicesDrag.contains(sel)){
|
|
|
+ selectedDevicesDrag.add(sel);
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
@@ -492,4 +542,34 @@ public class VisualisationInteractor implements MouseInputListener,
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void keyTyped(KeyEvent e) {
|
|
|
+ if(e.getKeyCode()==KeyEvent.VK_CONTROL){
|
|
|
+ if(controlDown!=true){
|
|
|
+ controlDown = true;
|
|
|
+ panel.repaint();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void keyPressed(KeyEvent e) {
|
|
|
+ if(e.getKeyCode()==KeyEvent.VK_CONTROL){
|
|
|
+ if(controlDown!=true){
|
|
|
+ controlDown = true;
|
|
|
+ panel.repaint();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void keyReleased(KeyEvent e) {
|
|
|
+ if(e.getKeyCode()==KeyEvent.VK_CONTROL){
|
|
|
+ if(controlDown==true){
|
|
|
+ controlDown = false;
|
|
|
+ panel.repaint();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|