GraphManager.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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. if(g.getNode(Main.getInstance().getGraphManager().getActiveSubGraph() + newId) == null|| g.getNode(Main.getInstance().getGraphManager().getActiveSubGraph() + newId).getAttribute("originalElement") == null){
  164. deletedEdges = new LinkedList<>();
  165. deletedNode = null;
  166. return;
  167. }
  168. String origElement = GraphHelper.propagateElementUndeletion(g, e,
  169. g.getNode(newId).getAttribute("originalElement"));
  170. if (origElement != null) {
  171. g.getEdge(id).addAttribute("originalElement", origElement);
  172. }
  173. }
  174. deletedEdges = new LinkedList<>();
  175. deletedNode = null;
  176. }
  177. /**
  178. * @return the activeSubGraph
  179. */
  180. public MyGraph getActiveSubGraph() {
  181. return activeSubGraph;
  182. }
  183. /**
  184. * returns a View of the Graph. The View lives in the Swing Thread and the
  185. * Graph in the Main thread.
  186. *
  187. *
  188. * @return a View of the Graph, inheriting from JPanel
  189. */
  190. public ViewPanel getView() {
  191. return view;
  192. }
  193. /**
  194. * Returns the ID of the currently selected Node.
  195. *
  196. * @return the node's ID
  197. */
  198. public String getSelectedNodeID() {
  199. return selectedNodeID;
  200. }
  201. /**
  202. * Returns the ID of the currently selected Edge.
  203. *
  204. * @return the edge's ID
  205. */
  206. public String getSelectedEdgeID() {
  207. return selectedEdgeID;
  208. }
  209. /**
  210. * Selects the Node with the given ID, resets Edge selection.
  211. *
  212. * @param nodeID
  213. * the ID of the Node to select
  214. */
  215. public void selectNode(String nodeID) {
  216. if (nodeID != null && g.getNode(nodeID) != null) {
  217. deselect();
  218. this.selectedNodeID = nodeID;
  219. MyNode n = g.getNode(nodeID);
  220. n.addCSSClass("selected");
  221. PropertiesManager.setItemsProperties();
  222. }
  223. }
  224. /**
  225. * Selects the Edge with the given ID, resets Node selection.
  226. *
  227. * @param edgeID
  228. * the ID of the Edge to select
  229. */
  230. public void selectEdge(String edgeID) {
  231. if (edgeID != null && g.getEdge(edgeID) != null) {
  232. deselect();
  233. this.selectedEdgeID = edgeID;
  234. g.<MyEdge>getEdge(edgeID).addCSSClass("selected");
  235. PropertiesManager.setItemsProperties();
  236. }
  237. }
  238. /**
  239. * Deselect any currently selected nodes or edges.
  240. */
  241. // TODO call this before save
  242. public void deselect() {
  243. // Set last selected Edge Color to Black
  244. if (getSelectedEdgeID() != null && g.getEdge(getSelectedEdgeID()) != null) {
  245. g.<MyEdge>getEdge(getSelectedEdgeID()).removeCSSClass("selected");
  246. }
  247. // Set last selected Node color to black
  248. else if (getSelectedNodeID() != null && g.getNode(getSelectedNodeID()) != null) {
  249. g.<MyNode>getNode(getSelectedNodeID()).removeCSSClass("selected");
  250. }
  251. PropertiesManager.setItemsProperties();
  252. this.selectedNodeID = null;
  253. this.selectedEdgeID = null;
  254. }
  255. /**
  256. * Returns a reference to the Graph object managed by this visualizer.
  257. *
  258. * @return the graph
  259. */
  260. public MyGraph getGraph() {
  261. return g;
  262. }
  263. /**
  264. * Zooms in the view of the graph by 5 percent.
  265. */
  266. public void zoomIn() {
  267. zoom(-0.05);
  268. }
  269. /**
  270. * Zooms out the view of the graph by 5 percent.
  271. */
  272. public void zoomOut() {
  273. zoom(0.05);
  274. }
  275. /**
  276. * Zooms the view by the given Amount, positive values zoom out, negative
  277. * values zoom in.
  278. *
  279. * @param amount
  280. * the amount of zoom, should usually be between -0.2 and 0.2 for
  281. * reasonable zoom.
  282. */
  283. public void zoom(double amount) {
  284. view.getCamera().setViewPercent(view.getCamera().getViewPercent() * (1 + amount));
  285. }
  286. /**
  287. * Pumps the Pipe from the graphical Representation to the underlying Graph,
  288. * propagating all Changes made.
  289. */
  290. public void pumpIt() {
  291. fromViewer.pump();
  292. }
  293. @Override
  294. public String toString() {
  295. return "Visualizer for Graph \"" + g.getId() + "\"";
  296. }
  297. /**
  298. * Returns the current Save Path on Disk for the Graph.
  299. *
  300. * @return the current Path
  301. */
  302. public String getCurrentPath() {
  303. return currentPath;
  304. }
  305. /**
  306. * Sets the Save Path.
  307. *
  308. * @param currentPath
  309. * the new Path to set
  310. */
  311. public void setCurrentPath(String currentPath) {
  312. this.currentPath = currentPath;
  313. }
  314. /**
  315. * Adds a <b>Copy</b> of the given Node to the graph. The Copy retains the
  316. * ID and all attributes.
  317. *
  318. * @param n
  319. * the Node to be added to the graph
  320. */
  321. public void addNode(MyNode n) {
  322. HashMap<String, Object> attributes = new HashMap<>();
  323. for (String s : n.getAttributeKeySet()) {
  324. attributes.put(s, n.getAttribute(s));
  325. }
  326. String nodeId = n.getId();
  327. if (activeSubGraph != null){
  328. nodeId = activeSubGraph.getId() + nodeId;
  329. }
  330. g.addNode(nodeId);
  331. g.getNode(nodeId).addAttributes(attributes);
  332. if (activeSubGraph != null && !activeSubGraph.equals(g)) {
  333. activeSubGraph.addNode(n.getId());
  334. activeSubGraph.getNode(n.getId()).addAttributes(attributes);
  335. g.getNode(nodeId).addAttribute("originalElement", activeSubGraph.getId() + "+#" + n.getId());
  336. g.getNode(nodeId).addAttribute("originalGraph", activeSubGraph.getId());
  337. }
  338. }
  339. /**
  340. * Returns the smallest X Coordinate of any Node in the Graph.
  341. *
  342. * @return the smallest X Coordinate in the Graph
  343. */
  344. public double getMinX() {
  345. return g.getMinX();
  346. }
  347. /**
  348. * Returns the biggest X Coordinate of any Node in the Graph.
  349. *
  350. * @return the biggest X Coordinate in the Graph
  351. */
  352. public double getMaxX() {
  353. return g.getMaxX();
  354. }
  355. /**
  356. * Returns the smallest Y Coordinate of any Node in the Graph.
  357. *
  358. * @return the smallest Y Coordinate in the Graph
  359. */
  360. public double getMinY() {
  361. return g.getMinY();
  362. }
  363. /**
  364. * Returns the biggest Y Coordinate of any Node in the Graph.
  365. *
  366. * @return the biggest Y Coordinate in the Graph
  367. */
  368. public double getMaxY() {
  369. return g.getMaxY();
  370. }
  371. /**
  372. * Sets the Stylesheet to be used by the Graph.
  373. */
  374. public void setStylesheet() {
  375. g.addAttribute("ui.stylesheet", "edge{text-offset: 4px,-4px;}");
  376. }
  377. /**
  378. * adds the given listener to the underlying graph the listener will be
  379. * notified, when an Edge is added.
  380. *
  381. * @param e
  382. * the EdgeCreatedListener
  383. */
  384. public void addEdgeCreatedListener(EdgeCreatedListener e) {
  385. g.addEdgeCreatedListener(e);
  386. }
  387. /**
  388. * adds the given listener to the underlying graph the listener will be
  389. * notified, when a Node is added.
  390. *
  391. * @param n
  392. * the NodeCreatedListener
  393. */
  394. public void addNodeCreatedListener(NodeCreatedListener n) {
  395. g.addNodeCreatedListener(n);
  396. }
  397. /**
  398. * Sets typeofNode as the ui.class of all Nodes.
  399. *
  400. */
  401. public void convertUiClass() {
  402. Collection<MyNode> allNodes = g.getNodeSet();
  403. for (MyNode n : allNodes) {
  404. if (n.hasAttribute("typeofNode")) {
  405. n.addAttribute("ui.class", n.getAttribute("typeofNode").toString());
  406. }
  407. }
  408. }
  409. /**
  410. * Create Edges based on CreateMode.
  411. *
  412. * @param id
  413. * The ID for the newly created Edge
  414. */
  415. public void createEdges(String id) {
  416. switch (Main.getInstance().getCreationMode()) {
  417. case CREATE_DIRECTED_EDGE:
  418. case CREATE_UNDIRECTED_EDGE:
  419. if (lastClickedID == null) {
  420. lastClickedID = id;
  421. if (!selectNodeForEdgeCreation(lastClickedID)) {
  422. lastClickedID = null;
  423. }
  424. } else if (id.equals(lastClickedID) || createEdge(id, lastClickedID)) {
  425. deselectNodesAfterEdgeCreation(lastClickedID);
  426. lastClickedID = null;
  427. }
  428. break;
  429. default:
  430. break;
  431. }
  432. PropertiesManager.setItemsProperties();
  433. }
  434. /**
  435. * creates a edge between two nodes.
  436. *
  437. * @author MW
  438. * @param to
  439. * ID of the destination node
  440. * @param from
  441. * ID of the origin node
  442. * @return true if the edge was created. false otherwise
  443. */
  444. protected boolean createEdge(String to, String from) {
  445. if (getGraph().getNode(from).hasEdgeBetween(to))
  446. return false;
  447. String newID = Main.getInstance().getUnusedID();
  448. boolean isDirected = (Main.getInstance().getCreationMode() == CreationMode.CREATE_DIRECTED_EDGE);
  449. MyNode sourceNode = g.getNode(from);
  450. MyNode targetNode = g.getNode(to);
  451. if (activeSubGraph != null && !activeSubGraph.equals(g)) {
  452. if (sourceNode.getAttribute("originalGraph").equals(activeSubGraph.getId())
  453. && targetNode.getAttribute("originalGraph").equals(activeSubGraph.getId())) {
  454. g.addEdge(newID, from, to, isDirected);
  455. activeSubGraph.addEdge(newID, from.substring(activeSubGraph.getId().length()), to.substring(activeSubGraph.getId().length()), isDirected);
  456. g.getEdge(newID).addAttribute("originalElement", activeSubGraph.getId() + "+#" + newID);
  457. } else {
  458. Debug.out(sourceNode.getAttribute("originalGraph").toString() + targetNode.getAttribute("originalGraph").toString());
  459. if (sourceNode.getAttribute("originalGraph").equals(targetNode.getAttribute("originalGraph"))){
  460. Debug.out("Can Only add edges to currently active Subgraph!", 2);
  461. } else {
  462. Debug.out("Can not create Edge between Nodes of different Subgraphs!", 2);
  463. }
  464. return false;
  465. }
  466. } else {
  467. g.addEdge(newID, from, to, isDirected);
  468. }
  469. selectEdge(newID);
  470. return true;
  471. }
  472. /**
  473. * Selects a Node as the starting point for creating a new Edge.
  474. *
  475. * @param nodeID
  476. * the ID of the Node to select
  477. */
  478. protected boolean selectNodeForEdgeCreation(String nodeID) {
  479. deselect();
  480. MyNode n = getGraph().getNode(nodeID);
  481. if (!hasClass(n, UI_CLASS_PROCESSING_ENABLED) || !GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING)) {
  482. n.addCSSClass("selectedForEdgeCreation");
  483. }
  484. return true;
  485. }
  486. /**
  487. * Reset the Selection of the Node after Edge has been successfully created.
  488. *
  489. * @param nodeID
  490. * the Id of the node to deselect.
  491. */
  492. protected void deselectNodesAfterEdgeCreation(String nodeID) {
  493. MyNode n = getGraph().getNode(nodeID);
  494. if (n == null) {
  495. return;
  496. }
  497. if (!hasClass(n, UI_CLASS_PROCESSING_ENABLED) || !GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING)) {
  498. n.removeCSSClass("selectedForEdgeCreation");
  499. }
  500. }
  501. /**
  502. * Resets the selction of the Node for Edge selection
  503. */
  504. public void deselectEdgeCreationNodes() {
  505. if (lastClickedID != null)
  506. deselectNodesAfterEdgeCreation(lastClickedID);
  507. lastClickedID = null;
  508. }
  509. public void setActiveSubGraph(String id) {
  510. for (MyGraph subGraph : g.getAllSubGraphs()) {
  511. if (subGraph.getId().equals(id)) {
  512. activeSubGraph = subGraph;
  513. return;
  514. }
  515. }
  516. }
  517. protected boolean addClass(String id, String className) {
  518. Element e = getGraph().getEdge(id);
  519. if (e == null)
  520. e = getGraph().getNode(id);
  521. if (e == null)
  522. return false;
  523. String eClass = e.getAttribute("ui.class");
  524. if (eClass == null || eClass.equals(""))
  525. eClass = className;
  526. else if (!(eClass.equals(className) || eClass.startsWith(className.concat(", "))
  527. || eClass.contains(", ".concat(className))))
  528. eClass = className.concat(", ").concat(eClass);
  529. e.addAttribute("ui.class", eClass);
  530. Debug.out("added " + className + ": " + eClass);
  531. return true;
  532. }
  533. protected boolean removeClass(String id, String className) {
  534. Element e = getGraph().getEdge(id);
  535. if (e == null)
  536. e = getGraph().getNode(id);
  537. if (e == null)
  538. return false;
  539. String eClass = e.getAttribute("ui.class");
  540. if (eClass == null || eClass.equals(className))
  541. eClass = "";
  542. else
  543. eClass = eClass.replace(className.concat(", "), "").replace(", ".concat(className), "");
  544. e.addAttribute("ui.class", eClass);
  545. Debug.out("removed " + className + ": " + eClass);
  546. return true;
  547. }
  548. protected boolean toggleClass(String id, String className) {
  549. Element e = getGraph().getEdge(id);
  550. if (e == null)
  551. e = getGraph().getNode(id);
  552. if (e == null)
  553. return false;
  554. String eClass = e.getAttribute("ui.class");
  555. if (eClass == null || !(eClass.equals(className) || eClass.startsWith(className.concat(", "))
  556. || eClass.contains(", ".concat(className))))
  557. return addClass(id, className);
  558. return removeClass(id, className);
  559. }
  560. protected boolean hasClass(String id, String className) {
  561. Element e = getGraph().getEdge(id);
  562. if (e == null)
  563. e = getGraph().getNode(id);
  564. if (e == null)
  565. return false;
  566. String eClass = e.getAttribute("ui.class");
  567. return (eClass != null && (eClass.equals(className) || eClass.startsWith(className.concat(", "))
  568. || eClass.contains(", ".concat(className))));
  569. }
  570. protected boolean hasClass(MyEdge e, String className) {
  571. if (e == null)
  572. return false;
  573. String eClass = e.getAttribute("ui.class");
  574. return (eClass != null && (eClass.equals(className) || eClass.startsWith(className.concat(", "))
  575. || eClass.contains(", ".concat(className))));
  576. }
  577. protected boolean hasClass(MyNode n, String className) {
  578. if (n == null)
  579. return false;
  580. String nClass = n.getAttribute("ui.class");
  581. /*
  582. * TODO: nochmal angucken, wenn CSS Manager steht, ist gerade gehackt
  583. * damit, Vorführung läuft. return (nClass != null &&
  584. * (nClass.equals(className) ||
  585. * nClass.startsWith(className.concat(", ")) ||
  586. * nClass.contains(", ".concat(className))));
  587. */
  588. return (nClass != null && nClass.contains(className));
  589. }
  590. }