From 807e13d23936965009cc3c4f343f6099a13cb9e6 Mon Sep 17 00:00:00 2001 From: Nathan McRae Date: Sat, 18 Jan 2025 21:47:50 -0800 Subject: [PATCH] Add up/down control to time field --- .../MainSettingsController.java | 51 +++++++++++++++++-- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/src/main/java/name/nathanmcrae/numbersstation/MainSettingsController.java b/src/main/java/name/nathanmcrae/numbersstation/MainSettingsController.java index bc6a17d..cc7e242 100644 --- a/src/main/java/name/nathanmcrae/numbersstation/MainSettingsController.java +++ b/src/main/java/name/nathanmcrae/numbersstation/MainSettingsController.java @@ -140,15 +140,49 @@ public class MainSettingsController { // 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)) { + code == KeyCode.END)) { 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 && (code == KeyCode.DIGIT0 || code == KeyCode.DIGIT1 || @@ -178,10 +212,17 @@ public class MainSettingsController { int minutes = Integer.parseInt(timeParts[1]); 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)) { scheduleStartTimeField.setText(editedTime); scheduleStartTimeField.positionCaret(cursorPosition + 1); } + } else { + scheduleStartTimeField.setText("00:00:00"); } } });