Search in sources :

Example 1 with Binder

use of com.vaadin.flow.data.binder.Binder in project karnak by OsiriX-Foundation.

the class TextFieldsBindSwitchingAlbum method buildBinder.

private Binder<KheopsAlbumsEntity> buildBinder() {
    Binder<KheopsAlbumsEntity> b = new BeanValidationBinder<>(KheopsAlbumsEntity.class);
    b.forField(textAuthorizationDestination).withValidator(StringUtils::isNotBlank, "Token destination is mandatory").withValidator(value -> {
        if (!textUrlAPI.getValue().isBlank()) {
            return validateToken(value, textUrlAPI.getValue(), SwitchingAlbum.MIN_SCOPE_DESTINATION);
        }
        return true;
    }, "Token can't be validate, minimum permissions: [write]").bind(KheopsAlbumsEntity::getAuthorizationDestination, KheopsAlbumsEntity::setAuthorizationDestination);
    b.forField(textAuthorizationSource).withValidator(StringUtils::isNotBlank, "Token source is mandatory").withValidator(value -> {
        if (!textUrlAPI.getValue().isBlank()) {
            return validateToken(value, textUrlAPI.getValue(), SwitchingAlbum.MIN_SCOPE_SOURCE);
        }
        return true;
    }, "Token can't be validate, minimum permissions: [read, send]").bind(KheopsAlbumsEntity::getAuthorizationSource, KheopsAlbumsEntity::setAuthorizationSource);
    b.forField(textUrlAPI).withValidator(StringUtils::isNotBlank, "Url API is mandatory").bind(KheopsAlbumsEntity::getUrlAPI, KheopsAlbumsEntity::setUrlAPI);
    b.forField(textCondition).withValidator(value -> {
        if (!textCondition.getValue().equals("")) {
            expressionError = ExpressionResult.isValid(textCondition.getValue(), new ExprCondition(), Boolean.class);
            textErrorConditionMsg.setText(expressionError.getMsg());
            return expressionError.isValid();
        }
        textErrorConditionMsg.setText("");
        return true;
    }, "Condition is not valid").bind(KheopsAlbumsEntity::getCondition, KheopsAlbumsEntity::setCondition);
    return b;
}
Also used : KheopsAlbumsEntity(org.karnak.backend.data.entity.KheopsAlbumsEntity) Binder(com.vaadin.flow.data.binder.Binder) StringUtils(org.apache.commons.lang3.StringUtils) List(java.util.List) JSONObject(org.json.JSONObject) ExpressionError(org.karnak.backend.model.expression.ExpressionError) BeanValidationBinder(com.vaadin.flow.data.binder.BeanValidationBinder) KheopsApi(org.karnak.backend.api.KheopsApi) TextField(com.vaadin.flow.component.textfield.TextField) ExpressionResult(org.karnak.backend.model.expression.ExpressionResult) Span(com.vaadin.flow.component.html.Span) SwitchingAlbum(org.karnak.backend.service.kheops.SwitchingAlbum) ExprCondition(org.karnak.backend.model.expression.ExprCondition) ExprCondition(org.karnak.backend.model.expression.ExprCondition) StringUtils(org.apache.commons.lang3.StringUtils) BeanValidationBinder(com.vaadin.flow.data.binder.BeanValidationBinder) KheopsAlbumsEntity(org.karnak.backend.data.entity.KheopsAlbumsEntity)

Example 2 with Binder

use of com.vaadin.flow.data.binder.Binder in project karnak by OsiriX-Foundation.

the class DestinationCondition method setBinder.

private void setBinder(Binder<DestinationEntity> binder) {
    binder.forField(condition).withValidator(value -> {
        if (!condition.getValue().equals("")) {
            ExpressionError expressionError = ExpressionResult.isValid(condition.getValue(), new ExprCondition(), Boolean.class);
            textErrorConditionMsg.setText(expressionError.getMsg());
            return expressionError.isValid();
        }
        return true;
    }, "Condition not valid").withValidationStatusHandler(status -> {
        if (!status.isError()) {
            textErrorConditionMsg.setText("");
        }
    }).bind(DestinationEntity::getCondition, DestinationEntity::setCondition);
}
Also used : Binder(com.vaadin.flow.data.binder.Binder) ExpressionError(org.karnak.backend.model.expression.ExpressionError) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) Div(com.vaadin.flow.component.html.Div) DestinationEntity(org.karnak.backend.data.entity.DestinationEntity) UIS(org.karnak.frontend.util.UIS) TextField(com.vaadin.flow.component.textfield.TextField) ExpressionResult(org.karnak.backend.model.expression.ExpressionResult) Span(com.vaadin.flow.component.html.Span) ExprCondition(org.karnak.backend.model.expression.ExprCondition) DestinationEntity(org.karnak.backend.data.entity.DestinationEntity) ExprCondition(org.karnak.backend.model.expression.ExprCondition) ExpressionError(org.karnak.backend.model.expression.ExpressionError)

Example 3 with Binder

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

the class Helper method createPersonEditor.

static CrudEditor<Person> createPersonEditor() {
    TextField firstName = new TextField("First name");
    firstName.getElement().setAttribute("editor-role", "first-name");
    TextField lastName = new TextField("Last name");
    lastName.getElement().setAttribute("editor-role", "last-name");
    FormLayout form = new FormLayout(firstName, lastName);
    Binder<Person> binder = new Binder<>(Person.class);
    binder.forField(firstName).asRequired().bind(Person::getFirstName, Person::setFirstName);
    binder.forField(lastName).withValidator(value -> value != null && value.startsWith("O"), "Only last names starting with 'O' allowed").bind(Person::getLastName, Person::setLastName);
    return new BinderCrudEditor<>(binder, form);
}
Also used : FormLayout(com.vaadin.flow.component.formlayout.FormLayout) FormLayout(com.vaadin.flow.component.formlayout.FormLayout) BinderCrudEditor(com.vaadin.flow.component.crud.BinderCrudEditor) CrudI18n(com.vaadin.flow.component.crud.CrudI18n) Binder(com.vaadin.flow.data.binder.Binder) TextField(com.vaadin.flow.component.textfield.TextField) CrudEditor(com.vaadin.flow.component.crud.CrudEditor) Binder(com.vaadin.flow.data.binder.Binder) TextField(com.vaadin.flow.component.textfield.TextField) BinderCrudEditor(com.vaadin.flow.component.crud.BinderCrudEditor)

Example 4 with Binder

use of com.vaadin.flow.data.binder.Binder 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 5 with Binder

use of com.vaadin.flow.data.binder.Binder 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)

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