MyGraph.java 8.7 KB

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