From 4444b82edc32aeb89919f58412622e5e9ec1e4cb Mon Sep 17 00:00:00 2001 From: Nathan McRae Date: Thu, 9 Jan 2025 22:29:11 -0800 Subject: [PATCH] Persist station list across sessions --- .../nathanmcrae/NumbersStationController.java | 34 ++++++++++++++----- .../NumbersStationSelectionController.java | 31 ++++++++++++++--- .../nathanmcrae/NumbersStationSettings.java | 29 ++++++++++++++++ .../name/nathanmcrae/StationSettings.java | 6 ++++ 4 files changed, 87 insertions(+), 13 deletions(-) diff --git a/src/main/java/name/nathanmcrae/NumbersStationController.java b/src/main/java/name/nathanmcrae/NumbersStationController.java index 8e41269..241e3f7 100644 --- a/src/main/java/name/nathanmcrae/NumbersStationController.java +++ b/src/main/java/name/nathanmcrae/NumbersStationController.java @@ -4,6 +4,9 @@ import com.fasterxml.jackson.dataformat.xml.XmlMapper; import java.io.File; import java.io.IOException; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ResourceBundle; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; @@ -85,12 +88,7 @@ public class NumbersStationController implements Initializable { } settings.setSelectedStationName(selectedStationName.get()); - XmlMapper xmlMapper = new XmlMapper(); - try { - xmlMapper.writeValue(new File("setting-test.xml"), settings); - } catch (IOException e) { - e.printStackTrace(); - } + settings.save(); }); } catch (Exception e) { e.printStackTrace(); @@ -101,9 +99,29 @@ public class NumbersStationController implements Initializable { } private void loadSettings() throws IOException { - File file = new File("setting-test.xml"); XmlMapper xmlMapper = new XmlMapper(); - settings = xmlMapper.readValue(file, NumbersStationSettings.class); + String userHome = System.getProperty("user.home"); + Path directoryPath = Paths.get(userHome, ".numbers-station"); + Path filePath = directoryPath.resolve("settings.xml"); + try { + + // Create the directory if it doesn't exist + if (!Files.exists(directoryPath)) { + Files.createDirectories(directoryPath); + } + + if (!Files.exists(filePath)) { + settings = new NumbersStationSettings(); + + xmlMapper.writeValue(new File(filePath.toString()), settings); + } else { + settings = xmlMapper.readValue(new File(filePath.toString()), NumbersStationSettings.class); + } + } catch (IOException e) { + // Print the contents of filePath + System.out.println("File contents: " + Files.readString(filePath)); + e.printStackTrace(); + } } @Override diff --git a/src/main/java/name/nathanmcrae/NumbersStationSelectionController.java b/src/main/java/name/nathanmcrae/NumbersStationSelectionController.java index 9ba779b..0f1e194 100644 --- a/src/main/java/name/nathanmcrae/NumbersStationSelectionController.java +++ b/src/main/java/name/nathanmcrae/NumbersStationSelectionController.java @@ -1,5 +1,6 @@ package name.nathanmcrae; +import javafx.collections.ListChangeListener; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; @@ -10,11 +11,12 @@ import javafx.event.ActionEvent; import javafx.event.Event; import javafx.scene.Node; import javafx.stage.Stage; - import java.util.Optional; public class NumbersStationSelectionController { + private NumbersStationSettings settings; + @FXML private ListView stationListView; @@ -23,6 +25,8 @@ public class NumbersStationSelectionController { public NumbersStationSelectionController() { // Initialize the list with an empty list stationList = FXCollections.observableArrayList(); + // Add listener to the stationList + stationList.addListener((ListChangeListener) change -> onStationListChanged(change)); } @FXML @@ -41,8 +45,8 @@ public class NumbersStationSelectionController { }); } - public void setSettings(NumbersStationSettings settings) { - // Populate the list with stations from settings + public void setSettings(NumbersStationSettings newSettings) { + settings = newSettings; stationList.addAll(settings.getStations()); stationListView.setItems(stationList); } @@ -59,8 +63,7 @@ public class NumbersStationSelectionController { Optional result = dialog.showAndWait(); result.ifPresent(stationName -> { // Add the new station to the list - StationSettings newStation = new StationSettings(); - newStation.setName(stationName); + StationSettings newStation = new StationSettings(stationName); stationList.add(newStation); }); } @@ -86,4 +89,22 @@ public class NumbersStationSelectionController { } stage.close(); } + + private void onStationListChanged(ListChangeListener.Change change) { + while (change.next()) { + if (change.wasAdded()) { + for (StationSettings addedStation : change.getAddedSubList()) { + System.out.println("Added: " + addedStation.getName()); + } + } + if (change.wasRemoved()) { + for (StationSettings removedStation : change.getRemoved()) { + System.out.println("Removed: " + removedStation.getName()); + } + } + } + settings.getStations().clear(); + settings.getStations().addAll(stationList); + settings.save(); + } } diff --git a/src/main/java/name/nathanmcrae/NumbersStationSettings.java b/src/main/java/name/nathanmcrae/NumbersStationSettings.java index 92ccb3c..1b7ac70 100644 --- a/src/main/java/name/nathanmcrae/NumbersStationSettings.java +++ b/src/main/java/name/nathanmcrae/NumbersStationSettings.java @@ -1,5 +1,11 @@ package name.nathanmcrae; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; public class NumbersStationSettings { @@ -10,6 +16,11 @@ public class NumbersStationSettings { private ArrayList stations; + public NumbersStationSettings() { + stations = new ArrayList<>(); + stations.add(new StationSettings("Station 1")); + } + public ArrayList getStations() { return stations; } @@ -37,4 +48,22 @@ public class NumbersStationSettings { public void setSelectedStationName(String selectedStationName) { this.selectedStationName = selectedStationName; } + + public void save() { + XmlMapper xmlMapper = new XmlMapper(); + try { + String userHome = System.getProperty("user.home"); + Path directoryPath = Paths.get(userHome, ".numbers-station"); + Path filePath = directoryPath.resolve("settings.xml"); + + // Create the directory if it doesn't exist + if (!Files.exists(directoryPath)) { + Files.createDirectories(directoryPath); + } + + xmlMapper.writeValue(new File(filePath.toString()), this); + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/name/nathanmcrae/StationSettings.java b/src/main/java/name/nathanmcrae/StationSettings.java index 51fe0b1..03e4d6a 100644 --- a/src/main/java/name/nathanmcrae/StationSettings.java +++ b/src/main/java/name/nathanmcrae/StationSettings.java @@ -5,6 +5,12 @@ public class StationSettings { private int digitsPerGroup; private int messageLength; + public StationSettings() { } + + public StationSettings(String newName) { + name = newName; + } + public String getName() { return name; }