From 9cb0eae10fef3445f93961219fb7a37e85e1b5cf Mon Sep 17 00:00:00 2001 From: Nathan McRae Date: Wed, 15 Jan 2025 22:37:27 -0800 Subject: [PATCH] Add schedule start time setting --- .../MainSettingsController.java | 80 ++++++++++++++++++- .../numbersstation/MainSettingsView.fxml | 2 +- .../numbersstation/StationSettings.java | 10 +++ 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/src/main/java/name/nathanmcrae/numbersstation/MainSettingsController.java b/src/main/java/name/nathanmcrae/numbersstation/MainSettingsController.java index b73388e..bc6a17d 100644 --- a/src/main/java/name/nathanmcrae/numbersstation/MainSettingsController.java +++ b/src/main/java/name/nathanmcrae/numbersstation/MainSettingsController.java @@ -5,8 +5,10 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import java.io.File; import java.io.IOException; -import java.util.Optional; +import java.time.format.DateTimeFormatter; import java.time.LocalDate; +import java.time.LocalTime; +import java.util.Optional; import javafx.beans.property.BooleanProperty; import javafx.beans.property.IntegerProperty; import javafx.beans.property.ObjectProperty; @@ -45,6 +47,7 @@ public class MainSettingsController { private StringProperty stationAddress = new SimpleStringProperty(); private StringProperty stationName = new SimpleStringProperty(); private ObjectProperty scheduleStartDate = new SimpleObjectProperty<>(); + private StringProperty scheduleStartTime = new SimpleStringProperty(); private StringProperty username = new SimpleStringProperty(); @FXML @@ -86,6 +89,9 @@ public class MainSettingsController { @FXML private RadioButton scpRadioButton; + @FXML + private TextField scheduleStartTimeField; + @FXML private RadioButton externalProgramRadioButton; @@ -114,6 +120,7 @@ public class MainSettingsController { stationAddressField.textProperty().bindBidirectional(stationAddress); passwordField.textProperty().bindBidirectional(password); scheduleStartDatePicker.valueProperty().bindBidirectional(scheduleStartDate); + scheduleStartTimeField.textProperty().bindBidirectional(scheduleStartTime); usernameField.textProperty().bindBidirectional(username); digitsPerGroupSpinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 100, 1)); @@ -122,6 +129,63 @@ public class MainSettingsController { digitsPerGroupSpinner.getValueFactory().valueProperty().bindBidirectional(digitsPerGroup.asObject()); messageLengthSpinner.getValueFactory().valueProperty().bindBidirectional(messageLength.asObject()); + scheduleStartTimeField.addEventFilter(KeyEvent.ANY, event -> { + int cursorPosition = scheduleStartTimeField.getCaretPosition(); + + String character = event.getCharacter(); + KeyCode code = event.getCode(); + + String str = event.getText(); + + // Consume the event to block the default behavior + if (!(code == KeyCode.LEFT || + code == KeyCode.RIGHT || + code == KeyCode.UP || + code == KeyCode.DOWN || + code == KeyCode.HOME || + code == KeyCode.END || + code == KeyCode.PAGE_UP || + code == KeyCode.PAGE_DOWN)) { + event.consume(); + } + + 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 editedTime = scheduleStartTimeField.getText(); + + if (cursorPosition < scheduleStartTimeField.getLength()) { + char currentChar = scheduleStartTimeField.getText().charAt(cursorPosition); + if (currentChar == ':') { + cursorPosition++; + } + editedTime = editedTime.substring(0, cursorPosition) + str + editedTime.substring(cursorPosition + 1); + } + + // Validate the time format + String[] timeParts = editedTime.split(":"); + if (timeParts.length == 3) { + int hours = Integer.parseInt(timeParts[0]); + int minutes = Integer.parseInt(timeParts[1]); + int seconds = Integer.parseInt(timeParts[2]); + + if (!(hours < 0 || hours > 23 || minutes < 0 || minutes > 59 || seconds < 0 || seconds > 59)) { + scheduleStartTimeField.setText(editedTime); + scheduleStartTimeField.positionCaret(cursorPosition + 1); + } + } + } + }); + messageMethodGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) -> { if (newValue == ftpRadioButton) { messageMethod.set(StationSettings.MessageMethod.FTP); @@ -219,6 +283,14 @@ public class MainSettingsController { settings.setScheduleStartDate(scheduleStartDatePicker.getValue()); settings.setUsername(username.get()); + try { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); + LocalTime startTime = LocalTime.parse(scheduleStartTimeField.getText(), formatter); + settings.setScheduleStartTime(startTime); + } catch (Exception ex) { + ex.printStackTrace(); + } + Node node = (Node) e.getSource(); Stage stage = (Stage) node.getScene().getWindow(); stage.setUserData(true); @@ -250,5 +322,11 @@ public class MainSettingsController { System.out.println(settings.getPrefixes()); prefixListView.getItems().addAll(settings.getPrefixes()); scheduleStartDate.set(settings.getScheduleStartDate()); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); + LocalTime startTime = settings.getScheduleStartTime(); + if (startTime == null) { + startTime = LocalTime.now(); + } + scheduleStartTime.set(startTime.format(formatter)); } } diff --git a/src/main/java/name/nathanmcrae/numbersstation/MainSettingsView.fxml b/src/main/java/name/nathanmcrae/numbersstation/MainSettingsView.fxml index 908ec8d..fdd95bb 100644 --- a/src/main/java/name/nathanmcrae/numbersstation/MainSettingsView.fxml +++ b/src/main/java/name/nathanmcrae/numbersstation/MainSettingsView.fxml @@ -150,7 +150,7 @@