Store StationSettings directly in ListView
This commit is contained in:
		| @@ -3,6 +3,7 @@ package name.nathanmcrae; | ||||
| import javafx.collections.FXCollections; | ||||
| import javafx.collections.ObservableList; | ||||
| import javafx.fxml.FXML; | ||||
| import javafx.scene.control.ListCell; | ||||
| import javafx.scene.control.ListView; | ||||
| import javafx.scene.control.TextInputDialog; | ||||
| import javafx.event.ActionEvent; | ||||
| @@ -15,9 +16,9 @@ import java.util.Optional; | ||||
| public class NumbersStationSelectionController { | ||||
|  | ||||
|     @FXML | ||||
|     private ListView<String> stationListView; | ||||
|     private ListView<StationSettings> stationListView; | ||||
|  | ||||
|     private ObservableList<String> stationList; | ||||
|     private ObservableList<StationSettings> stationList; | ||||
|  | ||||
|     public NumbersStationSelectionController() { | ||||
|         // Initialize the list with an empty list | ||||
| @@ -26,16 +27,25 @@ public class NumbersStationSelectionController { | ||||
|  | ||||
|     @FXML | ||||
|     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) { | ||||
|         // Populate the list with stations from settings | ||||
|         for (StationSettings station : settings.getStations()) { | ||||
|             System.out.println("Adding station: " + station.getName()); | ||||
|             stationList.add(station.getName()); | ||||
|         stationList.addAll(settings.getStations()); | ||||
|         stationListView.setItems(stationList); | ||||
|     } | ||||
|     } | ||||
|  | ||||
|     @FXML | ||||
|     private void handleAddButtonPress(ActionEvent event) { | ||||
| @@ -49,7 +59,9 @@ public class NumbersStationSelectionController { | ||||
|         Optional<String> result = dialog.showAndWait(); | ||||
|         result.ifPresent(stationName -> { | ||||
|             // 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) { | ||||
|         Node node = (Node) e.getSource(); | ||||
|         Stage stage = (Stage) node.getScene().getWindow(); | ||||
|         String selectedStation = stationListView.getSelectionModel().getSelectedItem(); | ||||
|         StationSettings selectedStation = stationListView.getSelectionModel().getSelectedItem(); | ||||
|  | ||||
|         if (selectedStation != null) { | ||||
|             stage.setUserData(selectedStation); | ||||
|             System.out.println("Selected Station: " + selectedStation); | ||||
|             stage.setUserData(selectedStation.getName()); | ||||
|             System.out.println("Selected Station: " + selectedStation.getName()); | ||||
|         } | ||||
|         stage.close(); | ||||
|     } | ||||
|   | ||||
| @@ -28,4 +28,9 @@ public class StationSettings { | ||||
|     public void setMessageLength(int newMessageLength) { | ||||
|         messageLength = newMessageLength; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         return name; | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user