Skip to content

Commit 7244955

Browse files
javier-godoypaodb
authored andcommitted
feat: move items by doubleclick
Close #73
1 parent 7d8d715 commit 7244955

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

src/main/java/com/flowingcode/vaadin/addons/twincolgrid/TwinColGrid.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ private static final class TwinColModel<T> implements Serializable {
8787
HeaderRow headerRow;
8888
boolean droppedInsideGrid = false;
8989
boolean allowReordering = false;
90+
Registration moveItemsByDoubleClick;
9091

9192
TwinColModel(@NonNull Grid<T> grid, String className) {
9293
this.grid = grid;
@@ -1097,6 +1098,31 @@ public void setAutoResize(boolean autoResize) {
10971098
}
10981099
}
10991100

1101+
/**
1102+
* Sets whether a doubleclick event immediately moves an item to the other grid
1103+
*
1104+
* @param value if true, a a doubleclick event will immediately move an item to the other grid
1105+
*/
1106+
public void setMoveItemsByDoubleClick(boolean value) {
1107+
forEachSide(side -> {
1108+
if (value && side.moveItemsByDoubleClick == null) {
1109+
side.moveItemsByDoubleClick = side.grid.addItemDoubleClickListener(ev -> {
1110+
Set<T> item = Collections.singleton(ev.getItem());
1111+
if (side == available) {
1112+
updateSelection(item, Collections.emptySet(), true);
1113+
}
1114+
if (side == selection) {
1115+
updateSelection(Collections.emptySet(), item, true);
1116+
}
1117+
});
1118+
}
1119+
if (!value && side.moveItemsByDoubleClick != null) {
1120+
side.moveItemsByDoubleClick.remove();
1121+
side.moveItemsByDoubleClick = null;
1122+
}
1123+
});
1124+
}
1125+
11001126
@ClientCallable
11011127
private void updateOrientationOnResize(int width, int height) {
11021128
if (height > width) {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*-
2+
* #%L
3+
* TwinColGrid add-on
4+
* %%
5+
* Copyright (C) 2017 - 2022 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.addons.twincolgrid;
21+
22+
import com.flowingcode.vaadin.addons.demo.DemoSource;
23+
import com.vaadin.flow.component.html.Span;
24+
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
25+
import com.vaadin.flow.router.PageTitle;
26+
import com.vaadin.flow.router.Route;
27+
import java.util.ArrayList;
28+
import java.util.Comparator;
29+
import java.util.HashSet;
30+
import java.util.List;
31+
import java.util.Set;
32+
33+
@SuppressWarnings("serial")
34+
@PageTitle("Double click")
35+
@DemoSource
36+
@Route(value = "twincolgrid/doubleclick", layout = TwincolDemoView.class)
37+
public class DoubleClickDemo extends VerticalLayout {
38+
39+
private final Set<Book> selectedBooks = new HashSet<>();
40+
private final List<Book> availableBooks = new ArrayList<>();
41+
42+
public DoubleClickDemo() {
43+
initializeData();
44+
45+
final TwinColGrid<Book> twinColGrid =
46+
new TwinColGrid<>(availableBooks, null)
47+
.addSortableColumn(Book::getIsbn, Comparator.comparing(Book::getIsbn), "ISBN")
48+
.addSortableColumn(Book::getTitle, Comparator.comparing(Book::getTitle), "Title")
49+
.withAvailableGridCaption("Available books")
50+
.withSelectionGridCaption("Added books")
51+
.withSizeFull()
52+
.selectRowOnClick();
53+
twinColGrid.setValue(selectedBooks);
54+
twinColGrid.setMoveItemsByDoubleClick(true);
55+
56+
add(new Span("Move items by double click"), twinColGrid);
57+
setSizeFull();
58+
}
59+
60+
private void initializeData() {
61+
selectedBooks.add(new Book("1478375108", "Vaadin Recipes"));
62+
selectedBooks.add(new Book("9789526800677", "Book of Vaadin: Volume 2 "));
63+
availableBooks.add(new Book("1478375108", "Vaadin Recipes"));
64+
availableBooks.add(new Book("9781849515221", "Learning Vaadin"));
65+
availableBooks.add(
66+
new Book("9781782162261", "Vaadin 7 UI Design By Example: Beginner\u2019s Guide"));
67+
availableBooks.add(new Book("9781849518802", "Vaadin 7 Cookbook"));
68+
availableBooks.add(new Book("9526800605", "Book of Vaadin: 7th Edition, 1st Revision"));
69+
availableBooks.add(new Book("9789526800677", "Book of Vaadin: Volume 2 "));
70+
availableBooks.add(new Book("9529267533", "Book of Vaadin"));
71+
availableBooks.add(new Book("1782169776", "Learning Vaadin 7, Second Edition"));
72+
}
73+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public TwincolDemoView() {
3737
addDemo(FilterableDemo.class);
3838
addDemo(BoundDemo.class);
3939
addDemo(OrientationDemo.class);
40+
addDemo(DoubleClickDemo.class);
4041
setSizeFull();
4142
}
4243
}

0 commit comments

Comments
 (0)