GraphMLImporter.java 4.1 KB

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