private void registerBasicValidators() { Validator<String> noWhitespaceName = Validator.createRegexValidator("Name cannot contain whitespace", "\\S*+", Severity.ERROR); Validator<String> emptyName = Validator.createEmptyValidator("Name required"); Validator<String> uniqueName = (c, val) -> { long sameNameDescriptors = backingDescriptorList.getOrElse(FXCollections.emptyObservableList()) .stream() .map(PropertyDescriptorSpec::getName) .filter(getName()::equals) .count(); return new ValidationResult().addErrorIf(c, "The name must be unique", sameNameDescriptors > 1); }; validationSupport.registerValidator(nameField, Validator.combine(noWhitespaceName, emptyName, uniqueName)); Validator<String> noWhitespaceDescription = Validator.createRegexValidator("Message cannot be whitespace", "(\\s*+\\S.*)?", Severity.ERROR); Validator<String> emptyDescription = Validator.createEmptyValidator("Message required"); validationSupport.registerValidator(descriptionField, Validator.combine(noWhitespaceDescription, emptyDescription)); }
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); }
TextArea area = (TextArea) loader.getNamespace().get("stackTraceArea"); ValidationSupport validation = new ValidationSupport(); validation.registerValidator(area, Validator.createEmptyValidator("The stack trace may not be empty")); button.disableProperty().bind(validation.invalidProperty());
/** * Registers {@link Validator} for specified control and makes control required * @param c control to validate * @param validator {@link Validator} to be used * @return true if registration is successful */ public <T> boolean registerValidator( final Control c, final Validator<T> validator ) { return registerValidator(c, true, validator); }
/** * Factory method to create a validator, which checks if value exists. * Error is created if not if value does not exist * @param message of a error to be created if value is invalid * @return new validator */ public static <T> Validator<T> createEmptyValidator(final String message) { return createEmptyValidator(message, Severity.ERROR); }
@Override default public int compareTo(ValidationMessage msg) { return msg == null || getTarget() != msg.getTarget() ? -1: getSeverity().compareTo(msg.getSeverity()); } }
/** * Combine validation result with another. This will create a new instance of combined validation result * @param validationResult * @return new instance of combined validation result */ public ValidationResult combine( ValidationResult validationResult ) { return validationResult == null? copy(): copy().addAll(validationResult.getMessages()); }
/** * Factory method to create validation result out of one warning * @param target validation target * @param text message text * @param condition condition on which message will be added to validation result * @return New instance of validation result */ public static final ValidationResult fromWarningIf( Control target, String text, boolean condition ) { return new ValidationResult().addWarningIf(target, text, condition); }
/** * Factory method to create validation result out of several validation results * @param results results * @return New instance of validation result, combining all into one */ public static final ValidationResult fromResults( ValidationResult... results ) { return new ValidationResult().combineAll(results); }
@Override public void initialize(URL location, ResourceBundle resources) { commitButton.setOnAction(e -> { commitHandler.ifPresent(Runnable::run); getStage().close(); this.free(); }); commitButton.disableProperty().bind(validationSupport.invalidProperty()); Platform.runLater(() -> { typeId.bind(typeChoiceBox.getSelectionModel().selectedItemProperty()); typeChoiceBox.setConverter(DesignerUtil.stringConverter(PropertyTypeId::getStringId, PropertyTypeId::lookupMnemonic)); typeChoiceBox.getItems().addAll(PropertyTypeId.typeIdsToConstants().values()); FXCollections.sort(typeChoiceBox.getItems()); }); Platform.runLater(this::registerBasicValidators); typeIdProperty().values() .filter(Objects::nonNull) .subscribe(this::registerTypeDependentValidators); }
/** * Factory method to create validation result out of one warning * @param target validation target * @param text message text * @return New instance of validation result */ public static final ValidationResult fromWarning( Control target, String text ) { return fromMessages( ValidationMessage.warning(target, text)); }
/** * Add one message to validation result with condition * @param target validation target * @param text message text * @param severity message severity * @param condition condition on which message will be added * @return updated validation result */ public ValidationResult addMessageIf( Control target, String text, Severity severity, boolean condition) { return condition? add( new SimpleValidationMessage(target, text, severity)): this; }
/** * Factory method to create validation result out of one error * @param target validation target * @param text message text * @return New instance of validation result */ public static final ValidationResult fromError( Control target, String text ) { return fromMessages( ValidationMessage.error(target, text)); }
/** * Creates a copy of validation result * @return copy of validation result */ public ValidationResult copy() { return ValidationResult.fromMessages(getMessages()); }
/** * Add one warning to validation result with condition * @param target validation target * @param text message text * @param condition condition on which warning will be added * @return updated validation result */ public ValidationResult addWarningIf( Control target, String text, boolean condition) { return addMessageIf(target,text,Severity.WARNING,condition); }
/** * Factory method to create a simple error message * @param target message target * @param text message text * @return error message */ public static ValidationMessage error( Control target, String text ) { return new SimpleValidationMessage(target, text, Severity.ERROR); }
/** * Factory method to create a validator, which checks if value exists in the provided collection. * Error is created if not found * @param message text of a error to be created if value is not found * @param values * @return new validator */ public static <T> Validator<T> createEqualsValidator(final String message, final Collection<T> values) { return createEqualsValidator(message, Severity.ERROR, values); }
/** * Factory method to create a validator, which evaluates the value validity with a given predicate. * Error is created if the evaluation is <code>false</code>. * @param message text of a message to be created if value is invalid * @param predicate the predicate to be used for the value validity evaluation. * @return new validator */ static <T> Validator<T> createPredicateValidator(Predicate<T> predicate, String message) { return createPredicateValidator(predicate, message, Severity.ERROR); }
/** * Add one error to validation result with condition * @param target validation target * @param text message text * @param condition condition on which error will be added * @return updated validation result */ public ValidationResult addErrorIf( Control target, String text, boolean condition) { return addMessageIf(target,text,Severity.ERROR,condition); }
/** * Factory method to create a simple warning message * @param target message target * @param text message text * @return warning message */ public static ValidationMessage warning( Control target, String text ) { return new SimpleValidationMessage(target, text, Severity.WARNING); }