Skip to content

Commit

Permalink
feat: move items by doubleclick
Browse files Browse the repository at this point in the history
Close #73
  • Loading branch information
javier-godoy authored and paodb committed Nov 9, 2022
1 parent 7d8d715 commit 7244955
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private static final class TwinColModel<T> implements Serializable {
HeaderRow headerRow;
boolean droppedInsideGrid = false;
boolean allowReordering = false;
Registration moveItemsByDoubleClick;

TwinColModel(@NonNull Grid<T> grid, String className) {
this.grid = grid;
Expand Down Expand Up @@ -1097,6 +1098,31 @@ public void setAutoResize(boolean autoResize) {
}
}

/**
* Sets whether a doubleclick event immediately moves an item to the other grid
*
* @param value if true, a a doubleclick event will immediately move an item to the other grid
*/
public void setMoveItemsByDoubleClick(boolean value) {
forEachSide(side -> {
if (value && side.moveItemsByDoubleClick == null) {
side.moveItemsByDoubleClick = side.grid.addItemDoubleClickListener(ev -> {
Set<T> item = Collections.singleton(ev.getItem());
if (side == available) {
updateSelection(item, Collections.emptySet(), true);
}
if (side == selection) {
updateSelection(Collections.emptySet(), item, true);
}
});
}
if (!value && side.moveItemsByDoubleClick != null) {
side.moveItemsByDoubleClick.remove();
side.moveItemsByDoubleClick = null;
}
});
}

@ClientCallable
private void updateOrientationOnResize(int width, int height) {
if (height > width) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*-
* #%L
* TwinColGrid add-on
* %%
* Copyright (C) 2017 - 2022 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.flowingcode.vaadin.addons.twincolgrid;

import com.flowingcode.vaadin.addons.demo.DemoSource;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@SuppressWarnings("serial")
@PageTitle("Double click")
@DemoSource
@Route(value = "twincolgrid/doubleclick", layout = TwincolDemoView.class)
public class DoubleClickDemo extends VerticalLayout {

private final Set<Book> selectedBooks = new HashSet<>();
private final List<Book> availableBooks = new ArrayList<>();

public DoubleClickDemo() {
initializeData();

final TwinColGrid<Book> twinColGrid =
new TwinColGrid<>(availableBooks, null)
.addSortableColumn(Book::getIsbn, Comparator.comparing(Book::getIsbn), "ISBN")
.addSortableColumn(Book::getTitle, Comparator.comparing(Book::getTitle), "Title")
.withAvailableGridCaption("Available books")
.withSelectionGridCaption("Added books")
.withSizeFull()
.selectRowOnClick();
twinColGrid.setValue(selectedBooks);
twinColGrid.setMoveItemsByDoubleClick(true);

add(new Span("Move items by double click"), twinColGrid);
setSizeFull();
}

private void initializeData() {
selectedBooks.add(new Book("1478375108", "Vaadin Recipes"));
selectedBooks.add(new Book("9789526800677", "Book of Vaadin: Volume 2 "));
availableBooks.add(new Book("1478375108", "Vaadin Recipes"));
availableBooks.add(new Book("9781849515221", "Learning Vaadin"));
availableBooks.add(
new Book("9781782162261", "Vaadin 7 UI Design By Example: Beginner\u2019s Guide"));
availableBooks.add(new Book("9781849518802", "Vaadin 7 Cookbook"));
availableBooks.add(new Book("9526800605", "Book of Vaadin: 7th Edition, 1st Revision"));
availableBooks.add(new Book("9789526800677", "Book of Vaadin: Volume 2 "));
availableBooks.add(new Book("9529267533", "Book of Vaadin"));
availableBooks.add(new Book("1782169776", "Learning Vaadin 7, Second Edition"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public TwincolDemoView() {
addDemo(FilterableDemo.class);
addDemo(BoundDemo.class);
addDemo(OrientationDemo.class);
addDemo(DoubleClickDemo.class);
setSizeFull();
}
}

0 comments on commit 7244955

Please sign in to comment.