GraphMLImporter.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package de.tu_darmstadt.informatik.tk.scopviz.io;
  2. import java.io.IOException;
  3. import java.net.URL;
  4. import java.util.LinkedList;
  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.graphs.MyGraph;
  9. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  10. import de.tu_darmstadt.informatik.tk.scopviz.ui.OptionsManager;
  11. import javafx.stage.FileChooser;
  12. import javafx.stage.Stage;
  13. /**
  14. * Importer to import a graph from a GraphML file and return it as a Graph
  15. * object.
  16. *
  17. * @author Jascha Bohne
  18. * @version 1.1
  19. */
  20. public class GraphMLImporter {
  21. /**
  22. * Filesource from which to read the Graph Data.
  23. */
  24. private MyFileSourceGraphML fs = new MyFileSourceGraphML();
  25. /**
  26. * Imports a GraphML file.
  27. *
  28. * @param id
  29. * unique ID
  30. * @param fileName
  31. * path to the file on disk
  32. * @return the imported Graphstream-Graph
  33. */
  34. public MyGraph readGraph(String id, final String fileName) {
  35. MyGraph g = new MyGraph(id);
  36. fs.addSink(g);
  37. try {
  38. fs.readAll(fileName);
  39. } catch (IOException e) {
  40. System.out.println("GraphML File doesn't exist or can't be opened");
  41. e.printStackTrace();
  42. }
  43. fs.removeSink(g);
  44. handleAttributes(g);
  45. return g;
  46. }
  47. /**
  48. * adds default values for typeofNode and typeofDevice to all Nodes and
  49. * converts yEd attributes to regular ones.
  50. *
  51. * @param g
  52. * the graph that the attributes will be added onto
  53. */
  54. private void handleAttributes(MyGraph g) {
  55. for (Node n : g.getNodeSet()) {
  56. if (!n.hasAttribute("ui.label")){
  57. n.addAttribute("ui.label", "");
  58. }
  59. if (!n.hasAttribute("typeofNode") || n.getAttribute("typeofNode").equals("")) {
  60. n.addAttribute("typeofNode", "standard");
  61. }
  62. if (!n.hasAttribute("typeofDevice") || n.getAttribute("typeofDevice").equals("")) {
  63. n.addAttribute("typeofDevice", "unknown");
  64. }
  65. if (!n.hasAttribute("lat") || n.getAttribute("long").equals("")) {
  66. n.addAttribute("lat", OptionsManager.getDefaultLat());
  67. }
  68. if (!n.hasAttribute("long") || n.getAttribute("long").equals("")) {
  69. n.addAttribute("long", OptionsManager.getDefaultLong());
  70. }
  71. if (!n.hasAttribute("ui.label") && n.hasAttribute("yEd.label")) {
  72. n.addAttribute("ui.label", n.getAttribute("yEd.label").toString());
  73. n.removeAttribute("yEd.label");
  74. }
  75. if (n.hasAttribute("yEd.x") && !n.getAttribute("yEd.x").equals("")) {
  76. n.addAttribute("x", Double.parseDouble(n.getAttribute("yEd.x").toString()));
  77. n.removeAttribute("yEd.x");
  78. }
  79. if (n.hasAttribute("yEd.y") && !n.getAttribute("yEd.y").equals("")) {
  80. n.addAttribute("y", Double.parseDouble(n.getAttribute("yEd.y").toString()));
  81. n.removeAttribute("yEd.y");
  82. }
  83. if (!n.hasAttribute("process-need") || n.getAttribute("process-need").equals("")) {
  84. n.addAttribute("process-need", 0.0);
  85. }
  86. if (!n.hasAttribute("process-max") || n.getAttribute("process-max").equals("")) {
  87. n.addAttribute("process-max", 0.0);
  88. }
  89. }
  90. }
  91. /**
  92. * Imports a GraphML file. Opens a open dialog. Returns null if the process
  93. * is aborted.
  94. *
  95. * @param id
  96. * the id to use for the new Graph
  97. * @param stage
  98. * the parent window of the open file window
  99. * @return the imported Graphstream-Graph
  100. */
  101. public MyGraph readGraph(final String id, final Stage stage) {
  102. FileChooser fileChooser = new FileChooser();
  103. fileChooser.setTitle("open graph");
  104. try {
  105. String fileName = fileChooser.showOpenDialog(stage).getPath();
  106. Main.getInstance().getGraphManager().setCurrentPath(fileName);
  107. return readGraph(id, fileName);
  108. } catch (IllegalArgumentException e) {
  109. return null;
  110. }
  111. }
  112. /**
  113. * Imports a GraphML file.
  114. *
  115. * @param id
  116. * unique ID
  117. * @param fileURL
  118. * URL of the file
  119. * @return the imported Graphstream-Graph
  120. */
  121. // TODO backup reader/Exception handling
  122. public MyGraph readGraph(String id, final URL fileURL) {
  123. MyGraph g = new MyGraph(id);
  124. fs.addSink(g);
  125. try {
  126. fs.readAll(fileURL);
  127. } catch (IOException e) {
  128. Debug.out("GraphML File doesn't exist or can't be opened");
  129. e.printStackTrace();
  130. }
  131. fs.removeSink(g);
  132. handleAttributes(g);
  133. return g;
  134. }
  135. /**
  136. * Returns a List of all the Subgraphs within the FileSource.
  137. *
  138. * @return the list of subgraphs
  139. */
  140. public LinkedList<SingleGraph> subGraphs() {
  141. return fs.getSubGraphs();
  142. }
  143. }