Skip to content

Instantly share code, notes, and snippets.

@fastnsilver
Created June 1, 2012 05:27
Show Gist options
  • Save fastnsilver/2849103 to your computer and use it in GitHub Desktop.
Save fastnsilver/2849103 to your computer and use it in GitHub Desktop.
Custom GWT Column extension that offers JSR-303 client-side support for validating cell value within a column (incl. error message pop-up panel)
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import com.google.gwt.user.cellview.client.AbstractHasData;
import com.google.gwt.user.cellview.client.Column;
/**
* A {@link Column} implementation that encapsulates a {@link ValidatableInputCell}.
* Performs JSR-303 validation on a field (or nested field) of the type.
* @author cphillipson
*
* @param <T> the type
* @param <O> the owning type of the field to be validated; in many cases T may have only primitive or wrapper types, therefore O could be the same type as T
*/
public abstract class AbstractValidatableColumn<T, O> extends Column<T, String> {
/**
* Preferred constructor.
* Allows for definition of tabIndex but uses a default for the input cell size.
* @param tabIndex the <code>tabindex</code> attribute's value for the input cell
* @param table the grid instance
*/
public AbstractValidatableColumn(int tabIndex, final AbstractHasData<T> table) {
this(App.INSTANCE.defaultValidatableInputCellSize(), tabIndex, table);
}
/**
* Overloaded constructor.
* Allows for definition of tabIndex and allows for an override to the default for the input cell size.
* @param inputSize the <code>size</code> attribute's value for the input cell
* @param tabIndex the <code>tabindex</code> attribute's value for the input cell
* @param table the grid instance
*/
public AbstractValidatableColumn(int inputSize, int tabIndex, final AbstractHasData<T> table) {
super(new ValidatableInputCell());
getCell().setInputSize(inputSize);
getCell().setTabIndex(tabIndex);
init(table);
}
// meat and potatoes
private void init(final AbstractHasData<T> table) {
setFieldUpdater(new ValidatableFieldUpdater(table, this));
}
/**
* Attempts conversion of a String value into another type
* Instances are responsible for the conversion logic as it may vary from type to type
* @param value a String value to be converted into an owning class's property type
* @return a ConversionResult
*/
protected abstract ConversionResult attemptValueConversion(String value);
@Override
public ValidatableInputCell getCell() {
return (ValidatableInputCell) super.getCell();
}
/**
* Template method for updating a field (or nested field) value within a DTO
* @param index the row index for the instance of the DTO within the grid
* @param dto the object whose field we wish to update
* @param value the new value that will be set on a field (or nested field) of the DTO
*/
protected abstract void doUpdate(int index, T dto, String value);
/**
* Template method for specifying the property name of an owning class
* @return the field name of the owning class whose value is to be updated
*/
protected abstract String getPropertyName();
/**
* Template method for specifying the owning class
* @return the owning class of the field whose value is to be updated
*/
protected abstract Class<O> getPropertyOwner();
/**
* Validates a value against a set of constraints (i.e., JSR-303 annotations on a field)
* @param newValue the value to be validated
* @return the set of constraint violations induced by an inappropriate value
*/
protected Set<ConstraintViolation<O>> validate(Object newValue) {
final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
final Set<ConstraintViolation<O>> violations = validator.validateValue(getPropertyOwner(), getPropertyName(), newValue);
return violations;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment