diff --git a/src/main/java/name/nathanmcrae/numbersstation/StationSelectionController.java b/src/main/java/name/nathanmcrae/numbersstation/StationSelectionController.java index 436ade8..0cee5be 100644 --- a/src/main/java/name/nathanmcrae/numbersstation/StationSelectionController.java +++ b/src/main/java/name/nathanmcrae/numbersstation/StationSelectionController.java @@ -1,11 +1,15 @@ package name.nathanmcrae.numbersstation; +import java.io.IOException; +import java.util.logging.Logger; +import java.util.logging.Level; import java.util.Optional; import javafx.collections.ListChangeListener; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.scene.control.Alert; +import javafx.scene.control.ButtonType; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.control.TextInputDialog; @@ -15,6 +19,7 @@ import javafx.scene.Node; import javafx.stage.Stage; public class StationSelectionController { + private static final Logger logger = Logger.getLogger(Main.class.getName()); private MainSettings settings; @@ -94,7 +99,16 @@ public class StationSelectionController { @FXML private void handleRemoveButtonPress(ActionEvent event) { int selectedIndex = stationListView.getSelectionModel().getSelectedIndex(); - if (selectedIndex >= 0) { + StationSettings station = stationList.get(selectedIndex); + String stationName = station.toString(); + + String message = "Delete station '" + stationName + "'? All data will be erased."; + Alert alert = new Alert(Alert.AlertType.CONFIRMATION, message, ButtonType.YES, ButtonType.NO); + alert.setTitle("Delete station?"); + alert.setHeaderText(null); + alert.showAndWait(); + + if (selectedIndex >= 0 && alert.getResult() == ButtonType.YES) { stationList.remove(selectedIndex); } } @@ -122,6 +136,18 @@ public class StationSelectionController { if (change.wasRemoved()) { for (StationSettings removedStation : change.getRemoved()) { System.out.println("Removed: " + removedStation.getName()); + + try { + removedStation.deleteDir(); + } catch (IOException e) { + logger.log(Level.SEVERE, "Exception when removing station directory", e); + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Error removing station"); + alert.setHeaderText(null); + alert.setContentText("Exception when removing station directory: " + e.getMessage()); + alert.showAndWait(); + return; + } } } } diff --git a/src/main/java/name/nathanmcrae/numbersstation/StationSettings.java b/src/main/java/name/nathanmcrae/numbersstation/StationSettings.java index 8d0dd60..45a93b6 100644 --- a/src/main/java/name/nathanmcrae/numbersstation/StationSettings.java +++ b/src/main/java/name/nathanmcrae/numbersstation/StationSettings.java @@ -1,8 +1,13 @@ package name.nathanmcrae.numbersstation; +import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.Files; +import java.nio.file.FileVisitResult; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; import java.security.SecureRandom; import java.time.LocalDate; import java.time.LocalTime; @@ -60,6 +65,22 @@ public class StationSettings { scheduleStartTime = LocalTime.now(); } + public void deleteDir() throws IOException { + Files.walkFileTree(stationPath(), new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } + }); + } + public Path stationPath() { String configHome = System.getenv("XDG_CONFIG_HOME"); Path directoryPath;