Store StationSettings directly in ListView

This commit is contained in:
Nathan McRae 2025-01-05 13:25:51 -08:00
parent 4d1aa1842a
commit 36b2c51787
2 changed files with 28 additions and 11 deletions

View File

@ -3,6 +3,7 @@ package name.nathanmcrae;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView; import javafx.scene.control.ListView;
import javafx.scene.control.TextInputDialog; import javafx.scene.control.TextInputDialog;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
@ -15,9 +16,9 @@ import java.util.Optional;
public class NumbersStationSelectionController { public class NumbersStationSelectionController {
@FXML @FXML
private ListView<String> stationListView; private ListView<StationSettings> stationListView;
private ObservableList<String> stationList; private ObservableList<StationSettings> stationList;
public NumbersStationSelectionController() { public NumbersStationSelectionController() {
// Initialize the list with an empty list // Initialize the list with an empty list
@ -26,15 +27,24 @@ public class NumbersStationSelectionController {
@FXML @FXML
private void initialize() { private void initialize() {
// Set custom cell factory to display station name
stationListView.setCellFactory(param -> new ListCell<StationSettings>() {
@Override
protected void updateItem(StationSettings item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null || item.getName() == null) {
setText(null);
} else {
setText(item.getName());
}
}
});
} }
public void setSettings(NumbersStationSettings settings) { public void setSettings(NumbersStationSettings settings) {
// Populate the list with stations from settings // Populate the list with stations from settings
for (StationSettings station : settings.getStations()) { stationList.addAll(settings.getStations());
System.out.println("Adding station: " + station.getName()); stationListView.setItems(stationList);
stationList.add(station.getName());
stationListView.setItems(stationList);
}
} }
@FXML @FXML
@ -49,7 +59,9 @@ 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
stationList.add(stationName); StationSettings newStation = new StationSettings();
newStation.setName(stationName);
stationList.add(newStation);
}); });
} }
@ -66,11 +78,11 @@ public class NumbersStationSelectionController {
private void handleSelectButtonPress(Event e) { private void handleSelectButtonPress(Event e) {
Node node = (Node) e.getSource(); Node node = (Node) e.getSource();
Stage stage = (Stage) node.getScene().getWindow(); Stage stage = (Stage) node.getScene().getWindow();
String selectedStation = stationListView.getSelectionModel().getSelectedItem(); StationSettings selectedStation = stationListView.getSelectionModel().getSelectedItem();
if (selectedStation != null) { if (selectedStation != null) {
stage.setUserData(selectedStation); stage.setUserData(selectedStation.getName());
System.out.println("Selected Station: " + selectedStation); System.out.println("Selected Station: " + selectedStation.getName());
} }
stage.close(); stage.close();
} }

View File

@ -28,4 +28,9 @@ public class StationSettings {
public void setMessageLength(int newMessageLength) { public void setMessageLength(int newMessageLength) {
messageLength = newMessageLength; messageLength = newMessageLength;
} }
@Override
public String toString() {
return name;
}
} }