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);
}
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);
}
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);
}
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);
}
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);
}
}
}
Aggregations