diff --git a/src/main/java/network/brightspots/rcv/GuiConfigController.java b/src/main/java/network/brightspots/rcv/GuiConfigController.java index 18cb6a73..ae2cc021 100644 --- a/src/main/java/network/brightspots/rcv/GuiConfigController.java +++ b/src/main/java/network/brightspots/rcv/GuiConfigController.java @@ -456,7 +456,7 @@ public String saveFile(Button fromButton, boolean useTemporaryFile) { Stage stage = (Stage) fromButton.getScene().getWindow(); File outputFile; if (useTemporaryFile) { - outputFile = new File(selectedFile.getAbsolutePath() + ".temp"); + outputFile = getTemporaryFile(); saveFile(outputFile); } else { outputFile = getSaveFile(stage); @@ -477,6 +477,15 @@ private void saveFile(File fileToSave) { loadFile(fileToSave, true); } + /** + * Where the temporary config files will be placed if saveFile's useTemporaryFile flag is used. + * + * @return A file that may or may not currently exist on disk + */ + public File getTemporaryFile() { + return new File(selectedFile.getAbsolutePath() + ".temp"); + } + /** * Action when save menu item is clicked. */ @@ -603,6 +612,7 @@ private void openTabulateWindow() { window.setScene(new Scene(root)); window.setX(GuiContext.getInstance().getMainWindow().getX() + 50); window.setY(GuiContext.getInstance().getMainWindow().getY() + 50); + window.setOnHiding((event) -> controller.cleanUp()); window.showAndWait(); } catch (IOException exception) { StringWriter sw = new StringWriter(); diff --git a/src/main/java/network/brightspots/rcv/GuiTabulateController.java b/src/main/java/network/brightspots/rcv/GuiTabulateController.java index f3e4ff17..c109fc8f 100644 --- a/src/main/java/network/brightspots/rcv/GuiTabulateController.java +++ b/src/main/java/network/brightspots/rcv/GuiTabulateController.java @@ -16,7 +16,9 @@ package network.brightspots.rcv; +import java.io.File; import java.io.IOException; +import java.nio.file.Files; import javafx.concurrent.Service; import javafx.concurrent.WorkerStateEvent; import javafx.event.ActionEvent; @@ -101,6 +103,21 @@ public void initialize(GuiConfigController controller, int numCandidates, int nu updateProgressText(); } + /** + * Call this before closing the window to delete any lingering temp files. + */ + public void cleanUp() { + if (useTemporaryConfigBeforeTabulation) { + try { + File tempFile = guiConfigController.getTemporaryFile(); + Files.deleteIfExists(tempFile.toPath()); + } catch (IOException e) { + Logger.severe("Could not delete temporary config file: " + e.getMessage()); + throw new RuntimeException(e); + } + } + } + /** * Action when a letter is typed in the name field. *