MyMouseManager.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui.handlers;
  2. import java.awt.event.MouseEvent;
  3. import org.graphstream.graph.Edge;
  4. import org.graphstream.graph.Graph;
  5. import org.graphstream.graph.Node;
  6. import org.graphstream.ui.geom.Point3;
  7. import org.graphstream.ui.graphicGraph.GraphicElement;
  8. import org.graphstream.ui.view.Camera;
  9. import org.graphstream.ui.view.util.DefaultMouseManager;
  10. import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  11. import de.tu_darmstadt.informatik.tk.scopviz.main.CreationMode;
  12. import de.tu_darmstadt.informatik.tk.scopviz.main.EdgeSelectionHelper;
  13. import de.tu_darmstadt.informatik.tk.scopviz.main.GraphManager;
  14. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  15. import de.tu_darmstadt.informatik.tk.scopviz.ui.PropertiesManager;
  16. /**
  17. * Mouse Manager to handle all Mouse based Interaction on the Graph Display
  18. *
  19. * @author Jan Enders
  20. * @version 1.0
  21. */
  22. public class MyMouseManager extends DefaultMouseManager {
  23. /**
  24. * A Reference to the GraphManager this MouseManager is attached to for
  25. * easier Access.
  26. */
  27. private GraphManager graphManager;
  28. /**
  29. * The Timestamp of when the Camera's View Center was last changed.
  30. */
  31. private long lastCamUpdate = 0;
  32. /**
  33. * The recorded Position of the Mouse Cursor (in Graph Units) when the Mouse
  34. * was last clicked.
  35. */
  36. private Point3 oldMousePos;
  37. /**
  38. * The recorded View Center of the Camera when the Mouse was last clicked.
  39. */
  40. private Point3 oldViewCenter;
  41. /**
  42. * Constructor, sets Reference to the GraphManager this MouseManager is
  43. * attached to.
  44. *
  45. * @param manager
  46. * the GraphManager for Reference
  47. */
  48. public MyMouseManager(GraphManager manager) {
  49. this.graphManager = manager;
  50. }
  51. /**
  52. * Gets called whenever a Mouse Button is pressed while the Mouse is not
  53. * over an Element.
  54. *
  55. * @param event
  56. * the corresponding MouseEvent
  57. */
  58. protected void mouseButtonPress(MouseEvent event) {
  59. view.requestFocus();
  60. Graph graph = graphManager.getGraph();
  61. Point3 cursorPos = graphManager.getView().getCamera().transformPxToGu(event.getX(), event.getY());
  62. Node n;
  63. Edge selectedEdge = EdgeSelectionHelper.getClosestEdge(cursorPos);
  64. switch (Main.getInstance().getCreationMode()) {
  65. // If not trying to create any Nodes or Edges, select the Edge that
  66. // was clicked on
  67. case CREATE_NONE:
  68. if (selectedEdge != null) {
  69. graphManager.selectEdge(selectedEdge.getId());
  70. }
  71. break;
  72. // Otherwise, create node based on creation Mode
  73. case CREATE_STANDARD_NODE:
  74. n = graph.addNode(Main.getInstance().getUnusedID());
  75. n.setAttribute("xyz", cursorPos);
  76. n.setAttribute("ui.class", "standard");
  77. graphManager.selectNode(n.getId());
  78. Debug.out("Added Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/" + cursorPos.y + ")");
  79. break;
  80. case CREATE_SOURCE_NODE:
  81. n = graph.addNode(Main.getInstance().getUnusedID());
  82. n.setAttribute("xyz", cursorPos);
  83. n.setAttribute("ui.class", "source");
  84. graphManager.selectNode(n.getId());
  85. Debug.out("Added Source Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/" + cursorPos.y
  86. + ")");
  87. break;
  88. case CREATE_SINK_NODE:
  89. n = graph.addNode(Main.getInstance().getUnusedID());
  90. n.setAttribute("xyz", cursorPos);
  91. n.setAttribute("ui.class", "sink");
  92. graphManager.selectNode(n.getId());
  93. Debug.out(
  94. "Added Sink Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/" + cursorPos.y + ")");
  95. break;
  96. case CREATE_PROC_NODE:
  97. n = graph.addNode(Main.getInstance().getUnusedID());
  98. n.setAttribute("xyz", cursorPos);
  99. n.setAttribute("ui.class", "procEn");
  100. graphManager.selectNode(n.getId());
  101. Debug.out("Added ProcEn Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/" + cursorPos.y
  102. + ")");
  103. break;
  104. case CREATE_OPERATOR_NODE:
  105. n = graph.addNode(Main.getInstance().getUnusedID());
  106. n.setAttribute("xyz", cursorPos);
  107. n.setAttribute("ui.class", "operator");
  108. graphManager.selectNode(n.getId());
  109. Debug.out("Added Operator Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/" + cursorPos.y
  110. + ")");
  111. break;
  112. default:
  113. break;
  114. }
  115. }
  116. /**
  117. * Gets Called whenever a Mouse Button is released while the Mouse is not
  118. * over an Element.
  119. *
  120. * @param event
  121. * the corresponding MouseEvent
  122. */
  123. protected void mouseButtonRelease(MouseEvent event) {
  124. // NOP
  125. }
  126. /**
  127. * Gets called whenever a Mouse Button is pressed down while the Mouse is
  128. * over an Element.
  129. *
  130. * @param element
  131. * the element that was clicked on
  132. * @param event
  133. * the corresponding MouseEvent
  134. */
  135. protected void mouseButtonPressOnElement(GraphicElement element, MouseEvent event) {
  136. view.freezeElement(element, true);
  137. String id = element.getId();
  138. // if in Creation Mode, try to create an Edge with the Node that was
  139. // clicked on
  140. if (Main.getInstance().getCreationMode() != CreationMode.CREATE_NONE) {
  141. graphManager.createEdges(id);
  142. return;
  143. }
  144. // otherwise select the Node that was clicked
  145. graphManager.selectNode(id);
  146. }
  147. /**
  148. * Gets called when a Mouse Button is released after having pressed it down
  149. * on an Element.
  150. *
  151. * @param element
  152. * the Element that the Mouse Button was pressed on
  153. * @param event
  154. * the corresponding MouseEvent
  155. */
  156. protected void mouseButtonReleaseOffElement(GraphicElement element, MouseEvent event) {
  157. view.freezeElement(element, false);
  158. // update the Attributes of a Node after moving it
  159. PropertiesManager.setItemsProperties();
  160. }
  161. // Mouse Listener
  162. /**
  163. * Gets called whenever a Mouse Button is pressed down.
  164. *
  165. * @param event
  166. * the corresponding MouseEvent
  167. */
  168. public void mousePressed(MouseEvent event) {
  169. // Left Click -> Find out whether the User clicked on an Element
  170. if (event.getButton() == MouseEvent.BUTTON1) {
  171. curElement = view.findNodeOrSpriteAt(event.getX(), event.getY());
  172. if (curElement != null) {
  173. mouseButtonPressOnElement(curElement, event);
  174. } else {
  175. mouseButtonPress(event);
  176. }
  177. // Middle Click -> The User wants to Pan the Camera, record the
  178. // current Position and View Center
  179. } else if (event.getButton() == MouseEvent.BUTTON2) {
  180. Camera cam = Main.getInstance().getGraphManager().getView().getCamera();
  181. oldMousePos = cam.transformPxToGu(event.getX(), event.getY());
  182. oldViewCenter = cam.getViewCenter();
  183. }
  184. }
  185. /**
  186. * Gets Called whenever the Mouse is dragged while a Mouse Button is being
  187. * held down.
  188. *
  189. * @param event
  190. * the corresponding MouseEvent
  191. */
  192. public void mouseDragged(MouseEvent event) {
  193. // If the Mouse is dragging an Element, move it
  194. if (curElement != null) {
  195. elementMoving(curElement, event);
  196. // If the Middle Mouse Button is pressed, move the Camera following
  197. // the dragging gesture
  198. } else if (event.getButton() == MouseEvent.BUTTON2) {
  199. Camera cam = Main.getInstance().getGraphManager().getView().getCamera();
  200. Point3 newMousePos = cam.transformPxToGu(event.getX(), event.getY());
  201. double offsetX = oldMousePos.x - newMousePos.x;
  202. double offsetY = oldMousePos.y - newMousePos.y;
  203. double newX = oldViewCenter.x + offsetX;
  204. double newY = oldViewCenter.y + offsetY;
  205. // Only change Camera Position every 50 milliseconds, otherwise the
  206. // camera does weird things
  207. long currTime = System.currentTimeMillis();
  208. if (currTime - lastCamUpdate >= 50) {
  209. cam.setViewCenter(newX, newY, 0);
  210. lastCamUpdate = currTime;
  211. }
  212. }
  213. }
  214. /**
  215. * Gets called when a Mouse Button is released.
  216. *
  217. * @param event
  218. * the corresponding MouseEvent
  219. */
  220. public void mouseReleased(MouseEvent event) {
  221. if (curElement != null) {
  222. mouseButtonReleaseOffElement(curElement, event);
  223. curElement = null;
  224. } else {
  225. }
  226. }
  227. }