private void registerTypeDependentValidators(PropertyTypeId typeId) { Validator<String> valueValidator = (c, val) -> ValidationResult.fromErrorIf(valueField, "The value couldn't be parsed", Try.tryGet(() -> getValueParser(typeId).valueOf(getValue())).isFailure()); validationSupport.registerValidator(valueField, valueValidator); }
ValidationResult.fromErrorIf(tfDistance, context.getResources().getString("st.dlg.exercise.error.distance"), !ValidationUtils.isValueDoubleBetween(newValue, exerciseViewModel.sportTypeRecordDistance.get() ? 0.001f : 0, Float.MAX_VALUE))); validationSupport.registerValidator(tfAvgSpeed, true, (Control control, String newValue) -> ValidationResult.fromErrorIf(tfAvgSpeed, context.getResources().getString("st.dlg.exercise.error.avg_speed"), !ValidationUtils.isValueSpeed(newValue, exerciseViewModel.getSpeedConverter(), exerciseViewModel.sportTypeRecordDistance.get()))); validationSupport.registerValidator(tfDuration, true, (Control control, String newValue) -> ValidationResult.fromErrorIf(tfDuration, context.getResources().getString("st.dlg.exercise.error.duration"), !ValidationUtils.isValueTimeInSecondsBetween(newValue, 1, Integer.MAX_VALUE))); ValidationResult.fromErrorIf(tfAscent, context.getResources().getString("st.dlg.exercise.error.ascent"), !ValidationUtils.isOptionalValueIntegerBetween(newValue, 0, Integer.MAX_VALUE))); validationSupport.registerValidator(tfDescent, false, (Control control, String newValue) -> ValidationResult.fromErrorIf(tfDescent, context.getResources().getString("st.dlg.exercise.error.descent"), !ValidationUtils.isOptionalValueIntegerBetween(newValue, 0, Integer.MAX_VALUE))); validationSupport.registerValidator(tfAvgHeartrate, false, (Control control, String newValue) -> ValidationResult.fromErrorIf(tfAvgHeartrate, context.getResources().getString("st.dlg.exercise.error.avg_heartrate"), !ValidationUtils.isOptionalValueIntegerBetween(newValue, 0, 299))); validationSupport.registerValidator(tfCalories, false, (Control control, String newValue) -> ValidationResult.fromErrorIf(tfCalories, context.getResources().getString("st.dlg.exercise.error.calories"), !ValidationUtils.isOptionalValueIntegerBetween(newValue, 0, Integer.MAX_VALUE)));
@Override protected void setupDialogControls() { laWeightUnit.setText(context.getFormatUtils().getWeightUnitName()); // setup binding between view model and the UI controls dpDate.valueProperty().bindBidirectional(weightViewModel.date); // use text formatter for time values => makes sure that the value is also valid final TextFormatter<LocalTime> timeTextFormatter = new TextFormatter<>(new TimeToStringConverter()); timeTextFormatter.valueProperty().bindBidirectional(weightViewModel.time); tfTime.setTextFormatter(timeTextFormatter); tfValue.textProperty().bindBidirectional(weightViewModel.value, new NumberStringConverter()); taComment.textProperty().bindBidirectional(weightViewModel.comment); FxWorkarounds.fixDatePickerTextEntry(dpDate); // setup validation of the UI controls // => due to a ControlsFX bug the validation setup must be executed after the dialog has been shown // (see https://bitbucket.org/controlsfx/controlsfx/issues/539/multiple-dialog-fields-with-validation ) Platform.runLater(() -> { validationSupport.registerValidator(dpDate, Validator.createEmptyValidator(context.getResources().getString("st.dlg.weight.error.date"))); validationSupport.registerValidator(tfTime, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.weight.error.time"))); validationSupport.registerValidator(tfValue, true, (Control control, String newValue) -> ValidationResult .fromErrorIf(tfValue, context.getResources().getString("st.dlg.weight.error.weight"), !ValidationUtils.isValueDoubleBetween(newValue, 0.1d, 1000))); }); }