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"> <FlowPane alignment="CENTER_LEFT" prefHeight="41.0" prefWidth="640.0">
<children> <children>
<Label prefHeight="17.0" prefWidth="49.0" text="Station: " /> <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> <FlowPane.margin>
<Insets right="10.0" /> <Insets right="10.0" />
</FlowPane.margin> </FlowPane.margin>

View File

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

View File

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