Compare commits

..

7 Commits

6 changed files with 288 additions and 20 deletions

View File

@ -0,0 +1,145 @@
package name.nathanmcrae.numbersstation;
import java.lang.StringBuilder;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
public class AddPrefixController {
@FXML
private TextField prefixField;
@FXML
private Button okButton;
@FXML
private Button cancelButton;
private int digitsPerGroup;
private MainSettingsController mainSettingsController;
public void setDigitsPerGroup(int newDigitsPerGroup) {
digitsPerGroup = newDigitsPerGroup;
}
public void setMainSettingsController(MainSettingsController mainSettingsController) {
this.mainSettingsController = mainSettingsController;
}
@FXML
private void initialize() {
okButton.setOnAction(event -> handleOkButtonPress());
cancelButton.setOnAction(event -> handleCancelButtonPress());
// Set focus on the text field when the window is shown
Platform.runLater(() -> prefixField.requestFocus());
// Add event filter to handle Enter key press
prefixField.addEventFilter(KeyEvent.ANY, event -> {
if (event.getCode() == KeyCode.ENTER) {
handleOkButtonPress();
event.consume();
}
int cursorPosition = prefixField.getCaretPosition();
String character = event.getCharacter();
KeyCode code = event.getCode();
String str = event.getText();
// Consume the event to block the default behavior
if (!(code == KeyCode.DOWN ||
code == KeyCode.END ||
code == KeyCode.HOME ||
code == KeyCode.LEFT ||
code == KeyCode.PAGE_DOWN ||
code == KeyCode.PAGE_UP ||
code == KeyCode.RIGHT ||
code == KeyCode.UP)) {
event.consume();
}
if (event.getEventType() == KeyEvent.KEY_PRESSED &&
(code == KeyCode.BACK_SPACE ||
code == KeyCode.DELETE)) {
String newText = prefixField.getText();
if (code == KeyCode.BACK_SPACE) {
newText = (newText.substring(0, cursorPosition - 1) + newText.substring(cursorPosition)).replaceAll(" ", "");
}
else if (code == KeyCode.DELETE) {
newText = (newText.substring(0, cursorPosition) + newText.substring(cursorPosition + 1)).replaceAll(" ", "");
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < newText.length(); i++) {
sb.append(newText.charAt(i));
if ((i + 1) % (digitsPerGroup) == 0) {
sb.append(' ');
}
}
prefixField.setText(sb.toString());
if (code == KeyCode.BACK_SPACE) {
prefixField.positionCaret(cursorPosition - 1);
} else if (code == KeyCode.DELETE) {
prefixField.positionCaret(cursorPosition);
}
}
if (event.getEventType() == KeyEvent.KEY_PRESSED &&
(code == KeyCode.DIGIT0 ||
code == KeyCode.DIGIT1 ||
code == KeyCode.DIGIT2 ||
code == KeyCode.DIGIT3 ||
code == KeyCode.DIGIT4 ||
code == KeyCode.DIGIT5 ||
code == KeyCode.DIGIT6 ||
code == KeyCode.DIGIT7 ||
code == KeyCode.DIGIT8 ||
code == KeyCode.DIGIT9)) {
String newText = prefixField.getText().substring(0, cursorPosition) + str + prefixField.getText().substring(cursorPosition);
newText = newText.replaceAll(" ", "");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < newText.length(); i++) {
sb.append(newText.charAt(i));
if ((i + 1) % (digitsPerGroup) == 0) {
sb.append(' ');
}
}
prefixField.setText(sb.toString());
if (prefixField.getText().charAt(cursorPosition) == ' ') {
cursorPosition++;
}
prefixField.positionCaret(cursorPosition + 1);
}
});
}
private void handleOkButtonPress() {
String prefix = prefixField.getText();
if (prefix != null && !prefix.isEmpty()) {
mainSettingsController.addPrefix(prefix.replaceAll(" ", ""));
closeWindow();
}
}
private void handleCancelButtonPress() {
closeWindow();
}
private void closeWindow() {
Stage stage = (Stage) okButton.getScene().getWindow();
stage.close();
}
}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="92.0" prefWidth="312.0" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="name.nathanmcrae.numbersstation.AddPrefixController">
<children>
<AnchorPane prefHeight="200.0" prefWidth="312.0">
<children>
<Button fx:id="okButton" layoutX="89.0" layoutY="67.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="65.0" text="OK" AnchorPane.bottomAnchor="15.0" AnchorPane.rightAnchor="89.0" />
<Button fx:id="cancelButton" layoutX="169.0" layoutY="67.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="65.0" text="Cancel" AnchorPane.bottomAnchor="15.0" AnchorPane.rightAnchor="9.0" />
<Label layoutX="14.0" layoutY="14.0" text="Enter a message prefix / identifier:" />
<TextField fx:id="prefixField" layoutX="203.0" layoutY="10.0" prefHeight="25.0" prefWidth="105.0" AnchorPane.leftAnchor="203.0" AnchorPane.rightAnchor="10.0" />
</children>
</AnchorPane>
</children>
</VBox>

View File

@ -134,6 +134,11 @@ public class MainController implements Initializable {
xmlMapper.writeValue(new File(filePath.toString()), settings); xmlMapper.writeValue(new File(filePath.toString()), settings);
} else { } else {
settings = xmlMapper.readValue(new File(filePath.toString()), MainSettings.class); settings = xmlMapper.readValue(new File(filePath.toString()), MainSettings.class);
for (StationSettings station : settings.getStations()) {
if (station.getDigitsPerGroup() == 0) {
station.setDigitsPerGroup(4);
}
}
} }
} catch (IOException e) { } catch (IOException e) {
// Print the contents of filePath // Print the contents of filePath

View File

@ -21,8 +21,11 @@ import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.event.Event; import javafx.event.Event;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox; import javafx.scene.control.CheckBox;
import javafx.scene.control.DatePicker; import javafx.scene.control.DatePicker;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView; import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton; import javafx.scene.control.RadioButton;
import javafx.scene.control.Spinner; import javafx.scene.control.Spinner;
@ -33,7 +36,11 @@ import javafx.scene.control.ToggleGroup;
import javafx.scene.input.KeyEvent; import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCode;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.util.Callback;
public class MainSettingsController { public class MainSettingsController {
private IntegerProperty digitsPerGroup = new SimpleIntegerProperty(); private IntegerProperty digitsPerGroup = new SimpleIntegerProperty();
@ -92,6 +99,9 @@ public class MainSettingsController {
@FXML @FXML
private TextField scheduleStartTimeField; private TextField scheduleStartTimeField;
@FXML
private Button testConnectionButton;
@FXML @FXML
private RadioButton externalProgramRadioButton; private RadioButton externalProgramRadioButton;
@ -140,15 +150,49 @@ public class MainSettingsController {
// Consume the event to block the default behavior // Consume the event to block the default behavior
if (!(code == KeyCode.LEFT || if (!(code == KeyCode.LEFT ||
code == KeyCode.RIGHT || code == KeyCode.RIGHT ||
code == KeyCode.UP ||
code == KeyCode.DOWN ||
code == KeyCode.HOME || code == KeyCode.HOME ||
code == KeyCode.END || code == KeyCode.END)) {
code == KeyCode.PAGE_UP ||
code == KeyCode.PAGE_DOWN)) {
event.consume(); event.consume();
} }
if (event.getEventType() == KeyEvent.KEY_PRESSED &&
(code == KeyCode.UP || code == KeyCode.DOWN)) {
boolean up = false;
if (code == KeyCode.UP) {
up = true;
}
String[] timeParts = scheduleStartTimeField.getText().split(":");
if (timeParts.length == 3) {
int hours = Integer.parseInt(timeParts[0]);
int minutes = Integer.parseInt(timeParts[1]);
int seconds = Integer.parseInt(timeParts[2]);
if (cursorPosition < 3) {
if (up && hours < 23) {
hours++;
} else if (!up && hours > 0) {
hours--;
}
} else if (cursorPosition < 6) {
if (up && minutes < 59) {
minutes++;
} else if (!up && minutes > 0) {
minutes--;
}
} else {
if (up && seconds < 59) {
seconds++;
} else if (!up && seconds > 0) {
seconds--;
}
}
scheduleStartTimeField.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
scheduleStartTimeField.positionCaret(cursorPosition);
}
}
if (event.getEventType() == KeyEvent.KEY_PRESSED && if (event.getEventType() == KeyEvent.KEY_PRESSED &&
(code == KeyCode.DIGIT0 || (code == KeyCode.DIGIT0 ||
code == KeyCode.DIGIT1 || code == KeyCode.DIGIT1 ||
@ -178,10 +222,17 @@ public class MainSettingsController {
int minutes = Integer.parseInt(timeParts[1]); int minutes = Integer.parseInt(timeParts[1]);
int seconds = Integer.parseInt(timeParts[2]); int seconds = Integer.parseInt(timeParts[2]);
if (hours > 23) {
hours = 23;
editedTime = "23" + editedTime.substring(2);
}
if (!(hours < 0 || hours > 23 || minutes < 0 || minutes > 59 || seconds < 0 || seconds > 59)) { if (!(hours < 0 || hours > 23 || minutes < 0 || minutes > 59 || seconds < 0 || seconds > 59)) {
scheduleStartTimeField.setText(editedTime); scheduleStartTimeField.setText(editedTime);
scheduleStartTimeField.positionCaret(cursorPosition + 1); scheduleStartTimeField.positionCaret(cursorPosition + 1);
} }
} else {
scheduleStartTimeField.setText("00:00:00");
} }
} }
}); });
@ -237,19 +288,66 @@ public class MainSettingsController {
stage.setUserData(false); stage.setUserData(false);
} }
}); });
prefixListView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> listView) {
return new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
} else {
String newText = item.replaceAll(" ", "");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < newText.length(); i++) {
sb.append(newText.charAt(i));
if ((i + 1) % (digitsPerGroup.get()) == 0) {
sb.append(' ');
}
}
setText(sb.toString());
}
}
};
}
});
externalProgramCommandField.disableProperty().bind(externalProgramRadioButton.selectedProperty().not());
stationAddressField.disableProperty().bind(externalProgramRadioButton.selectedProperty());
usernameField.disableProperty().bind(externalProgramRadioButton.selectedProperty());
passwordField.disableProperty().bind(externalProgramRadioButton.selectedProperty());
testConnectionButton.disableProperty().bind(externalProgramRadioButton.selectedProperty());
} }
@FXML @FXML
private void handleAddPrefixButtonPress() { private void handleAddPrefixButtonPress() {
TextInputDialog dialog = new TextInputDialog(); try {
dialog.setTitle("New prefix"); FXMLLoader loader = new FXMLLoader(getClass().getResource("AddPrefixView.fxml"));
dialog.setHeaderText("Add a new prefix"); Parent root = loader.load();
dialog.setContentText("Enter a message prefix / identifier:");
Optional<String> result = dialog.showAndWait(); AddPrefixController controller = loader.getController();
result.ifPresent(prefix -> { controller.setMainSettingsController(this);
controller.setDigitsPerGroup(settings.getDigitsPerGroup());
Stage stage = new Stage();
stage.setTitle("New prefix");
stage.setScene(new Scene(root));
stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
public void addPrefix(String prefix) {
if (!(prefix == null || prefix.isEmpty())) {
prefixListView.getItems().add(prefix); prefixListView.getItems().add(prefix);
}); }
} }
@FXML @FXML

View File

@ -97,7 +97,7 @@
<TextField fx:id="usernameField" layoutX="444.0" layoutY="65.0" prefHeight="25.0" prefWidth="135.0" AnchorPane.rightAnchor="0.0" /> <TextField fx:id="usernameField" layoutX="444.0" layoutY="65.0" prefHeight="25.0" prefWidth="135.0" AnchorPane.rightAnchor="0.0" />
<PasswordField fx:id="passwordField" layoutX="444.0" layoutY="95.0" prefHeight="25.0" prefWidth="135.0" AnchorPane.rightAnchor="0.0" /> <PasswordField fx:id="passwordField" layoutX="444.0" layoutY="95.0" prefHeight="25.0" prefWidth="135.0" AnchorPane.rightAnchor="0.0" />
<TextField fx:id="externalProgramCommandField" layoutX="140.0" layoutY="131.0" prefHeight="25.0" prefWidth="438.0" AnchorPane.leftAnchor="140.0" AnchorPane.rightAnchor="0.0" /> <TextField fx:id="externalProgramCommandField" layoutX="140.0" layoutY="131.0" prefHeight="25.0" prefWidth="438.0" AnchorPane.leftAnchor="140.0" AnchorPane.rightAnchor="0.0" />
<Button layoutX="188.0" layoutY="65.0" mnemonicParsing="false" onMousePressed="#handleTestConnectionButtonPress" text="Test connection" /> <Button fx:id="testConnectionButton" layoutX="188.0" layoutY="65.0" mnemonicParsing="false" onMousePressed="#handleTestConnectionButtonPress" text="Test connection" />
</children> </children>
<VBox.margin> <VBox.margin>
@ -133,24 +133,24 @@
<AnchorPane prefHeight="152.0" prefWidth="578.0"> <AnchorPane prefHeight="152.0" prefWidth="578.0">
<children> <children>
<CheckBox fx:id="manageScheduleExternallyCheckBox" layoutX="395.0" mnemonicParsing="false" text="Manage schedule externally" AnchorPane.rightAnchor="14.5" /> <CheckBox fx:id="manageScheduleExternallyCheckBox" layoutX="395.0" mnemonicParsing="false" text="Manage schedule externally" AnchorPane.rightAnchor="14.5" />
<RadioButton fx:id="dailyRadioButton" layoutX="14.0" layoutY="8.0" mnemonicParsing="false" text="Daily"> <RadioButton fx:id="dailyRadioButton" layoutX="14.0" layoutY="8.0" mnemonicParsing="false" text="Daily" disable="${manageScheduleExternallyCheckBox.selected}">
<toggleGroup> <toggleGroup>
<ToggleGroup fx:id="messagePeriodGroup" /> <ToggleGroup fx:id="messagePeriodGroup" />
</toggleGroup> </toggleGroup>
</RadioButton> </RadioButton>
<RadioButton fx:id="weeklyRadioButton" layoutX="14.0" layoutY="38.0" mnemonicParsing="false" text="Weekly"> <RadioButton fx:id="weeklyRadioButton" layoutX="14.0" layoutY="38.0" mnemonicParsing="false" text="Weekly" disable="${manageScheduleExternallyCheckBox.selected}">
<toggleGroup> <toggleGroup>
<fx:reference source="messagePeriodGroup" /> <fx:reference source="messagePeriodGroup" />
</toggleGroup> </toggleGroup>
</RadioButton> </RadioButton>
<RadioButton fx:id="monthlyRadioButton" layoutX="14.0" layoutY="68.0" mnemonicParsing="false" text="Monthly"> <RadioButton fx:id="monthlyRadioButton" layoutX="14.0" layoutY="68.0" mnemonicParsing="false" text="Monthly" disable="${manageScheduleExternallyCheckBox.selected}">
<toggleGroup> <toggleGroup>
<fx:reference source="messagePeriodGroup" /> <fx:reference source="messagePeriodGroup" />
</toggleGroup> </toggleGroup>
</RadioButton> </RadioButton>
<DatePicker fx:id="scheduleStartDatePicker" layoutX="115.0" layoutY="34.0" /> <DatePicker fx:id="scheduleStartDatePicker" layoutX="115.0" layoutY="34.0" disable="${manageScheduleExternallyCheckBox.selected}" />
<Label layoutX="115.0" layoutY="8.0" text="Starting from:" /> <Label layoutX="115.0" layoutY="8.0" text="Starting from:" />
<TextField fx:id="scheduleStartTimeField" layoutX="115.0" layoutY="64.0" text="23:24:49" /> <TextField fx:id="scheduleStartTimeField" layoutX="115.0" layoutY="64.0" text="23:24:49" disable="${manageScheduleExternallyCheckBox.selected}" />
<Label layoutX="48.0" layoutY="102.0" text="TODO: Jitter" /> <Label layoutX="48.0" layoutY="102.0" text="TODO: Jitter" />
</children> </children>

View File

@ -59,7 +59,7 @@
</children> </children>
</AnchorPane> </AnchorPane>
<Separator prefWidth="200.0" /> <Separator prefWidth="200.0" />
<AnchorPane prefHeight="50.0" prefWidth="570.0"> <AnchorPane prefHeight="50.0" prefWidth="640.0">
<children> <children>
<Label layoutX="14.0" text="Next message will be sent:" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="14.0"> <Label layoutX="14.0" text="Next message will be sent:" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="14.0">
<font> <font>
@ -79,7 +79,7 @@
<Insets bottom="10.0" /> <Insets bottom="10.0" />
</padding> </padding>
</VBox> </VBox>
<TextArea fx:id="messageTextArea" layoutY="110.0" prefHeight="270.0" prefWidth="640.0" text="0239 0480 2938 0928&#10;3093 2298 3923 8933" wrapText="true" AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="110.0"> <TextArea fx:id="messageTextArea" layoutY="110.0" prefHeight="270.0" prefWidth="640.0" text="0239 0480 2938 0928&#10;3093 2298 3923 8933" wrapText="true" AnchorPane.bottomAnchor="40.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="90.0">
<font> <font>
<Font size="30.0" /> <Font size="30.0" />
</font> </font>