|
|
@@ -15,19 +15,28 @@ |
|
|
|
*/ |
|
|
|
package com.vaadin.client.connectors.grid; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import com.google.gwt.dom.client.NativeEvent; |
|
|
|
import com.google.gwt.dom.client.TableRowElement; |
|
|
|
import com.vaadin.client.ServerConnector; |
|
|
|
import com.vaadin.client.extensions.DragSourceExtensionConnector; |
|
|
|
import com.vaadin.client.widget.escalator.RowContainer; |
|
|
|
import com.vaadin.client.widget.grid.selection.SelectionModel; |
|
|
|
import com.vaadin.client.widgets.Escalator; |
|
|
|
import com.vaadin.client.widgets.Grid; |
|
|
|
import com.vaadin.shared.Range; |
|
|
|
import com.vaadin.shared.ui.Connect; |
|
|
|
import com.vaadin.shared.ui.grid.GridDragSourceExtensionState; |
|
|
|
import com.vaadin.ui.GridDragSourceExtension; |
|
|
|
|
|
|
|
import elemental.events.Event; |
|
|
|
import elemental.json.Json; |
|
|
|
import elemental.json.JsonArray; |
|
|
|
import elemental.json.JsonObject; |
|
|
|
import elemental.json.JsonValue; |
|
|
|
|
|
|
|
/** |
|
|
|
* Adds HTML5 drag and drop functionality to a {@link com.vaadin.client.widgets.Grid |
|
|
@@ -66,11 +75,78 @@ public class GridDragSourceExtensionConnector extends |
|
|
|
|
|
|
|
JsonObject rowData = gridConnector.getDataSource().getRow(rowIndex); |
|
|
|
|
|
|
|
// Generate drag data. Dragged row or all the selected rows |
|
|
|
JsonValue dragData = dragMultipleRows(rowData) ? toJsonArray( |
|
|
|
getSelectedVisibleRows().stream().map(this::getDragData) |
|
|
|
.collect(Collectors.toList())) |
|
|
|
: getDragData(rowData); |
|
|
|
|
|
|
|
// Set drag data in DataTransfer object |
|
|
|
((NativeEvent) event).getDataTransfer() |
|
|
|
.setData(GridDragSourceExtensionState.DATA_TYPE_DRAG_DATA, |
|
|
|
getDragData(rowData).toJson()); |
|
|
|
dragData.toJson()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Tells if multiple rows are dragged. Returns true if multiple selection is |
|
|
|
* allowed and a selected row is dragged. |
|
|
|
* |
|
|
|
* @param draggedRow |
|
|
|
* Data of dragged row. |
|
|
|
* @return {@code true} if multiple rows are dragged, {@code false} |
|
|
|
* otherwise. |
|
|
|
*/ |
|
|
|
private boolean dragMultipleRows(JsonObject draggedRow) { |
|
|
|
SelectionModel<JsonObject> selectionModel = getGrid() |
|
|
|
.getSelectionModel(); |
|
|
|
return selectionModel.isSelectionAllowed() |
|
|
|
&& selectionModel instanceof MultiSelectionModelConnector.MultiSelectionModel |
|
|
|
&& selectionModel.isSelected(draggedRow); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Collects the data of all selected visible rows. |
|
|
|
* |
|
|
|
* @return List of data of all selected visible rows. |
|
|
|
*/ |
|
|
|
private List<JsonObject> getSelectedVisibleRows() { |
|
|
|
return getSelectedRowsInRange(getEscalator().getVisibleRowRange()); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get all selected rows from a subset of rows defined by {@code range}. |
|
|
|
* |
|
|
|
* @param range |
|
|
|
* Range of indexes. |
|
|
|
* @return List of data of all selected rows in the given range. |
|
|
|
*/ |
|
|
|
private List<JsonObject> getSelectedRowsInRange(Range range) { |
|
|
|
List<JsonObject> selectedRows = new ArrayList<>(); |
|
|
|
|
|
|
|
for (int i = range.getStart(); i < range.getEnd(); i++) { |
|
|
|
JsonObject row = gridConnector.getDataSource().getRow(i); |
|
|
|
if (SelectionModel.isItemSelected(row)) { |
|
|
|
selectedRows.add(row); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return selectedRows; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Converts a list of {@link JsonObject}s to a {@link JsonArray}. |
|
|
|
* |
|
|
|
* @param objects |
|
|
|
* List of json objects. |
|
|
|
* @return Json array containing all json objects. |
|
|
|
*/ |
|
|
|
private JsonArray toJsonArray(List<JsonObject> objects) { |
|
|
|
JsonArray array = Json.createArray(); |
|
|
|
for (int i = 0; i < objects.size(); i++) { |
|
|
|
array.set(i, objects.get(i)); |
|
|
|
} |
|
|
|
return array; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@@ -104,8 +180,12 @@ public class GridDragSourceExtensionConnector extends |
|
|
|
getGridBody().setNewEscalatorRowCallback(null); |
|
|
|
} |
|
|
|
|
|
|
|
private Grid<JsonObject> getGrid() { |
|
|
|
return gridConnector.getWidget(); |
|
|
|
} |
|
|
|
|
|
|
|
private Escalator getEscalator() { |
|
|
|
return gridConnector.getWidget().getEscalator(); |
|
|
|
return getGrid().getEscalator(); |
|
|
|
} |
|
|
|
|
|
|
|
private RowContainer.BodyRowContainer getGridBody() { |