Skip to content

Commit

Permalink
delete temp file on Tabulate Window close
Browse files Browse the repository at this point in the history
  • Loading branch information
artoonie committed Jun 6, 2024
1 parent db9a51a commit 0e84c5b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/main/java/network/brightspots/rcv/GuiConfigController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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();
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/network/brightspots/rcv/GuiTabulateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 0e84c5b

Please sign in to comment.