From 9ef82d15c9f7cc935412788a74ae4de78993a1a2 Mon Sep 17 00:00:00 2001 From: Sam Carlberg Date: Thu, 26 Jan 2017 15:48:50 -0500 Subject: [PATCH 1/2] Change default save dir, show warning if the project couldn't be saved --- .../edu/wpi/grip/ui/MainWindowController.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/ui/src/main/java/edu/wpi/grip/ui/MainWindowController.java b/ui/src/main/java/edu/wpi/grip/ui/MainWindowController.java index fe54a4b30d..47628a27b4 100644 --- a/ui/src/main/java/edu/wpi/grip/ui/MainWindowController.java +++ b/ui/src/main/java/edu/wpi/grip/ui/MainWindowController.java @@ -1,5 +1,6 @@ package edu.wpi.grip.ui; +import edu.wpi.grip.core.GripFileManager; import edu.wpi.grip.core.Palette; import edu.wpi.grip.core.Pipeline; import edu.wpi.grip.core.PipelineRunner; @@ -33,7 +34,6 @@ import java.io.IOException; import java.util.Optional; import java.util.Set; -import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Platform; @@ -154,11 +154,7 @@ private boolean showConfirmationDialogAndWait() { } else if (dialog.getResult().equals(save)) { // If the user chose "Save", automatically show a save dialog and block until the user // has had a chance to save the project. - try { - return saveProject(); - } catch (IOException e) { - logger.log(Level.SEVERE, e.getMessage(), e.getCause()); - } + return saveProject(); } } return true; @@ -219,10 +215,18 @@ public void openProject() { * @return true if the user does not cancel the save */ @FXML - public boolean saveProject() throws IOException { + public boolean saveProject() { if (project.getFile().isPresent()) { // Immediately save the project to whatever file it was loaded from or last saved to. - project.save(project.getFile().get()); + try { + project.save(project.getFile().get()); + } catch (IOException e) { + eventBus.post(new WarningEvent("Could not save project", + "The project could not be saved to " + project.getFile().get().getAbsolutePath() + + ".\n\nCause: " + e.getMessage() + )); + return false; + } return true; } else { return saveProjectAs(); @@ -237,10 +241,11 @@ public boolean saveProject() throws IOException { * @return true if the user does not cancel the save */ @FXML - public boolean saveProjectAs() throws IOException { + public boolean saveProjectAs() { final FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("Save Project As"); fileChooser.getExtensionFilters().add(new ExtensionFilter("GRIP File", "*.grip")); + fileChooser.setInitialDirectory(GripFileManager.GRIP_DIRECTORY); project.getFile().ifPresent(file -> fileChooser.setInitialDirectory(file.getParentFile())); @@ -249,7 +254,14 @@ public boolean saveProjectAs() throws IOException { return false; } - project.save(file); + try { + project.save(file); + } catch (IOException e) { + eventBus.post(new WarningEvent("Could not save project", + "The project could not be saved to " + file.getAbsolutePath() + + ".\n\nCause: " + e.getMessage())); + return false; + } return true; } From e765ef7e5b66b89805471e0ec0003f50f6aafaaa Mon Sep 17 00:00:00 2001 From: Sam Carlberg Date: Thu, 26 Jan 2017 18:27:02 -0500 Subject: [PATCH 2/2] Move safe save to project --- .../wpi/grip/core/serialization/Project.java | 27 +++++++++++++++++++ .../edu/wpi/grip/ui/MainWindowController.java | 24 ++--------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/edu/wpi/grip/core/serialization/Project.java b/core/src/main/java/edu/wpi/grip/core/serialization/Project.java index fc9272ee88..0324fcd77c 100644 --- a/core/src/main/java/edu/wpi/grip/core/serialization/Project.java +++ b/core/src/main/java/edu/wpi/grip/core/serialization/Project.java @@ -3,8 +3,10 @@ import edu.wpi.grip.core.Pipeline; import edu.wpi.grip.core.PipelineRunner; import edu.wpi.grip.core.events.DirtiesSaveEvent; +import edu.wpi.grip.core.events.WarningEvent; import com.google.common.annotations.VisibleForTesting; +import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; import com.google.common.reflect.ClassPath; import com.thoughtworks.xstream.XStream; @@ -36,6 +38,8 @@ public class Project { protected final XStream xstream = new XStream(new PureJavaReflectionProvider()); @Inject + private EventBus eventBus; + @Inject private Pipeline pipeline; @Inject private PipelineRunner pipelineRunner; @@ -120,6 +124,29 @@ void open(Reader reader) { saveIsDirty.set(false); } + /** + * Tries to save this project to the given file. Unlike {@link #save(File)}, this will not + * throw an IOException and will instead post a warning event to the event bus. + * + * @param file the file to save to + * + * @return true if the project was successfully saved to the given file, or false if the file + * could not be written to + */ + public boolean trySave(File file) { + try { + save(file); + return true; + } catch (IOException e) { + eventBus.post(new WarningEvent( + "Could not save project", + "The project could not be saved to " + file.getAbsolutePath() + + ".\n\nCause: " + e.getMessage() + )); + return false; + } + } + /** * Save the project to a file. */ diff --git a/ui/src/main/java/edu/wpi/grip/ui/MainWindowController.java b/ui/src/main/java/edu/wpi/grip/ui/MainWindowController.java index 47628a27b4..ed5a1bedd7 100644 --- a/ui/src/main/java/edu/wpi/grip/ui/MainWindowController.java +++ b/ui/src/main/java/edu/wpi/grip/ui/MainWindowController.java @@ -34,7 +34,6 @@ import java.io.IOException; import java.util.Optional; import java.util.Set; -import java.util.logging.Logger; import javafx.application.Platform; import javafx.fxml.FXML; @@ -63,8 +62,6 @@ */ public class MainWindowController { - private static final Logger logger = Logger.getLogger(MainWindowController.class.getName()); - @FXML private Parent root; @FXML @@ -218,16 +215,7 @@ public void openProject() { public boolean saveProject() { if (project.getFile().isPresent()) { // Immediately save the project to whatever file it was loaded from or last saved to. - try { - project.save(project.getFile().get()); - } catch (IOException e) { - eventBus.post(new WarningEvent("Could not save project", - "The project could not be saved to " + project.getFile().get().getAbsolutePath() - + ".\n\nCause: " + e.getMessage() - )); - return false; - } - return true; + return project.trySave(project.getFile().get()); } else { return saveProjectAs(); } @@ -254,15 +242,7 @@ public boolean saveProjectAs() { return false; } - try { - project.save(file); - } catch (IOException e) { - eventBus.post(new WarningEvent("Could not save project", - "The project could not be saved to " + file.getAbsolutePath() - + ".\n\nCause: " + e.getMessage())); - return false; - } - return true; + return project.trySave(file); } @FXML