GraphMLImporter.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  6. import de.tu_darmstadt.informatik.tk.scopviz.graphs.GraphHelper;
  7. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph;
  8. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyNode;
  9. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  10. import javafx.stage.FileChooser;
  11. import javafx.stage.FileChooser.ExtensionFilter;
  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. if (fileName.contains("shutdown.graphml")) {
  41. return new MyGraph((fileName.split(".")[0]));
  42. }
  43. System.out.println("GraphML File doesn't exist or can't be opened");
  44. e.printStackTrace();
  45. }
  46. if (fs.wasMultiGraph()) {
  47. g = GraphHelper.newMerge(false, fs.getSubGraphs().toArray(new MyGraph[0]));
  48. }
  49. fs.removeSink(g);
  50. for (MyNode n : g.<MyNode>getNodeSet()) {
  51. n.removeAttribute("ui.class");
  52. }
  53. yEdConversion(g);
  54. return g;
  55. }
  56. /**
  57. * Imports a GraphML file. Opens a open dialog. Returns null if the process
  58. * is aborted.
  59. *
  60. * @param id
  61. * the id to use for the new Graph
  62. * @param stage
  63. * the parent window of the open file window
  64. * @return the imported Graphstream-Graph
  65. */
  66. public MyGraph readGraph(final String id, final Stage stage) {
  67. FileChooser fileChooser = new FileChooser();
  68. fileChooser.setTitle("open graph");
  69. ExtensionFilter standard = new ExtensionFilter("GraphML Files", "*.graphml");
  70. fileChooser.getExtensionFilters().add(standard);
  71. fileChooser.getExtensionFilters().add(new ExtensionFilter("all Files", "*"));
  72. fileChooser.setSelectedExtensionFilter(standard);
  73. try {
  74. String fileName = fileChooser.showOpenDialog(stage).getPath();
  75. Main.getInstance().getGraphManager().setCurrentPath(fileName);
  76. return readGraph(id, fileName);
  77. } catch (NullPointerException e) {
  78. return null;
  79. }
  80. }
  81. /**
  82. * Imports a GraphML file.
  83. *
  84. * @param id
  85. * unique ID
  86. * @param fileURL
  87. * URL of the file
  88. * @return the imported Graphstream-Graph
  89. */
  90. // TODO backup reader/Exception handling
  91. public MyGraph readGraph(String id, final URL fileURL) {
  92. MyGraph g = new MyGraph(id);
  93. fs.addSink(g);
  94. try {
  95. fs.readAll(fileURL);
  96. } catch (IOException e) {
  97. Debug.out("GraphML File doesn't exist or can't be opened");
  98. e.printStackTrace();
  99. }
  100. fs.removeSink(g);
  101. for (MyNode n : g.<MyNode>getNodeSet()) {
  102. n.removeAttribute("ui.class");
  103. }
  104. yEdConversion(g);
  105. return g;
  106. }
  107. /**
  108. * Returns a List of all the Subgraphs within the FileSource.
  109. *
  110. * @return the list of subgraphs
  111. */
  112. public LinkedList<MyGraph> subGraphs() {
  113. return fs.getSubGraphs();
  114. }
  115. public void yEdConversion(MyGraph g) {
  116. for (MyNode n : g.<MyNode>getNodeSet()) {
  117. // yed conversion
  118. if ((!n.hasAttribute("ui.label") || n.getAttribute("ui.label").equals("")) && n.hasAttribute("yEd.label")) {
  119. n.addAttribute("ui.label", n.getAttribute("yEd.label").toString());
  120. n.removeAttribute("yEd.label");
  121. } else if (n.hasAttribute("ui.label")) {
  122. n.removeAttribute("yEd.label");
  123. }
  124. if (n.hasAttribute("yEd.x") && !n.getAttribute("yEd.x").equals("")) {
  125. n.addAttribute("x", Main.getInstance().convertAttributeTypes(n.getAttribute("yEd.x"), new Double(0.0)));
  126. n.removeAttribute("yEd.x");
  127. } else {
  128. n.removeAttribute("yEd.x");
  129. }
  130. if (n.hasAttribute("yEd.y") && !n.getAttribute("yEd.y").equals("")) {
  131. n.addAttribute("y", Main.getInstance().convertAttributeTypes(n.getAttribute("yEd.y"), new Double(0.0)));
  132. n.removeAttribute("yEd.y");
  133. } else {
  134. n.removeAttribute("yEd.y");
  135. }
  136. }
  137. }
  138. }