MyMouseManager.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. protected void mouseButtonPress(MouseEvent event) {
  60. view.requestFocus();
  61. Point3 cursorPos = graphManager.getView().getCamera().transformPxToGu(event.getX(), event.getY());
  62. MyNode n;
  63. MyGraph nodeProducer = new MyGraph("temp");
  64. MyEdge selectedEdge = EdgeSelectionHelper.getClosestEdge(cursorPos);
  65. switch (Main.getInstance().getCreationMode()) {
  66. // If not trying to create any Nodes or Edges, select the Edge that
  67. // was clicked on
  68. case CREATE_NONE:
  69. if (selectedEdge != null) {
  70. graphManager.selectEdge(selectedEdge.getId());
  71. }
  72. break;
  73. // Otherwise, create node based on creation Mode
  74. case CREATE_STANDARD_NODE:
  75. n = nodeProducer.addNode(Main.getInstance().getUnusedID());
  76. n.setAttribute("xyz", cursorPos);
  77. n.setAttribute("ui.class", "standard");
  78. n.setAttribute("typeofNode", "standard");
  79. graphManager.addNode(n);
  80. graphManager.selectNode(n.getId());
  81. Debug.out("INFORMATION: Added Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/"
  82. + cursorPos.y + ")", 1);
  83. break;
  84. case CREATE_SOURCE_NODE:
  85. n = nodeProducer.addNode(Main.getInstance().getUnusedID());
  86. n.setAttribute("xyz", cursorPos);
  87. n.setAttribute("ui.class", "source");
  88. n.setAttribute("typeofNode", "source");
  89. graphManager.addNode(n);
  90. graphManager.selectNode(n.getId());
  91. Debug.out("INFORMATION: Added Source Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/"
  92. + cursorPos.y + ")", 1);
  93. break;
  94. case CREATE_SINK_NODE:
  95. n = nodeProducer.addNode(Main.getInstance().getUnusedID());
  96. n.setAttribute("xyz", cursorPos);
  97. n.setAttribute("ui.class", "sink");
  98. n.setAttribute("typeofNode", "sink");
  99. graphManager.addNode(n);
  100. graphManager.selectNode(n.getId());
  101. Debug.out("INFORMATION: Added Sink Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/"
  102. + cursorPos.y + ")", 1);
  103. break;
  104. case CREATE_PROC_NODE:
  105. n = nodeProducer.addNode(Main.getInstance().getUnusedID());
  106. n.setAttribute("xyz", cursorPos);
  107. n.setAttribute("ui.class", "procEn");
  108. n.setAttribute("typeofNode", "procEn");
  109. ToolboxManager.createProcMaxDialog(n);
  110. graphManager.addNode(n);
  111. graphManager.selectNode(n.getId());
  112. Debug.out("INFORMATION: Added ProcEn Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/"
  113. + cursorPos.y + ")", 1);
  114. break;
  115. case CREATE_OPERATOR_NODE:
  116. n = nodeProducer.addNode(Main.getInstance().getUnusedID());
  117. n.setAttribute("xyz", cursorPos);
  118. n.setAttribute("ui.class", "operator");
  119. n.setAttribute("typeofNode", "operator");
  120. ToolboxManager.createProcNeedDialog(n);
  121. graphManager.addNode(n);
  122. graphManager.selectNode(n.getId());
  123. Debug.out("INFORMATION: Added Operator Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/"
  124. + cursorPos.y + ")", 1);
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. /**
  131. * Gets Called whenever a Mouse Button is released while the Mouse is not
  132. * over an Element.
  133. *
  134. * @param event
  135. * the corresponding MouseEvent
  136. */
  137. protected void mouseButtonRelease(MouseEvent event) {
  138. PropertiesManager.setItemsProperties();
  139. }
  140. /**
  141. * Gets called whenever a Mouse Button is pressed down while the Mouse is
  142. * over an Element.
  143. *
  144. * @param element
  145. * the element that was clicked on
  146. * @param event
  147. * the corresponding MouseEvent
  148. */
  149. protected void mouseButtonPressOnElement(GraphicElement element, MouseEvent event) {
  150. view.freezeElement(element, true);
  151. String id = element.getId();
  152. // if in Creation Mode, try to create an Edge with the Node that was
  153. // clicked on
  154. if (Main.getInstance().getCreationMode() != CreationMode.CREATE_NONE) {
  155. graphManager.createEdges(id);
  156. return;
  157. }
  158. // otherwise select the Node that was clicked
  159. graphManager.selectNode(id);
  160. }
  161. /**
  162. * Gets called when a Mouse Button is released after having pressed it down
  163. * on an Element.
  164. *
  165. * @param element
  166. * the Element that the Mouse Button was pressed on
  167. * @param event
  168. * the corresponding MouseEvent
  169. */
  170. protected void mouseButtonReleaseOffElement(GraphicElement element, MouseEvent event) {
  171. view.freezeElement(element, false);
  172. // update the Attributes of a Node after moving it
  173. PropertiesManager.setItemsProperties();
  174. }
  175. // Mouse Listener
  176. /**
  177. * Gets called whenever a Mouse Button is pressed down.
  178. *
  179. * @param event
  180. * the corresponding MouseEvent
  181. */
  182. public void mousePressed(MouseEvent event) {
  183. // Left Click -> Find out whether the User clicked on an Element
  184. if (event.getButton() == MouseEvent.BUTTON1) {
  185. curElement = view.findNodeOrSpriteAt(event.getX(), event.getY());
  186. if (curElement != null) {
  187. mouseButtonPressOnElement(curElement, event);
  188. } else {
  189. mouseButtonPress(event);
  190. }
  191. // Middle Click -> The User wants to Pan the Camera, record the
  192. // current Position and View Center
  193. } else if (event.getButton() == MouseEvent.BUTTON3) {
  194. Camera cam = Main.getInstance().getGraphManager().getView().getCamera();
  195. oldMousePos = cam.transformPxToGu(event.getX(), event.getY());
  196. oldViewCenter = cam.getViewCenter();
  197. }
  198. }
  199. /**
  200. * Gets Called whenever the Mouse is dragged while a Mouse Button is being
  201. * held down.
  202. *
  203. * @param event
  204. * the corresponding MouseEvent
  205. */
  206. public void mouseDragged(MouseEvent event) {
  207. // If the Mouse is dragging an Element, move it
  208. if (curElement != null) {
  209. elementMoving(curElement, event);
  210. // If the Middle Mouse Button is pressed, move the Camera following
  211. // the dragging gesture
  212. } else if (event.getButton() == MouseEvent.BUTTON3) {
  213. Camera cam = Main.getInstance().getGraphManager().getView().getCamera();
  214. Point3 newMousePos = cam.transformPxToGu(event.getX(), event.getY());
  215. double offsetX = oldMousePos.x - newMousePos.x;
  216. double offsetY = oldMousePos.y - newMousePos.y;
  217. double newX = oldViewCenter.x + offsetX;
  218. double newY = oldViewCenter.y + offsetY;
  219. // Only change Camera Position every 50 milliseconds, otherwise the
  220. // camera does weird things
  221. long currTime = System.currentTimeMillis();
  222. if (currTime - lastCamUpdate >= 50) {
  223. cam.setViewCenter(newX, newY, 0);
  224. lastCamUpdate = currTime;
  225. }
  226. }
  227. }
  228. /**
  229. * Gets called when a Mouse Button is released.
  230. *
  231. * @param event
  232. * the corresponding MouseEvent
  233. */
  234. public void mouseReleased(MouseEvent event) {
  235. if (curElement != null) {
  236. mouseButtonReleaseOffElement(curElement, event);
  237. curElement = null;
  238. } else {
  239. mouseButtonRelease(event);
  240. }
  241. }
  242. }