Search in sources :

Example 1 with Editor

use of com.vaadin.flow.component.grid.editor.Editor in project flow-components by vaadin.

the class GridViewEditorPage method createBufferedEditor.

private void createBufferedEditor() {
    Div message = new Div();
    message.setId("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);
    Editor<Person> editor = grid.getEditor();
    editor.setBinder(binder);
    editor.setBuffered(true);
    Div validationStatus = new Div();
    validationStatus.setId("validation");
    TextField field = new TextField();
    binder.forField(field).withValidator(name -> name.startsWith("Person"), "Name should start with Person").withStatusLabel(validationStatus).bind("firstName");
    nameColumn.setEditorComponent(field);
    Checkbox checkbox = new Checkbox();
    binder.bind(checkbox, "subscriber");
    subscriberColumn.setEditorComponent(checkbox);
    Collection<Button> editButtons = Collections.newSetFromMap(new WeakHashMap<>());
    Column<Person> editorColumn = grid.addComponentColumn(person -> {
        Button edit = new Button("Edit");
        edit.addClassName("edit");
        edit.addClickListener(e -> {
            editor.editItem(person);
            field.focus();
        });
        edit.setEnabled(!editor.isOpen());
        editButtons.add(edit);
        return edit;
    });
    editor.addOpenListener(e -> editButtons.stream().forEach(button -> button.setEnabled(!editor.isOpen())));
    editor.addCloseListener(e -> editButtons.stream().forEach(button -> button.setEnabled(!editor.isOpen())));
    Button save = new Button("Save", e -> editor.save());
    save.addClassName("save");
    Button cancel = new Button("Cancel", e -> editor.cancel());
    cancel.addClassName("cancel");
    // Add a keypress listener that listens for an escape key up event.
    // Note! some browsers return key as Escape and some as Esc
    grid.getElement().addEventListener("keyup", event -> editor.cancel()).setFilter("event.key === 'Escape' || event.key === 'Esc'");
    Div buttons = new Div(save, cancel);
    editorColumn.setEditorComponent(buttons);
    editor.addSaveListener(event -> message.setText(event.getItem().getFirstName() + ", " + event.getItem().isSubscriber()));
    grid.setId("buffered-editor");
    addCard("Grid Editor", "Editor in Buffered Mode", message, validationStatus, grid);
}
Also used : 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) Grid(com.vaadin.flow.component.grid.Grid) Div(com.vaadin.flow.component.html.Div) Binder(com.vaadin.flow.data.binder.Binder) Button(com.vaadin.flow.component.button.Button) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) TextField(com.vaadin.flow.component.textfield.TextField) Person(com.vaadin.flow.data.bean.Person)

Example 2 with Editor

use of com.vaadin.flow.component.grid.editor.Editor in project flow-components by vaadin.

the class GridViewEditorPage method createNotBufferedDynamicEditor.

private void createNotBufferedDynamicEditor() {
    Div message = new Div();
    message.setId("not-buffered-dynamic-editor-msg");
    Grid<Person> grid = new Grid<>();
    List<Person> persons = new ArrayList<>();
    persons.addAll(createItems());
    grid.setItems(persons);
    Column<Person> nameColumn = grid.addColumn(Person::getFirstName).setHeader("Name");
    Column<Person> subscriberColumn = grid.addColumn(Person::isSubscriber).setHeader("Subscriber");
    Column<Person> emailColumn = grid.addColumn(Person::getEmail).setHeader("E-mail");
    Binder<Person> binder = new Binder<>(Person.class);
    Editor<Person> editor = grid.getEditor();
    editor.setBinder(binder);
    TextField field = new TextField();
    // Close the editor in case of backward navigation 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 components
    checkbox.getElement().addEventListener("keydown", event -> {
        if (!checkbox.getValue()) {
            grid.getEditor().closeEditor();
        }
    }).setFilter("event.key === 'Tab' && !event.shiftKey");
    TextField emailField = new TextField();
    emailColumn.setEditorComponent(item -> {
        if (item.isSubscriber()) {
            binder.bind(emailField, "email");
            return emailField;
        } else {
            return null;
        }
    });
    // Close the editor in case of forward navigation between components
    emailField.getElement().addEventListener("keydown", event -> grid.getEditor().closeEditor()).setFilter("event.key === 'Tab' && !event.shiftKey");
    grid.addItemDoubleClickListener(event -> {
        grid.getEditor().editItem(event.getItem());
        field.focus();
    });
    // Re-validates the editors every time something changes on the Binder.
    // This is needed for the email column to turn into nothing when the
    // checkbox is deselected, for example.
    binder.addValueChangeListener(event -> {
        // Only updates from the client-side should be taken into account
        if (event.isFromClient()) {
            grid.getEditor().refresh();
        }
    });
    grid.addItemClickListener(event -> {
        if (binder.getBean() != null) {
            message.setText(binder.getBean().getFirstName() + ", " + binder.getBean().isSubscriber() + ", " + binder.getBean().getEmail());
        }
    });
    grid.setId("not-buffered-dynamic-editor");
    addCard("Grid Editor", "Dynamic Editor in Not Buffered Mode", message, grid);
}
Also used : 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) Grid(com.vaadin.flow.component.grid.Grid) ArrayList(java.util.ArrayList) Div(com.vaadin.flow.component.html.Div) Binder(com.vaadin.flow.data.binder.Binder) Checkbox(com.vaadin.flow.component.checkbox.Checkbox) TextField(com.vaadin.flow.component.textfield.TextField) Person(com.vaadin.flow.data.bean.Person)

Example 3 with Editor

use of com.vaadin.flow.component.grid.editor.Editor 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 4 with Editor

use of com.vaadin.flow.component.grid.editor.Editor in project furms by unity-idm.

the class SitesView method updateAction.

private void updateAction(Editor<SiteGridItem> siteEditor) {
    if (siteEditor.getBinder().isValid()) {
        Optional<Component> component = siteEditor.getGrid().getColumnByKey("name").getEditorComponent().getChildren().filter(c -> c instanceof TextField).findFirst();
        if (component.isPresent()) {
            TextField name = component.map(c -> (TextField) c).get();
            try {
                siteService.update(Site.builder().id(siteEditor.getItem().getId()).name(name.getValue()).build());
                siteEditor.cancel();
                refreshGrid(siteEditor);
                showSuccessNotification(getTranslation("view.sites.form.save.success"));
            } catch (DuplicatedNameValidationError e) {
                name.setErrorMessage(getTranslation("view.sites.form.error.validation.field.name.unique"));
                name.setInvalid(true);
            } catch (RuntimeException e) {
                LOG.warn("Could not update Site.", e);
                showErrorNotification(getTranslation("view.sites.form.error.unexpected", "update"));
            }
        }
    }
}
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) DuplicatedNameValidationError(io.imunity.furms.api.validation.exceptions.DuplicatedNameValidationError) TextField(com.vaadin.flow.component.textfield.TextField) Component(com.vaadin.flow.component.Component) FurmsViewComponent(io.imunity.furms.ui.components.FurmsViewComponent)

Example 5 with Editor

use of com.vaadin.flow.component.grid.editor.Editor in project karnak by OsiriX-Foundation.

the class ExternalIDGrid method setElements.

private void setElements() {
    extidColumn = addColumn(CachedPatient::getPseudonym).setHeader("External Pseudonym").setSortable(true);
    patientIdColumn = addColumn(CachedPatient::getPatientId).setHeader("Patient ID").setSortable(true);
    patientFirstNameColumn = addColumn(CachedPatient::getPatientFirstName).setHeader("Patient first name").setSortable(true);
    patientLastNameColumn = addColumn(CachedPatient::getPatientLastName).setHeader("Patient last name").setSortable(true);
    issuerOfPatientIDColumn = addColumn(CachedPatient::getIssuerOfPatientId).setHeader("Issuer of patient ID").setSortable(true);
    Grid.Column<CachedPatient> editorColumn = addComponentColumn(patient -> {
        Button edit = new Button("Edit");
        edit.addClassName("edit");
        edit.addClickListener(e -> {
            editor.editItem(patient);
            externalIdField.focus();
        });
        edit.setEnabled(!editor.isOpen());
        editButtons.add(edit);
        return edit;
    });
    addFilterElements();
    editButtons = Collections.newSetFromMap(new WeakHashMap<>());
    editor = getEditor();
    editor.setBinder(binder);
    editor.setBuffered(true);
    externalIdField = new TextField();
    patientIdField = new TextField();
    patientFirstNameField = new TextField();
    patientLastNameField = new TextField();
    issuerOfPatientIdField = new TextField();
    extidColumn.setEditorComponent(externalIdField);
    patientIdColumn.setEditorComponent(patientIdField);
    patientFirstNameColumn.setEditorComponent(patientFirstNameField);
    patientLastNameColumn.setEditorComponent(patientLastNameField);
    issuerOfPatientIDColumn.setEditorComponent(issuerOfPatientIdField);
    deleteColumn = addComponentColumn(patient -> {
        deletePatientButton = new Button("Delete");
        deletePatientButton.addThemeVariants(ButtonVariant.LUMO_ERROR, ButtonVariant.LUMO_PRIMARY);
        deletePatientButton.addClickListener(e -> {
            externalIDCache.remove(PatientClientUtil.generateKey(patient, projectEntity.getId()));
            readAllCacheValue();
        });
        return deletePatientButton;
    });
    saveEditPatientButton = new Button(LABEL_SAVE);
    cancelEditPatientButton = new Button(LABEL_CANCEL);
    Div buttons = new Div(saveEditPatientButton, cancelEditPatientButton);
    editorColumn.setEditorComponent(buttons);
}
Also used : CachedPatient(org.karnak.backend.cache.CachedPatient) Binder(com.vaadin.flow.data.binder.Binder) Div(com.vaadin.flow.component.html.Div) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) HeaderRow(com.vaadin.flow.component.grid.HeaderRow) AppConfig(org.karnak.backend.config.AppConfig) ProjectEntity(org.karnak.backend.data.entity.ProjectEntity) PaginatedGrid(org.vaadin.klaudeta.PaginatedGrid) TextField(com.vaadin.flow.component.textfield.TextField) ValueChangeMode(com.vaadin.flow.data.value.ValueChangeMode) WeakHashMap(java.util.WeakHashMap) PatientClient(org.karnak.backend.cache.PatientClient) Grid(com.vaadin.flow.component.grid.Grid) CachedPatient(org.karnak.backend.cache.CachedPatient) Iterator(java.util.Iterator) ButtonVariant(com.vaadin.flow.component.button.ButtonVariant) Editor(com.vaadin.flow.component.grid.editor.Editor) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) Button(com.vaadin.flow.component.button.Button) List(java.util.List) PseudonymPatient(org.karnak.backend.cache.PseudonymPatient) Collections(java.util.Collections) PatientClientUtil(org.karnak.backend.util.PatientClientUtil) Div(com.vaadin.flow.component.html.Div) Button(com.vaadin.flow.component.button.Button) PaginatedGrid(org.vaadin.klaudeta.PaginatedGrid) Grid(com.vaadin.flow.component.grid.Grid) TextField(com.vaadin.flow.component.textfield.TextField) WeakHashMap(java.util.WeakHashMap)

Aggregations

Button (com.vaadin.flow.component.button.Button)9 Grid (com.vaadin.flow.component.grid.Grid)9 Editor (com.vaadin.flow.component.grid.editor.Editor)9 Div (com.vaadin.flow.component.html.Div)9 TextField (com.vaadin.flow.component.textfield.TextField)9 Binder (com.vaadin.flow.data.binder.Binder)9 List (java.util.List)8 Route (com.vaadin.flow.router.Route)7 ArrayList (java.util.ArrayList)6 Collection (java.util.Collection)6 Collections (java.util.Collections)6 WeakHashMap (java.util.WeakHashMap)6 ClickEvent (com.vaadin.flow.component.ClickEvent)3 Component (com.vaadin.flow.component.Component)3 Key (com.vaadin.flow.component.Key)3 UI (com.vaadin.flow.component.UI)3 LUMO_TERTIARY (com.vaadin.flow.component.button.ButtonVariant.LUMO_TERTIARY)3 Checkbox (com.vaadin.flow.component.checkbox.Checkbox)3 END (com.vaadin.flow.component.grid.ColumnTextAlign.END)3 Column (com.vaadin.flow.component.grid.Grid.Column)3