2121package com .flowingcode .vaadin .addons .twincolgrid ;
2222
2323import com .flowingcode .vaadin .addons .demo .DemoSource ;
24+ import com .vaadin .flow .component .checkbox .Checkbox ;
25+ import com .vaadin .flow .component .html .Div ;
2426import com .vaadin .flow .component .html .Label ;
27+ import com .vaadin .flow .component .html .Span ;
2528import com .vaadin .flow .component .orderedlayout .VerticalLayout ;
29+ import com .vaadin .flow .function .SerializableRunnable ;
2630import com .vaadin .flow .router .PageTitle ;
2731import java .util .ArrayList ;
2832import 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