Add basic station selection

This commit is contained in:
Nathan McRae 2025-01-05 11:24:47 -08:00
parent 47e0ad7393
commit 3dcd3adf03
3 changed files with 122 additions and 12 deletions

View File

@ -1,23 +1,26 @@
package name.nathanmcrae;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
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.input.KeyEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import java.net.URL;
import java.util.ResourceBundle;
public class NumbersStationController implements Initializable {
private Stage settingsStage;
private Stage selectStationStage;
private NumbersStationSettings settings;
@FXML
private Label lastRetrievedLabel;
@ -52,10 +55,29 @@ public class NumbersStationController implements Initializable {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("NumbersStationSelection.fxml"));
Parent root = fxmlLoader.load();
// Pass settings to the controller
NumbersStationSelectionController controller = fxmlLoader.getController();
controller.setSettings(settings);
selectStationStage = new Stage();
selectStationStage.initModality(Modality.APPLICATION_MODAL);
selectStationStage.setUserData("test");
selectStationStage.setTitle("Numbers Station Selection");
selectStationStage.setScene(new Scene(root));
selectStationStage.show();
selectStationStage.setOnHiding(event -> {
String selectedStation = (String) selectStationStage.getUserData();
if (selectedStation != null) {
StationSettings stationSettings = settings.getStations().stream()
.filter(station -> station.getName().equals(selectedStation))
.findFirst()
.orElse(null);
if (stationSettings != null) {
System.out.println("Selected Station Settings: " + stationSettings);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
@ -64,8 +86,20 @@ public class NumbersStationController implements Initializable {
}
}
private void loadSettings() throws IOException {
File file = new File("setting-test.xml");
XmlMapper xmlMapper = new XmlMapper();
settings = xmlMapper.readValue(file, NumbersStationSettings.class);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
loadSettings();
} catch (IOException e) {
e.printStackTrace();
}
messageTextArea.addEventFilter(KeyEvent.ANY, event -> {
int cursorPosition = messageTextArea.getCaretPosition();

View File

@ -6,18 +6,17 @@
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="437.0" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1">
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="437.0" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="name.nathanmcrae.NumbersStationSelectionController">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Numbers Stations">
<font>
<Font size="20.0" />
</font>
</Label>
<Button layoutX="358.0" layoutY="17.0" mnemonicParsing="false" text="+" AnchorPane.rightAnchor="54.5" AnchorPane.topAnchor="17.0" />
<Button layoutX="391.0" layoutY="17.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="25.0" text="-" AnchorPane.rightAnchor="21.0" AnchorPane.topAnchor="17.0" />
<ListView layoutX="14.0" layoutY="57.0" prefHeight="301.0" prefWidth="409.0" AnchorPane.bottomAnchor="42.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="57.0" />
<Button layoutX="313.0" layoutY="365.0" mnemonicParsing="false" text="Select" AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="76.5" />
<Button layoutX="358.0" layoutY="17.0" mnemonicParsing="false" text="+" AnchorPane.rightAnchor="54.5" AnchorPane.topAnchor="17.0" onAction="#handleAddButtonPress"/>
<Button layoutX="391.0" layoutY="17.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="25.0" text="-" AnchorPane.rightAnchor="21.0" AnchorPane.topAnchor="17.0" onAction="#handleRemoveButtonPress"/>
<ListView fx:id="stationListView" layoutX="14.0" layoutY="57.0" prefHeight="301.0" prefWidth="409.0" AnchorPane.bottomAnchor="42.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="57.0" />
<Button layoutX="313.0" layoutY="365.0" mnemonicParsing="false" onMousePressed="#handleSelectButtonPress" text="Select" AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="76.5" />
<Button layoutX="370.0" layoutY="365.0" mnemonicParsing="false" text="Cancel" AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="15.5" />
</children>
</AnchorPane>

View File

@ -0,0 +1,77 @@
package name.nathanmcrae;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.control.TextInputDialog;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.scene.Node;
import javafx.stage.Stage;
import java.util.Optional;
public class NumbersStationSelectionController {
@FXML
private ListView<String> stationListView;
private ObservableList<String> stationList;
public NumbersStationSelectionController() {
// Initialize the list with an empty list
stationList = FXCollections.observableArrayList();
}
@FXML
private void initialize() {
}
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());
stationListView.setItems(stationList);
}
}
@FXML
private void handleAddButtonPress(ActionEvent event) {
// Create a TextInputDialog
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("New Station");
dialog.setHeaderText("Add a New Station");
dialog.setContentText("Please enter the station name:");
// Show the dialog and capture the result
Optional<String> result = dialog.showAndWait();
result.ifPresent(stationName -> {
// Add the new station to the list
stationList.add(stationName);
});
}
@FXML
private void handleRemoveButtonPress(ActionEvent event) {
// Remove the selected item from the list
int selectedIndex = stationListView.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
stationList.remove(selectedIndex);
}
}
@FXML
private void handleSelectButtonPress(Event e) {
Node node = (Node) e.getSource();
Stage stage = (Stage) node.getScene().getWindow();
String selectedStation = stationListView.getSelectionModel().getSelectedItem();
if (selectedStation != null) {
stage.setUserData(selectedStation);
System.out.println("Selected Station: " + selectedStation);
}
stage.close();
}
}