MyMouseManager.java 8.8 KB

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