Search in sources :

Example 1 with MultiSelectListBox

use of com.vaadin.flow.component.listbox.MultiSelectListBox in project flow-components by vaadin.

the class ListBoxDataViewPage method createIdentifierProviderForMultiSelectListBox.

private void createIdentifierProviderForMultiSelectListBox() {
    CustomItem first = new CustomItem(1L, "First");
    CustomItem second = new CustomItem(2L, "Second");
    CustomItem third = new CustomItem(3L, "Third");
    CustomItem fourth = new CustomItem(4L, "Fourth");
    List<CustomItem> items = new ArrayList<>(Arrays.asList(first, second, third, fourth));
    MultiSelectListBox<CustomItem> multiSelectListBox = new MultiSelectListBox<>();
    ListBoxListDataView<CustomItem> listDataView = multiSelectListBox.setItems(items);
    // Setting the following Identifier Provider makes the component
    // independent from the CustomItem's equals method implementation:
    listDataView.setIdentifierProvider(CustomItem::getId);
    Set<CustomItem> selected = new HashSet<>(Arrays.asList(new CustomItem(1L), third));
    multiSelectListBox.setValue(selected);
    Span selectedIdsSpan = new Span();
    selectedIdsSpan.setId(MULTI_SELECT_LIST_BOX_SELECTED_IDS_SPAN);
    multiSelectListBox.getSelectedItems().stream().map(item -> String.valueOf(item.getId())).sorted().reduce((a, b) -> a + ", " + b).ifPresent(selectedIdsSpan::setText);
    Button updateAndSelectByIdButton = new Button("Update & Select by Id", click -> {
        // Make the names of unselected items similar to the name of
        // selected
        // one to mess with the <equals> implementation in
        // CustomItem:
        second.setName("First");
        listDataView.refreshItem(second);
        fourth.setName("Third");
        listDataView.refreshItem(fourth);
        // Select the items not only with the reference of existing
        // items,
        // but also the Id to verify <equals> is not in use and the
        // selection is happening only based on identifier:
        Set<CustomItem> newSelected = new HashSet<>(Arrays.asList(second, new CustomItem(4L)));
        multiSelectListBox.setValue(newSelected);
        multiSelectListBox.getSelectedItems().stream().map(item -> String.valueOf(item.getId())).sorted().reduce((a, b) -> a + ", " + b).ifPresent(selectedIdsSpan::setText);
    });
    updateAndSelectByIdButton.setId(MULTI_SELECT_LIST_BOX_SELECTION_UPDATE_BUTTON);
    Button selectByIdAndNameButton = new Button("Select by Id and Name", click -> {
        // Select the items not only with the reference of existing
        // items,
        // but also the Id and a challenging name to verify <equals>
        // is not
        // in use and the selection is happening only based on
        // identifier:
        Set<CustomItem> newSelected = new HashSet<>(Arrays.asList(first, new CustomItem(3L, "Third")));
        multiSelectListBox.setValue(newSelected);
        multiSelectListBox.getSelectedItems().stream().map(item -> String.valueOf(item.getId())).sorted().reduce((a, b) -> a + ", " + b).ifPresent(selectedIdsSpan::setText);
    });
    selectByIdAndNameButton.setId(MULTI_SELECT_LIST_BOX_SELECTION_BY_ID_AND_NAME_BUTTON);
    add(multiSelectListBox, selectedIdsSpan, updateAndSelectByIdButton, selectByIdAndNameButton);
}
Also used : ListBoxListDataView(com.vaadin.flow.component.listbox.dataview.ListBoxListDataView) ListDataProvider(com.vaadin.flow.data.provider.ListDataProvider) Arrays(java.util.Arrays) Query(com.vaadin.flow.data.provider.Query) MultiSelectListBox(com.vaadin.flow.component.listbox.MultiSelectListBox) AbstractDataProvider(com.vaadin.flow.data.provider.AbstractDataProvider) Set(java.util.Set) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) ListBox(com.vaadin.flow.component.listbox.ListBox) AtomicReference(java.util.concurrent.atomic.AtomicReference) ListBoxDataView(com.vaadin.flow.component.listbox.dataview.ListBoxDataView) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) Button(com.vaadin.flow.component.button.Button) Stream(java.util.stream.Stream) DataProvider(com.vaadin.flow.data.provider.DataProvider) Span(com.vaadin.flow.component.html.Span) NativeButton(com.vaadin.flow.component.html.NativeButton) Button(com.vaadin.flow.component.button.Button) ArrayList(java.util.ArrayList) Span(com.vaadin.flow.component.html.Span) MultiSelectListBox(com.vaadin.flow.component.listbox.MultiSelectListBox) HashSet(java.util.HashSet)

Aggregations

Button (com.vaadin.flow.component.button.Button)1 Div (com.vaadin.flow.component.html.Div)1 NativeButton (com.vaadin.flow.component.html.NativeButton)1 Span (com.vaadin.flow.component.html.Span)1 ListBox (com.vaadin.flow.component.listbox.ListBox)1 MultiSelectListBox (com.vaadin.flow.component.listbox.MultiSelectListBox)1 ListBoxDataView (com.vaadin.flow.component.listbox.dataview.ListBoxDataView)1 ListBoxListDataView (com.vaadin.flow.component.listbox.dataview.ListBoxListDataView)1 AbstractDataProvider (com.vaadin.flow.data.provider.AbstractDataProvider)1 DataProvider (com.vaadin.flow.data.provider.DataProvider)1 ListDataProvider (com.vaadin.flow.data.provider.ListDataProvider)1 Query (com.vaadin.flow.data.provider.Query)1 Route (com.vaadin.flow.router.Route)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1