use of com.vaadin.ui.AbstractField in project vaadin-tips by marcushellberg.
the class FieldGroupUtil method improveUX.
/**
* Improves validation UX and enables/disables save/reset buttons depending on FieldGroup state.
*
* NOTE: This is not a fully complete/foolproof version. Please see AbstractForm, MBeanFieldGroup, and MTextField
* Viritin for a more complete solution. https://github.com/viritin/viritin
*/
public static void improveUX(FieldGroup fieldGroup, Button saveButton, Button clearButton) {
saveButton.setEnabled(false);
if (clearButton != null) {
clearButton.setEnabled(false);
}
Property.ValueChangeListener buttonStateListener = event -> {
// Only enable clearing if user has changed something
if (clearButton != null) {
clearButton.setEnabled(true);
}
// Only enable save if the fieldgroup is valid
saveButton.setEnabled(fieldGroup.isValid());
};
fieldGroup.getFields().forEach(field -> {
AbstractField f = (AbstractField) field;
// Validate on the fly
f.setImmediate(true);
f.addValueChangeListener(buttonStateListener);
if (!f.getValidators().isEmpty()) {
// Don't tell the user they've done something wrong if they haven't even tried filling the form yet
f.setValidationVisible(false);
f.addValueChangeListener(new Property.ValueChangeListener() {
@Override
public void valueChange(Property.ValueChangeEvent event) {
f.setValidationVisible(true);
f.removeValueChangeListener(this);
}
});
}
});
}
use of com.vaadin.ui.AbstractField in project cia by Hack23.
the class FormFactoryImpl method addRequestInputFormFields.
@Override
public <T extends Serializable> void addRequestInputFormFields(final FormLayout panelContent, final T item, final Class<T> beanType, final List<String> displayProperties, final String buttonLabel, final ClickListener buttonListener) {
final BeanValidationBinder<T> binder = new BeanValidationBinder<>(beanType);
binder.setBean(item);
binder.setReadOnly(true);
for (final String property : displayProperties) {
final AbstractField buildAndBind;
if (property.contains(HIDDEN_FIELD_NAME)) {
buildAndBind = new PasswordField();
binder.bind(buildAndBind, property);
} else {
buildAndBind = new TextField();
binder.bind(buildAndBind, property);
}
buildAndBind.setCaption(property);
buildAndBind.setId(MessageFormat.format("{0}.{1}", buttonLabel, property));
buildAndBind.setReadOnly(false);
buildAndBind.setWidth(ContentSize.HALF_SIZE);
panelContent.addComponent(buildAndBind);
}
final VerticalLayout verticalLayout = new VerticalLayout();
verticalLayout.setWidth("50%");
final Button button = new Button(buttonLabel, new CommitFormWrapperClickListener(binder, buttonListener));
button.setId(buttonLabel);
button.setWidth("25%");
button.setIcon(VaadinIcons.BULLSEYE);
button.setEnabled(false);
binder.addStatusChangeListener(event -> {
button.setEnabled(event.getBinder().isValid());
});
verticalLayout.addComponent(button);
verticalLayout.setComponentAlignment(button, Alignment.MIDDLE_RIGHT);
panelContent.addComponent(verticalLayout);
}
Aggregations