MyGraph.java 10 KB

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