Search in sources :

Example 31 with Binder

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

the class SitesView method addTable.

private void addTable() {
    FlexLayout tableLayout = new FlexLayout();
    tableLayout.setWidthFull();
    List<SiteGridItem> sites = fetchSites();
    DenseGrid<SiteGridItem> siteGrid = new DenseGrid<>(SiteGridItem.class);
    siteGrid.setItems(sites);
    Binder<SiteGridItem> siteBinder = new Binder<>(SiteGridItem.class);
    Editor<SiteGridItem> siteEditor = siteGrid.getEditor();
    siteEditor.setBinder(siteBinder);
    siteEditor.setBuffered(true);
    siteEditor.addOpenListener(event -> onEditorOpen(event, siteBinder));
    siteEditor.addCloseListener(event -> onEditorClose(siteBinder));
    siteGrid.addComponentColumn(site -> new RouterLink(site.getName(), SitesAdminsView.class, site.getId())).setHeader(getTranslation("view.sites.main.grid.column.name")).setKey("name").setSortable(true).setComparator(SiteGridItem::getName).setEditorComponent(addEditForm(siteEditor));
    siteGrid.addComponentColumn(site -> createLastColumnContent(site, siteGrid)).setHeader(getTranslation("view.sites.main.grid.column.actions")).setKey("actions").setEditorComponent(addEditButtons(siteEditor)).setTextAlign(END);
    tableLayout.add(siteGrid);
    getContent().add(tableLayout);
}
Also used : DuplicatedNameValidationError(io.imunity.furms.api.validation.exceptions.DuplicatedNameValidationError) Component(com.vaadin.flow.component.Component) LoggerFactory(org.slf4j.LoggerFactory) MenuButton(io.imunity.furms.ui.components.MenuButton) PageTitle(io.imunity.furms.ui.components.PageTitle) Route(com.vaadin.flow.router.Route) FlexLayout(com.vaadin.flow.component.orderedlayout.FlexLayout) DenseGrid(io.imunity.furms.ui.components.DenseGrid) Key(com.vaadin.flow.component.Key) UI(com.vaadin.flow.component.UI) TextField(com.vaadin.flow.component.textfield.TextField) USERS(com.vaadin.flow.component.icon.VaadinIcon.USERS) MethodHandles(java.lang.invoke.MethodHandles) Editor(com.vaadin.flow.component.grid.editor.Editor) RouterGridLink(io.imunity.furms.ui.components.RouterGridLink) END(com.vaadin.flow.component.grid.ColumnTextAlign.END) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) NotificationUtils.showSuccessNotification(io.imunity.furms.ui.utils.NotificationUtils.showSuccessNotification) ViewHeaderLayout(io.imunity.furms.ui.components.ViewHeaderLayout) EditorOpenEvent(com.vaadin.flow.component.grid.editor.EditorOpenEvent) NotificationUtils.showErrorNotification(io.imunity.furms.ui.utils.NotificationUtils.showErrorNotification) SiteHasResourceCreditsRemoveValidationError(io.imunity.furms.api.validation.exceptions.SiteHasResourceCreditsRemoveValidationError) Binder(com.vaadin.flow.data.binder.Binder) Div(com.vaadin.flow.component.html.Div) FurmsDialog(io.imunity.furms.ui.components.FurmsDialog) PLUS_CIRCLE(com.vaadin.flow.component.icon.VaadinIcon.PLUS_CIRCLE) LUMO_TERTIARY(com.vaadin.flow.component.button.ButtonVariant.LUMO_TERTIARY) FurmsViewComponent(io.imunity.furms.ui.components.FurmsViewComponent) RouterLink(com.vaadin.flow.router.RouterLink) SiteService(io.imunity.furms.api.sites.SiteService) Icon(com.vaadin.flow.component.icon.Icon) EAGER(com.vaadin.flow.data.value.ValueChangeMode.EAGER) Logger(org.slf4j.Logger) Grid(com.vaadin.flow.component.grid.Grid) TRASH(com.vaadin.flow.component.icon.VaadinIcon.TRASH) FENIX_ADMIN_SITES(io.imunity.furms.domain.constant.RoutesConst.FENIX_ADMIN_SITES) EDIT(com.vaadin.flow.component.icon.VaadinIcon.EDIT) GridActionMenu(io.imunity.furms.ui.components.GridActionMenu) GridActionsButtonLayout(io.imunity.furms.ui.components.GridActionsButtonLayout) ClickEvent(com.vaadin.flow.component.ClickEvent) Site(io.imunity.furms.domain.sites.Site) FenixAdminMenu(io.imunity.furms.ui.views.fenix.menu.FenixAdminMenu) Collectors.toList(java.util.stream.Collectors.toList) Button(com.vaadin.flow.component.button.Button) SitesAdminsView(io.imunity.furms.ui.views.fenix.sites.admins.SitesAdminsView) Comparator(java.util.Comparator) SitesAddView(io.imunity.furms.ui.views.fenix.sites.add.SitesAddView) NAME_MAX_LENGTH(io.imunity.furms.ui.utils.FormSettings.NAME_MAX_LENGTH) Binder(com.vaadin.flow.data.binder.Binder) RouterLink(com.vaadin.flow.router.RouterLink) FlexLayout(com.vaadin.flow.component.orderedlayout.FlexLayout) DenseGrid(io.imunity.furms.ui.components.DenseGrid)

Example 32 with Binder

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

the class SettingsView method addForm.

private void addForm(SiteSettingsDto dto) {
    FormLayout formLayout = new FurmsFormLayout();
    Binder<SiteSettingsDto> binder = new Binder<>(SiteSettingsDto.class);
    binder.setBean(dto);
    formLayout.add(furmsIdRow(binder.getBean().getId(), getTranslation("view.site-admin.settings.form.furms-id")));
    formLayout.addFormItem(externalIdRow(binder), getTranslation("view.site-admin.settings.form.external-id"));
    formLayout.addFormItem(nameRow(binder), getTranslation("view.site-admin.settings.form.name"));
    formLayout.addFormItem(oauthClientIdRow(binder), getTranslation("view.site-admin.settings.form.oauth-client"));
    formLayout.addFormItem(connectionInfoRow(binder), getTranslation("view.site-admin.settings.form.info"));
    formLayout.addFormItem(sshKeyFromMandatory(binder), "");
    formLayout.addFormItem(prohibitOldSSHKey(binder), "");
    formLayout.addFormItem(uploadRow(binder), getTranslation("view.site-admin.settings.form.logo"));
    formLayout.addFormItem(policyRow(binder), getTranslation("view.site-admin.settings.form.policy"));
    formLayout.add(buttonsRow(binder));
    getContent().add(formLayout);
}
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)

Example 33 with Binder

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

the class RequiredComboboxPage method requiredComboBoxSetItemsAfter.

private void requiredComboBoxSetItemsAfter() {
    Binder<TestItem> binder = new Binder<>();
    ComboBox<String> comboBox = new ComboBox<>();
    binder.forField(comboBox).asRequired().bind(TestItem::getName, TestItem::setName);
    binder.setBean(new TestItem(0));
    // Set items last:
    comboBox.setItems("foo", "bar");
    add(comboBox);
}
Also used : Binder(com.vaadin.flow.data.binder.Binder) ComboBox(com.vaadin.flow.component.combobox.ComboBox) TestItem(com.vaadin.flow.component.combobox.bean.TestItem)

Example 34 with Binder

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

the class FormLayoutView method createFormLayoutWithBinder.

private void createFormLayoutWithBinder() {
    FormLayout layoutWithBinder = new FormLayout();
    Binder<Contact> binder = new Binder<>();
    // The object that will be edited
    Contact contactBeingEdited = new Contact();
    // Create the fields
    TextField firstName = new TextField();
    firstName.setValueChangeMode(ValueChangeMode.EAGER);
    TextField lastName = new TextField();
    lastName.setValueChangeMode(ValueChangeMode.EAGER);
    TextField phone = new TextField();
    phone.setValueChangeMode(ValueChangeMode.EAGER);
    TextField email = new TextField();
    email.setValueChangeMode(ValueChangeMode.EAGER);
    DatePicker birthDate = new DatePicker();
    Checkbox doNotCall = new Checkbox("Do not call");
    Label infoLabel = new Label();
    NativeButton save = new NativeButton("Save");
    NativeButton reset = new NativeButton("Reset");
    layoutWithBinder.addFormItem(firstName, "First name");
    layoutWithBinder.addFormItem(lastName, "Last name");
    layoutWithBinder.addFormItem(birthDate, "Birthdate");
    layoutWithBinder.addFormItem(email, "E-mail");
    FormItem phoneItem = layoutWithBinder.addFormItem(phone, "Phone");
    phoneItem.add(doNotCall);
    // Button bar
    HorizontalLayout actions = new HorizontalLayout();
    actions.add(save, reset);
    save.getStyle().set("marginRight", "10px");
    SerializablePredicate<String> phoneOrEmailPredicate = value -> !phone.getValue().trim().isEmpty() || !email.getValue().trim().isEmpty();
    // E-mail and phone have specific validators
    Binding<Contact, String> emailBinding = binder.forField(email).withValidator(phoneOrEmailPredicate, "Both phone and email cannot be empty").withValidator(new EmailValidator("Incorrect email address")).bind(Contact::getEmail, Contact::setEmail);
    Binding<Contact, String> phoneBinding = binder.forField(phone).withValidator(phoneOrEmailPredicate, "Both phone and email cannot be empty").bind(Contact::getPhone, Contact::setPhone);
    // Trigger cross-field validation when the other field is changed
    email.addValueChangeListener(event -> phoneBinding.validate());
    phone.addValueChangeListener(event -> emailBinding.validate());
    // First name and last name are required fields
    firstName.setRequiredIndicatorVisible(true);
    lastName.setRequiredIndicatorVisible(true);
    binder.forField(firstName).withValidator(new StringLengthValidator("Please add the first name", 1, null)).bind(Contact::getFirstName, Contact::setFirstName);
    binder.forField(lastName).withValidator(new StringLengthValidator("Please add the last name", 1, null)).bind(Contact::getLastName, Contact::setLastName);
    // Birthdate and doNotCall don't need any special validators
    binder.bind(doNotCall, Contact::isDoNotCall, Contact::setDoNotCall);
    binder.bind(birthDate, Contact::getBirthDate, Contact::setBirthDate);
    // Click listeners for the buttons
    save.addClickListener(event -> {
        if (binder.writeBeanIfValid(contactBeingEdited)) {
            infoLabel.setText("Saved bean values: " + contactBeingEdited);
        } else {
            BinderValidationStatus<Contact> validate = binder.validate();
            String errorText = validate.getFieldValidationStatuses().stream().filter(BindingValidationStatus::isError).map(BindingValidationStatus::getMessage).map(Optional::get).distinct().collect(Collectors.joining(", "));
            infoLabel.setText("There are errors: " + errorText);
        }
    });
    reset.addClickListener(event -> {
        // clear fields by setting null
        binder.readBean(null);
        infoLabel.setText("");
        doNotCall.setValue(false);
    });
    infoLabel.setId("binder-info");
    firstName.setId("binder-first-name");
    lastName.setId("binder-last-name");
    phone.setId("binder-phone");
    email.setId("binder-email");
    birthDate.setId("binder-birth-date");
    doNotCall.setId("binder-do-not-call");
    save.setId("binder-save");
    reset.setId("binder-reset");
    addCard("A form layout with fields using Binder", layoutWithBinder, actions, infoLabel);
}
Also used : FormLayout(com.vaadin.flow.component.formlayout.FormLayout) BinderValidationStatus(com.vaadin.flow.data.binder.BinderValidationStatus) Component(com.vaadin.flow.component.Component) Composite(com.vaadin.flow.component.Composite) EmailValidator(com.vaadin.flow.data.validator.EmailValidator) Binder(com.vaadin.flow.data.binder.Binder) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) NativeButton(com.vaadin.flow.component.html.NativeButton) ResponsiveStep(com.vaadin.flow.component.formlayout.FormLayout.ResponsiveStep) FormItem(com.vaadin.flow.component.formlayout.FormLayout.FormItem) Route(com.vaadin.flow.router.Route) BindingValidationStatus(com.vaadin.flow.data.binder.BindingValidationStatus) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) TextField(com.vaadin.flow.component.textfield.TextField) ValueChangeMode(com.vaadin.flow.data.value.ValueChangeMode) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) H2(com.vaadin.flow.component.html.H2) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) FormLayout(com.vaadin.flow.component.formlayout.FormLayout) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) DatePicker(com.vaadin.flow.component.datepicker.DatePicker) LocalDate(java.time.LocalDate) Optional(java.util.Optional) Binding(com.vaadin.flow.data.binder.Binder.Binding) NativeButton(com.vaadin.flow.component.html.NativeButton) EmailValidator(com.vaadin.flow.data.validator.EmailValidator) Optional(java.util.Optional) FormItem(com.vaadin.flow.component.formlayout.FormLayout.FormItem) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) Label(com.vaadin.flow.component.html.Label) BindingValidationStatus(com.vaadin.flow.data.binder.BindingValidationStatus) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Binder(com.vaadin.flow.data.binder.Binder) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) TextField(com.vaadin.flow.component.textfield.TextField) DatePicker(com.vaadin.flow.component.datepicker.DatePicker)

Example 35 with Binder

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

the class MainView method createRichTextEditorWithHtmlBinder.

private void createRichTextEditorWithHtmlBinder() {
    RichTextEditor rte = new RichTextEditor();
    rte.setId("html-rte");
    add(rte);
    Div valuePanel = new Div();
    valuePanel.setId("html-binder-value-panel");
    Div infoPanel = new Div();
    Binder<HtmlEntry> binder = new Binder<>();
    // The object that will be edited
    HtmlEntry entryBeingEdited = new HtmlEntry();
    // Create the action buttons
    Button save = new Button("Save");
    Button reset = new Button("Reset");
    Button setBeanHtmlValue = new Button("Set bean html value");
    Button getValueButton = new Button("Get value");
    getValueButton.setId("get-html-binder-rte-value");
    getValueButton.addClickListener(event -> {
        String value = rte.asHtml().getValue();
        String webcomponentValue = rte.getElement().getProperty("htmlValue");
        valuePanel.setText(value + ' ' + webcomponentValue);
    });
    // Button bar
    HorizontalLayout actions = new HorizontalLayout();
    actions.add(save, reset, getValueButton, setBeanHtmlValue);
    save.getStyle().set("marginRight", "10px");
    SerializablePredicate<String> htmlValuePredicate = value -> {
        String htmlValue = rte.asHtml().getValue();
        return htmlValue != null && !htmlValue.trim().isEmpty();
    };
    Binding<HtmlEntry, String> asHtmlValueBinding = binder.forField(rte.asHtml()).withValidator(htmlValuePredicate, "html value should contain something").bind(HtmlEntry::getHtmlValue, HtmlEntry::setHtmlValue);
    // Editor is a required field
    rte.asHtml().setRequiredIndicatorVisible(true);
    // Click listeners for the buttons
    save.addClickListener(event -> {
        if (binder.writeBeanIfValid(entryBeingEdited)) {
            infoPanel.setText("Saved bean values: " + entryBeingEdited);
        } else {
            BinderValidationStatus<HtmlEntry> 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("");
    });
    setBeanHtmlValue.addClickListener(event -> {
        entryBeingEdited.setHtmlValue("<p><b>Foo</b></p>");
        binder.readBean(entryBeingEdited);
    });
    infoPanel.setId("html-binder-info");
    save.setId("html-binder-save");
    setBeanHtmlValue.setId("html-binder-set-bean-value");
    reset.setId("html-binder-reset");
    add(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)

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