constrain locktime datetimepicker to show valid values only

This commit is contained in:
Craig Raw 2021-12-09 10:28:33 +02:00
parent b013b5f50f
commit eb90d6a31a
2 changed files with 39 additions and 1 deletions

View file

@ -30,6 +30,7 @@ import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.control.DateCell;
import javafx.scene.input.Clipboard; import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent; import javafx.scene.input.ClipboardContent;
import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCode;
@ -40,6 +41,7 @@ import javafx.stage.FileChooser;
import javafx.stage.Modality; import javafx.stage.Modality;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.util.Duration; import javafx.util.Duration;
import javafx.util.StringConverter;
import org.controlsfx.glyphfont.Glyph; import org.controlsfx.glyphfont.Glyph;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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 BLOCK_TIMESTAMP_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss ZZZ";
public static final String UNFINALIZED_TXID_CLASS = "unfinalized-txid"; 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; private HeadersForm headersForm;
@FXML @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.setFormat(LOCKTIME_DATE_FORMAT);
locktimeDate.dateTimeValueProperty().addListener((obs, oldValue, newValue) -> { locktimeDate.dateTimeValueProperty().addListener((obs, oldValue, newValue) -> {
int caret = locktimeDate.getEditor().getCaretPosition(); 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<LocalDate>() {
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) -> { locktimeDate.getEditor().textProperty().addListener((observable, oldValue, newValue) -> {
String controlValue = locktimeDate.getDateTimeValue().format(DateTimeFormatter.ofPattern(locktimeDate.getFormat())); String controlValue = locktimeDate.getDateTimeValue().format(DateTimeFormatter.ofPattern(locktimeDate.getFormat()));
if(!controlValue.equals(newValue) && !locktimeDate.getStyleClass().contains("edited")) { if(!controlValue.equals(newValue) && !locktimeDate.getStyleClass().contains("edited")) {

View file

@ -97,7 +97,7 @@
<Spinner fx:id="locktimeNone" disable="true" prefWidth="120"/> <Spinner fx:id="locktimeNone" disable="true" prefWidth="120"/>
</Field> </Field>
<Field fx:id="locktimeBlockField" text="Block:"> <Field fx:id="locktimeBlockField" text="Block:">
<Spinner fx:id="locktimeBlock" editable="true" prefWidth="120"/> <Spinner fx:id="locktimeBlock" editable="true" max="499999999" prefWidth="120"/>
<Hyperlink fx:id="locktimeCurrentHeight" text="Set current height" onAction="#setLocktimeToCurrentHeight" /> <Hyperlink fx:id="locktimeCurrentHeight" text="Set current height" onAction="#setLocktimeToCurrentHeight" />
<Label fx:id="futureBlockWarning"> <Label fx:id="futureBlockWarning">
<graphic> <graphic>