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.

GridDragSourceExtension.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.Optional;
  18. import com.vaadin.data.provider.DataGenerator;
  19. import com.vaadin.event.dnd.DragSourceExtension;
  20. import com.vaadin.server.SerializableFunction;
  21. import com.vaadin.shared.ui.grid.GridDragSourceExtensionState;
  22. import elemental.json.JsonObject;
  23. /**
  24. * Makes a Grid's rows draggable for HTML5 drag and drop functionality.
  25. * <p>
  26. * When dragging a selected row, all the visible selected rows are dragged. Note
  27. * that ONLY visible rows are taken into account.
  28. *
  29. * @param <T>
  30. * The Grid bean type.
  31. * @author Vaadin Ltd.
  32. * @since
  33. */
  34. public class GridDragSourceExtension<T> extends DragSourceExtension<Grid<T>> {
  35. /**
  36. * Drag data generator that appends drag data to each row.
  37. */
  38. private DataGenerator<T> dragDataGenerator;
  39. /**
  40. * Drag data generator function that is executed for each row.
  41. */
  42. private SerializableFunction<T, JsonObject> generatorFunction;
  43. /**
  44. * Extends a Grid and makes it's rows draggable.
  45. *
  46. * @param target
  47. * Grid to be extended.
  48. */
  49. public GridDragSourceExtension(Grid<T> target) {
  50. super(target);
  51. // Create drag data generator
  52. dragDataGenerator = this::generateDragData;
  53. // Add drag data generator to Grid
  54. target.addDataGenerator(dragDataGenerator);
  55. }
  56. /**
  57. * Drag data generator. Appends drag data to row data json if generator
  58. * function is set by the user of this extension.
  59. *
  60. * @param item
  61. * Row item for data generation.
  62. * @param jsonObject
  63. * Row data in json format.
  64. */
  65. private void generateDragData(T item, JsonObject jsonObject) {
  66. Optional.ofNullable(generatorFunction).ifPresent(generator -> jsonObject
  67. .put(GridDragSourceExtensionState.JSONKEY_DRAG_DATA,
  68. generator.apply(item)));
  69. }
  70. /**
  71. * Sets a generator function for customizing drag data. The function is
  72. * executed for each item in the Grid during data generation. Return a
  73. * {@link JsonObject} to be appended to the row data.
  74. * <p>
  75. * Example:
  76. * <pre>
  77. * dragSourceExtension.setDragDataGenerator(item -> {
  78. * JsonObject dragData = Json.createObject();
  79. * dragData.put("someKey", item.getValue());
  80. * return dragData;
  81. * });
  82. * </pre>
  83. *
  84. * @param generator
  85. * Function to be executed on row data generation.
  86. */
  87. public void setDragDataGenerator(
  88. SerializableFunction<T, JsonObject> generator) {
  89. generatorFunction = generator;
  90. }
  91. /**
  92. * Returns the generator function for customizing drag data.
  93. *
  94. * @return Drag data generator function.
  95. */
  96. public SerializableFunction<T, JsonObject> getDragDataGenerator() {
  97. return generatorFunction;
  98. }
  99. @Override
  100. public void remove() {
  101. super.remove();
  102. getParent().removeDataGenerator(dragDataGenerator);
  103. }
  104. @Override
  105. protected GridDragSourceExtensionState getState() {
  106. return (GridDragSourceExtensionState) super.getState();
  107. }
  108. @Override
  109. protected GridDragSourceExtensionState getState(boolean markAsDirty) {
  110. return (GridDragSourceExtensionState) super.getState(markAsDirty);
  111. }
  112. }