You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GridDragSourceConnector.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.connectors.grid;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.stream.Collectors;
  20. import com.google.gwt.dom.client.TableRowElement;
  21. import com.vaadin.client.ServerConnector;
  22. import com.vaadin.client.extensions.DragSourceExtensionConnector;
  23. import com.vaadin.client.widget.escalator.RowContainer;
  24. import com.vaadin.client.widget.grid.selection.SelectionModel;
  25. import com.vaadin.client.widgets.Escalator;
  26. import com.vaadin.client.widgets.Grid;
  27. import com.vaadin.shared.Range;
  28. import com.vaadin.shared.ui.Connect;
  29. import com.vaadin.shared.ui.dnd.DropEffect;
  30. import com.vaadin.shared.ui.grid.GridDragSourceRpc;
  31. import com.vaadin.shared.ui.grid.GridDragSourceState;
  32. import com.vaadin.shared.ui.grid.GridState;
  33. import com.vaadin.ui.GridDragSource;
  34. import elemental.events.Event;
  35. import elemental.json.Json;
  36. import elemental.json.JsonArray;
  37. import elemental.json.JsonObject;
  38. /**
  39. * Adds HTML5 drag and drop functionality to a {@link com.vaadin.client.widgets.Grid
  40. * Grid}'s rows. This is the client side counterpart of {@link GridDragSource}.
  41. *
  42. * @author Vaadin Ltd
  43. * @since
  44. */
  45. @Connect(GridDragSource.class)
  46. public class GridDragSourceConnector extends
  47. DragSourceExtensionConnector {
  48. private GridConnector gridConnector;
  49. /**
  50. * List of dragged item keys.
  51. */
  52. private List<String> draggedItemKeys;
  53. @Override
  54. protected void extend(ServerConnector target) {
  55. this.gridConnector = (GridConnector) target;
  56. // Set newly added rows draggable
  57. getGridBody().setNewEscalatorRowCallback(
  58. rows -> rows.forEach(this::setDraggable));
  59. // Add drag listeners to body element
  60. addDragListeners(getGridBody().getElement());
  61. }
  62. @Override
  63. protected void onDragStart(Event event) {
  64. // Collect the keys of dragged rows
  65. draggedItemKeys = getDraggedRows(event).stream()
  66. .map(row -> row.getString(GridState.JSONKEY_ROWKEY))
  67. .collect(Collectors.toList());
  68. super.onDragStart(event);
  69. }
  70. @Override
  71. protected String createDataTransferText(Event dragStartEvent) {
  72. JsonArray dragData = toJsonArray(
  73. getDraggedRows(dragStartEvent).stream().map(this::getDragData)
  74. .collect(Collectors.toList()));
  75. return dragData.toJson();
  76. }
  77. @Override
  78. protected void sendDragStartEventToServer(Event dragStartEvent) {
  79. // Start server RPC with dragged item keys
  80. getRpcProxy(GridDragSourceRpc.class).dragStart(draggedItemKeys);
  81. }
  82. private List<JsonObject> getDraggedRows(Event dragStartEvent) {
  83. List<JsonObject> draggedRows = new ArrayList<>();
  84. if (dragStartEvent.getTarget() instanceof TableRowElement) {
  85. TableRowElement row = (TableRowElement) dragStartEvent.getTarget();
  86. int rowIndex = ((Escalator.AbstractRowContainer) getGridBody())
  87. .getLogicalRowIndex(row);
  88. JsonObject rowData = gridConnector.getDataSource().getRow(rowIndex);
  89. if (dragMultipleRows(rowData)) {
  90. getSelectedVisibleRows().forEach(draggedRows::add);
  91. } else {
  92. draggedRows.add(rowData);
  93. }
  94. }
  95. return draggedRows;
  96. }
  97. @Override
  98. protected void onDragEnd(Event event) {
  99. super.onDragEnd(event);
  100. // Clear item key list
  101. draggedItemKeys = null;
  102. }
  103. @Override
  104. protected void sendDragEndEventToServer(Event dragEndEvent,
  105. DropEffect dropEffect) {
  106. // Send server RPC with dragged item keys
  107. getRpcProxy(GridDragSourceRpc.class)
  108. .dragEnd(dropEffect, draggedItemKeys);
  109. }
  110. /**
  111. * Tells if multiple rows are dragged. Returns true if multiple selection is
  112. * allowed and a selected row is dragged.
  113. *
  114. * @param draggedRow
  115. * Data of dragged row.
  116. * @return {@code true} if multiple rows are dragged, {@code false}
  117. * otherwise.
  118. */
  119. private boolean dragMultipleRows(JsonObject draggedRow) {
  120. SelectionModel<JsonObject> selectionModel = getGrid()
  121. .getSelectionModel();
  122. return selectionModel.isSelectionAllowed()
  123. && selectionModel instanceof MultiSelectionModelConnector.MultiSelectionModel
  124. && selectionModel.isSelected(draggedRow);
  125. }
  126. /**
  127. * Collects the data of all selected visible rows.
  128. *
  129. * @return List of data of all selected visible rows.
  130. */
  131. private List<JsonObject> getSelectedVisibleRows() {
  132. return getSelectedRowsInRange(getEscalator().getVisibleRowRange());
  133. }
  134. /**
  135. * Get all selected rows from a subset of rows defined by {@code range}.
  136. *
  137. * @param range
  138. * Range of indexes.
  139. * @return List of data of all selected rows in the given range.
  140. */
  141. private List<JsonObject> getSelectedRowsInRange(Range range) {
  142. List<JsonObject> selectedRows = new ArrayList<>();
  143. for (int i = range.getStart(); i < range.getEnd(); i++) {
  144. JsonObject row = gridConnector.getDataSource().getRow(i);
  145. if (SelectionModel.isItemSelected(row)) {
  146. selectedRows.add(row);
  147. }
  148. }
  149. return selectedRows;
  150. }
  151. /**
  152. * Converts a list of {@link JsonObject}s to a {@link JsonArray}.
  153. *
  154. * @param objects
  155. * List of json objects.
  156. * @return Json array containing all json objects.
  157. */
  158. private JsonArray toJsonArray(List<JsonObject> objects) {
  159. JsonArray array = Json.createArray();
  160. for (int i = 0; i < objects.size(); i++) {
  161. array.set(i, objects.get(i));
  162. }
  163. return array;
  164. }
  165. /**
  166. * Gets drag data from the row data if exists or returns complete row data
  167. * otherwise.
  168. *
  169. * @param row
  170. * Row data.
  171. * @return Drag data if present or row data otherwise.
  172. */
  173. private JsonObject getDragData(JsonObject row) {
  174. return row.hasKey(GridDragSourceState.JSONKEY_DRAG_DATA)
  175. ? row.getObject(GridDragSourceState.JSONKEY_DRAG_DATA) : row;
  176. }
  177. @Override
  178. public void onUnregister() {
  179. super.onUnregister();
  180. // Remove draggable from all row elements in the escalator
  181. Range visibleRange = getEscalator().getVisibleRowRange();
  182. for (int i = visibleRange.getStart(); i < visibleRange.getEnd(); i++) {
  183. removeDraggable(getGridBody().getRowElement(i));
  184. }
  185. // Remove drag listeners from body element
  186. removeDragListeners(getGridBody().getElement());
  187. // Remove callback for newly added rows
  188. getGridBody().setNewEscalatorRowCallback(null);
  189. }
  190. private Grid<JsonObject> getGrid() {
  191. return gridConnector.getWidget();
  192. }
  193. private Escalator getEscalator() {
  194. return getGrid().getEscalator();
  195. }
  196. private RowContainer.BodyRowContainer getGridBody() {
  197. return getEscalator().getBody();
  198. }
  199. @Override
  200. public GridDragSourceState getState() {
  201. return (GridDragSourceState) super.getState();
  202. }
  203. }