Skip to content

Commit 6459049

Browse files
authored
Improve UX for saving files (#801)
* Change default save dir, show warning if the project couldn't be saved * Move safe save to project * Closes #800
1 parent 4ce1943 commit 6459049

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

core/src/main/java/edu/wpi/grip/core/serialization/Project.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import edu.wpi.grip.core.Pipeline;
44
import edu.wpi.grip.core.PipelineRunner;
55
import edu.wpi.grip.core.events.DirtiesSaveEvent;
6+
import edu.wpi.grip.core.events.WarningEvent;
67

78
import com.google.common.annotations.VisibleForTesting;
9+
import com.google.common.eventbus.EventBus;
810
import com.google.common.eventbus.Subscribe;
911
import com.google.common.reflect.ClassPath;
1012
import com.thoughtworks.xstream.XStream;
@@ -36,6 +38,8 @@ public class Project {
3638

3739
protected final XStream xstream = new XStream(new PureJavaReflectionProvider());
3840
@Inject
41+
private EventBus eventBus;
42+
@Inject
3943
private Pipeline pipeline;
4044
@Inject
4145
private PipelineRunner pipelineRunner;
@@ -120,6 +124,29 @@ void open(Reader reader) {
120124
saveIsDirty.set(false);
121125
}
122126

127+
/**
128+
* Tries to save this project to the given file. Unlike {@link #save(File)}, this will <i>not</i>
129+
* throw an IOException and will instead post a warning event to the event bus.
130+
*
131+
* @param file the file to save to
132+
*
133+
* @return true if the project was successfully saved to the given file, or false if the file
134+
* could not be written to
135+
*/
136+
public boolean trySave(File file) {
137+
try {
138+
save(file);
139+
return true;
140+
} catch (IOException e) {
141+
eventBus.post(new WarningEvent(
142+
"Could not save project",
143+
"The project could not be saved to " + file.getAbsolutePath()
144+
+ ".\n\nCause: " + e.getMessage()
145+
));
146+
return false;
147+
}
148+
}
149+
123150
/**
124151
* Save the project to a file.
125152
*/

ui/src/main/java/edu/wpi/grip/ui/MainWindowController.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.wpi.grip.ui;
22

3+
import edu.wpi.grip.core.GripFileManager;
34
import edu.wpi.grip.core.Palette;
45
import edu.wpi.grip.core.Pipeline;
56
import edu.wpi.grip.core.PipelineRunner;
@@ -33,8 +34,6 @@
3334
import java.io.IOException;
3435
import java.util.Optional;
3536
import java.util.Set;
36-
import java.util.logging.Level;
37-
import java.util.logging.Logger;
3837

3938
import javafx.application.Platform;
4039
import javafx.fxml.FXML;
@@ -63,8 +62,6 @@
6362
*/
6463
public class MainWindowController {
6564

66-
private static final Logger logger = Logger.getLogger(MainWindowController.class.getName());
67-
6865
@FXML
6966
private Parent root;
7067
@FXML
@@ -154,11 +151,7 @@ private boolean showConfirmationDialogAndWait() {
154151
} else if (dialog.getResult().equals(save)) {
155152
// If the user chose "Save", automatically show a save dialog and block until the user
156153
// has had a chance to save the project.
157-
try {
158-
return saveProject();
159-
} catch (IOException e) {
160-
logger.log(Level.SEVERE, e.getMessage(), e.getCause());
161-
}
154+
return saveProject();
162155
}
163156
}
164157
return true;
@@ -219,11 +212,10 @@ public void openProject() {
219212
* @return true if the user does not cancel the save
220213
*/
221214
@FXML
222-
public boolean saveProject() throws IOException {
215+
public boolean saveProject() {
223216
if (project.getFile().isPresent()) {
224217
// Immediately save the project to whatever file it was loaded from or last saved to.
225-
project.save(project.getFile().get());
226-
return true;
218+
return project.trySave(project.getFile().get());
227219
} else {
228220
return saveProjectAs();
229221
}
@@ -237,10 +229,11 @@ public boolean saveProject() throws IOException {
237229
* @return true if the user does not cancel the save
238230
*/
239231
@FXML
240-
public boolean saveProjectAs() throws IOException {
232+
public boolean saveProjectAs() {
241233
final FileChooser fileChooser = new FileChooser();
242234
fileChooser.setTitle("Save Project As");
243235
fileChooser.getExtensionFilters().add(new ExtensionFilter("GRIP File", "*.grip"));
236+
fileChooser.setInitialDirectory(GripFileManager.GRIP_DIRECTORY);
244237

245238
project.getFile().ifPresent(file -> fileChooser.setInitialDirectory(file.getParentFile()));
246239

@@ -249,8 +242,7 @@ public boolean saveProjectAs() throws IOException {
249242
return false;
250243
}
251244

252-
project.save(file);
253-
return true;
245+
return project.trySave(file);
254246
}
255247

256248
@FXML

0 commit comments

Comments
 (0)