From eb90d6a31a35c71d70e91766e90ff047d2201b82 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 9 Dec 2021 10:28:33 +0200 Subject: [PATCH] constrain locktime datetimepicker to show valid values only --- .../transaction/HeadersController.java | 38 +++++++++++++++++++ .../sparrow/transaction/headers.fxml | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java index 0266c3ba..1245b736 100644 --- a/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java +++ b/src/main/java/com/sparrowwallet/sparrow/transaction/HeadersController.java @@ -30,6 +30,7 @@ import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.Node; import javafx.scene.control.*; +import javafx.scene.control.DateCell; import javafx.scene.input.Clipboard; import javafx.scene.input.ClipboardContent; import javafx.scene.input.KeyCode; @@ -40,6 +41,7 @@ import javafx.stage.FileChooser; import javafx.stage.Modality; import javafx.stage.Stage; import javafx.util.Duration; +import javafx.util.StringConverter; import org.controlsfx.glyphfont.Glyph; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -64,6 +66,9 @@ public class HeadersController extends TransactionFormController implements Init public static final String BLOCK_TIMESTAMP_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss ZZZ"; public static final String UNFINALIZED_TXID_CLASS = "unfinalized-txid"; + public static final String MAX_LOCKTIME_DATE = "2106-02-07T06:28:15Z"; + public static final String MIN_LOCKTIME_DATE = "1985-11-05T00:53:20Z"; + private HeadersForm headersForm; @FXML @@ -321,6 +326,17 @@ public class HeadersController extends TransactionFormController implements Init } }); + LocalDateTime maxLocktimeDate = Instant.parse(MAX_LOCKTIME_DATE).atZone(ZoneId.systemDefault()).toLocalDateTime(); + LocalDateTime minLocktimeDate = Instant.parse(MIN_LOCKTIME_DATE).atZone(ZoneId.systemDefault()).toLocalDateTime(); + locktimeDate.setDayCellFactory(d -> + new DateCell() { + @Override + public void updateItem(LocalDate item, boolean empty) { + super.updateItem(item, empty); + setDisable(item.isAfter(maxLocktimeDate.toLocalDate()) || item.isBefore(minLocktimeDate.toLocalDate())); + } + }); + locktimeDate.setFormat(LOCKTIME_DATE_FORMAT); locktimeDate.dateTimeValueProperty().addListener((obs, oldValue, newValue) -> { int caret = locktimeDate.getEditor().getCaretPosition(); @@ -333,6 +349,28 @@ public class HeadersController extends TransactionFormController implements Init } }); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(LOCKTIME_DATE_FORMAT); + locktimeDate.setConverter(new StringConverter() { + public String toString(LocalDate object) { + LocalDateTime value = locktimeDate.getDateTimeValue(); + return (value != null) ? value.format(formatter) : ""; + } + + public LocalDate fromString(String value) { + if(value == null) { + locktimeDate.setDateTimeValue(null); + return null; + } + + LocalDateTime localDateTime = LocalDateTime.parse(value, formatter); + if(localDateTime.isAfter(maxLocktimeDate) || localDateTime.isBefore(minLocktimeDate)) { + throw new IllegalArgumentException("Invalid locktime date"); + } + locktimeDate.setDateTimeValue(localDateTime); + return locktimeDate.getDateTimeValue().toLocalDate(); + } + }); + locktimeDate.getEditor().textProperty().addListener((observable, oldValue, newValue) -> { String controlValue = locktimeDate.getDateTimeValue().format(DateTimeFormatter.ofPattern(locktimeDate.getFormat())); if(!controlValue.equals(newValue) && !locktimeDate.getStyleClass().contains("edited")) { diff --git a/src/main/resources/com/sparrowwallet/sparrow/transaction/headers.fxml b/src/main/resources/com/sparrowwallet/sparrow/transaction/headers.fxml index 8002b30b..220fed77 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/transaction/headers.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/transaction/headers.fxml @@ -97,7 +97,7 @@ - +