Visualizer.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package de.tu_darmstadt.informatik.tk.scopviz.ui;
  2. import java.util.LinkedList;
  3. import java.util.*;
  4. import org.graphstream.graph.Edge;
  5. import org.graphstream.graph.Graph;
  6. import org.graphstream.graph.Node;
  7. import org.graphstream.ui.swingViewer.ViewPanel;
  8. import org.graphstream.ui.view.Viewer;
  9. import org.graphstream.ui.view.ViewerPipe;
  10. import de.tu_darmstadt.informatik.tk.scopviz.main.MyViewerListener;
  11. /**
  12. * Interface between GUI and internal Graph representation.
  13. *
  14. * @version 3.0.0.0
  15. * @author jascha-b
  16. *
  17. */
  18. public class Visualizer {
  19. //The graph of this Visualizer
  20. Graph g;
  21. //last deleted elements for undelete
  22. private Node deletedNode;
  23. private LinkedList<Edge> deletedEdges = new LinkedList<>();
  24. //Currently selected Edge or Node at least on of these is always null
  25. private String selectedNodeID = null;
  26. //TODO figure out how to do this
  27. private String selectedEdgeID = null;
  28. //View Panel of the Graph
  29. private ViewPanel view;
  30. public Visualizer(Graph graph){
  31. g=graph;
  32. Viewer viewer = new Viewer(g, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
  33. view = viewer.addDefaultView(false);
  34. viewer.setCloseFramePolicy(Viewer.CloseFramePolicy.EXIT);
  35. ViewerPipe fromViewer = viewer.newViewerPipe();
  36. fromViewer.addViewerListener(new MyViewerListener(this));
  37. fromViewer.addSink(graph);
  38. }
  39. /**
  40. * deletes the Node corresponding to the given ID from the Graph.
  41. * The referenced Graph is modified directly.
  42. * Will throw an ElementNotFoundException, when the Node is not Found
  43. * Will also remove all Edges connected to the given Node
  44. *
  45. * @param g the Graph with the Node that shall be removed
  46. * @param id the ID of the node that will be removed
  47. */
  48. public void deleteNode (final String id) {
  49. deletedEdges.removeAll(deletedEdges);
  50. deletedNode = null;
  51. //Edges have to be deleted first because they clear deletedNode
  52. //and need the Node to still be in the Graph
  53. deleteEdgesOfNode(id);
  54. deletedNode = g.removeNode(id);
  55. }
  56. /**
  57. * deletes the Edge corresponding to the given ID from the Graph.
  58. * The referenced Graph is modified directly.
  59. * Will throw an ElementNotFoundException, when the Edge is not Found
  60. *
  61. * @param g the Graph with the Edge that shall be removed
  62. * @param id the ID of the Edge that will be removed
  63. */
  64. public void deleteEdge (final String id) {
  65. deletedEdges.removeAll(deletedEdges);
  66. deletedNode = null;
  67. deletedEdges.add(g.removeEdge(id));
  68. }
  69. /**
  70. * deletes all Edges connected to the given Node
  71. * The referenced Graph is modified Directly
  72. * Will throw an ElementNotFoundException, when the Node is not Found
  73. *
  74. * @param g the Graph containing the Node
  75. * @param id the Id of the Node, whose Edges shall be removed
  76. */
  77. public void deleteEdgesOfNode (final String id) {
  78. Node node = g.getNode(id);
  79. deletedEdges.removeAll(deletedEdges);
  80. deletedNode = null;
  81. Edge[] temp = new Edge[0];
  82. temp = g.getEdgeSet().toArray(temp);
  83. for (Edge e : temp){
  84. if (e.getSourceNode().equals(node) || e.getTargetNode().equals(node)){
  85. //adds the Edge to the list of deleted Edges and remove sit from the Graph
  86. deletedEdges.add(g.removeEdge(e));
  87. }
  88. }
  89. }
  90. //TODO make undeletes Graph specific
  91. /**
  92. * Undos the last deleting operation on the given Graph
  93. * Deleting operations are: deleteNode, deleteEdge and deleteEdgesOfNode
  94. * only undos the last deleting operation even if that operation didn't change the Graph
  95. *
  96. * @param g the Graph, whose Elements shall be undeleted
  97. */
  98. public void undelete () {
  99. HashMap<String, Object> attributes = new HashMap<String, Object>();
  100. if(deletedNode!=null){
  101. for (String s : deletedNode.getAttributeKeySet()){
  102. attributes.put(s, deletedNode.getAttribute(s));
  103. }
  104. g.addNode(deletedNode.getId());
  105. g.getNode(deletedNode.getId()).addAttributes(attributes);
  106. }
  107. for (Edge e : deletedEdges){
  108. attributes = new HashMap<String, Object>();
  109. for (String s : e.getAttributeKeySet()){
  110. attributes.put(s, e.getAttribute(s));
  111. }
  112. g.addEdge(e.getId(),(Node) e.getSourceNode(),(Node) e.getTargetNode());
  113. g.getEdge(e.getId()).addAttributes(attributes);
  114. }
  115. }
  116. /**
  117. * returns a View of the Graph. The View is in the Swing Thread and the
  118. * Graph in the Main thread.
  119. *
  120. *
  121. * @return a View of the Graph, inheriting from JPanel
  122. */
  123. public ViewPanel getView (){
  124. return view;
  125. }
  126. public String getSelectedNodeID() {
  127. return selectedNodeID;
  128. }
  129. public void setSelectedNodeID(String selectedNodeID) {
  130. this.selectedNodeID = selectedNodeID;
  131. }
  132. public String getSelectedEdgeID() {
  133. return selectedEdgeID;
  134. }
  135. public void setSelectedEdgeID(String selectedEdgeID) {
  136. this.selectedEdgeID = selectedEdgeID;
  137. }
  138. public Graph getGraph() {
  139. return g;
  140. }
  141. }