Flesh out station selection

Also, save selected station in settings. So that the application opens
with the most recently used station.
This commit is contained in:
Nathan McRae 2025-01-05 22:53:53 -08:00
parent 36b2c51787
commit c5e0b41dab
3 changed files with 48 additions and 7 deletions

View File

@ -50,7 +50,7 @@
<FlowPane alignment="CENTER_LEFT" prefHeight="41.0" prefWidth="640.0">
<children>
<Label prefHeight="17.0" prefWidth="49.0" text="Station: " />
<TextField editable="false" style="-fx-background-color: #EEEEEE; -fx-border-color: #AAAAAA;" text="My Station">
<TextField fx:id="stationNameField" editable="false" style="-fx-background-color: #EEEEEE; -fx-border-color: #AAAAAA;" text="My Station">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>

View File

@ -5,11 +5,14 @@ import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.Parent;
@ -21,6 +24,9 @@ public class NumbersStationController implements Initializable {
private Stage settingsStage;
private Stage selectStationStage;
private NumbersStationSettings settings;
private StationSettings selectedStation;
private StringProperty selectedStationName = new SimpleStringProperty();
@FXML
private Label lastRetrievedLabel;
@ -28,6 +34,9 @@ public class NumbersStationController implements Initializable {
@FXML
private TextArea messageTextArea;
@FXML
private TextField stationNameField;
@FXML
private void handleSettingsButtonPress() {
System.out.println("Button pressed!");
@ -67,15 +76,20 @@ public class NumbersStationController implements Initializable {
selectStationStage.setScene(new Scene(root));
selectStationStage.show();
selectStationStage.setOnHiding(event -> {
String selectedStation = (String) selectStationStage.getUserData();
selectedStationName.set((String) selectStationStage.getUserData());
if (selectedStation != null) {
StationSettings stationSettings = settings.getStations().stream()
.filter(station -> station.getName().equals(selectedStation))
selectedStation = settings.getStations().stream()
.filter(station -> station.getName().equals(selectedStationName.get()))
.findFirst()
.orElse(null);
if (stationSettings != null) {
System.out.println("Selected Station Settings: " + stationSettings);
}
settings.setSelectedStationName(selectedStationName.get());
XmlMapper xmlMapper = new XmlMapper();
try {
xmlMapper.writeValue(new File("setting-test.xml"), settings);
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (Exception e) {
@ -94,12 +108,30 @@ public class NumbersStationController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
stationNameField.textProperty().bindBidirectional(selectedStationName);
try {
loadSettings();
} catch (IOException e) {
e.printStackTrace();
}
selectedStationName.set(settings.getSelectedStationName());
if (selectedStationName.get() == null || selectedStationName.get() == "") {
selectedStation = settings.getStations().stream().findFirst().orElse(null);
selectedStationName.set(selectedStation.getName());
}
selectedStation = settings.getStations().stream()
.filter(station -> station.getName().equals(selectedStationName.get()))
.findFirst()
.orElse(null);
if (selectedStation == null) {
// TODO: create a new station and save it?
}
messageTextArea.addEventFilter(KeyEvent.ANY, event -> {
int cursorPosition = messageTextArea.getCaretPosition();

View File

@ -6,6 +6,7 @@ public class NumbersStationSettings {
private int digitsPerGroup;
private String username;
private int refreshInterval;
private String selectedStationName;
private ArrayList<StationSettings> stations;
@ -28,4 +29,12 @@ public class NumbersStationSettings {
public void setRefreshInterval(int refreshInterval) {
this.refreshInterval = refreshInterval;
}
public String getSelectedStationName() {
return selectedStationName;
}
public void setSelectedStationName(String selectedStationName) {
this.selectedStationName = selectedStationName;
}
}