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