GraphMLImporter.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.Layer;
  10. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  11. import javafx.stage.FileChooser;
  12. import javafx.stage.FileChooser.ExtensionFilter;
  13. import javafx.stage.Stage;
  14. /**
  15. * Importer to import a graph from a GraphML file and return it as a Graph
  16. * object.
  17. *
  18. * @author Jascha Bohne
  19. * @version 1.1
  20. */
  21. public class GraphMLImporter {
  22. /**
  23. * Filesource from which to read the Graph Data.
  24. */
  25. private MyFileSourceGraphML fs = new MyFileSourceGraphML();
  26. /**
  27. * Imports a GraphML file.
  28. *
  29. * @param id
  30. * unique ID
  31. * @param fileName
  32. * path to the file on disk
  33. * @return the imported Graphstream-Graph
  34. */
  35. public MyGraph readGraph(String id, final String fileName) {
  36. MyGraph g = new MyGraph(id);
  37. fs.addSink(g);
  38. try {
  39. fs.readAll(fileName);
  40. } catch (IOException e) {
  41. if (fileName.contains("shutdown.graphml")) {
  42. return new MyGraph((fileName.split(".")[0]));
  43. }
  44. System.out.println("GraphML File doesn't exist or can't be opened");
  45. e.printStackTrace();
  46. }
  47. if (fs.wasMultiGraph()) {
  48. for (MyGraph gSub : fs.getSubGraphs()) {
  49. if ("UNDERLAY".equals(gSub.getAttribute("layer"))) {
  50. gSub.removeAttribute("layer");
  51. gSub.addAttribute("layer", Layer.UNDERLAY);
  52. } else if ("OPERATOR".equals(gSub.getAttribute("layer"))) {
  53. gSub.removeAttribute("layer");
  54. gSub.addAttribute("layer", Layer.OPERATOR);
  55. } else if ("MAPPING".equals(gSub.getAttribute("layer"))) {
  56. gSub.removeAttribute("layer");
  57. gSub.addAttribute("layer", Layer.MAPPING);
  58. } else if ("SYMBOL".equals(gSub.getAttribute("layer"))) {
  59. gSub.removeAttribute("layer");
  60. gSub.addAttribute("layer", Layer.SYMBOL);
  61. }
  62. }
  63. g = GraphHelper.newMerge(false, fs.getSubGraphs().toArray(new MyGraph[0]));
  64. }
  65. fs.removeSink(g);
  66. for (MyNode n : g.<MyNode>getNodeSet()) {
  67. n.removeAttribute("ui.class");
  68. }
  69. yEdConversion(g);
  70. return g;
  71. }
  72. /**
  73. * Imports a GraphML file. Opens a open dialog. Returns null if the process
  74. * is aborted.
  75. *
  76. * @param id
  77. * the id to use for the new Graph
  78. * @param stage
  79. * the parent window of the open file window
  80. * @return the imported Graphstream-Graph
  81. */
  82. public MyGraph readGraph(final String id, final Stage stage) {
  83. FileChooser fileChooser = new FileChooser();
  84. fileChooser.setTitle("open graph");
  85. ExtensionFilter standard = new ExtensionFilter("GraphML Files", "*.graphml");
  86. fileChooser.getExtensionFilters().add(standard);
  87. fileChooser.getExtensionFilters().add(new ExtensionFilter("all Files", "*"));
  88. fileChooser.setSelectedExtensionFilter(standard);
  89. try {
  90. String fileName = fileChooser.showOpenDialog(stage).getPath();
  91. Main.getInstance().getGraphManager().setCurrentPath(fileName);
  92. return readGraph(id, fileName);
  93. } catch (NullPointerException e) {
  94. return null;
  95. }
  96. }
  97. /**
  98. * Imports a GraphML file.
  99. *
  100. * @param id
  101. * unique ID
  102. * @param fileURL
  103. * URL of the file
  104. * @return the imported Graphstream-Graph
  105. */
  106. // TODO backup reader/Exception handling
  107. public MyGraph readGraph(String id, final URL fileURL) {
  108. MyGraph g = new MyGraph(id);
  109. fs.addSink(g);
  110. try {
  111. fs.readAll(fileURL);
  112. } catch (IOException e) {
  113. Debug.out("GraphML File doesn't exist or can't be opened");
  114. e.printStackTrace();
  115. }
  116. fs.removeSink(g);
  117. for (MyNode n : g.<MyNode>getNodeSet()) {
  118. n.removeAttribute("ui.class");
  119. }
  120. yEdConversion(g);
  121. return g;
  122. }
  123. /**
  124. * Returns a List of all the Subgraphs within the FileSource.
  125. *
  126. * @return the list of subgraphs
  127. */
  128. public LinkedList<MyGraph> subGraphs() {
  129. return fs.getSubGraphs();
  130. }
  131. public void yEdConversion(MyGraph g) {
  132. for (MyNode n : g.<MyNode>getNodeSet()) {
  133. // yed conversion
  134. if ((!n.hasAttribute("ui.label") || n.getAttribute("ui.label").equals("")) && n.hasAttribute("yEd.label")) {
  135. n.addAttribute("ui.label", n.getAttribute("yEd.label").toString());
  136. n.removeAttribute("yEd.label");
  137. } else if (n.hasAttribute("ui.label")) {
  138. n.removeAttribute("yEd.label");
  139. }
  140. if (n.hasAttribute("yEd.x") && !n.getAttribute("yEd.x").equals("")) {
  141. n.addAttribute("x", Main.getInstance().convertAttributeTypes(n.getAttribute("yEd.x"), new Double(0.0)));
  142. n.removeAttribute("yEd.x");
  143. } else {
  144. n.removeAttribute("yEd.x");
  145. }
  146. if (n.hasAttribute("yEd.y") && !n.getAttribute("yEd.y").equals("")) {
  147. n.addAttribute("y", Main.getInstance().convertAttributeTypes(n.getAttribute("yEd.y"), new Double(0.0)));
  148. n.removeAttribute("yEd.y");
  149. } else {
  150. n.removeAttribute("yEd.y");
  151. }
  152. }
  153. }
  154. }