MyGraph.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package de.tu_darmstadt.informatik.tk.scopviz.graphs;
  2. import java.util.Iterator;
  3. import java.util.LinkedList;
  4. import org.graphstream.graph.Edge;
  5. import org.graphstream.graph.Node;
  6. import org.graphstream.graph.implementations.SingleGraph;
  7. import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  8. import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
  9. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  10. import de.tu_darmstadt.informatik.tk.scopviz.ui.OptionsManager;
  11. import de.tu_darmstadt.informatik.tk.scopviz.ui.ToolboxManager;
  12. /**
  13. * Our own Class to extend GraphStreams Graph with our own Functionality.
  14. *
  15. * @author Jan Enders (jan.enders@stud.tu-darmstadt.de)
  16. * @version 1.1
  17. *
  18. */
  19. public class MyGraph extends SingleGraph {
  20. /** List of all Edge Creation listeners. */
  21. private LinkedList<EdgeCreatedListener> allEdgeListeners = new LinkedList<EdgeCreatedListener>();
  22. /** List of all Node Creation listeners. */
  23. private LinkedList<NodeCreatedListener> allNodeListeners = new LinkedList<NodeCreatedListener>();
  24. private boolean composite = false;
  25. private LinkedList<MyGraph> children = new LinkedList<MyGraph>();
  26. /**
  27. * Creates an empty graph with strict checking and without auto-creation.
  28. *
  29. * @param id
  30. * Unique identifier of the graph.
  31. */
  32. public MyGraph(final String id) {
  33. super(id);
  34. }
  35. /**
  36. * Creates an empty graph with default edge and node capacity.
  37. *
  38. * @param id
  39. * Unique identifier of the graph
  40. * @param strictChecking
  41. * If true any non-fatal error throws an exception.
  42. * @param autoCreate
  43. * If true (and strict checking is false), nodes are
  44. * automatically created when referenced when creating a edge,
  45. * even if not yet inserted in the graph.
  46. */
  47. public MyGraph(final String id, final boolean strictChecking, final boolean autoCreate) {
  48. super(id, strictChecking, autoCreate);
  49. }
  50. /**
  51. * Creates an empty graph.
  52. *
  53. * @param id
  54. * Unique identifier of the graph.
  55. * @param strictChecking
  56. * If true any non-fatal error throws an exception.
  57. * @param autoCreate
  58. * If true (and strict checking is false), nodes are
  59. * automatically created when referenced when creating a edge,
  60. * even if not yet inserted in the graph.
  61. * @param initialNodeCapacity
  62. * Initial capacity of the node storage data structures. Use this
  63. * if you know the approximate maximum number of nodes of the
  64. * graph. The graph can grow beyond this limit, but storage
  65. * reallocation is expensive operation.
  66. * @param initialEdgeCapacity
  67. * Initial capacity of the edge storage data structures. Use this
  68. * if you know the approximate maximum number of edges of the
  69. * graph. The graph can grow beyond this limit, but storage
  70. * reallocation is expensive operation.
  71. */
  72. public MyGraph(final String id, final boolean strictChecking, final boolean autoCreate,
  73. final int initialNodeCapacity, final int initialEdgeCapacity) {
  74. super(id, strictChecking, autoCreate, initialNodeCapacity, initialEdgeCapacity);
  75. }
  76. /**
  77. * adds the given Listener to the Graph all listeners will be notified when
  78. * an Edge is created.
  79. *
  80. * @param e
  81. * the listener that has to be added
  82. */
  83. public void addEdgeCreatedListener(EdgeCreatedListener e) {
  84. allEdgeListeners.add(e);
  85. }
  86. /**
  87. * Notifies all added EdgeCreatedListeners.
  88. *
  89. * @param e
  90. * the Edge that was just created
  91. */
  92. private void edgeCreatedNotify(Edge e) {
  93. boolean doWeight = Layer.UNDERLAY.equals(this.getAttribute("layer")) && (e.getAttribute("weight") == null
  94. || (e.getAttribute("weight") != null && (OptionsManager.getDefaultWeight() == Main.getInstance()
  95. .convertAttributeTypes(e.getAttribute("weight"), new Double(0.0)))));
  96. if (doWeight) {
  97. Debug.out("MyGraph:105 " + e.getId() + " " + e.getAttribute("weight"));
  98. ToolboxManager.createWeightDialog(e);
  99. }
  100. for (EdgeCreatedListener list : allEdgeListeners) {
  101. list.edgeCreated(e, id);
  102. }
  103. }
  104. /**
  105. * adds the given Listener to the Graph all listeners will be notified when
  106. * a Node is created.
  107. *
  108. * @param n
  109. * the listener that has to be added
  110. */
  111. public void addNodeCreatedListener(NodeCreatedListener n) {
  112. allNodeListeners.add(n);
  113. }
  114. /**
  115. * Notifies all added NodeCreatedListener. also sets defaults
  116. *
  117. * @param n
  118. * the Edge that was just created
  119. */
  120. private void nodeCreatedNotify(Node n) {
  121. GraphHelper.setAllDefaults(this);
  122. for (NodeCreatedListener list : allNodeListeners) {
  123. list.nodeCreated(n, id);
  124. }
  125. }
  126. /**
  127. * {@inheritDoc}
  128. */
  129. @Override
  130. public <T extends Edge> T addEdge(String id, int index1, int index2) {
  131. T e = super.addEdge(id, index1, index2);
  132. edgeCreatedNotify(e);
  133. return e;
  134. }
  135. /**
  136. * {@inheritDoc}
  137. */
  138. @Override
  139. public <T extends Edge> T addEdge(String id, Node node1, Node node2) {
  140. T e = super.addEdge(id, node1, node2);
  141. edgeCreatedNotify(e);
  142. return e;
  143. }
  144. /**
  145. * {@inheritDoc}
  146. */
  147. @Override
  148. public <T extends Edge> T addEdge(String id, String node1, String node2) {
  149. T e = super.addEdge(id, node1, node2);
  150. edgeCreatedNotify(e);
  151. return e;
  152. }
  153. /**
  154. * {@inheritDoc}
  155. */
  156. @Override
  157. public <T extends Edge> T addEdge(String id, int fromIndex, int toIndex, boolean directed) {
  158. T e = super.addEdge(id, fromIndex, toIndex, directed);
  159. edgeCreatedNotify(e);
  160. return e;
  161. }
  162. /**
  163. * {@inheritDoc}
  164. */
  165. @Override
  166. public <T extends Edge> T addEdge(String id, Node from, Node to, boolean directed) {
  167. T e = super.addEdge(id, from, to, directed);
  168. edgeCreatedNotify(e);
  169. return e;
  170. }
  171. /**
  172. * {@inheritDoc}
  173. */
  174. @Override
  175. public <T extends Edge> T addEdge(String id, String from, String to, boolean directed) {
  176. T e = super.addEdge(id, from, to, directed);
  177. edgeCreatedNotify(e);
  178. return e;
  179. }
  180. /**
  181. * {@inheritDoc}
  182. */
  183. @Override
  184. public <T extends Node> T addNode(String id) {
  185. T n = super.addNode(id);
  186. nodeCreatedNotify(n);
  187. return n;
  188. }
  189. /**
  190. * Returns the smallest X Coordinate of any Node in the Graph.
  191. *
  192. * @return the smallest X Coordinate in the Graph
  193. */
  194. public double getMinX() {
  195. double currentMin = Double.MAX_VALUE;
  196. Node n = null;
  197. Iterator<Node> allNodes = getNodeIterator();
  198. while (allNodes.hasNext()) {
  199. n = allNodes.next();
  200. if (n.hasAttribute("x") && currentMin > (Double) n.getAttribute("x")) {
  201. currentMin = (Double) n.getAttribute("x");
  202. }
  203. }
  204. if (currentMin == Double.MAX_VALUE) {
  205. return 0;
  206. }
  207. return currentMin;
  208. }
  209. /**
  210. * Returns the biggest X Coordinate of any Node in the Graph.
  211. *
  212. * @return the biggest X Coordinate in the Graph
  213. */
  214. public double getMaxX() {
  215. double currentMax = Double.MIN_VALUE;
  216. Node n = null;
  217. Iterator<Node> allNodes = getNodeIterator();
  218. while (allNodes.hasNext()) {
  219. n = allNodes.next();
  220. if (n.hasAttribute("x") && currentMax < (Double) n.getAttribute("x")) {
  221. currentMax = (Double) n.getAttribute("x");
  222. }
  223. }
  224. if (currentMax == Double.MIN_VALUE) {
  225. return 0;
  226. }
  227. return currentMax;
  228. }
  229. /**
  230. * Returns the smallest Y Coordinate of any Node in the Graph.
  231. *
  232. * @return the smallest Y Coordinate in the Graph
  233. */
  234. public double getMinY() {
  235. double currentMin = Double.MAX_VALUE;
  236. Node n = null;
  237. Iterator<Node> allNodes = getNodeIterator();
  238. while (allNodes.hasNext()) {
  239. n = allNodes.next();
  240. if (n.hasAttribute("y") && currentMin > (Double) n.getAttribute("y")) {
  241. currentMin = (Double) n.getAttribute("y");
  242. }
  243. }
  244. if (currentMin == Double.MAX_VALUE) {
  245. return 0;
  246. }
  247. return currentMin;
  248. }
  249. /**
  250. * Returns the biggest Y Coordinate of any Node in the Graph.
  251. *
  252. * @return the biggest Y Coordinate in the Graph
  253. */
  254. public double getMaxY() {
  255. double currentMax = Double.MIN_VALUE;
  256. Node n = null;
  257. Iterator<Node> allNodes = getNodeIterator();
  258. while (allNodes.hasNext()) {
  259. n = allNodes.next();
  260. if (n.hasAttribute("y") && currentMax < (Double) n.getAttribute("y")) {
  261. currentMax = (Double) n.getAttribute("y");
  262. }
  263. }
  264. if (currentMax == Double.MIN_VALUE) {
  265. return 0;
  266. }
  267. return currentMax;
  268. }
  269. public void addSubGraph(MyGraph g) {
  270. composite = true;
  271. children.add(g);
  272. }
  273. public boolean isComposite() {
  274. return composite;
  275. }
  276. public LinkedList<MyGraph> getAllSubGraphs() {
  277. LinkedList<MyGraph> result = new LinkedList<MyGraph>();
  278. result.add(this);
  279. for (MyGraph g : children) {
  280. result.addAll(g.getAllSubGraphs());
  281. }
  282. return result;
  283. }
  284. public LinkedList<MyGraph> getChildren() {
  285. return children;
  286. }
  287. }