|
@@ -23,6 +23,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
|
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
|
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
|
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
|
|
|
|
|
|
/**
|
|
|
* Listener which detects User Interaction with the {@link VisualisationPanel},
|
|
@@ -131,6 +132,11 @@ public class VisualisationInteractor implements MouseInputListener,
|
|
|
* SmartDevice that was clicked
|
|
|
*/
|
|
|
private SmartDevice clicked = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Clicked Connections
|
|
|
+ */
|
|
|
+ public LinkedList<Pair<Connection,Pair<Port,Port>>> clickedConnection = new LinkedList<Pair<Connection,Pair<Port,Port>>>();
|
|
|
|
|
|
public SmartDevice connectionFrom = null;
|
|
|
|
|
@@ -200,6 +206,18 @@ public class VisualisationInteractor implements MouseInputListener,
|
|
|
mode = NOTHING;
|
|
|
else if(!selectedDevices.isEmpty())
|
|
|
mode = SELECTED;
|
|
|
+
|
|
|
+ //TODO: Click on Edge functionalities
|
|
|
+ LinkedList<Pair<Connection, Pair<Port, Port>>> clickedCons = getConnectionsAtPosition(e.getX(), e.getY());
|
|
|
+ System.out.println("Click on Edge?: "+!clickedCons.isEmpty());
|
|
|
+ if(!(clickedCons.isEmpty()&&clickedConnection.isEmpty())){
|
|
|
+ clickedConnection.clear();
|
|
|
+ clickedConnection = clickedCons;
|
|
|
+ if(!clickedConnection.isEmpty())
|
|
|
+ mode = SELECTED;
|
|
|
+ panel.repaint();
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
break;
|
|
|
default:
|
|
@@ -447,6 +465,67 @@ public class VisualisationInteractor implements MouseInputListener,
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns all Connections, which cross point (x,y)
|
|
|
+ * @param x xPosition of the point (x,y)
|
|
|
+ * @param y yPosition of the point (x,y)
|
|
|
+ * @return List of all possible clicked Connections
|
|
|
+ */
|
|
|
+ private LinkedList<Pair<Connection, Pair<Port, Port>>> getConnectionsAtPosition(int x, int y) {
|
|
|
+ LinkedList<Pair<Connection,Pair<Port,Port>>> edges = new LinkedList<Pair<Connection,Pair<Port,Port>>>();
|
|
|
+ // Check is device is inside visualization radius
|
|
|
+ for(Connection c: model.getConnections()){
|
|
|
+ if(c.getProtocol()==null)
|
|
|
+ continue;
|
|
|
+ for(Pair<Port, Port> p: c.getProtocol().getTopology()){
|
|
|
+ if(pointOnConnection(x,y,p)){
|
|
|
+ edges.add(new Pair<Connection, Pair<Port,Port>>(c, p));
|
|
|
+ break;//Connection was clicked - check for further connections
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return edges;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns true if the point (x,y) is on the connection part p.
|
|
|
+ *
|
|
|
+ * @param x xPosition of the Point
|
|
|
+ * @param y yPosition of the Point
|
|
|
+ * @param p connection part, which connects to Ports with a line
|
|
|
+ * @return true if point is on connection, else false
|
|
|
+ */
|
|
|
+ private boolean pointOnConnection(int x, int y, Pair<Port, Port> p) {
|
|
|
+ //No valid connection part
|
|
|
+ if(p.getLeft()==null||p.getRight()==null||p.getLeft().getOwner()==null||p.getRight().getOwner()==null)
|
|
|
+ return false;
|
|
|
+ int lX = p.getLeft().getOwner().getX();
|
|
|
+ int lY = p.getLeft().getOwner().getY();
|
|
|
+ int rX = p.getRight().getOwner().getX();
|
|
|
+ int rY = p.getRight().getOwner().getY();
|
|
|
+ //Point out of bounds
|
|
|
+ if(x < Math.min(lX, rX) || x > Math.max(lX, rX) || y < Math.min(lY, rY) || y > Math.max(lY, rY))
|
|
|
+ return false;
|
|
|
+ double dLP = distance(lX,lY,x,y);
|
|
|
+ double dPR = distance(x,y,rX,rY);
|
|
|
+ double dLR = distance(lX,lY,rX,rY);
|
|
|
+ double diff = Math.abs(dLP+dPR-dLR);
|
|
|
+ return diff < 0.5;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Calculates distance between (x1,y1) and (x2,y2)
|
|
|
+ *
|
|
|
+ * @param x1 x Position of Point 1
|
|
|
+ * @param y1 y Position of Point 1
|
|
|
+ * @param x2 x Position of Point 1
|
|
|
+ * @param y2 y Position of Point 1
|
|
|
+ * @return distance
|
|
|
+ */
|
|
|
+ private double distance(int x1, int y1, int x2, int y2) {
|
|
|
+ return Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public void keyTyped(KeyEvent e) {
|