@@ -62,18 +62,20 @@ public class GridDragger<T> implements Serializable { | |||
private boolean removeFromSource = true; | |||
/** | |||
* Extends a Grid and makes it's row orderable by dragging entries up or | |||
* down. | |||
* Enables DnD reordering for the rows in the given grid. | |||
* <p> | |||
* {@link DropMode#BETWEEN} is used. | |||
* | |||
* @param grid | |||
* Grid to be extended. | |||
*/ | |||
public GridDragger(Grid<T> grid) { | |||
this(grid, grid, DropMode.ON_TOP_OR_BETWEEN); | |||
this(grid, grid, DropMode.BETWEEN); | |||
} | |||
/** | |||
* Enables DnD reordering the rows in the given grid. | |||
* Enables DnD reordering the rows in the given grid with the given drop | |||
* mode. | |||
* <p> | |||
* <em>NOTE: this only works when the grid has a | |||
* {@link ListDataProvider}.</em> Use the custom handlers | |||
@@ -93,7 +95,7 @@ public class GridDragger<T> implements Serializable { | |||
/** | |||
* Enables DnD moving of rows from the source grid to the target grid. | |||
* <p> | |||
* {@link DropMode#ON_TOP_OR_BETWEEN} is used. | |||
* {@link DropMode#BETWEEN} is used. | |||
* <p> | |||
* <em>NOTE: this only works when the grids have a | |||
* {@link ListDataProvider}.</em> Use the custom handlers | |||
@@ -107,14 +109,14 @@ public class GridDragger<T> implements Serializable { | |||
* the target grid dropped to. | |||
*/ | |||
public GridDragger(Grid<T> source, Grid<T> target) { | |||
this(source, target, DropMode.ON_TOP_OR_BETWEEN); | |||
this(source, target, DropMode.BETWEEN); | |||
} | |||
/** | |||
* Enables DnD moving of rows from the source grid to the target grid with | |||
* the custom data provider updaters. | |||
* <p> | |||
* {@link DropMode#ON_TOP_OR_BETWEEN} is used. | |||
* {@link DropMode#BETWEEN} is used. | |||
* | |||
* @param source | |||
* grid dragged from | |||
@@ -128,7 +130,7 @@ public class GridDragger<T> implements Serializable { | |||
public GridDragger(Grid<T> source, Grid<T> target, | |||
TargetDataProviderUpdater<T> targetDataProviderUpdater, | |||
SourceDataProviderUpdater<T> sourceDataProviderUpdater) { | |||
this(source, target, DropMode.ON_TOP_OR_BETWEEN); | |||
this(source, target, DropMode.BETWEEN); | |||
this.targetDataProviderUpdater = targetDataProviderUpdater; | |||
this.sourceDataProviderUpdater = sourceDataProviderUpdater; | |||
} | |||
@@ -203,7 +205,7 @@ public class GridDragger<T> implements Serializable { | |||
* handle updating instead. | |||
* <p> | |||
* <em>NOTE: this is not triggered when | |||
* {@link #setRemoveItemsFromSourceGrid(boolean)} has been set to | |||
* {@link #setRemoveItemsFromSource(boolean)} has been set to | |||
* {@code false}</em> | |||
* | |||
* @param sourceDataProviderUpdater | |||
@@ -318,7 +320,7 @@ public class GridDragger<T> implements Serializable { | |||
* {@code true} to remove dropped items, {@code false} to not | |||
* remove. | |||
*/ | |||
public void setRemoveItemsFromSourceGrid(boolean removeFromSource) { | |||
public void setRemoveItemsFromSource(boolean removeFromSource) { | |||
this.removeFromSource = removeFromSource; | |||
} | |||
@@ -335,7 +337,7 @@ public class GridDragger<T> implements Serializable { | |||
* @return {@code true} to remove dropped items, {@code false} to not | |||
* remove. | |||
*/ | |||
public boolean isRemoveItemsFromSourceGrid() { | |||
public boolean isRemoveItemsFromSource() { | |||
return removeFromSource; | |||
} | |||
@@ -343,7 +345,7 @@ public class GridDragger<T> implements Serializable { | |||
Grid<T> source = getGridDragSource().getGrid(); | |||
if (getSourceGridDropHandler() == null) { | |||
if (!(source.getDataProvider() instanceof ListDataProvider)) { | |||
throwIllegalStateException(true); | |||
throwIllegalStateExceptionForUnsupportedDataProvider(true); | |||
} | |||
ListDataProvider<T> listDataProvider = (ListDataProvider<T>) source | |||
.getDataProvider(); | |||
@@ -361,7 +363,7 @@ public class GridDragger<T> implements Serializable { | |||
Grid<T> target = getGridDropTarget().getGrid(); | |||
if (targetDataProviderUpdater == null) { | |||
if (!(target.getDataProvider() instanceof ListDataProvider)) { | |||
throwIllegalStateException(false); | |||
throwIllegalStateExceptionForUnsupportedDataProvider(false); | |||
} | |||
ListDataProvider<T> listDataProvider = (ListDataProvider<T>) target | |||
.getDataProvider(); | |||
@@ -394,7 +396,7 @@ public class GridDragger<T> implements Serializable { | |||
} | |||
} | |||
private static void throwIllegalStateException(boolean sourceGrid) { | |||
private static void throwIllegalStateExceptionForUnsupportedDataProvider(boolean sourceGrid) { | |||
throw new IllegalStateException( | |||
new StringBuilder().append(sourceGrid ? "Source " : "Target ") | |||
.append("grid does not have a ListDataProvider, cannot automatically ") |
@@ -0,0 +1,115 @@ | |||
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"); | |||
} | |||
}); | |||
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, 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)); | |||
} | |||
} |
@@ -15,12 +15,9 @@ | |||
*/ | |||
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,20 +27,14 @@ 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; | |||
@@ -59,69 +50,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"); | |||
} | |||
}); | |||
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); | |||
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) { | |||
@@ -213,17 +142,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)); | |||
} | |||
} |
@@ -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); | |||
} | |||
} |
@@ -0,0 +1,57 @@ | |||
/* | |||
* 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.GridDragger; | |||
@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", | |||
gridDragger.isAddItemsToEnd()); | |||
addItemsToEnd.addValueChangeListener( | |||
event -> gridDragger.setAddItemsToEnd(event.getValue())); | |||
CheckBox removeItemsFromSource = new CheckBox( | |||
"Remove items from source grid", | |||
gridDragger.isRemoveItemsFromSource()); | |||
removeItemsFromSource.addValueChangeListener(event -> gridDragger | |||
.setRemoveItemsFromSource(event.getValue())); | |||
controls.addComponents(addItemsToEnd, removeItemsFromSource); | |||
initializeTestFor(gridDragger); | |||
} | |||
} |