Fix save on Exit Event

This commit is contained in:
Oystein Kristoffer Tveit 2021-04-26 13:09:23 +02:00
parent 22406222f3
commit 0c16ae3f15
2 changed files with 12 additions and 10 deletions

View File

@ -13,7 +13,6 @@ import app.controllers.*;
import app.events.ExitApplicationEvent; import app.events.ExitApplicationEvent;
import app.events.LanguageChangedEvent; import app.events.LanguageChangedEvent;
import app.events.OpenLinkInBrowserEvent; import app.events.OpenLinkInBrowserEvent;
import app.events.SaveFileEvent;
import app.events.ThemeChangedEvent; import app.events.ThemeChangedEvent;
import app.model.Model; import app.model.Model;
import javafx.application.HostServices; import javafx.application.HostServices;
@ -138,13 +137,9 @@ public class MainController implements Initializable {
int g = JOptionPane.showConfirmDialog(null, "Your files are not saved.\nSave before exit?", "Exit", int g = JOptionPane.showConfirmDialog(null, "Your files are not saved.\nSave before exit?", "Exit",
JOptionPane.YES_NO_OPTION); JOptionPane.YES_NO_OPTION);
if (g == JOptionPane.YES_OPTION) { if (g == JOptionPane.YES_OPTION)
this.eventBus.post(new SaveFileEvent()); this.editorController.saveCodeArea(Model.getActiveFilePath().isEmpty());
}
Platform.exit(); Platform.exit();
} }
} else {
Platform.exit();
}
}
} }

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.NoSuchElementException;
import java.util.Optional; import java.util.Optional;
import java.util.Scanner; import java.util.Scanner;
@ -48,9 +49,15 @@ public class FileOperations {
} }
public static boolean saveFileWithDialog(Stage stage, String content) { public static boolean saveFileWithDialog(Stage stage, String content) {
File chosenLocation = DialogBoxes.showSaveFileWithDialog(stage); File chosenLocation;
if (chosenLocation == null)
try {
chosenLocation = DialogBoxes.showSaveFileWithDialog(stage);
} catch (NoSuchElementException e) {
return false; return false;
}
if (chosenLocation == null) return false;
if (saveFile(chosenLocation.toPath(), content)) { if (saveFile(chosenLocation.toPath(), content)) {
Model.setActiveFilePath(Optional.of(chosenLocation.toPath())); Model.setActiveFilePath(Optional.of(chosenLocation.toPath()));