com.holonplatform.vaadin7.components.ValidatableInput Maven / Gradle / Ivy
/*
* Copyright 2000-2017 Holon TDCN.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.holonplatform.vaadin7.components;
import java.util.Collection;
import java.util.Optional;
import com.holonplatform.core.Validator;
import com.holonplatform.core.internal.utils.ObjectUtils;
import com.holonplatform.vaadin7.Registration;
import com.holonplatform.vaadin7.components.builders.ValidatableInputBuilder;
import com.holonplatform.vaadin7.internal.components.ValidatableInputWrapper;
import com.holonplatform.vaadin7.internal.components.ValidationUtils;
import com.holonplatform.vaadin7.internal.components.builders.DefaultValidatableInputBuilder;
import com.vaadin.ui.Field;
/**
* An {@link Input} component with validation support using {@link Validator}s.
*
* @param Value type
*
* @since 5.0.0
*/
public interface ValidatableInput extends Input, Validatable {
/**
* Adds a {@link Validator} to validate the input value.
* @param validator The validator to add (not null)
* @return The validator registration reference
*/
Registration addValidator(Validator validator);
/**
* Sets whether to validate the value, using registered {@link Validator}s, every time the {@link Input} value
* changes.
* @param validateOnValueChange true
to perform value validation every time the {@link Input} value
* changes, false
if not
*/
void setValidateOnValueChange(boolean validateOnValueChange);
/**
* Gets whether to validate the value, using registered {@link Validator}s, every time the {@link Input} value
* changes.
*
* Default is true
.
*
* @return true
if the value validation must be performed every time the {@link Input} value changes
*/
boolean isValidateOnValueChange();
/**
* Set the {@link ValidationStatusHandler} to use to track validation status changes.
* @param validationStatusHandler the {@link ValidationStatusHandler} to set
*/
void setValidationStatusHandler(ValidationStatusHandler validationStatusHandler);
/**
* Get the {@link ValidationStatusHandler} to use to track validation status changes, if available.
* @return the optional {@link ValidationStatusHandler}
*/
Optional getValidationStatusHandler();
/**
* Create a {@link ValidatableInput} from given {@link Input} instance.
* @param Value type
* @param input The {@link Input} instance (not null)
* @return A new {@link ValidatableInput} component which wraps the given input
*/
static ValidatableInput from(Input input) {
ObjectUtils.argumentNotNull(input, "Input must be not null");
return (input instanceof ValidatableInput) ? (ValidatableInput) input : new ValidatableInputWrapper<>(input);
}
/**
* Create a {@link ValidatableInput} component type from given {@link Field} instance.
*
* Any {@link Field} validator will be inherited from the returned {@link ValidatableInput} instance.
*
* @param Value type
* @param field The field instance (not null)
* @return A new {@link ValidatableInput} component which wraps the given field
*/
static ValidatableInput from(Field field) {
return from(field, true);
}
/**
* Create a {@link ValidatableInput} component type from given {@link Field} instance.
* @param Value type
* @param field The field instance (not null)
* @param inheritValidators Whether to inherit {@link Field} validators, if any
* @return A new {@link ValidatableInput} component which wraps the given field
*/
static ValidatableInput from(Field field, boolean inheritValidators) {
final ValidatableInput input = from(Input.from(field));
if (inheritValidators) {
Collection fieldValidators = field.getValidators();
if (fieldValidators != null) {
fieldValidators.forEach(v -> input.addValidator(ValidationUtils.asValidator(v)));
}
}
return input;
}
/**
* Get a fluent builder to create and setup a {@link ValidatableInput} from given {@link Input}.
* @param Value type
* @param input Concrete input component (not null)
* @return {@link ValidatableInput} builder
*/
static ValidatableInputBuilder> builder(Input input) {
return new DefaultValidatableInputBuilder<>(input);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy