Advertisement
Bobybee

Untitled

Mar 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. import com.sun.media.sound.InvalidDataException;
  2. import java.awt.*;
  3. import java.io.*;
  4. import java.nio.file.NoSuchFileException;
  5. import java.nio.file.Path;
  6. import java.nio.file.Paths;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. public class Catalog implements CatalogIO, Serializable {
  11.  
  12. private String path;
  13. private List<Graph> graphs = new ArrayList<>();
  14.  
  15. public Catalog(String path){
  16. this.path = path;
  17. }
  18.  
  19. public void add(Graph name){
  20. graphs.add(name);
  21. }
  22.  
  23. public List<Graph> getGraphs()
  24. {
  25. return graphs;
  26. }
  27.  
  28. public void list()
  29. {
  30. if (graphs.isEmpty())
  31. {
  32. System.out.println("The catalog is empty!");
  33. return;
  34. }
  35.  
  36. System.out.println("The catalog contains the following:");
  37. graphs.stream().forEach(System.out::println);
  38. }
  39.  
  40. @Override
  41. public void open(String name){
  42. Graph found = graphs.stream()
  43. .filter(graph->graph.getName().compareTo(name)==0)
  44. .findAny()
  45. .orElse(null);
  46. if(found == null){
  47. System.err.println("The graph you're looking for does not exist");
  48. return;
  49. }
  50.  
  51. openFile(Paths.get(found.getDefinition()));
  52. openFile(Paths.get(found.getImage()));
  53. }
  54.  
  55. private void openFile(Path filePath)
  56. {
  57. Path fullPath = Paths.get(path).resolve(filePath);
  58.  
  59. try
  60. {
  61. fullPath = fullPath.toRealPath();
  62. }
  63. catch (NoSuchFileException exception)
  64. {
  65. System.err.println(exception);
  66. }
  67. catch (IOException exception)
  68. {
  69. System.err.println(exception);
  70. }
  71.  
  72. File file = fullPath.toFile();
  73.  
  74. try
  75. {
  76. Desktop.getDesktop().open(file);
  77. }
  78. catch (IOException exception)
  79. {
  80. System.err.println(exception);
  81. }
  82. }
  83.  
  84. @Override
  85. public void load(String savePath)
  86. {
  87. try
  88. {
  89. String inputFilePath = Paths.get(path).resolve(Paths.get(savePath)).toString();
  90. FileInputStream inputFile = new FileInputStream(inputFilePath);
  91. ObjectInputStream in = new ObjectInputStream(inputFile);
  92.  
  93. Catalog catalog = (Catalog) in.readObject();
  94. this.graphs = catalog.graphs;
  95. this.path = catalog.path;
  96.  
  97. in.close();
  98. inputFile.close();
  99. }
  100. catch (IOException i)
  101. {
  102. System.err.println(i.getMessage());
  103. }
  104. catch (ClassNotFoundException c)
  105. {
  106. System.err.println(c.getMessage());
  107. }
  108. }
  109.  
  110. @Override
  111. public void save(String savePath)
  112. {
  113. try
  114. {
  115. if (graphs.size() == 0)
  116. throw new InvalidDataException("The catalog is empty.");
  117.  
  118. String outputFilePath = Paths.get(path).resolve(Paths.get(savePath)).toString();
  119. FileOutputStream outputFile = new FileOutputStream(outputFilePath);
  120. ObjectOutputStream out = new ObjectOutputStream(outputFile);
  121.  
  122. out.writeObject(this);
  123. out.close();
  124.  
  125. outputFile.close();
  126. System.err.println("File saved in: " + outputFilePath);
  127. }
  128. catch (InvalidDataException exception)
  129. {
  130. System.err.println(exception.getMessage());
  131. }
  132. catch (IOException exception)
  133. {
  134. System.err.println(exception.getMessage());
  135. }
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement