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.

GridDragSource.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.ui;
  17. import java.util.List;
  18. import java.util.Optional;
  19. import java.util.Set;
  20. import java.util.stream.Collectors;
  21. import com.vaadin.data.provider.DataGenerator;
  22. import com.vaadin.event.dnd.DragSourceExtension;
  23. import com.vaadin.event.dnd.grid.GridDragEndEvent;
  24. import com.vaadin.event.dnd.grid.GridDragEndListener;
  25. import com.vaadin.event.dnd.grid.GridDragStartEvent;
  26. import com.vaadin.event.dnd.grid.GridDragStartListener;
  27. import com.vaadin.server.SerializableFunction;
  28. import com.vaadin.shared.Registration;
  29. import com.vaadin.shared.ui.dnd.DragSourceState;
  30. import com.vaadin.shared.ui.dnd.DropEffect;
  31. import com.vaadin.shared.ui.grid.GridDragSourceRpc;
  32. import com.vaadin.shared.ui.grid.GridDragSourceState;
  33. import elemental.json.JsonObject;
  34. /**
  35. * Makes a Grid's rows draggable for HTML5 drag and drop functionality.
  36. * <p>
  37. * When dragging a selected row, all the visible selected rows are dragged. Note
  38. * that ONLY visible rows are taken into account.
  39. *
  40. * @param <T>
  41. * The Grid bean type.
  42. * @author Vaadin Ltd.
  43. * @since
  44. */
  45. public class GridDragSource<T> extends DragSourceExtension<Grid<T>> {
  46. /**
  47. * Drag data generator that appends drag data to each row.
  48. */
  49. private DataGenerator<T> dragDataGenerator;
  50. /**
  51. * Drag data generator function that is executed for each row.
  52. */
  53. private SerializableFunction<T, JsonObject> generatorFunction;
  54. /**
  55. * Extends a Grid and makes it's rows draggable.
  56. *
  57. * @param target
  58. * Grid to be extended.
  59. */
  60. public GridDragSource(Grid<T> target) {
  61. super(target);
  62. // Create drag data generator
  63. dragDataGenerator = this::generateDragData;
  64. // Add drag data generator to Grid
  65. target.addDataGenerator(dragDataGenerator);
  66. }
  67. @Override
  68. protected void registerDragSourceRpc(Grid<T> target) {
  69. registerRpc(new GridDragSourceRpc() {
  70. @Override
  71. public void dragStart(List<String> draggedItemKeys) {
  72. GridDragStartEvent<T> event = new GridDragStartEvent<>(target,
  73. getState(false).effectAllowed,
  74. getDraggedItems(target, draggedItemKeys));
  75. fireEvent(event);
  76. }
  77. @Override
  78. public void dragEnd(DropEffect dropEffect,
  79. List<String> draggedItemKeys) {
  80. GridDragEndEvent<T> event = new GridDragEndEvent<>(target,
  81. dropEffect, getDraggedItems(target, draggedItemKeys));
  82. fireEvent(event);
  83. }
  84. });
  85. }
  86. /**
  87. * Collects the dragged items of a Grid given the list of item keys.
  88. */
  89. private Set<T> getDraggedItems(Grid<T> grid, List<String> draggedItemKeys) {
  90. if (draggedItemKeys == null || draggedItemKeys.isEmpty()) {
  91. throw new IllegalStateException(
  92. "The drag event does not contain dragged items");
  93. }
  94. return draggedItemKeys.stream()
  95. .map(key -> grid.getDataCommunicator().getKeyMapper().get(key))
  96. .collect(Collectors.toSet());
  97. }
  98. /**
  99. * Drag data generator. Appends drag data to row data json if generator
  100. * function is set by the user of this extension.
  101. *
  102. * @param item
  103. * Row item for data generation.
  104. * @param jsonObject
  105. * Row data in json format.
  106. */
  107. private void generateDragData(T item, JsonObject jsonObject) {
  108. Optional.ofNullable(generatorFunction).ifPresent(generator -> jsonObject
  109. .put(GridDragSourceState.JSONKEY_DRAG_DATA,
  110. generator.apply(item)));
  111. }
  112. /**
  113. * Sets a generator function for customizing drag data. The function is
  114. * executed for each item in the Grid during data generation. Return a
  115. * {@link JsonObject} to be appended to the row data.
  116. * <p>
  117. * Example:
  118. * <pre>
  119. * dragSourceExtension.setDragDataGenerator(item -> {
  120. * JsonObject dragData = Json.createObject();
  121. * dragData.put("someKey", item.getValue());
  122. * return dragData;
  123. * });
  124. * </pre>
  125. *
  126. * @param generator
  127. * Function to be executed on row data generation.
  128. */
  129. public void setDragDataGenerator(
  130. SerializableFunction<T, JsonObject> generator) {
  131. generatorFunction = generator;
  132. }
  133. @Override
  134. public void setDataTransferText(String data) throws
  135. UnsupportedOperationException {
  136. throw new UnsupportedOperationException(
  137. "Setting dataTransferText is not supported");
  138. }
  139. /**
  140. * Attaches dragstart listener for the current drag source grid.
  141. *
  142. * @param listener
  143. * Listener to handle the dragstart event.
  144. * @return Handle to be used to remove this listener.
  145. * @see GridDragStartEvent
  146. */
  147. public Registration addGridDragStartListener(
  148. GridDragStartListener<T> listener) {
  149. return addListener(DragSourceState.EVENT_DRAGSTART,
  150. GridDragStartEvent.class, listener,
  151. GridDragStartListener.DRAG_START_METHOD);
  152. }
  153. /**
  154. * Attaches dragend listener for the current drag source grid.
  155. *
  156. * @param listener
  157. * Listener to handle the dragend event.
  158. * @return Handle to be used to remove this listener.
  159. * @see GridDragEndEvent
  160. */
  161. public Registration addGridDragEndListener(
  162. GridDragEndListener<T> listener) {
  163. return addListener(DragSourceState.EVENT_DRAGEND,
  164. GridDragEndEvent.class, listener,
  165. GridDragEndListener.DRAG_END_METHOD);
  166. }
  167. /**
  168. * Returns the generator function for customizing drag data.
  169. *
  170. * @return Drag data generator function.
  171. */
  172. public SerializableFunction<T, JsonObject> getDragDataGenerator() {
  173. return generatorFunction;
  174. }
  175. @Override
  176. public void remove() {
  177. super.remove();
  178. getParent().removeDataGenerator(dragDataGenerator);
  179. }
  180. @Override
  181. protected GridDragSourceState getState() {
  182. return (GridDragSourceState) super.getState();
  183. }
  184. @Override
  185. protected GridDragSourceState getState(boolean markAsDirty) {
  186. return (GridDragSourceState) super.getState(markAsDirty);
  187. }
  188. }