Search in sources :

Example 1 with GridMultiSelectionModel

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

the class GridSelectionTest method multiSelectionListeners.

@Test
public void multiSelectionListeners() {
    Grid<String> grid = new Grid<>();
    grid.setSelectionMode(SelectionMode.MULTI);
    Set<String> oldSelection = new LinkedHashSet<>();
    Set<String> selection = new LinkedHashSet<>(Arrays.asList("0", "1", "2"));
    AtomicInteger selectionListenerCalled = new AtomicInteger();
    grid.addSelectionListener(event -> {
        selectionListenerCalled.incrementAndGet();
        Assert.assertEquals(selection, event.getAllSelectedItems());
    });
    AtomicInteger valueChangeListenerCalled = new AtomicInteger();
    grid.asMultiSelect().addValueChangeListener(event -> {
        valueChangeListenerCalled.incrementAndGet();
        Assert.assertEquals(oldSelection, event.getOldValue());
        Assert.assertEquals(selection, event.getValue());
    });
    AtomicInteger multiSelectionListenerCalled = new AtomicInteger();
    ((GridMultiSelectionModel<String>) grid.getSelectionModel()).addMultiSelectionListener(event -> {
        multiSelectionListenerCalled.incrementAndGet();
        Assert.assertEquals(oldSelection, event.getOldSelection());
        Assert.assertEquals(selection, event.getNewSelection());
        Set<String> oldCopy = new LinkedHashSet<>(oldSelection);
        Set<String> copy = new LinkedHashSet<>(selection);
        oldCopy.removeAll(copy);
        Assert.assertEquals(oldCopy, event.getRemovedSelection());
        oldCopy = new LinkedHashSet<>(oldSelection);
        copy.removeAll(oldCopy);
        Assert.assertEquals(copy, event.getAddedSelection());
    });
    grid.asMultiSelect().setValue(selection);
    oldSelection.addAll(selection);
    selection.clear();
    selection.addAll(Arrays.asList("10", "1", "5"));
    grid.asMultiSelect().setValue(selection);
    Assert.assertEquals(2, selectionListenerCalled.get());
    Assert.assertEquals(2, valueChangeListenerCalled.get());
    Assert.assertEquals(2, multiSelectionListenerCalled.get());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GridMultiSelectionModel(com.vaadin.flow.component.grid.GridMultiSelectionModel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Grid(com.vaadin.flow.component.grid.Grid) Test(org.junit.Test)

Example 2 with GridMultiSelectionModel

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

the class GridViewConfiguringColumnsPage method createColumnApiExample.

private void createColumnApiExample() {
    Grid<Person> grid = new Grid<>();
    GridSelectionModel<Person> selectionMode = grid.setSelectionMode(SelectionMode.MULTI);
    grid.setItems(getItems());
    Column<Person> idColumn = grid.addColumn(Person::getId).setHeader("ID").setFlexGrow(0).setWidth("75px");
    grid.addColumn(Person::getFirstName).setHeader("Name").setResizable(true);
    // Setting a column-key allows fetching the column later
    grid.addColumn(Person::getAge).setHeader("Age").setKey("age");
    grid.getColumnByKey("age").setResizable(true);
    NativeButton idColumnVisibility = new NativeButton("Toggle visibility of the ID column");
    idColumnVisibility.addClickListener(event -> idColumn.setVisible(!idColumn.isVisible()));
    NativeButton userReordering = new NativeButton("Toggle user reordering of columns");
    userReordering.addClickListener(event -> grid.setColumnReorderingAllowed(!grid.isColumnReorderingAllowed()));
    NativeButton freezeIdColumn = new NativeButton("Toggle frozen state of ID column");
    freezeIdColumn.addClickListener(event -> idColumn.setFrozen(!idColumn.isFrozen()));
    NativeButton freezeSelectionColumn = new NativeButton("Toggle frozen state of selection column");
    GridMultiSelectionModel<?> multiSlection = (GridMultiSelectionModel<?>) selectionMode;
    freezeSelectionColumn.addClickListener(event -> multiSlection.setSelectionColumnFrozen(!multiSlection.isSelectionColumnFrozen()));
    RadioButtonGroup<ColumnTextAlign> alignments = new RadioButtonGroup<>();
    alignments.setItems(ColumnTextAlign.values());
    alignments.setLabel("Text alignment for the Age column");
    alignments.setValue(ColumnTextAlign.START);
    alignments.addValueChangeListener(event -> grid.getColumnByKey("age").setTextAlign(event.getValue()));
    grid.setId("column-api-example");
    idColumnVisibility.setId("toggle-id-column-visibility");
    userReordering.setId("toggle-user-reordering");
    freezeIdColumn.setId("toggle-id-column-frozen");
    freezeSelectionColumn.setId("toggle-selection-column-frozen");
    alignments.setId("toggle-text-align");
    addCard("Configuring columns", "Column API example", grid, new VerticalLayout(idColumnVisibility, userReordering, freezeIdColumn, freezeSelectionColumn, alignments));
}
Also used : NativeButton(com.vaadin.flow.component.html.NativeButton) GridMultiSelectionModel(com.vaadin.flow.component.grid.GridMultiSelectionModel) Grid(com.vaadin.flow.component.grid.Grid) RadioButtonGroup(com.vaadin.flow.component.radiobutton.RadioButtonGroup) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Person(com.vaadin.flow.data.bean.Person) ColumnTextAlign(com.vaadin.flow.component.grid.ColumnTextAlign)

Example 3 with GridMultiSelectionModel

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

the class TreeGridBasicFeaturesPage method createSelectionModeMenu.

@SuppressWarnings("rawtypes")
private void createSelectionModeMenu() {
    LinkedHashMap<String, SelectionMode> options = new LinkedHashMap<>();
    options.put("none", SelectionMode.NONE);
    options.put("single", SelectionMode.SINGLE);
    options.put("multi", SelectionMode.MULTI);
    options.entrySet().forEach(entry -> {
        addAction("Selection mode - " + entry.getKey(), () -> {
            grid.setSelectionMode(entry.getValue());
            if (entry.getValue() == SelectionMode.MULTI) {
                GridSelectionModel model = grid.getSelectionModel();
                if (model instanceof GridMultiSelectionModel) {
                    ((GridMultiSelectionModel) model).setSelectAllCheckboxVisibility(SelectAllCheckboxVisibility.VISIBLE);
                }
            }
        });
    });
}
Also used : GridMultiSelectionModel(com.vaadin.flow.component.grid.GridMultiSelectionModel) GridSelectionModel(com.vaadin.flow.component.grid.GridSelectionModel) SelectionMode(com.vaadin.flow.component.grid.Grid.SelectionMode) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

GridMultiSelectionModel (com.vaadin.flow.component.grid.GridMultiSelectionModel)3 Grid (com.vaadin.flow.component.grid.Grid)2 ColumnTextAlign (com.vaadin.flow.component.grid.ColumnTextAlign)1 SelectionMode (com.vaadin.flow.component.grid.Grid.SelectionMode)1 GridSelectionModel (com.vaadin.flow.component.grid.GridSelectionModel)1 NativeButton (com.vaadin.flow.component.html.NativeButton)1 VerticalLayout (com.vaadin.flow.component.orderedlayout.VerticalLayout)1 RadioButtonGroup (com.vaadin.flow.component.radiobutton.RadioButtonGroup)1 Person (com.vaadin.flow.data.bean.Person)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1