From b224e4408f2b25c6079ad3af6f7b58fcbb6464a9 Mon Sep 17 00:00:00 2001 From: Sergey Vinogradov Date: Thu, 19 Dec 2024 15:47:47 +0400 Subject: [PATCH] polish --- .../demo/component/grid/GridRangeSelection.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/vaadin/demo/component/grid/GridRangeSelection.java b/src/main/java/com/vaadin/demo/component/grid/GridRangeSelection.java index 69d87ec36f..cfb316a525 100644 --- a/src/main/java/com/vaadin/demo/component/grid/GridRangeSelection.java +++ b/src/main/java/com/vaadin/demo/component/grid/GridRangeSelection.java @@ -33,30 +33,32 @@ public GridRangeSelection() { .addClientItemToggleListener(event -> { Person item = event.getItem(); + // If the anchor point isn't set, set it to the current item startItem = startItem != null ? startItem : item; + if (event.isShiftKey()) { + // Calculcate the range of items between the anchor point and + // the current item int startIndex = grid.getListDataView().getItemIndex(startItem).get(); int endIndex = grid.getListDataView().getItemIndex(endItem).get(); Set rangeItems = grid.getListDataView().getItems().skip(Math.min(startIndex, endIndex)).limit(Math.abs(startIndex - endIndex) + 1).collect(Collectors.toSet()); + + // Update the selection state of the items within the range + // based on the state of the current item if (event.isSelected()) { grid.asMultiSelect().select(rangeItems); } else { grid.asMultiSelect().deselect(rangeItems); } } + + // Update the anchor point to the current item startItem = item; }); // end::snippet[] - add(grid); } - // tag::snippet2[] - private Set fetchItemsRange(Grid grid, T startItem, T endItem) { - - } - // end::snippet2[] - public static class Exporter // hidden-source-line extends DemoExporter { // hidden-source-line } // hidden-source-line