GraphManager.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. package de.tu_darmstadt.informatik.tk.scopviz.graphs;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import org.graphstream.graph.Element;
  6. import org.graphstream.ui.swingViewer.ViewPanel;
  7. import org.graphstream.ui.view.Viewer;
  8. import org.graphstream.ui.view.ViewerPipe;
  9. import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  10. import de.tu_darmstadt.informatik.tk.scopviz.main.CreationMode;
  11. import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
  12. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  13. import de.tu_darmstadt.informatik.tk.scopviz.ui.GraphDisplayManager;
  14. import de.tu_darmstadt.informatik.tk.scopviz.ui.PropertiesManager;
  15. import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.MyMouseManager;
  16. /**
  17. * Interface between GUI and internal Graph representation. Manages internal
  18. * representation of the Graph to accommodate creation and deletion of nodes and
  19. * edges.
  20. *
  21. * @author Jascha Bohne
  22. * @version 3.0.0.0
  23. *
  24. */
  25. public class GraphManager {
  26. /** String for the processing enabled type of node. */
  27. public static final String UI_CLASS_PROCESSING_ENABLED = "procEn";
  28. /** The Graph this instance of GraphManager manages. */
  29. protected MyGraph g;
  30. protected MyGraph activeSubGraph;
  31. /** The last Node that was deleted. */
  32. protected MyNode deletedNode;
  33. /** The last Edge that was deleted. */
  34. protected LinkedList<MyEdge> deletedEdges = new LinkedList<>();
  35. /** The currently selected Node, mutually exclusive with selectedEdgeID. */
  36. protected String selectedNodeID = null;
  37. /** The currently selected Edge, mutually exclusive with selectedNodeID. */
  38. protected String selectedEdgeID = null;
  39. /** The ViewPanel the Graph is drawn in. */
  40. protected ViewPanel view;
  41. /** The Path on Disk the Graph will be saved to. */
  42. protected String currentPath;
  43. /** The Viewer the Graph provides, grants Access to Camera Manipulation. */
  44. protected Viewer viewer;
  45. /**
  46. * The Pipe that notifies the underlying Graph of any Changes within the
  47. * graphic Representation.
  48. */
  49. protected ViewerPipe fromViewer;
  50. /**
  51. * The Id of the Node that was last clicked.
  52. */
  53. protected String lastClickedID;
  54. /**
  55. * Creates a new Manager for the given graph.
  56. *
  57. * @param graph
  58. * the graph this visualizer should handle
  59. */
  60. public GraphManager(MyGraph graph) {
  61. g = graph;
  62. /* Viewer */ viewer = new Viewer(g, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
  63. view = viewer.addDefaultView(false);
  64. viewer.setCloseFramePolicy(Viewer.CloseFramePolicy.EXIT);
  65. /* ViewerPipe */fromViewer = viewer.newViewerPipe();
  66. view.setMouseManager(new MyMouseManager(this));
  67. fromViewer.addSink(graph);
  68. fromViewer.removeElementSink(graph);
  69. }
  70. /**
  71. * Deletes the Node corresponding to the given ID from the Graph. The
  72. * referenced Graph is modified directly. Will throw an
  73. * ElementNotFoundException, when the Node is not Found Will also remove all
  74. * Edges connected to the given Node
  75. *
  76. * @param id
  77. * the ID of the node that will be removed
  78. */
  79. public void deleteNode(final String id) {
  80. deletedEdges.removeAll(deletedEdges);
  81. deletedNode = null;
  82. // Edges have to be deleted first because they clear deletedNode
  83. // and need the Node to still be in the Graph
  84. deleteEdgesOfNode(id);
  85. deletedNode = g.removeNode(id);
  86. }
  87. /**
  88. * Deletes the Edge corresponding to the given ID from the Graph. The
  89. * referenced Graph is modified directly. Will throw an
  90. * ElementNotFoundException, when the Edge is not Found
  91. *
  92. * @param id
  93. * the ID of the Edge that will be removed
  94. */
  95. public void deleteEdge(final String id) {
  96. deselect();
  97. deletedEdges.removeAll(deletedEdges);
  98. deletedNode = null;
  99. deletedEdges.add(g.removeEdge(id));
  100. }
  101. /**
  102. * Deletes all Edges connected to the given Node. The referenced Graph is
  103. * modified directly. Will throw an ElementNotFoundException if the Node is
  104. * not Found
  105. *
  106. * @param id
  107. * the Id of the Node, whose Edges shall be removed
  108. */
  109. protected void deleteEdgesOfNode(final String id) {
  110. deselect();
  111. MyNode node = g.getNode(id);
  112. deletedEdges.removeAll(deletedEdges);
  113. deletedNode = null;
  114. MyEdge[] temp = new MyEdge[0];
  115. temp = g.getEdgeSet().toArray(temp);
  116. for (MyEdge e : temp) {
  117. if (e.getSourceNode().equals(node) || e.getTargetNode().equals(node)) {
  118. // adds the Edge to the list of deleted Edges and remove sit
  119. // from the Graph
  120. deletedEdges.add(g.removeEdge(e));
  121. }
  122. }
  123. GraphHelper.propagateElementDeletion(g, deletedEdges);
  124. }
  125. /**
  126. * Undoes the last deleting operation on the given Graph. Deleting
  127. * operations are: deleteNode, deleteEdge and deleteEdgesOfNode. Only undoes
  128. * the last deleting operation even if that operation didn't change the
  129. * Graph
  130. */
  131. public void undelete() {
  132. String newId = "";
  133. HashMap<String, Object> attributes = new HashMap<String, Object>();
  134. if (deletedNode != null) {
  135. for (String s : deletedNode.getAttributeKeySet()) {
  136. attributes.put(s, deletedNode.getAttribute(s));
  137. }
  138. newId = Main.getInstance().getUnusedID();
  139. g.addNode(newId);
  140. g.getNode(newId).addAttributes(attributes);
  141. String origElement = GraphHelper.propagateElementUndeletion(g, deletedNode, null);
  142. if (origElement != null) {
  143. g.getNode(newId).addAttribute("originalElement", origElement);
  144. }
  145. }
  146. for (MyEdge e : deletedEdges) {
  147. String sourceId = null;
  148. String targetId = null;
  149. attributes = new HashMap<String, Object>();
  150. for (String s : e.getAttributeKeySet()) {
  151. attributes.put(s, e.getAttribute(s));
  152. }
  153. String id = Main.getInstance().getUnusedID();
  154. if (deletedNode != null) {
  155. sourceId = (e.getSourceNode().getId().equals(deletedNode.getId())) ? newId : e.getSourceNode().getId();
  156. targetId = (e.getTargetNode().getId().equals(deletedNode.getId())) ? newId : e.getTargetNode().getId();
  157. } else {
  158. sourceId = e.getSourceNode().getId();
  159. targetId = e.getTargetNode().getId();
  160. }
  161. g.addEdge(id, sourceId, targetId, e.isDirected());
  162. g.getEdge(id).addAttributes(attributes);
  163. String origElement = GraphHelper.propagateElementUndeletion(g, e,
  164. g.getNode(newId).getAttribute("originalElement"));
  165. if (origElement != null) {
  166. g.getEdge(id).addAttribute("originalElement", origElement);
  167. }
  168. }
  169. deletedEdges = new LinkedList<>();
  170. deletedNode = null;
  171. }
  172. /**
  173. * returns a View of the Graph. The View lives in the Swing Thread and the
  174. * Graph in the Main thread.
  175. *
  176. *
  177. * @return a View of the Graph, inheriting from JPanel
  178. */
  179. public ViewPanel getView() {
  180. return view;
  181. }
  182. /**
  183. * Returns the ID of the currently selected Node.
  184. *
  185. * @return the node's ID
  186. */
  187. public String getSelectedNodeID() {
  188. return selectedNodeID;
  189. }
  190. /**
  191. * Returns the ID of the currently selected Edge.
  192. *
  193. * @return the edge's ID
  194. */
  195. public String getSelectedEdgeID() {
  196. return selectedEdgeID;
  197. }
  198. /**
  199. * Selects the Node with the given ID, resets Edge selection.
  200. *
  201. * @param nodeID
  202. * the ID of the Node to select
  203. */
  204. public void selectNode(String nodeID) {
  205. if (nodeID != null && g.getNode(nodeID) != null) {
  206. deselect();
  207. this.selectedNodeID = nodeID;
  208. MyNode n = g.getNode(nodeID);
  209. n.addCSSClass("selected");
  210. PropertiesManager.setItemsProperties();
  211. }
  212. }
  213. /**
  214. * Selects the Edge with the given ID, resets Node selection.
  215. *
  216. * @param edgeID
  217. * the ID of the Edge to select
  218. */
  219. public void selectEdge(String edgeID) {
  220. if (edgeID != null && g.getEdge(edgeID) != null) {
  221. deselect();
  222. this.selectedEdgeID = edgeID;
  223. g.<MyEdge>getEdge(edgeID).addCSSClass("selected");
  224. PropertiesManager.setItemsProperties();
  225. }
  226. }
  227. /**
  228. * Deselect any currently selected nodes or edges.
  229. */
  230. // TODO call this before save
  231. protected void deselect() {
  232. // Set last selected Edge Color to Black
  233. if (getSelectedEdgeID() != null && g.getEdge(getSelectedEdgeID()) != null) {
  234. g.<MyEdge>getEdge(getSelectedEdgeID()).removeCSSClass("selected");
  235. }
  236. // Set last selected Node color to black
  237. else if (getSelectedNodeID() != null && g.getNode(getSelectedNodeID()) != null) {
  238. g.<MyNode>getNode(getSelectedNodeID()).removeCSSClass("selected");
  239. }
  240. PropertiesManager.setItemsProperties();
  241. this.selectedNodeID = null;
  242. this.selectedEdgeID = null;
  243. }
  244. /**
  245. * Returns a reference to the Graph object managed by this visualizer.
  246. *
  247. * @return the graph
  248. */
  249. public MyGraph getGraph() {
  250. return g;
  251. }
  252. /**
  253. * Zooms in the view of the graph by 5 percent.
  254. */
  255. public void zoomIn() {
  256. zoom(-0.05);
  257. }
  258. /**
  259. * Zooms out the view of the graph by 5 percent.
  260. */
  261. public void zoomOut() {
  262. zoom(0.05);
  263. }
  264. /**
  265. * Zooms the view by the given Amount, positive values zoom out, negative
  266. * values zoom in.
  267. *
  268. * @param amount
  269. * the amount of zoom, should usually be between -0.2 and 0.2 for
  270. * reasonable zoom.
  271. */
  272. public void zoom(double amount) {
  273. view.getCamera().setViewPercent(view.getCamera().getViewPercent() * (1 + amount));
  274. }
  275. /**
  276. * Pumps the Pipe from the graphical Representation to the underlying Graph,
  277. * propagating all Changes made.
  278. */
  279. public void pumpIt() {
  280. fromViewer.pump();
  281. }
  282. @Override
  283. public String toString() {
  284. return "Visualizer for Graph \"" + g.getId() + "\"";
  285. }
  286. /**
  287. * Returns the current Save Path on Disk for the Graph.
  288. *
  289. * @return the current Path
  290. */
  291. public String getCurrentPath() {
  292. return currentPath;
  293. }
  294. /**
  295. * Sets the Save Path.
  296. *
  297. * @param currentPath
  298. * the new Path to set
  299. */
  300. public void setCurrentPath(String currentPath) {
  301. this.currentPath = currentPath;
  302. }
  303. /**
  304. * Adds a <b>Copy</b> of the given Edge to the graph. The Copy retains the
  305. * ID and all attributes.
  306. *
  307. * @param e
  308. * the Edge to be added to the graph
  309. */
  310. public void addEdge(MyEdge e) {
  311. HashMap<String, Object> attributes = new HashMap<>();
  312. for (String s : e.getAttributeKeySet()) {
  313. attributes.put(s, e.getAttribute(s));
  314. }
  315. g.addEdge(e.getId(), (MyNode) e.getSourceNode(), (MyNode) e.getTargetNode(), e.isDirected());
  316. g.getEdge(e.getId()).addAttributes(attributes);
  317. if (activeSubGraph != null) {
  318. activeSubGraph.addEdge(e.getId(), (MyNode) e.getSourceNode(), (MyNode) e.getTargetNode(), e.isDirected());
  319. activeSubGraph.getEdge(e.getId()).addAttributes(attributes);
  320. g.getEdge(e.getId()).addAttribute("originalElement", activeSubGraph.getId() + "+#" + e.getId());
  321. }
  322. }
  323. /**
  324. * Adds a <b>Copy</b> of the given Node to the graph. The Copy retains the
  325. * ID and all attributes.
  326. *
  327. * @param n
  328. * the Node to be added to the graph
  329. */
  330. public void addNode(MyNode n) {
  331. HashMap<String, Object> attributes = new HashMap<>();
  332. for (String s : n.getAttributeKeySet()) {
  333. attributes.put(s, n.getAttribute(s));
  334. }
  335. g.addNode(n.getId());
  336. g.getNode(n.getId()).addAttributes(attributes);
  337. if (activeSubGraph != null) {
  338. activeSubGraph.addNode(n.getId());
  339. activeSubGraph.getNode(n.getId()).addAttributes(attributes);
  340. g.getNode(n.getId()).addAttribute("originalElement", activeSubGraph.getId() + "+#" + n.getId());
  341. }
  342. }
  343. /**
  344. * Returns the smallest X Coordinate of any Node in the Graph.
  345. *
  346. * @return the smallest X Coordinate in the Graph
  347. */
  348. public double getMinX() {
  349. return g.getMinX();
  350. }
  351. /**
  352. * Returns the biggest X Coordinate of any Node in the Graph.
  353. *
  354. * @return the biggest X Coordinate in the Graph
  355. */
  356. public double getMaxX() {
  357. return g.getMaxX();
  358. }
  359. /**
  360. * Returns the smallest Y Coordinate of any Node in the Graph.
  361. *
  362. * @return the smallest Y Coordinate in the Graph
  363. */
  364. public double getMinY() {
  365. return g.getMinY();
  366. }
  367. /**
  368. * Returns the biggest Y Coordinate of any Node in the Graph.
  369. *
  370. * @return the biggest Y Coordinate in the Graph
  371. */
  372. public double getMaxY() {
  373. return g.getMaxY();
  374. }
  375. /**
  376. * Sets the Stylesheet to be used by the Graph.
  377. */
  378. public void setStylesheet() {
  379. g.addAttribute("ui.stylesheet", "edge{text-offset: 4px,-4px;}");
  380. }
  381. /**
  382. * adds the given listener to the underlying graph the listener will be
  383. * notified, when an Edge is added.
  384. *
  385. * @param e
  386. * the EdgeCreatedListener
  387. */
  388. public void addEdgeCreatedListener(EdgeCreatedListener e) {
  389. g.addEdgeCreatedListener(e);
  390. }
  391. /**
  392. * adds the given listener to the underlying graph the listener will be
  393. * notified, when a Node is added.
  394. *
  395. * @param n
  396. * the NodeCreatedListener
  397. */
  398. public void addNodeCreatedListener(NodeCreatedListener n) {
  399. g.addNodeCreatedListener(n);
  400. }
  401. /**
  402. * Sets typeofNode as the ui.class of all Nodes.
  403. *
  404. */
  405. public void convertUiClass() {
  406. Collection<MyNode> allNodes = g.getNodeSet();
  407. for (MyNode n : allNodes) {
  408. if (n.hasAttribute("typeofNode")) {
  409. n.addAttribute("ui.class", n.getAttribute("typeofNode").toString());
  410. }
  411. }
  412. }
  413. /**
  414. * Create Edges based on CreateMode.
  415. *
  416. * @param id
  417. * The ID for the newly created Edge
  418. */
  419. public void createEdges(String id) {
  420. switch (Main.getInstance().getCreationMode()) {
  421. case CREATE_DIRECTED_EDGE:
  422. case CREATE_UNDIRECTED_EDGE:
  423. if (lastClickedID == null) {
  424. lastClickedID = id;
  425. if (!selectNodeForEdgeCreation(lastClickedID)) {
  426. lastClickedID = null;
  427. }
  428. } else if (id.equals(lastClickedID) || createEdge(id, lastClickedID)) {
  429. deselectNodesAfterEdgeCreation(lastClickedID);
  430. lastClickedID = null;
  431. }
  432. break;
  433. default:
  434. break;
  435. }
  436. PropertiesManager.setItemsProperties();
  437. }
  438. /**
  439. * creates a edge between two nodes.
  440. *
  441. * @author MW
  442. * @param to
  443. * ID of the destination node
  444. * @param from
  445. * ID of the origin node
  446. * @return true if the edge was created. false otherwise
  447. */
  448. protected boolean createEdge(String to, String from) {
  449. if (getGraph().getNode(from).hasEdgeBetween(to))
  450. return false;
  451. String newID = Main.getInstance().getUnusedID();
  452. if (Main.getInstance().getCreationMode() == CreationMode.CREATE_DIRECTED_EDGE) {
  453. getGraph().addEdge(newID, from, to, true);
  454. Debug.out("Created an directed edge with Id " + newID + " between " + from + " and " + to);
  455. } else {
  456. getGraph().addEdge(newID, from, to);
  457. Debug.out("Created an undirected edge with Id " + newID + " between " + from + " and " + to);
  458. }
  459. selectEdge(newID);
  460. return true;
  461. }
  462. /**
  463. * Selects a Node as the starting point for creating a new Edge.
  464. *
  465. * @param nodeID
  466. * the ID of the Node to select
  467. */
  468. protected boolean selectNodeForEdgeCreation(String nodeID) {
  469. deselect();
  470. MyNode n = getGraph().getNode(nodeID);
  471. if (!hasClass(n, UI_CLASS_PROCESSING_ENABLED) || !GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING)) {
  472. n.addCSSClass("selectedForEdgeCreation");
  473. }
  474. return true;
  475. }
  476. /**
  477. * Reset the Selection of the Node after Edge has been successfully created.
  478. *
  479. * @param nodeID
  480. * the Id of the node to deselect.
  481. */
  482. protected void deselectNodesAfterEdgeCreation(String nodeID) {
  483. MyNode n = getGraph().getNode(nodeID);
  484. if (n == null) {
  485. return;
  486. }
  487. if (!hasClass(n, UI_CLASS_PROCESSING_ENABLED) || !GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING)) {
  488. n.removeCSSClass("selectedForEdgeCreation");
  489. }
  490. }
  491. /**
  492. * Resets the selction of the Node for Edge selection
  493. */
  494. public void deselectEdgeCreationNodes() {
  495. if (lastClickedID != null)
  496. deselectNodesAfterEdgeCreation(lastClickedID);
  497. lastClickedID = null;
  498. }
  499. public void setActiveSubGraph(String id) {
  500. for (MyGraph subGraph : g.getAllSubGraphs()) {
  501. if (subGraph.getId().equals(id)) {
  502. activeSubGraph = subGraph;
  503. return;
  504. }
  505. }
  506. }
  507. protected boolean addClass(String id, String className) {
  508. Element e = getGraph().getEdge(id);
  509. if (e == null)
  510. e = getGraph().getNode(id);
  511. if (e == null)
  512. return false;
  513. String eClass = e.getAttribute("ui.class");
  514. if (eClass == null || eClass.equals(""))
  515. eClass = className;
  516. else if (!(eClass.equals(className) || eClass.startsWith(className.concat(", "))
  517. || eClass.contains(", ".concat(className))))
  518. eClass = className.concat(", ").concat(eClass);
  519. e.addAttribute("ui.class", eClass);
  520. Debug.out("added " + className + ": " + eClass);
  521. return true;
  522. }
  523. protected boolean removeClass(String id, String className) {
  524. Element e = getGraph().getEdge(id);
  525. if (e == null)
  526. e = getGraph().getNode(id);
  527. if (e == null)
  528. return false;
  529. String eClass = e.getAttribute("ui.class");
  530. if (eClass == null || eClass.equals(className))
  531. eClass = "";
  532. else
  533. eClass = eClass.replace(className.concat(", "), "").replace(", ".concat(className), "");
  534. e.addAttribute("ui.class", eClass);
  535. Debug.out("removed " + className + ": " + eClass);
  536. return true;
  537. }
  538. protected boolean toggleClass(String id, String className) {
  539. Element e = getGraph().getEdge(id);
  540. if (e == null)
  541. e = getGraph().getNode(id);
  542. if (e == null)
  543. return false;
  544. String eClass = e.getAttribute("ui.class");
  545. if (eClass == null || !(eClass.equals(className) || eClass.startsWith(className.concat(", "))
  546. || eClass.contains(", ".concat(className))))
  547. return addClass(id, className);
  548. return removeClass(id, className);
  549. }
  550. protected boolean hasClass(String id, String className) {
  551. Element e = getGraph().getEdge(id);
  552. if (e == null)
  553. e = getGraph().getNode(id);
  554. if (e == null)
  555. return false;
  556. String eClass = e.getAttribute("ui.class");
  557. return (eClass != null && (eClass.equals(className) || eClass.startsWith(className.concat(", "))
  558. || eClass.contains(", ".concat(className))));
  559. }
  560. protected boolean hasClass(MyEdge e, String className) {
  561. if (e == null)
  562. return false;
  563. String eClass = e.getAttribute("ui.class");
  564. return (eClass != null && (eClass.equals(className) || eClass.startsWith(className.concat(", "))
  565. || eClass.contains(", ".concat(className))));
  566. }
  567. protected boolean hasClass(MyNode n, String className) {
  568. if (n == null)
  569. return false;
  570. String nClass = n.getAttribute("ui.class");
  571. /*
  572. * TODO: nochmal angucken, wenn CSS Manager steht, ist gerade gehackt
  573. * damit, Vorführung läuft. return (nClass != null &&
  574. * (nClass.equals(className) ||
  575. * nClass.startsWith(className.concat(", ")) ||
  576. * nClass.contains(", ".concat(className))));
  577. */
  578. return (nClass != null && nClass.contains(className));
  579. }
  580. }