GraphMLExporter.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package de.tu_darmstadt.informatik.tk.scopviz.io;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph;
  5. import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
  6. import javafx.stage.FileChooser;
  7. import javafx.stage.FileChooser.ExtensionFilter;
  8. import javafx.stage.Stage;
  9. /**
  10. * Exporter to write a given Graph object to a GraphML file on disk.
  11. *
  12. * @author Jascha Bohne
  13. * @version 1.0
  14. *
  15. */
  16. public class GraphMLExporter {
  17. /**
  18. * Exports the current state of the Graph to a GraphML file.
  19. *
  20. * @param g
  21. * The Graphstream-Graph to be exported
  22. * @param fileName
  23. * The Location on disk the File will be saved on
  24. */
  25. public void writeGraph(final MyGraph g, final String fileName, boolean exportAsSingleGraph) {
  26. MyFileSinkGraphML writer = new MyFileSinkGraphML();
  27. String newFileName = fileName;
  28. if (g.isComposite() && !exportAsSingleGraph) {
  29. writer.exportGraphs(g.getAllSubGraphs(), fileName);
  30. return;
  31. }
  32. try {
  33. writer.writeAll(g, new FileOutputStream(newFileName));
  34. } catch (IOException e) {
  35. System.out.println("cannot Acces File or invalid path");
  36. e.printStackTrace();
  37. }
  38. }
  39. /**
  40. * Exports the current state of the Graph to a GraphML file. Opens a
  41. * FileSaveDialog
  42. *
  43. * @param g
  44. * The Graphstream-Graph to be exported
  45. * @param stage
  46. * The parent window of the save Window
  47. */
  48. public void writeGraph(final MyGraph g, final Stage stage) {
  49. String fileName;
  50. FileChooser fileChooser = new FileChooser();
  51. fileChooser.setTitle("Saving graph");
  52. fileChooser.setInitialFileName("*.graphml");
  53. ExtensionFilter standard = new ExtensionFilter("GraphML Files", "*.graphml");
  54. fileChooser.getExtensionFilters().add(standard);
  55. fileChooser.getExtensionFilters().add(new ExtensionFilter("all Files", "*"));
  56. fileChooser.setSelectedExtensionFilter(standard);
  57. try {
  58. fileName = fileChooser.showSaveDialog(stage).getPath();
  59. Main.getInstance().getGraphManager().setCurrentPath(fileName);
  60. if (fileName != null) {
  61. writeGraph(g, fileName, false);
  62. }
  63. } catch (NullPointerException e) {
  64. }
  65. }
  66. /**
  67. * Appends a string to the fileName before the fileExtension
  68. *
  69. * @param fileName
  70. * the fileName
  71. * @param append
  72. * the string that will be appended
  73. */
  74. public String fileNameAppend(String fileName, String append) {
  75. String[] parts = fileName.split(".");
  76. if (parts.length < 2) {
  77. fileName = fileName.concat(append);
  78. } else {
  79. fileName = "";
  80. int i = 0;
  81. for (; i < parts.length - 1; i++) {
  82. fileName = fileName.concat(parts[0]);
  83. }
  84. fileName.concat(append);
  85. fileName.concat(parts[i]);
  86. }
  87. return fileName;
  88. }
  89. public void exportMapping(MyGraph g){
  90. Stage stage = Main.getInstance().getPrimaryStage();
  91. String fileName;
  92. FileChooser fileChooser = new FileChooser();
  93. fileChooser.setTitle("Saving graph");
  94. fileChooser.setInitialFileName("*.graphmlMap");
  95. ExtensionFilter standard = new ExtensionFilter("GraphML Mapping underlay Files", "*.graphmlMap");
  96. fileChooser.getExtensionFilters().add(standard);
  97. fileChooser.getExtensionFilters().add(new ExtensionFilter("all Files", "*.*"));
  98. fileChooser.setSelectedExtensionFilter(standard);
  99. try {
  100. fileName = fileChooser.showSaveDialog(stage).getPath();
  101. Main.getInstance().getGraphManager().setCurrentPath(fileName);
  102. if (fileName != null) {
  103. writeGraph(g, fileName, false);
  104. }
  105. } catch (NullPointerException e) {}
  106. }
  107. }