Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.media.sound.InvalidDataException;
- import java.awt.*;
- import java.io.*;
- import java.nio.file.NoSuchFileException;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.List;
- public class Catalog implements CatalogIO, Serializable {
- private String path;
- private List<Graph> graphs = new ArrayList<>();
- public Catalog(String path){
- this.path = path;
- }
- public void add(Graph name){
- graphs.add(name);
- }
- public List<Graph> getGraphs()
- {
- return graphs;
- }
- public void list()
- {
- if (graphs.isEmpty())
- {
- System.out.println("The catalog is empty!");
- return;
- }
- System.out.println("The catalog contains the following:");
- graphs.stream().forEach(System.out::println);
- }
- @Override
- public void open(String name){
- Graph found = graphs.stream()
- .filter(graph->graph.getName().compareTo(name)==0)
- .findAny()
- .orElse(null);
- if(found == null){
- System.err.println("The graph you're looking for does not exist");
- return;
- }
- openFile(Paths.get(found.getDefinition()));
- openFile(Paths.get(found.getImage()));
- }
- private void openFile(Path filePath)
- {
- Path fullPath = Paths.get(path).resolve(filePath);
- try
- {
- fullPath = fullPath.toRealPath();
- }
- catch (NoSuchFileException exception)
- {
- System.err.println(exception);
- }
- catch (IOException exception)
- {
- System.err.println(exception);
- }
- File file = fullPath.toFile();
- try
- {
- Desktop.getDesktop().open(file);
- }
- catch (IOException exception)
- {
- System.err.println(exception);
- }
- }
- @Override
- public void load(String savePath)
- {
- try
- {
- String inputFilePath = Paths.get(path).resolve(Paths.get(savePath)).toString();
- FileInputStream inputFile = new FileInputStream(inputFilePath);
- ObjectInputStream in = new ObjectInputStream(inputFile);
- Catalog catalog = (Catalog) in.readObject();
- this.graphs = catalog.graphs;
- this.path = catalog.path;
- in.close();
- inputFile.close();
- }
- catch (IOException i)
- {
- System.err.println(i.getMessage());
- }
- catch (ClassNotFoundException c)
- {
- System.err.println(c.getMessage());
- }
- }
- @Override
- public void save(String savePath)
- {
- try
- {
- if (graphs.size() == 0)
- throw new InvalidDataException("The catalog is empty.");
- String outputFilePath = Paths.get(path).resolve(Paths.get(savePath)).toString();
- FileOutputStream outputFile = new FileOutputStream(outputFilePath);
- ObjectOutputStream out = new ObjectOutputStream(outputFile);
- out.writeObject(this);
- out.close();
- outputFile.close();
- System.err.println("File saved in: " + outputFilePath);
- }
- catch (InvalidDataException exception)
- {
- System.err.println(exception.getMessage());
- }
- catch (IOException exception)
- {
- System.err.println(exception.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement