Skip to content

Commit 6c3a5ec

Browse files
javier-godoypaodb
authored andcommitted
feat(demo): add toggle for the reordering feature
1 parent da2e630 commit 6c3a5ec

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/test/java/com/flowingcode/vaadin/addons/twincolgrid/DragAndDropDemo.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
package com.flowingcode.vaadin.addons.twincolgrid;
2222

2323
import com.flowingcode.vaadin.addons.demo.DemoSource;
24+
import com.vaadin.flow.component.checkbox.Checkbox;
25+
import com.vaadin.flow.component.html.Div;
2426
import com.vaadin.flow.component.html.Label;
27+
import com.vaadin.flow.component.html.Span;
2528
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
29+
import com.vaadin.flow.function.SerializableRunnable;
2630
import com.vaadin.flow.router.PageTitle;
2731
import java.util.ArrayList;
2832
import java.util.Comparator;
@@ -39,11 +43,12 @@ public class DragAndDropDemo extends VerticalLayout {
3943
private final Set<Book> selectedBooks = new HashSet<>();
4044
private final List<Book> availableBooks = new ArrayList<>();
4145

46+
private TwinColGrid<Book> twinColGrid;
47+
4248
public DragAndDropDemo() {
4349
initializeData();
4450

45-
final TwinColGrid<Book> twinColGrid =
46-
new TwinColGrid<>(availableBooks, "TwinColGrid demo with drag and drop support")
51+
twinColGrid = new TwinColGrid<>(availableBooks, "TwinColGrid demo with drag and drop support")
4752
.addSortableColumn(Book::getIsbn, Comparator.comparing(Book::getIsbn), "ISBN")
4853
.addSortableColumn(Book::getTitle, Comparator.comparing(Book::getTitle), "Title")
4954
.withAvailableGridCaption("Available books")
@@ -53,6 +58,7 @@ public DragAndDropDemo() {
5358
.withDragAndDropSupport()
5459
.withSelectionGridReordering()
5560
.selectRowOnClick();
61+
5662
twinColGrid.setValue(selectedBooks);
5763

5864
final Label countLabel = new Label("Selected items in left grid: 0");
@@ -61,6 +67,8 @@ public DragAndDropDemo() {
6167
twinColGrid.addValueChangeListener(e -> countLabel.setText("Selected items in left grid: 0"));
6268

6369
add(twinColGrid, countLabel);
70+
71+
addReorderingToggle();
6472
setSizeFull();
6573
}
6674

@@ -79,4 +87,24 @@ private void initializeData() {
7987
availableBooks.add(new Book("9529267533", "Book of Vaadin"));
8088
availableBooks.add(new Book("1782169776", "Learning Vaadin 7, Second Edition"));
8189
}
90+
91+
private void addReorderingToggle() {
92+
Checkbox checkbox = new Checkbox("Selection Grid reordering allowed", true);
93+
Span description = new Span("(Reordering is disabled while the grid is sorted)");
94+
description.setVisible(false);
95+
96+
SerializableRunnable refresh = () -> {
97+
boolean sorted = !twinColGrid.getSelectionGrid().getSortOrder().isEmpty();
98+
boolean allowed = twinColGrid.isSelectionGridReorderingAllowed();
99+
description.setVisible(sorted && allowed);
100+
};
101+
102+
checkbox.addValueChangeListener(ev -> {
103+
twinColGrid.setSelectionGridReorderingAllowed(ev.getValue());
104+
refresh.run();
105+
});
106+
107+
twinColGrid.getSelectionGrid().addSortListener(ev -> refresh.run());
108+
add(new Div(checkbox, description));
109+
}
82110
}

0 commit comments

Comments
 (0)