Add confirmation to station delete

And actually delete the station dir
This commit is contained in:
Nathan McRae 2025-02-08 17:04:14 -08:00
parent fa0130b4b4
commit 741055b200
2 changed files with 48 additions and 1 deletions

View File

@ -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;
}
}
}
}

View File

@ -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<Path>() {
@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;