Search in sources :

Example 6 with Binder

use of com.vaadin.flow.data.binder.Binder in project flow-components by vaadin.

the class GridViewEditorPage method createNotBufferedEditor.

private void createNotBufferedEditor() {
    Div message = new Div();
    message.setId("not-buffered-editor-msg");
    Grid<Person> grid = new Grid<>();
    List<Person> persons = getItems();
    grid.setItems(persons);
    Column<Person> nameColumn = grid.addColumn(Person::getFirstName).setHeader("Name");
    Column<Person> subscriberColumn = grid.addColumn(Person::isSubscriber).setHeader("Subscriber");
    Binder<Person> binder = new Binder<>(Person.class);
    grid.getEditor().setBinder(binder);
    TextField field = new TextField();
    // Close the editor in case of backward between components
    field.getElement().addEventListener("keydown", event -> grid.getEditor().closeEditor()).setFilter("event.key === 'Tab' && event.shiftKey");
    binder.bind(field, "firstName");
    nameColumn.setEditorComponent(field);
    Checkbox checkbox = new Checkbox();
    binder.bind(checkbox, "subscriber");
    subscriberColumn.setEditorComponent(checkbox);
    // Close the editor in case of forward navigation between
    checkbox.getElement().addEventListener("keydown", event -> grid.getEditor().closeEditor()).setFilter("event.key === 'Tab' && !event.shiftKey");
    grid.addItemDoubleClickListener(event -> {
        grid.getEditor().editItem(event.getItem());
        field.focus();
    });
    grid.addItemClickListener(event -> {
        if (binder.getBean() != null) {
            message.setText(binder.getBean().getFirstName() + ", " + binder.getBean().isSubscriber());
        }
    });
    grid.setId("not-buffered-editor");
    addCard("Grid Editor", "Editor in Not Buffered Mode", message, grid);
}
Also used : Div(com.vaadin.flow.component.html.Div) Grid(com.vaadin.flow.component.grid.Grid) EmailValidator(com.vaadin.flow.data.validator.EmailValidator) Collection(java.util.Collection) Editor(com.vaadin.flow.component.grid.editor.Editor) Binder(com.vaadin.flow.data.binder.Binder) Div(com.vaadin.flow.component.html.Div) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) List(java.util.List) Button(com.vaadin.flow.component.button.Button) Column(com.vaadin.flow.component.grid.Grid.Column) Person(com.vaadin.flow.data.bean.Person) Collections(java.util.Collections) TextField(com.vaadin.flow.component.textfield.TextField) WeakHashMap(java.util.WeakHashMap) Binder(com.vaadin.flow.data.binder.Binder) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) Grid(com.vaadin.flow.component.grid.Grid) TextField(com.vaadin.flow.component.textfield.TextField) Person(com.vaadin.flow.data.bean.Person)

Example 7 with Binder

use of com.vaadin.flow.data.binder.Binder in project flow-components by vaadin.

the class MainView method createRichTextEditorWithBinder.

private void createRichTextEditorWithBinder() {
    RichTextEditor rteWithBinder = new RichTextEditor();
    Div valuePanel = new Div();
    valuePanel.setId("binder-value-panel");
    Div infoPanel = new Div();
    Binder<Entry> binder = new Binder<>();
    // The object that will be edited
    Entry entryBeingEdited = new Entry();
    rteWithBinder.setValueChangeMode(ValueChangeMode.EAGER);
    // Create the action buttons
    Button save = new Button("Save");
    Button reset = new Button("Reset");
    Button getValueButton = new Button("Get value");
    getValueButton.setId("get-binder-rte-value");
    getValueButton.addClickListener(event -> {
        String value = rteWithBinder.getValue();
        valuePanel.setText(value);
    });
    // Button bar
    HorizontalLayout actions = new HorizontalLayout();
    actions.add(save, reset, getValueButton);
    save.getStyle().set("marginRight", "10px");
    SerializablePredicate<String> deltaValuePredicate = value -> !rteWithBinder.getValue().trim().isEmpty();
    Binding<Entry, String> deltaValueBinding = binder.forField(rteWithBinder).withValidator(deltaValuePredicate, "Delta value should contain something").bind(Entry::getDeltaValue, Entry::setDeltaValue);
    // Editor is a required field
    rteWithBinder.setRequiredIndicatorVisible(true);
    // Click listeners for the buttons
    save.addClickListener(event -> {
        if (binder.writeBeanIfValid(entryBeingEdited)) {
            infoPanel.setText("Saved bean values: " + entryBeingEdited);
        } else {
            BinderValidationStatus<Entry> validate = binder.validate();
            String errorText = validate.getFieldValidationStatuses().stream().filter(BindingValidationStatus::isError).map(BindingValidationStatus::getMessage).map(Optional::get).distinct().collect(Collectors.joining(", "));
            infoPanel.setText("There are errors: " + errorText);
        }
    });
    reset.addClickListener(event -> {
        // clear fields by setting null
        binder.readBean(null);
        infoPanel.setText("");
    });
    infoPanel.setId("binder-info");
    rteWithBinder.setId("binder-delta-value");
    save.setId("binder-save");
    reset.setId("binder-reset");
    add(rteWithBinder, actions, infoPanel, valuePanel);
}
Also used : BinderValidationStatus(com.vaadin.flow.data.binder.BinderValidationStatus) Binder(com.vaadin.flow.data.binder.Binder) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Div(com.vaadin.flow.component.html.Div) Collectors(java.util.stream.Collectors) Route(com.vaadin.flow.router.Route) Serializable(java.io.Serializable) Button(com.vaadin.flow.component.button.Button) RichTextEditor(com.vaadin.flow.component.richtexteditor.RichTextEditor) BindingValidationStatus(com.vaadin.flow.data.binder.BindingValidationStatus) Optional(java.util.Optional) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) Binding(com.vaadin.flow.data.binder.Binder.Binding) ValueChangeMode(com.vaadin.flow.data.value.ValueChangeMode) Optional(java.util.Optional) BindingValidationStatus(com.vaadin.flow.data.binder.BindingValidationStatus) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Div(com.vaadin.flow.component.html.Div) Binder(com.vaadin.flow.data.binder.Binder) Button(com.vaadin.flow.component.button.Button) RichTextEditor(com.vaadin.flow.component.richtexteditor.RichTextEditor)

Example 8 with Binder

use of com.vaadin.flow.data.binder.Binder in project DoodleVerse by davidemarcoli.

the class CitizenManagementView method createEditor.

/**
 * Create the CRUD Editor
 * @return the CrudEditor
 */
private CrudEditor<Person> createEditor() {
    TextField firstName = new TextField("First name");
    TextField lastName = new TextField("Last name");
    NumberField money = new NumberField("Money");
    money.setReadOnly(true);
    Double randomMoneyAmount = rand.nextDouble(100000 + 1 - 10000) + 10000;
    money.setValue(randomMoneyAmount);
    FormLayout form = new FormLayout(firstName, lastName, money);
    form.setColspan(money, 2);
    form.setMaxWidth("480px");
    form.setResponsiveSteps(new FormLayout.ResponsiveStep("0", 1), new FormLayout.ResponsiveStep("30em", 2));
    Binder<Person> binder = new Binder<>(Person.class);
    binder.forField(firstName).asRequired().bind(Person::getFirstName, Person::setFirstName);
    binder.forField(lastName).asRequired().bind(Person::getLastName, Person::setLastName);
    binder.forField(money).asRequired().bind(Person::getMoney, Person::setMoney).setReadOnly(true);
    return new BinderCrudEditor<>(binder, form);
}
Also used : FormLayout(com.vaadin.flow.component.formlayout.FormLayout) Binder(com.vaadin.flow.data.binder.Binder) TextField(com.vaadin.flow.component.textfield.TextField) BinderCrudEditor(com.vaadin.flow.component.crud.BinderCrudEditor) Person(com.dala.data.person.Person) NumberField(com.vaadin.flow.component.textfield.NumberField)

Example 9 with Binder

use of com.vaadin.flow.data.binder.Binder in project furms by unity-idm.

the class SitesAddView method addForm.

private void addForm() {
    FormLayout formLayout = new FurmsFormLayout();
    formLayout.setSizeFull();
    SiteCreationParam formData = new SiteCreationParam();
    Binder<SiteCreationParam> binder = new Binder<>(SiteCreationParam.class);
    binder.setBean(formData);
    name.setPlaceholder(getTranslation("view.sites.add.form.name.placeholder"));
    name.setRequiredIndicatorVisible(true);
    name.setValueChangeMode(EAGER);
    name.setMaxLength(NAME_MAX_LENGTH);
    Button cancel = new Button(getTranslation("view.sites.add.form.button.cancel"), e -> doCancelAction());
    cancel.addThemeVariants(LUMO_TERTIARY);
    Button save = new Button(getTranslation("view.sites.add.form.button.save"), e -> doSaveAction(formData, binder));
    save.addThemeVariants(LUMO_PRIMARY);
    save.addClickShortcut(Key.ENTER);
    FormButtons buttons = new FormButtons(cancel, save);
    binder.addStatusChangeListener(status -> save.setEnabled(!isBlank(name.getValue()) && !status.hasValidationErrors()));
    binder.forField(name).withValidator(getNotEmptyStringValidator(), getTranslation("view.sites.form.error.validation.field.name.required")).withValidator(siteService::isNamePresent, getTranslation("view.sites.form.error.validation.field.name.unique")).bind(SiteCreationParam::getName, SiteCreationParam::setName);
    formLayout.addFormItem(name, getTranslation("view.sites.add.form.name"));
    getContent().add(formLayout, buttons);
}
Also used : FurmsFormLayout(io.imunity.furms.ui.components.FurmsFormLayout) FormLayout(com.vaadin.flow.component.formlayout.FormLayout) Binder(com.vaadin.flow.data.binder.Binder) FurmsFormLayout(io.imunity.furms.ui.components.FurmsFormLayout) Button(com.vaadin.flow.component.button.Button) FormButtons(io.imunity.furms.ui.components.FormButtons)

Example 10 with Binder

use of com.vaadin.flow.data.binder.Binder in project ArchCNL by Mari-Wie.

the class ArchitecturalStyleForm method addKnownGroupsToUi.

/**
 * Adds known groups of architecture information to the UI.
 */
private void addKnownGroupsToUi() {
    // initialize Map
    if (architectureInformationGroupsAndEntries == null) {
        architectureInformationGroupsAndEntries = new HashMap<Integer, Set<TwoColumnGridEntry>>();
    }
    // groups
    for (Entry<Integer, List<ArchitectureInformation>> entry : knownArchitectureInformationGroups.entrySet()) {
        Integer key = entry.getKey();
        // list of ArchitectureInformation belonging to the same group
        List<ArchitectureInformation> information = entry.getValue();
        // if needed UI Components to group more ArchitectureInformation need to be created
        if (information.size() == 2) {
            Binder<TwoColumnGridEntry> gridEntriesBinder = new Binder<>(TwoColumnGridEntry.class);
            Set<TwoColumnGridEntry> gridItems = new HashSet<TwoColumnGridEntry>();
            TwoColumnGridAndInputTextFieldsComponent comp = new TwoColumnGridAndInputTextFieldsComponent(// list index starts at 0
            information.get(0).getName(), information.get(1).getName(), gridItems, gridEntriesBinder);
            // this collection is used to save the grid entries as nested binding with
            // Vaadin did not work out
            // TODO Check for better solution with nested binding & vaadin instead
            architectureInformationGroupsAndEntries.put(key, gridItems);
            add(comp);
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TwoColumnGridEntry(org.archcnl.ui.common.TwoColumnGridEntry) TwoColumnGridAndInputTextFieldsComponent(org.archcnl.ui.common.TwoColumnGridAndInputTextFieldsComponent) Binder(com.vaadin.flow.data.binder.Binder) ArchitectureInformation(org.archcnl.domain.input.model.presets.ArchitectureInformation) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

Binder (com.vaadin.flow.data.binder.Binder)36 TextField (com.vaadin.flow.component.textfield.TextField)29 FormLayout (com.vaadin.flow.component.formlayout.FormLayout)21 BinderCrudEditor (com.vaadin.flow.component.crud.BinderCrudEditor)15 Person (com.vaadin.demo.domain.Person)12 Div (com.vaadin.flow.component.html.Div)11 Button (com.vaadin.flow.component.button.Button)10 EmailField (com.vaadin.flow.component.textfield.EmailField)9 Route (com.vaadin.flow.router.Route)8 List (java.util.List)8 Grid (com.vaadin.flow.component.grid.Grid)7 Editor (com.vaadin.flow.component.grid.editor.Editor)7 ArrayList (java.util.ArrayList)7 Checkbox (com.vaadin.flow.component.checkbox.Checkbox)6 Collection (java.util.Collection)6 Collections (java.util.Collections)6 WeakHashMap (java.util.WeakHashMap)6 RichTextEditor (com.vaadin.flow.component.richtexteditor.RichTextEditor)5 EmailValidator (com.vaadin.flow.data.validator.EmailValidator)5 StringLengthValidator (com.vaadin.flow.data.validator.StringLengthValidator)5