use of com.vaadin.flow.component.grid.editor.Editor in project karnak by OsiriX-Foundation.
the class GridSwitchingAlbums method setEditorColumn.
private void setEditorColumn() {
editor = getEditor();
editor.setBinder(binder);
editor.setBuffered(true);
Column<KheopsAlbumsEntity> editorColumn = addComponentColumn(kheopsAlbums -> {
Button edit = new Button("Edit");
edit.addClickListener(e -> editor.editItem(kheopsAlbums));
edit.setEnabled(!editor.isOpen());
Button remove = new Button("Remove");
remove.addThemeVariants(ButtonVariant.LUMO_ERROR, ButtonVariant.LUMO_PRIMARY);
remove.addClickListener(e -> {
dataProvider.getItems().remove(kheopsAlbums);
dataProvider.refreshAll();
});
remove.setEnabled(!editor.isOpen());
editButtons.add(edit);
editButtons.add(remove);
return new Div(edit, remove);
}).setFlexGrow(15);
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("Validate");
save.addClickListener(event -> {
// Get the current edited Kheops album
KheopsAlbumsEntity currentEditedKheopsAlbumsEntity = new KheopsAlbumsEntity();
if (binder.writeBeanIfValid(currentEditedKheopsAlbumsEntity)) {
// Save only if not already existing in table
if (!dataProvider.getItems().contains(currentEditedKheopsAlbumsEntity)) {
editor.save();
setItems(dataProvider);
} else {
// Show a notification
Span content = new Span("Already existing");
content.getStyle().set("color", "var(--lumo-error-text-color)");
Notification notification = new Notification(content);
notification.setDuration(3000);
notification.setPosition(Position.BOTTOM_END);
notification.open();
}
}
});
save.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
Button cancel = new Button("Cancel", e -> editor.cancel());
Div buttons = new Div(save, cancel);
editorColumn.setEditorComponent(buttons);
}
use of com.vaadin.flow.component.grid.editor.Editor in project furms by unity-idm.
the class SitesView method addEditForm.
private Component addEditForm(Editor<SiteGridItem> siteEditor) {
TextField siteNameField = new TextField();
siteNameField.setMaxLength(NAME_MAX_LENGTH);
siteNameField.setWidthFull();
siteNameField.setValueChangeMode(EAGER);
siteEditor.getBinder().forField(siteNameField).withValidator(getNotEmptyStringValidator(), getTranslation("view.sites.form.error.validation.field.name.required")).withValidator(siteName -> !siteService.isNamePresentIgnoringRecord(siteName, siteEditor.getItem().getId()), getTranslation("view.sites.form.error.validation.field.name.unique")).bind(SiteGridItem::getName, SiteGridItem::setName);
return new Div(siteNameField);
}
use of com.vaadin.flow.component.grid.editor.Editor 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);
}
use of com.vaadin.flow.component.grid.editor.Editor in project flow-components by vaadin.
the class GridViewEditorPage method createBufferedDynamicEditor.
private void createBufferedDynamicEditor() {
Div message = new Div();
message.setId("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);
editor.setBuffered(true);
TextField field = new TextField();
binder.bind(field, "firstName");
nameColumn.setEditorComponent(field);
Div validationStatus = new Div();
validationStatus.getStyle().set("color", "red");
validationStatus.setId("email-validation");
Checkbox checkbox = new Checkbox();
binder.bind(checkbox, "subscriber");
subscriberColumn.setEditorComponent(checkbox);
TextField emailField = new TextField();
// When not a subscriber, we want to show a read-only text-field that
// ignores whatever is set to it
TextField readOnlyEmail = new TextField();
readOnlyEmail.setValue("Not a subscriber");
readOnlyEmail.setReadOnly(true);
Runnable bindEmail = () -> binder.forField(emailField).withValidator(new EmailValidator("Invalid email")).withStatusLabel(validationStatus).bind("email");
Runnable setEmail = () -> emailColumn.setEditorComponent(item -> {
if (item.isSubscriber()) {
bindEmail.run();
return emailField;
} else {
return readOnlyEmail;
}
});
// Sets the binding based on the Person bean state
setEmail.run();
// Refresh subscriber editor component when checkbox value is changed
checkbox.addValueChangeListener(event -> {
// Only updates from the client-side should be taken into account
if (event.isFromClient()) {
// When using buffered mode, the partial updates shouldn't be
// propagated to the bean before the Save button is clicked, so
// here we need to override the binding function to take the
// checkbox state into consideration instead
emailColumn.setEditorComponent(item -> {
if (checkbox.getValue()) {
bindEmail.run();
return emailField;
} else {
return readOnlyEmail;
}
});
grid.getEditor().refresh();
}
});
Collection<Button> editButtons = Collections.newSetFromMap(new WeakHashMap<>());
// Resets the binding function to use the bean state whenever the editor
// is closed
editor.addCloseListener(event -> {
setEmail.run();
editButtons.stream().forEach(button -> button.setEnabled(true));
});
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() + ", " + event.getItem().getEmail()));
grid.setId("buffered-dynamic-editor");
addCard("Grid Editor", "Dynamic Editor in Buffered Mode", message, validationStatus, grid);
}
Aggregations