Persist station list across sessions
This commit is contained in:
		| @@ -4,6 +4,9 @@ import com.fasterxml.jackson.dataformat.xml.XmlMapper; | |||||||
| import java.io.File; | import java.io.File; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| import java.net.URL; | import java.net.URL; | ||||||
|  | import java.nio.file.Files; | ||||||
|  | import java.nio.file.Path; | ||||||
|  | import java.nio.file.Paths; | ||||||
| import java.util.ResourceBundle; | import java.util.ResourceBundle; | ||||||
| import javafx.beans.property.SimpleStringProperty; | import javafx.beans.property.SimpleStringProperty; | ||||||
| import javafx.beans.property.StringProperty; | import javafx.beans.property.StringProperty; | ||||||
| @@ -85,12 +88,7 @@ public class NumbersStationController implements Initializable { | |||||||
|                     } |                     } | ||||||
|                     settings.setSelectedStationName(selectedStationName.get()); |                     settings.setSelectedStationName(selectedStationName.get()); | ||||||
|  |  | ||||||
|                     XmlMapper xmlMapper = new XmlMapper(); |                     settings.save(); | ||||||
|                     try { |  | ||||||
|                         xmlMapper.writeValue(new File("setting-test.xml"), settings); |  | ||||||
|                     } catch (IOException e) { |  | ||||||
|                         e.printStackTrace(); |  | ||||||
|                     } |  | ||||||
|                 }); |                 }); | ||||||
|             } catch (Exception e) { |             } catch (Exception e) { | ||||||
|                 e.printStackTrace(); |                 e.printStackTrace(); | ||||||
| @@ -101,9 +99,29 @@ public class NumbersStationController implements Initializable { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void loadSettings() throws IOException { |     private void loadSettings() throws IOException { | ||||||
|         File file = new File("setting-test.xml"); |  | ||||||
|         XmlMapper xmlMapper = new XmlMapper(); |         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 |     @Override | ||||||
|   | |||||||
| @@ -1,5 +1,6 @@ | |||||||
| package name.nathanmcrae; | package name.nathanmcrae; | ||||||
|  |  | ||||||
|  | import javafx.collections.ListChangeListener; | ||||||
| import javafx.collections.FXCollections; | import javafx.collections.FXCollections; | ||||||
| import javafx.collections.ObservableList; | import javafx.collections.ObservableList; | ||||||
| import javafx.fxml.FXML; | import javafx.fxml.FXML; | ||||||
| @@ -10,11 +11,12 @@ import javafx.event.ActionEvent; | |||||||
| import javafx.event.Event; | import javafx.event.Event; | ||||||
| import javafx.scene.Node; | import javafx.scene.Node; | ||||||
| import javafx.stage.Stage; | import javafx.stage.Stage; | ||||||
|  |  | ||||||
| import java.util.Optional; | import java.util.Optional; | ||||||
|  |  | ||||||
| public class NumbersStationSelectionController { | public class NumbersStationSelectionController { | ||||||
|  |  | ||||||
|  |     private NumbersStationSettings settings; | ||||||
|  |  | ||||||
|     @FXML |     @FXML | ||||||
|     private ListView<StationSettings> stationListView; |     private ListView<StationSettings> stationListView; | ||||||
|  |  | ||||||
| @@ -23,6 +25,8 @@ public class NumbersStationSelectionController { | |||||||
|     public NumbersStationSelectionController() { |     public NumbersStationSelectionController() { | ||||||
|         // Initialize the list with an empty list |         // Initialize the list with an empty list | ||||||
|         stationList = FXCollections.observableArrayList(); |         stationList = FXCollections.observableArrayList(); | ||||||
|  |         // Add listener to the stationList | ||||||
|  |         stationList.addListener((ListChangeListener<StationSettings>) change -> onStationListChanged(change)); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @FXML |     @FXML | ||||||
| @@ -41,8 +45,8 @@ public class NumbersStationSelectionController { | |||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void setSettings(NumbersStationSettings settings) { |     public void setSettings(NumbersStationSettings newSettings) { | ||||||
|         // Populate the list with stations from settings |         settings = newSettings; | ||||||
|         stationList.addAll(settings.getStations()); |         stationList.addAll(settings.getStations()); | ||||||
|         stationListView.setItems(stationList); |         stationListView.setItems(stationList); | ||||||
|     } |     } | ||||||
| @@ -59,8 +63,7 @@ public class NumbersStationSelectionController { | |||||||
|         Optional<String> result = dialog.showAndWait(); |         Optional<String> result = dialog.showAndWait(); | ||||||
|         result.ifPresent(stationName -> { |         result.ifPresent(stationName -> { | ||||||
|             // Add the new station to the list |             // Add the new station to the list | ||||||
|             StationSettings newStation = new StationSettings(); |             StationSettings newStation = new StationSettings(stationName); | ||||||
|             newStation.setName(stationName); |  | ||||||
|             stationList.add(newStation); |             stationList.add(newStation); | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| @@ -86,4 +89,22 @@ public class NumbersStationSelectionController { | |||||||
|         } |         } | ||||||
|         stage.close(); |         stage.close(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     private void onStationListChanged(ListChangeListener.Change<? extends StationSettings> 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(); | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,5 +1,11 @@ | |||||||
| package name.nathanmcrae; | 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; | import java.util.ArrayList; | ||||||
|  |  | ||||||
| public class NumbersStationSettings { | public class NumbersStationSettings { | ||||||
| @@ -10,6 +16,11 @@ public class NumbersStationSettings { | |||||||
|  |  | ||||||
|     private ArrayList<StationSettings> stations; |     private ArrayList<StationSettings> stations; | ||||||
|  |  | ||||||
|  |     public NumbersStationSettings() { | ||||||
|  |         stations = new ArrayList<>(); | ||||||
|  |         stations.add(new StationSettings("Station 1")); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     public ArrayList<StationSettings> getStations() { |     public ArrayList<StationSettings> getStations() { | ||||||
|         return stations; |         return stations; | ||||||
|     } |     } | ||||||
| @@ -37,4 +48,22 @@ public class NumbersStationSettings { | |||||||
|     public void setSelectedStationName(String selectedStationName) { |     public void setSelectedStationName(String selectedStationName) { | ||||||
|         this.selectedStationName = 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(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -5,6 +5,12 @@ public class StationSettings { | |||||||
|     private int digitsPerGroup; |     private int digitsPerGroup; | ||||||
|     private int messageLength; |     private int messageLength; | ||||||
|  |  | ||||||
|  |     public StationSettings() { } | ||||||
|  |  | ||||||
|  |     public StationSettings(String newName) { | ||||||
|  |         name = newName; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     public String getName() { |     public String getName() { | ||||||
|         return name; |         return name; | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user