Skip to content

Instantly share code, notes, and snippets.

@fastnsilver
Created June 1, 2012 05:29
Show Gist options
  • Save fastnsilver/2849115 to your computer and use it in GitHub Desktop.
Save fastnsilver/2849115 to your computer and use it in GitHub Desktop.
Updates field contents of a AbstractValidatableColumn
import java.util.Set;
import javax.validation.ConstraintViolation;
import org.spp.im.mui.gwt.client.module.common.widget.grid.ValidatableInputCell.ValidationData;
import org.spp.im.mui.gwt.shared.i18n.UiMessages;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.user.cellview.client.AbstractHasData;
import com.google.gwt.view.client.ProvidesKey;
/**
* Encapsulates logic for validating then updating a row-column value.
* @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
*/
final class ValidatableFieldUpdater<T, O> implements FieldUpdater<T, String> {
private final AbstractHasData<T> table;
private final AbstractValidatableColumn<T, O> column;
ValidatableFieldUpdater(AbstractHasData<T> table, AbstractValidatableColumn<T, O> column) {
this.table = table;
this.column = column;
}
@Override
public void update(int index, T dto, String newValue) {
final ValidatableInputCell cell = column.getCell();
final ConversionResult cr = column.attemptValueConversion(newValue);
final ProvidesKey<T> keyProvider = table.getKeyProvider();
final ValidationData viewData = cell.getViewData(keyProvider.getKey(dto));
if (cr.wasConvertedSuccessfully()) {
final Set<ConstraintViolation<O>> violations = column.validate(cr.getValue());
if (!violations.isEmpty()) { // invalid
final StringBuffer errorMessage = new StringBuffer();
for (final ConstraintViolation<O> constraintViolation : violations) {
errorMessage.append(constraintViolation.getMessage());
}
viewData.setInvalid(true);
cell.setErrorMessage(errorMessage.toString());
table.redraw();
} else { // valid
viewData.setInvalid(false);
cell.setErrorMessage("");
column.doUpdate(index, dto, newValue);
}
} else { // conversion exception
viewData.setInvalid(true);
cell.setErrorMessage(UiMessages.INSTANCE.improper_input_format());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment