|
@@ -1,336 +1,393 @@
|
|
|
-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;
|
|
|
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
|
|
|
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
|
|
|
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
|
|
|
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
|
|
|
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
|
|
|
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleConnection;
|
|
|
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
|
|
|
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
|
|
|
-
|
|
|
-/**
|
|
|
- * Listener which detects User Interaction with the {@link VisualisationPanel},
|
|
|
- * stores interaction information, triggers the controller and helps
|
|
|
- * visualization of the interaction
|
|
|
- *
|
|
|
- * @author Andreas T. Meyer-Berg
|
|
|
- */
|
|
|
-public class VisualisationInteractor implements MouseInputListener, MouseMotionListener, ActionListener {
|
|
|
-
|
|
|
- /**
|
|
|
- * Smart Home model which should be interacted with
|
|
|
- */
|
|
|
- private Model model;
|
|
|
-
|
|
|
- /**
|
|
|
- * Controller to notify when the model changes
|
|
|
- */
|
|
|
- private Controller control;
|
|
|
-
|
|
|
- /**
|
|
|
- * 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;
|
|
|
-
|
|
|
- /**
|
|
|
- * RightClick MenuItem for debug purposes
|
|
|
- */
|
|
|
- private JMenuItem itemDebug;
|
|
|
-
|
|
|
- /**
|
|
|
- * SmartDevice that is dragged on Screen
|
|
|
- */
|
|
|
- public SmartDevice dragged = null;
|
|
|
-
|
|
|
- /**
|
|
|
- * Current x Position of the dragged device
|
|
|
- */
|
|
|
- public int dragged_x;
|
|
|
-
|
|
|
- /**
|
|
|
- * Current y Position of the dragged device
|
|
|
- */
|
|
|
- public int dragged_y;
|
|
|
-
|
|
|
- /**
|
|
|
- * SmartDevice that was clicked
|
|
|
- */
|
|
|
- private SmartDevice clicked = null;
|
|
|
-
|
|
|
- public SmartDevice connectionFrom = null;
|
|
|
-
|
|
|
- /**
|
|
|
- * Creates a new VisualisationInteractor
|
|
|
- *
|
|
|
- * @param model Model which is visualized
|
|
|
- * @param controller controller that is accessed
|
|
|
- * @param panel which should visualize the interactions
|
|
|
- */
|
|
|
- public VisualisationInteractor(Model model, Controller controller, VisualisationPanel panel) {
|
|
|
- //Initialize the values
|
|
|
- this.model = model;
|
|
|
- this.control = controller;
|
|
|
- this.panel = panel;
|
|
|
-
|
|
|
- this.rightClickMenu = new JPopupMenu();
|
|
|
-
|
|
|
- //Create device option
|
|
|
- itemCreate = new JMenuItem("Create SmartDevice");
|
|
|
-
|
|
|
- itemCreate.addActionListener(e -> {
|
|
|
- SmartDeviceCreationPopUp popUp = new SmartDeviceCreationPopUp(dragged_x, dragged_y, model.getWidth(), model.getHeight(), panel.getVisualisationRadius(), control, panel);
|
|
|
- popUp.setLocationRelativeTo(panel);
|
|
|
- popUp.setEnabled(true);
|
|
|
- popUp.setVisible(true);
|
|
|
-
|
|
|
- });
|
|
|
-
|
|
|
- rightClickMenu.add(itemCreate);
|
|
|
-
|
|
|
- //Delete device option
|
|
|
- itemDelete = new JMenuItem("Delete");
|
|
|
-
|
|
|
- itemDelete.addActionListener(e -> {
|
|
|
- //Delete the clicked object
|
|
|
- controller.deleteSmartDevice(clicked);
|
|
|
- clicked = null;
|
|
|
- panel.repaint();
|
|
|
- });
|
|
|
-
|
|
|
- rightClickMenu.add(itemDelete);
|
|
|
-
|
|
|
- /*
|
|
|
- * Add PrintDebug option
|
|
|
- */
|
|
|
- itemDebug = new JMenuItem("Print NetworkState");
|
|
|
- itemDebug.addActionListener(e->{
|
|
|
- //Print Links, Devices and Connections
|
|
|
- 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());
|
|
|
- }
|
|
|
- }
|
|
|
- //Print Terminating Packages
|
|
|
- for(Connection c: model.getTerminatingConnections()){
|
|
|
- for(Packet p:c.getTerminationPackages(1000))
|
|
|
- System.out.println(p.toString());
|
|
|
- }
|
|
|
- });
|
|
|
- rightClickMenu.add(itemDebug);
|
|
|
-
|
|
|
- this.panel.add(rightClickMenu);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void mouseClicked(MouseEvent e) {
|
|
|
- //Finish operations
|
|
|
- if(dragged!=null){
|
|
|
- finishDrag();
|
|
|
- }
|
|
|
- connectionFrom =null;
|
|
|
-
|
|
|
- /*
|
|
|
- * FindSmartDevice that was clicked
|
|
|
- */
|
|
|
- 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()) {
|
|
|
- clicked = d;
|
|
|
- }
|
|
|
- }
|
|
|
- //If Rightclick
|
|
|
- if(e.getButton()==MouseEvent.BUTTON3) {
|
|
|
- //Show the RightClickMenu
|
|
|
- showRightClickMenu(clicked);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Shows the RightClick Menu on the Visualization 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);
|
|
|
- }
|
|
|
- //Show the RightClickMenu
|
|
|
- rightClickMenu.show(panel, mousePos.x, mousePos.y);
|
|
|
- rightClickMenu.setEnabled(true);
|
|
|
- rightClickMenu.setVisible(true);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void mousePressed(MouseEvent e) {
|
|
|
- //Save mouse position for right Click options
|
|
|
- dragged_x = e.getX();
|
|
|
- dragged_y = e.getY();
|
|
|
- // Check if SmartDevice was clicked
|
|
|
- SmartDevice pressedOn = getSmartDeviceAtPosition(e.getX(), e.getY());
|
|
|
- if(pressedOn == null)return;
|
|
|
-
|
|
|
- // Recognize possible drag operation
|
|
|
- if (e.getButton() == MouseEvent.BUTTON1 && connectionFrom == null) {
|
|
|
- dragged = pressedOn;
|
|
|
- dragged_x = pressedOn.getX();
|
|
|
- dragged_y = pressedOn.getY();
|
|
|
- } else if(e.getButton() == MouseEvent.BUTTON3 && dragged == null){
|
|
|
- connectionFrom = pressedOn;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void mouseReleased(MouseEvent e) {
|
|
|
- // Finish drag operation
|
|
|
- //if (e.getButton() == MouseEvent.BUTTON1)
|
|
|
- finishDrag();
|
|
|
- //else if(e.getButton() == MouseEvent.BUTTON3){
|
|
|
- finishConnectionCreation();
|
|
|
- //}
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void mouseEntered(MouseEvent e) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void mouseExited(MouseEvent e) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void actionPerformed(ActionEvent e) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void mouseDragged(MouseEvent e) {
|
|
|
- // move dragged object on screen along y Axis
|
|
|
- if (e.getX() <= panel.getVisualisationRadius())
|
|
|
- dragged_x = panel.getVisualisationRadius();
|
|
|
- else if (e.getX() >= model.getWidth() - panel.getVisualisationRadius())
|
|
|
- dragged_x = model.getWidth() - panel.getVisualisationRadius();
|
|
|
- else
|
|
|
- dragged_x = e.getX();
|
|
|
- // move dragged object on screen along y Axis
|
|
|
- if (e.getY() <= panel.getVisualisationRadius())
|
|
|
- dragged_y = panel.getVisualisationRadius();
|
|
|
- else if (e.getY() >= model.getHeight() - panel.getVisualisationRadius())
|
|
|
- dragged_y = model.getHeight() - panel.getVisualisationRadius();
|
|
|
- else
|
|
|
- dragged_y = e.getY();
|
|
|
- // repaint the dragged smartDevice or new connection
|
|
|
- if (dragged != null || connectionFrom != null)
|
|
|
- panel.repaint();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void mouseMoved(MouseEvent e) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Finishes the drag operation, if a SmartDevice was dragged, repaint the panel and set dragged to 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;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Finishes the create connection operation and open a PopUp for Link/Connection creation
|
|
|
- */
|
|
|
- private void finishConnectionCreation() {
|
|
|
- /**
|
|
|
- * SmartDevice the connection was
|
|
|
- */
|
|
|
- SmartDevice connectionTo = getSmartDeviceAtPosition(dragged_x, dragged_y);
|
|
|
- if(connectionFrom != null && connectionTo != null && connectionFrom != connectionTo){
|
|
|
- //Create new Connection
|
|
|
- Link l = new SimpleLink("Ethernet: "+connectionFrom.getName()+" to "+connectionTo.getName());
|
|
|
- l.addDevice(connectionFrom);
|
|
|
- l.addDevice(connectionTo);
|
|
|
- Connection c = new SimpleConnection(connectionFrom, connectionTo, l, new SimpleProtocol(connectionFrom, connectionTo));
|
|
|
- connectionTo.addLink(l);
|
|
|
- connectionTo.addConnection(c);
|
|
|
- connectionFrom.addLink(l);
|
|
|
- connectionFrom.addConnection(c);
|
|
|
- connectionFrom = null;
|
|
|
- }else{
|
|
|
- connectionFrom = null;
|
|
|
- }
|
|
|
- panel.repaint();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Returns SmartDevice which is at position (x,y) or within its visualization radius.
|
|
|
- * Returns null, if no SmartDevice is within the range.
|
|
|
- *
|
|
|
- * @param x x-position which should be checked
|
|
|
- * @param y y-position which should be checked
|
|
|
- * @return {@link SmartDevice} at position (x,y) or null if there is no
|
|
|
- */
|
|
|
- private SmartDevice getSmartDeviceAtPosition(int x, int y) {
|
|
|
- //Check is device is inside visualization radius
|
|
|
- for (SmartDevice d : model.getDevices()) {
|
|
|
- if (Math.abs(d.getX() - x) < panel.getVisualisationRadius()
|
|
|
- && Math.abs(d.getY() - y) < panel.getVisualisationRadius()) {
|
|
|
- return d;
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-}
|
|
|
+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 java.util.LinkedList;
|
|
|
+
|
|
|
+import javax.swing.JMenuItem;
|
|
|
+import javax.swing.JPopupMenu;
|
|
|
+import javax.swing.event.MouseInputListener;
|
|
|
+
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleConnection;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleProtocol;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Listener which detects User Interaction with the {@link VisualisationPanel},
|
|
|
+ * stores interaction information, triggers the controller and helps
|
|
|
+ * visualization of the interaction
|
|
|
+ *
|
|
|
+ * @author Andreas T. Meyer-Berg
|
|
|
+ */
|
|
|
+public class VisualisationInteractor implements MouseInputListener,
|
|
|
+ MouseMotionListener, ActionListener {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Smart Home model which should be interacted with
|
|
|
+ */
|
|
|
+ private Model model;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Controller to notify when the model changes
|
|
|
+ */
|
|
|
+ private Controller control;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * RightClick MenuItem for debug purposes
|
|
|
+ */
|
|
|
+ private JMenuItem itemDebug;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * SmartDevice that is dragged on Screen
|
|
|
+ */
|
|
|
+ public SmartDevice dragged = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Current x Position of the dragged device
|
|
|
+ */
|
|
|
+ public int dragged_x;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Current y Position of the dragged device
|
|
|
+ */
|
|
|
+ public int dragged_y;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * SmartDevice that was clicked
|
|
|
+ */
|
|
|
+ private SmartDevice clicked = null;
|
|
|
+
|
|
|
+ public SmartDevice connectionFrom = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Creates a new VisualisationInteractor
|
|
|
+ *
|
|
|
+ * @param model
|
|
|
+ * Model which is visualized
|
|
|
+ * @param controller
|
|
|
+ * controller that is accessed
|
|
|
+ * @param panel
|
|
|
+ * which should visualize the interactions
|
|
|
+ */
|
|
|
+ public VisualisationInteractor(Model model, Controller controller,
|
|
|
+ VisualisationPanel panel) {
|
|
|
+ // Initialize the values
|
|
|
+ this.model = model;
|
|
|
+ this.control = controller;
|
|
|
+ this.panel = panel;
|
|
|
+
|
|
|
+ this.rightClickMenu = new JPopupMenu();
|
|
|
+
|
|
|
+ // Create device option
|
|
|
+ itemCreate = new JMenuItem("Create SmartDevice");
|
|
|
+
|
|
|
+ itemCreate.addActionListener(e -> {
|
|
|
+ SmartDeviceCreationPopUp popUp = new SmartDeviceCreationPopUp(
|
|
|
+ dragged_x, dragged_y, model.getWidth(), model.getHeight(),
|
|
|
+ panel.getVisualisationRadius(), control, panel);
|
|
|
+ popUp.setLocationRelativeTo(panel);
|
|
|
+ popUp.setEnabled(true);
|
|
|
+ popUp.setVisible(true);
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ rightClickMenu.add(itemCreate);
|
|
|
+
|
|
|
+ // Delete device option
|
|
|
+ itemDelete = new JMenuItem("Delete");
|
|
|
+
|
|
|
+ itemDelete.addActionListener(e -> {
|
|
|
+ // Delete the clicked object
|
|
|
+ controller.deleteSmartDevice(clicked);
|
|
|
+ clicked = null;
|
|
|
+ panel.repaint();
|
|
|
+ });
|
|
|
+
|
|
|
+ rightClickMenu.add(itemDelete);
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Add PrintDebug option
|
|
|
+ */
|
|
|
+ itemDebug = new JMenuItem("Print NetworkState");
|
|
|
+ itemDebug.addActionListener(e -> {
|
|
|
+ // Print Links, Devices and Connections
|
|
|
+ for (Link l : model.getConnectionNetworks())
|
|
|
+ System.out.println("Link: " + l.getName());
|
|
|
+ for (SmartDevice d : model.getDevices()) {
|
|
|
+ System.out.println("Device: " + d.getName());
|
|
|
+ for (Port p : d.getPorts()) {
|
|
|
+ if(p==null){
|
|
|
+ System.out.println("Port: null");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(p.getConnection()==null){
|
|
|
+ System.out.println("Connection: null");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ switch (p.getConnection().getStatus()) {
|
|
|
+ case Connection.ACTIVE:
|
|
|
+ System.out.println("Connection: active");
|
|
|
+ break;
|
|
|
+ case Connection.FINISHED:
|
|
|
+ case Connection.TERMINATED:
|
|
|
+ System.out.println("Connection: terminated");
|
|
|
+
|
|
|
+ break;
|
|
|
+
|
|
|
+ case Connection.HALTED:
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Print Terminating Packages
|
|
|
+ LinkedList<Connection> terminated = new LinkedList<Connection>();
|
|
|
+ model.getConnections()
|
|
|
+ .stream()
|
|
|
+ .filter(c -> (c.getStatus() == Connection.FINISHED || c
|
|
|
+ .getStatus() == Connection.TERMINATED))
|
|
|
+ .forEach(c -> {
|
|
|
+ terminated.add(c);
|
|
|
+ for (Packet p : c.getTerminationPackages(1000))
|
|
|
+ System.out.println(p.toString());
|
|
|
+ });
|
|
|
+ model.getConnections().removeAll(terminated);
|
|
|
+ panel.repaint();
|
|
|
+ });
|
|
|
+ rightClickMenu.add(itemDebug);
|
|
|
+
|
|
|
+ this.panel.add(rightClickMenu);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void mouseClicked(MouseEvent e) {
|
|
|
+ // Finish operations
|
|
|
+ if (dragged != null) {
|
|
|
+ finishDrag();
|
|
|
+ }
|
|
|
+ connectionFrom = null;
|
|
|
+
|
|
|
+ /*
|
|
|
+ * FindSmartDevice that was clicked
|
|
|
+ */
|
|
|
+ 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()) {
|
|
|
+ clicked = d;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // If Rightclick
|
|
|
+ if (e.getButton() == MouseEvent.BUTTON3) {
|
|
|
+ // Show the RightClickMenu
|
|
|
+ showRightClickMenu(clicked);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Shows the RightClick Menu on the Visualization 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);
|
|
|
+ }
|
|
|
+ // Show the RightClickMenu
|
|
|
+ rightClickMenu.show(panel, mousePos.x, mousePos.y);
|
|
|
+ rightClickMenu.setEnabled(true);
|
|
|
+ rightClickMenu.setVisible(true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void mousePressed(MouseEvent e) {
|
|
|
+ // Save mouse position for right Click options
|
|
|
+ dragged_x = e.getX();
|
|
|
+ dragged_y = e.getY();
|
|
|
+ // Check if SmartDevice was clicked
|
|
|
+ SmartDevice pressedOn = getSmartDeviceAtPosition(e.getX(), e.getY());
|
|
|
+ if (pressedOn == null)
|
|
|
+ return;
|
|
|
+
|
|
|
+ // Recognize possible drag operation
|
|
|
+ if (e.getButton() == MouseEvent.BUTTON1 && connectionFrom == null) {
|
|
|
+ dragged = pressedOn;
|
|
|
+ dragged_x = pressedOn.getX();
|
|
|
+ dragged_y = pressedOn.getY();
|
|
|
+ } else if (e.getButton() == MouseEvent.BUTTON3 && dragged == null) {
|
|
|
+ connectionFrom = pressedOn;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void mouseReleased(MouseEvent e) {
|
|
|
+ // Finish drag operation
|
|
|
+ // if (e.getButton() == MouseEvent.BUTTON1)
|
|
|
+ finishDrag();
|
|
|
+ // else if(e.getButton() == MouseEvent.BUTTON3){
|
|
|
+ finishConnectionCreation();
|
|
|
+ // }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void mouseEntered(MouseEvent e) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void mouseExited(MouseEvent e) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void actionPerformed(ActionEvent e) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void mouseDragged(MouseEvent e) {
|
|
|
+ // move dragged object on screen along y Axis
|
|
|
+ if (e.getX() <= panel.getVisualisationRadius())
|
|
|
+ dragged_x = panel.getVisualisationRadius();
|
|
|
+ else if (e.getX() >= model.getWidth() - panel.getVisualisationRadius())
|
|
|
+ dragged_x = model.getWidth() - panel.getVisualisationRadius();
|
|
|
+ else
|
|
|
+ dragged_x = e.getX();
|
|
|
+ // move dragged object on screen along y Axis
|
|
|
+ if (e.getY() <= panel.getVisualisationRadius())
|
|
|
+ dragged_y = panel.getVisualisationRadius();
|
|
|
+ else if (e.getY() >= model.getHeight() - panel.getVisualisationRadius())
|
|
|
+ dragged_y = model.getHeight() - panel.getVisualisationRadius();
|
|
|
+ else
|
|
|
+ dragged_y = e.getY();
|
|
|
+ // repaint the dragged smartDevice or new connection
|
|
|
+ if (dragged != null || connectionFrom != null)
|
|
|
+ panel.repaint();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void mouseMoved(MouseEvent e) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Finishes the drag operation, if a SmartDevice was dragged, repaint the
|
|
|
+ * panel and set dragged to 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Finishes the create connection operation and open a PopUp for
|
|
|
+ * Link/Connection creation
|
|
|
+ */
|
|
|
+ private void finishConnectionCreation() {
|
|
|
+ /**
|
|
|
+ * SmartDevice the connection was
|
|
|
+ */
|
|
|
+ SmartDevice connectionTo = getSmartDeviceAtPosition(dragged_x,
|
|
|
+ dragged_y);
|
|
|
+ if (connectionFrom != null && connectionTo != null
|
|
|
+ && connectionFrom != connectionTo) {
|
|
|
+ // Create new Connection
|
|
|
+ Link l = new SimpleLink("Ethernet: " + connectionFrom.getName()
|
|
|
+ + " to " + connectionTo.getName());
|
|
|
+ l.addDevice(connectionFrom);
|
|
|
+ l.addDevice(connectionTo);
|
|
|
+ Port p1 = new Port(connectionFrom, (short) 1);
|
|
|
+ Port p2 = new Port(connectionTo, (short) 2);
|
|
|
+
|
|
|
+ Connection c = new SimpleConnection(p1, p2,
|
|
|
+ l, new SimpleProtocol(p1, p2));
|
|
|
+ p1.setConnection(c);
|
|
|
+ p2.setConnection(c);
|
|
|
+ connectionTo.addLink(l);
|
|
|
+ connectionTo.addPort(p2);
|
|
|
+ connectionFrom.addLink(l);
|
|
|
+ connectionFrom.addPort(p1);
|
|
|
+ l.addConnection(c);
|
|
|
+ model.addConnection(c);
|
|
|
+ connectionFrom = null;
|
|
|
+ } else {
|
|
|
+ connectionFrom = null;
|
|
|
+ }
|
|
|
+ panel.repaint();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns SmartDevice which is at position (x,y) or within its
|
|
|
+ * visualization radius. Returns null, if no SmartDevice is within the
|
|
|
+ * range.
|
|
|
+ *
|
|
|
+ * @param x
|
|
|
+ * x-position which should be checked
|
|
|
+ * @param y
|
|
|
+ * y-position which should be checked
|
|
|
+ * @return {@link SmartDevice} at position (x,y) or null if there is no
|
|
|
+ */
|
|
|
+ private SmartDevice getSmartDeviceAtPosition(int x, int y) {
|
|
|
+ // Check is device is inside visualization radius
|
|
|
+ for (SmartDevice d : model.getDevices()) {
|
|
|
+ if (Math.abs(d.getX() - x) < panel.getVisualisationRadius()
|
|
|
+ && Math.abs(d.getY() - y) < panel.getVisualisationRadius()) {
|
|
|
+ return d;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|