diff options
author | Pekka Hyvönen <pekka@vaadin.com> | 2017-11-15 14:37:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-15 14:37:33 +0200 |
commit | 6e01211931df3c294f8e3c077849c5d433c1d35f (patch) | |
tree | 4d2006267d7b2bde598d588947e52d95bca8bf31 /uitest/src | |
parent | 3344de2f9aa238d90205a0b8ed11b0853e8b85c4 (diff) | |
download | vaadin-framework-6e01211931df3c294f8e3c077849c5d433c1d35f.tar.gz vaadin-framework-6e01211931df3c294f8e3c077849c5d433c1d35f.zip |
Add helper for Grid row DnD (#10255)
* Added Griddragger.
* Refactored the GridDragger to match FW style
Unit tests, Test UI and documentation to come next.
* Change DropMode to BETWEEN and add Test UIs
* Simplify GridDragger API
* Fixes and unit tests for GridDragger
Fixed issues regarding drop index calculation when sourche and target grid is the same.
When the ListDataProvider is not internally using List, always drops to the end.
Updates the old data provider instead of creating a new one to preserve filters&sorting.
Diffstat (limited to 'uitest/src')
4 files changed, 222 insertions, 95 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/AbstractGridDnD.java b/uitest/src/main/java/com/vaadin/tests/components/grid/AbstractGridDnD.java new file mode 100644 index 0000000000..1e1c790898 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/AbstractGridDnD.java @@ -0,0 +1,120 @@ +package com.vaadin.tests.components.grid; + +import java.util.Arrays; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.vaadin.shared.ui.grid.DropMode; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.tests.util.Person; +import com.vaadin.tests.util.TestDataGenerator; +import com.vaadin.ui.CheckBox; +import com.vaadin.ui.Grid; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Layout; +import com.vaadin.ui.RadioButtonGroup; +import com.vaadin.ui.components.grid.GridDragSource; +import com.vaadin.ui.components.grid.GridDragger; +import com.vaadin.ui.components.grid.GridDropTarget; + +public abstract class AbstractGridDnD extends AbstractTestUIWithLog { + + protected final Layout controls = new HorizontalLayout(); + + protected void initializeTestFor(GridDragger<Person> gridDragger) { + initializeTestFor(gridDragger.getGridDragSource().getGrid(), + gridDragger.getGridDropTarget().getGrid(), + gridDragger.getGridDragSource(), + gridDragger.getGridDropTarget()); + } + + protected void initializeTestFor(Grid<Person> source, Grid<Person> target, + GridDragSource<Person> dragSource, + GridDropTarget<Person> dropTarget) { + // Layout the two grids + Layout layout = new HorizontalLayout(); + + layout.addComponent(source); + layout.addComponent(target); // noop if source == target + layout.setWidth("100%"); + + // Selection modes + List<Grid.SelectionMode> selectionModes = Arrays + .asList(Grid.SelectionMode.SINGLE, Grid.SelectionMode.MULTI); + RadioButtonGroup<Grid.SelectionMode> selectionModeSelect = new RadioButtonGroup<>( + "Selection mode", selectionModes); + selectionModeSelect.setSelectedItem(Grid.SelectionMode.SINGLE); + selectionModeSelect.addValueChangeListener( + event -> source.setSelectionMode(event.getValue())); + + // Drop locations + List<DropMode> dropLocations = Arrays.asList(DropMode.values()); + RadioButtonGroup<DropMode> dropLocationSelect = new RadioButtonGroup<>( + "Allowed drop location", dropLocations); + dropLocationSelect.setSelectedItem(DropMode.BETWEEN); + dropLocationSelect.addValueChangeListener( + event -> dropTarget.setDropMode(event.getValue())); + + CheckBox transitionCheckBox = new CheckBox("Transition layout", false); + transitionCheckBox.addValueChangeListener(event -> { + if (event.getValue()) { + layout.addStyleName("transitioned"); + } else { + layout.removeStyleName("transitioned"); + } + }); + CheckBox dropOnSortedGridRows = new CheckBox("Drop on Sorted Grid Rows", + dropTarget.isDropAllowedOnSortedGridRows()); + dropOnSortedGridRows.addValueChangeListener(event -> { + dropTarget.setDropAllowedOnSortedGridRows(event.getValue()); + }); + + RadioButtonGroup<Integer> frozenColumnSelect = new RadioButtonGroup<>( + "Frozen columns", Arrays.asList(new Integer[] { -1, 0, 1 })); + frozenColumnSelect.setValue(source.getFrozenColumnCount()); + frozenColumnSelect.addValueChangeListener(event -> { + source.setFrozenColumnCount(event.getValue()); + target.setFrozenColumnCount(event.getValue()); + }); + + controls.addComponents(selectionModeSelect, dropLocationSelect, + transitionCheckBox, dropOnSortedGridRows, frozenColumnSelect); + + addComponents(controls, layout); + + getPage().getStyles() + .add(".transitioned { transform: translate(-30px, 30px);}"); + } + + protected Grid<Person> createGridAndFillWithData(int numberOfItems) { + Grid<Person> grid = new Grid<>(); + grid.setWidth("100%"); + + grid.setItems(generateItems(numberOfItems)); + grid.addColumn( + person -> person.getFirstName() + " " + person.getLastName()) + .setCaption("Name"); + grid.addColumn(person -> person.getAddress().getStreetAddress()) + .setCaption("Street Address"); + grid.addColumn(person -> person.getAddress().getCity()) + .setCaption("City"); + + return grid; + } + + private List<Person> generateItems(int num) { + return Stream.generate(() -> generateRandomPerson(new Random())) + .limit(num).collect(Collectors.toList()); + } + + private Person generateRandomPerson(Random r) { + return new Person(TestDataGenerator.getFirstName(r), + TestDataGenerator.getLastName(r), "foo@bar.com", + TestDataGenerator.getPhoneNumber(r), + TestDataGenerator.getStreetAddress(r), + TestDataGenerator.getPostalCode(r), + TestDataGenerator.getCity(r)); + } +} diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridDragAndDrop.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridDragAndDrop.java index 209bb77a69..b6e2dc2af7 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/GridDragAndDrop.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridDragAndDrop.java @@ -15,12 +15,8 @@ */ package com.vaadin.tests.components.grid; -import java.util.Arrays; import java.util.List; -import java.util.Random; -import java.util.Set; import java.util.stream.Collectors; -import java.util.stream.Stream; import com.vaadin.annotations.Theme; import com.vaadin.annotations.Widgetset; @@ -30,22 +26,16 @@ import com.vaadin.shared.ui.dnd.DropEffect; import com.vaadin.shared.ui.dnd.EffectAllowed; import com.vaadin.shared.ui.grid.DropLocation; import com.vaadin.shared.ui.grid.DropMode; -import com.vaadin.tests.components.AbstractTestUIWithLog; import com.vaadin.tests.util.Person; -import com.vaadin.tests.util.TestDataGenerator; -import com.vaadin.ui.CheckBox; import com.vaadin.ui.Grid; -import com.vaadin.ui.HorizontalLayout; -import com.vaadin.ui.Layout; -import com.vaadin.ui.RadioButtonGroup; import com.vaadin.ui.components.grid.GridDragSource; import com.vaadin.ui.components.grid.GridDropTarget; @Theme("valo") @Widgetset("com.vaadin.DefaultWidgetSet") -public class GridDragAndDrop extends AbstractTestUIWithLog { +public class GridDragAndDrop extends AbstractGridDnD { - private Set<Person> draggedItems; + private List<Person> draggedItems; @Override protected void setup(VaadinRequest request) { @@ -59,76 +49,7 @@ public class GridDragAndDrop extends AbstractTestUIWithLog { Grid<Person> right = createGridAndFillWithData(0); GridDropTarget<Person> dropTarget = applyDropTarget(right); - // Layout the two grids - Layout grids = new HorizontalLayout(); - - grids.addComponents(left, right); - grids.setWidth("100%"); - - // Selection modes - List<Grid.SelectionMode> selectionModes = Arrays - .asList(Grid.SelectionMode.SINGLE, Grid.SelectionMode.MULTI); - RadioButtonGroup<Grid.SelectionMode> selectionModeSelect = new RadioButtonGroup<>( - "Selection mode", selectionModes); - selectionModeSelect.setSelectedItem(Grid.SelectionMode.SINGLE); - selectionModeSelect.addValueChangeListener( - event -> left.setSelectionMode(event.getValue())); - - // Drop locations - List<DropMode> dropLocations = Arrays.asList(DropMode.values()); - RadioButtonGroup<DropMode> dropLocationSelect = new RadioButtonGroup<>( - "Allowed drop location", dropLocations); - dropLocationSelect.setSelectedItem(DropMode.BETWEEN); - dropLocationSelect.addValueChangeListener( - event -> dropTarget.setDropMode(event.getValue())); - - CheckBox transitionCheckBox = new CheckBox("Transition layout", false); - transitionCheckBox.addValueChangeListener(event -> { - if (event.getValue()) { - grids.addStyleName("transitioned"); - } else { - grids.removeStyleName("transitioned"); - } - }); - - CheckBox dropOnSortedGridRows = new CheckBox("Drop on Sorted Grid Rows", - dropTarget.isDropAllowedOnSortedGridRows()); - dropOnSortedGridRows.addValueChangeListener(event -> { - dropTarget.setDropAllowedOnSortedGridRows(event.getValue()); - }); - - RadioButtonGroup<Integer> frozenColumnSelect = new RadioButtonGroup<>( - "Frozen columns", Arrays.asList(new Integer[] { -1, 0, 1 })); - frozenColumnSelect.setValue(left.getFrozenColumnCount()); - frozenColumnSelect.addValueChangeListener(event -> { - left.setFrozenColumnCount(event.getValue()); - right.setFrozenColumnCount(event.getValue()); - }); - - Layout controls = new HorizontalLayout(selectionModeSelect, - dropLocationSelect, transitionCheckBox, frozenColumnSelect, - dropOnSortedGridRows); - - addComponents(controls, grids); - - getPage().getStyles() - .add(".transitioned { transform: translate(-30px, 30px);}"); - } - - private Grid<Person> createGridAndFillWithData(int numberOfItems) { - Grid<Person> grid = new Grid<>(); - grid.setWidth("100%"); - - grid.setItems(generateItems(numberOfItems)); - grid.addColumn( - person -> person.getFirstName() + " " + person.getLastName()) - .setCaption("Name"); - grid.addColumn(person -> person.getAddress().getStreetAddress()) - .setCaption("Street Address"); - grid.addColumn(person -> person.getAddress().getCity()) - .setCaption("City"); - - return grid; + initializeTestFor(left, right, dragSource, dropTarget); } private GridDragSource<Person> applyDragSource(Grid<Person> grid) { @@ -220,17 +141,4 @@ public class GridDragAndDrop extends AbstractTestUIWithLog { return dropTarget; } - private List<Person> generateItems(int num) { - return Stream.generate(() -> generateRandomPerson(new Random())) - .limit(num).collect(Collectors.toList()); - } - - private Person generateRandomPerson(Random r) { - return new Person(TestDataGenerator.getFirstName(r), - TestDataGenerator.getLastName(r), "foo@bar.com", - TestDataGenerator.getPhoneNumber(r), - TestDataGenerator.getStreetAddress(r), - TestDataGenerator.getPostalCode(r), - TestDataGenerator.getCity(r)); - } } diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridDraggerOneGrid.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridDraggerOneGrid.java new file mode 100644 index 0000000000..752d0d07d3 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridDraggerOneGrid.java @@ -0,0 +1,40 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * 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. + */ +package com.vaadin.tests.components.grid; + +import com.vaadin.annotations.Theme; +import com.vaadin.annotations.Widgetset; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.util.Person; +import com.vaadin.ui.Grid; +import com.vaadin.ui.components.grid.GridDragger; + +@Theme("valo") +@Widgetset("com.vaadin.DefaultWidgetSet") +public class GridDraggerOneGrid extends AbstractGridDnD { + + @Override + protected void setup(VaadinRequest request) { + getUI().setMobileHtml5DndEnabled(true); + + Grid<Person> grid = createGridAndFillWithData(50); + + GridDragger<Person> gridDragger = new GridDragger<>(grid); + + initializeTestFor(gridDragger); + } + +} diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridDraggerTwoGrids.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridDraggerTwoGrids.java new file mode 100644 index 0000000000..4942dc7810 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridDraggerTwoGrids.java @@ -0,0 +1,59 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * 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. + */ +package com.vaadin.tests.components.grid; + +import com.vaadin.annotations.Theme; +import com.vaadin.annotations.Widgetset; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.util.Person; +import com.vaadin.ui.CheckBox; +import com.vaadin.ui.Grid; +import com.vaadin.ui.components.grid.DropIndexCalculator; +import com.vaadin.ui.components.grid.GridDragger; +import com.vaadin.ui.components.grid.SourceDataProviderUpdater; + +@Theme("valo") +@Widgetset("com.vaadin.DefaultWidgetSet") +public class GridDraggerTwoGrids extends AbstractGridDnD { + + @Override + protected void setup(VaadinRequest request) { + getUI().setMobileHtml5DndEnabled(true); + + // Drag source Grid + Grid<Person> left = createGridAndFillWithData(50); + + // Drop target Grid + Grid<Person> right = createGridAndFillWithData(0); + + GridDragger<Person> gridDragger = new GridDragger<>(left, right); + + CheckBox addItemsToEnd = new CheckBox("Add Items To End", false); + addItemsToEnd.addValueChangeListener( + event -> gridDragger.setDropIndexCalculator(event.getValue() + ? DropIndexCalculator.ALWAYS_DROP_TO_END : null)); + CheckBox removeItemsFromSource = new CheckBox( + "Remove items from source grid", true); + removeItemsFromSource.addValueChangeListener(event -> gridDragger + .setSourceDataProviderUpdater(event.getValue() ? null + : SourceDataProviderUpdater.NOOP)); + + controls.addComponents(addItemsToEnd, removeItemsFromSource); + + initializeTestFor(gridDragger); + } + +} |