Search in sources :

Example 1 with AbstractField

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);
                }
            });
        }
    });
}
Also used : Button(com.vaadin.ui.Button) AbstractField(com.vaadin.ui.AbstractField) Property(com.vaadin.data.Property) FieldGroup(com.vaadin.data.fieldgroup.FieldGroup) AbstractField(com.vaadin.ui.AbstractField) Property(com.vaadin.data.Property)

Example 2 with AbstractField

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);
}
Also used : AbstractField(com.vaadin.ui.AbstractField) Button(com.vaadin.ui.Button) TextField(com.vaadin.ui.TextField) VerticalLayout(com.vaadin.ui.VerticalLayout) BeanValidationBinder(com.vaadin.data.BeanValidationBinder) PasswordField(com.vaadin.ui.PasswordField) CommitFormWrapperClickListener(com.hack23.cia.web.impl.ui.application.views.pageclicklistener.CommitFormWrapperClickListener)

Aggregations

AbstractField (com.vaadin.ui.AbstractField)2 Button (com.vaadin.ui.Button)2 CommitFormWrapperClickListener (com.hack23.cia.web.impl.ui.application.views.pageclicklistener.CommitFormWrapperClickListener)1 BeanValidationBinder (com.vaadin.data.BeanValidationBinder)1 Property (com.vaadin.data.Property)1 FieldGroup (com.vaadin.data.fieldgroup.FieldGroup)1 PasswordField (com.vaadin.ui.PasswordField)1 TextField (com.vaadin.ui.TextField)1 VerticalLayout (com.vaadin.ui.VerticalLayout)1