Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

GridRowDragger.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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.components.grid;
  17. import java.io.Serializable;
  18. import java.util.Collection;
  19. import java.util.List;
  20. import java.util.Optional;
  21. import com.vaadin.data.provider.DataProvider;
  22. import com.vaadin.data.provider.ListDataProvider;
  23. import com.vaadin.shared.ui.dnd.DropEffect;
  24. import com.vaadin.shared.ui.grid.DropLocation;
  25. import com.vaadin.shared.ui.grid.DropMode;
  26. import com.vaadin.ui.Grid;
  27. import com.vaadin.ui.Grid.Column;
  28. /**
  29. * Allows dragging rows for reordering within a Grid and between two separate
  30. * Grids when the item type is the same.
  31. * <p>
  32. * When dragging a selected row, all the visible selected rows are dragged. Note
  33. * that ONLY currently visible rows are taken into account. The drop mode for
  34. * the target grid is by default {@link DropMode#BETWEEN}.
  35. * <p>
  36. * To customize the settings for either the source or the target grid, use
  37. * {@link #getGridDragSource()} and {@link #getGridDropTarget()}.The drop target
  38. * grid has been set to not allow drops for a target row when the grid has been
  39. * sorted, since the visual drop target location would not match where the item
  40. * would actually be dropped into.
  41. * <p>
  42. * <em>NOTE: this helper works only with {@link ListDataProvider} on both grids.
  43. * If you have another data provider, you should customize data provider
  44. * updating on drop with
  45. * {@link #setSourceDataProviderUpdater(SourceDataProviderUpdater)} &
  46. * {@link #setTargetDataProviderUpdater(TargetDataProviderUpdater)} and add a
  47. * custom drop index calculator with
  48. * {@link #setDropIndexCalculator(DropIndexCalculator)}.</em>
  49. * <p>
  50. * In case you are not using a {@link ListDataProvider} and don't have custom
  51. * handlers, {@link UnsupportedOperationException} is thrown on drop event.
  52. *
  53. * @param <T>
  54. * The Grid bean type.
  55. * @author Vaadin Ltd
  56. * @since 8.2
  57. */
  58. public class GridRowDragger<T> implements Serializable {
  59. private final GridDropTarget<T> gridDropTarget;
  60. private final GridDragSource<T> gridDragSource;
  61. private DropIndexCalculator<T> dropTargetIndexCalculator = null;
  62. private SourceDataProviderUpdater<T> sourceDataProviderUpdater = null;
  63. private TargetDataProviderUpdater<T> targetDataProviderUpdater = null;
  64. /**
  65. * Set of items currently being dragged.
  66. */
  67. private List<T> draggedItems;
  68. private int shiftedDropIndex;
  69. /**
  70. * Enables DnD reordering for the rows in the given grid.
  71. * <p>
  72. * {@link DropMode#BETWEEN} is used.
  73. * <p>
  74. * <em>NOTE:</em> this only works when the grid has a
  75. * {@link ListDataProvider}. Use the custom handlers
  76. * {@link #setSourceDataProviderUpdater(SourceDataProviderUpdater)} and
  77. * {@link #setTargetDataProviderUpdater(TargetDataProviderUpdater)} for
  78. * other data providers.
  79. * <p>
  80. * <em>NOTE:</em> When allowing the user to DnD reorder a grid's rows, you
  81. * should not allow the user to sort the grid since when the grid is sorted,
  82. * as the reordering doens't make any sense since the drop target cannot be
  83. * shown for the correct place due to the sorting. Sorting columns is
  84. * enabled by default for in-memory data provider grids. Sorting can be
  85. * disabled for columns with {@link Grid#getColumns()} and
  86. * {@link Column#setSortable(boolean)}.
  87. *
  88. * @param grid
  89. * Grid to be extended.
  90. */
  91. public GridRowDragger(Grid<T> grid) {
  92. this(grid, DropMode.BETWEEN);
  93. }
  94. /**
  95. * Enables DnD reordering the rows in the given grid with the given drop
  96. * mode.
  97. * <p>
  98. * <em>NOTE:</em> this only works when the grid has a
  99. * {@link ListDataProvider}. Use the custom handlers
  100. * {@link #setSourceDataProviderUpdater(SourceDataProviderUpdater)} and
  101. * {@link #setTargetDataProviderUpdater(TargetDataProviderUpdater)} for
  102. * other data providers.
  103. * <p>
  104. * <em>NOTE:</em> When allowing the user to DnD reorder a grid's rows, you
  105. * should not allow the user to sort the grid since when the grid is sorted,
  106. * as the reordering doens't make any sense since the drop target cannot be
  107. * shown for the correct place due to the sorting. Sorting columns is
  108. * enabled by default for in-memory data provider grids. Sorting can be
  109. * disabled for columns with {@link Grid#getColumns()} and
  110. * {@link Column#setSortable(boolean)}.
  111. *
  112. * @param grid
  113. * the grid to enable row DnD reordering on
  114. * @param dropMode
  115. * DropMode to be used.
  116. */
  117. public GridRowDragger(Grid<T> grid, DropMode dropMode) {
  118. this(grid, grid, dropMode);
  119. }
  120. /**
  121. * Enables DnD moving of rows from the source grid to the target grid.
  122. * <p>
  123. * {@link DropMode#BETWEEN} is used.
  124. * <p>
  125. * <em>NOTE: this only works when the grids have a
  126. * {@link ListDataProvider}.</em> Use the custom handlers
  127. * {@link #setSourceDataProviderUpdater(SourceDataProviderUpdater)} and
  128. * {@link #setTargetDataProviderUpdater(TargetDataProviderUpdater)} for
  129. * other data providers.
  130. *
  131. * @param source
  132. * the source grid dragged from.
  133. * @param target
  134. * the target grid dropped to.
  135. */
  136. public GridRowDragger(Grid<T> source, Grid<T> target) {
  137. this(source, target, DropMode.BETWEEN);
  138. }
  139. /**
  140. * Enables DnD moving of rows from the source grid to the target grid with
  141. * the custom data provider updaters.
  142. * <p>
  143. * {@link DropMode#BETWEEN} is used.
  144. *
  145. * @param source
  146. * grid dragged from
  147. * @param target
  148. * grid dragged to
  149. * @param targetDataProviderUpdater
  150. * handler for updating target grid data provider
  151. * @param sourceDataProviderUpdater
  152. * handler for updating source grid data provider
  153. */
  154. public GridRowDragger(Grid<T> source, Grid<T> target,
  155. TargetDataProviderUpdater<T> targetDataProviderUpdater,
  156. SourceDataProviderUpdater<T> sourceDataProviderUpdater) {
  157. this(source, target, DropMode.BETWEEN);
  158. this.targetDataProviderUpdater = targetDataProviderUpdater;
  159. this.sourceDataProviderUpdater = sourceDataProviderUpdater;
  160. }
  161. /**
  162. * Enables DnD moving of rows from the source grid to the target grid with
  163. * the given drop mode.
  164. * <p>
  165. * <em>NOTE: this only works when the grids have a
  166. * {@link ListDataProvider}.</em> Use the other constructors or custom
  167. * handlers {@link #setSourceDataProviderUpdater(SourceDataProviderUpdater)}
  168. * and {@link #setTargetDataProviderUpdater(TargetDataProviderUpdater)} for
  169. * other data providers.
  170. *
  171. * @param source
  172. * the drag source grid
  173. * @param target
  174. * the drop target grid
  175. * @param dropMode
  176. * the drop mode to use
  177. */
  178. public GridRowDragger(Grid<T> source, Grid<T> target, DropMode dropMode) {
  179. gridDragSource = new GridDragSource<>(source);
  180. gridDropTarget = new GridDropTarget<>(target, dropMode);
  181. gridDropTarget.setDropAllowedOnRowsWhenSorted(false);
  182. gridDragSource.addGridDragStartListener(event -> {
  183. draggedItems = event.getDraggedItems();
  184. });
  185. gridDropTarget.addGridDropListener(this::handleDrop);
  186. }
  187. /**
  188. * Sets the target data provider updater, which handles adding the dropped
  189. * items to the target grid.
  190. * <p>
  191. * By default, items are added to the index where they were dropped on for
  192. * any {@link ListDataProvider}. If another type of data provider is used,
  193. * this updater should be set to handle updating instead.
  194. *
  195. * @param targetDataProviderUpdater
  196. * the target drop handler to set, or {@code null} to remove
  197. */
  198. public void setTargetDataProviderUpdater(
  199. TargetDataProviderUpdater<T> targetDataProviderUpdater) {
  200. this.targetDataProviderUpdater = targetDataProviderUpdater;
  201. }
  202. /**
  203. * Returns the target grid data provider updater.
  204. *
  205. * @return target grid drop handler
  206. */
  207. public TargetDataProviderUpdater<T> getTargetDataProviderUpdater() {
  208. return targetDataProviderUpdater;
  209. }
  210. /**
  211. * Sets the source data provider updater, which handles removing items from
  212. * the drag source grid.
  213. * <p>
  214. * By default the items are removed from any {@link ListDataProvider}. If
  215. * another type of data provider is used, this updater should be set to
  216. * handle updating instead.
  217. * <p>
  218. * If you want to skip removing items from the source, you can use
  219. * {@link SourceDataProviderUpdater#NOOP}.
  220. *
  221. * @param sourceDataProviderUpdater
  222. * the drag source data provider updater to set, or {@code null}
  223. * to remove
  224. */
  225. public void setSourceDataProviderUpdater(
  226. SourceDataProviderUpdater<T> sourceDataProviderUpdater) {
  227. this.sourceDataProviderUpdater = sourceDataProviderUpdater;
  228. }
  229. /**
  230. * Returns the source grid data provider updater.
  231. * <p>
  232. * Default is {@code null} and the items are just removed from the source
  233. * grid, which only works for {@link ListDataProvider}.
  234. *
  235. * @return the source grid drop handler
  236. */
  237. public SourceDataProviderUpdater<T> getSourceDataProviderUpdater() {
  238. return sourceDataProviderUpdater;
  239. }
  240. /**
  241. * Sets the drop index calculator for the target grid. With this callback
  242. * you can have a custom drop location instead of the actual one.
  243. * <p>
  244. * By default, items are placed on the index they are dropped into in the
  245. * target grid.
  246. * <p>
  247. * If you want to always drop items to the end of the target grid, you can
  248. * use {@link DropIndexCalculator#ALWAYS_DROP_TO_END}.
  249. *
  250. * @param dropIndexCalculator
  251. * the drop index calculator
  252. */
  253. public void setDropIndexCalculator(
  254. DropIndexCalculator<T> dropIndexCalculator) {
  255. this.dropTargetIndexCalculator = dropIndexCalculator;
  256. }
  257. /**
  258. * Gets the drop index calculator.
  259. * <p>
  260. * Default is {@code null} and the dropped items are placed on the drop
  261. * location.
  262. *
  263. * @return the drop index calculator
  264. */
  265. public DropIndexCalculator<T> getDropIndexCalculator() {
  266. return dropTargetIndexCalculator;
  267. }
  268. /**
  269. * Returns the drop target grid to allow performing customizations such as
  270. * altering {@link DropEffect}.
  271. *
  272. * @return the drop target grid
  273. */
  274. public GridDropTarget<T> getGridDropTarget() {
  275. return gridDropTarget;
  276. }
  277. /**
  278. * Returns the drag source grid, exposing it for customizations.
  279. *
  280. * @return the drag source grid
  281. */
  282. public GridDragSource<T> getGridDragSource() {
  283. return gridDragSource;
  284. }
  285. /**
  286. * Returns the currently dragged items captured from the source grid no drag
  287. * start event, or {@code null} if no drag active.
  288. *
  289. * @return the currently dragged items or {@code null}
  290. */
  291. protected List<T> getDraggedItems() {
  292. return draggedItems;
  293. }
  294. /**
  295. * This method is triggered when there has been a drop on the target grid.
  296. * <p>
  297. * <em>This method is protected only for testing reasons, you should not
  298. * override this</em> but instead use
  299. * {@link #setSourceDataProviderUpdater(SourceDataProviderUpdater)},
  300. * {@link #setTargetDataProviderUpdater(TargetDataProviderUpdater)} and
  301. * {@link #setDropIndexCalculator(DropIndexCalculator)} to customize how to
  302. * handle the drops.
  303. *
  304. * @param event
  305. * the drop event on the target grid
  306. */
  307. protected void handleDrop(GridDropEvent<T> event) {
  308. // there is a case that the drop happened from some other grid than the
  309. // source one
  310. if (getDraggedItems() == null) {
  311. return;
  312. }
  313. // don't do anything if not supported data providers used without custom
  314. // handlers
  315. verifySupportedDataProviders();
  316. shiftedDropIndex = -1;
  317. handleSourceGridDrop(event, getDraggedItems());
  318. int index = calculateDropIndex(event);
  319. handleTargetGridDrop(event, index, getDraggedItems());
  320. draggedItems = null;
  321. }
  322. private void handleSourceGridDrop(GridDropEvent<T> event,
  323. final Collection<T> droppedItems) {
  324. Grid<T> source = getGridDragSource().getGrid();
  325. if (getSourceDataProviderUpdater() != null) {
  326. getSourceDataProviderUpdater().removeItems(event.getDropEffect(),
  327. source.getDataProvider(), droppedItems);
  328. return;
  329. }
  330. ListDataProvider<T> listDataProvider = (ListDataProvider<T>) source
  331. .getDataProvider();
  332. // use the existing data source to keep filters and sort orders etc. in
  333. // place.
  334. Collection<T> sourceItems = listDataProvider.getItems();
  335. // if reordering the same grid and dropping on top of one of the dragged
  336. // rows, need to calculate the new drop index before removing the items
  337. if (getGridDragSource().getGrid() == getGridDropTarget().getGrid()
  338. && event.getDropTargetRow().isPresent()
  339. && getDraggedItems().contains(event.getDropTargetRow().get())) {
  340. List<T> sourceItemsList = (List<T>) sourceItems;
  341. shiftedDropIndex = sourceItemsList
  342. .indexOf(event.getDropTargetRow().get());
  343. shiftedDropIndex -= getDraggedItems().stream().filter(
  344. item -> sourceItemsList.indexOf(item) < shiftedDropIndex)
  345. .count();
  346. }
  347. sourceItems.removeAll(droppedItems);
  348. listDataProvider.refreshAll();
  349. }
  350. private void handleTargetGridDrop(GridDropEvent<T> event, final int index,
  351. Collection<T> droppedItems) {
  352. Grid<T> target = getGridDropTarget().getGrid();
  353. if (getTargetDataProviderUpdater() != null) {
  354. getTargetDataProviderUpdater().onDrop(event.getDropEffect(),
  355. target.getDataProvider(), index, droppedItems);
  356. return;
  357. }
  358. ListDataProvider<T> listDataProvider = (ListDataProvider<T>) target
  359. .getDataProvider();
  360. // update the existing to keep filters etc.
  361. List<T> targetItems = (List<T>) listDataProvider.getItems();
  362. if (index != Integer.MAX_VALUE) {
  363. targetItems.addAll(index, droppedItems);
  364. } else {
  365. targetItems.addAll(droppedItems);
  366. }
  367. // instead of using setItems or creating a new data provider,
  368. // refresh the existing one to keep filters etc. in place
  369. listDataProvider.refreshAll();
  370. // if dropped to the end of the grid, the grid should scroll there so
  371. // that the dropped row is visible, but that is just recommended in
  372. // documentation and left for the users to take into use
  373. }
  374. private int calculateDropIndex(GridDropEvent<T> event) {
  375. // use custom calculator if present
  376. if (getDropIndexCalculator() != null) {
  377. return getDropIndexCalculator().calculateDropIndex(event);
  378. }
  379. // if the source and target grids are the same, then the index has been
  380. // calculated before removing the items. In this case the drop location
  381. // is always above, since the items will be starting from that point on
  382. if (shiftedDropIndex != -1) {
  383. return shiftedDropIndex;
  384. }
  385. ListDataProvider<T> targetDataProvider = (ListDataProvider<T>) getGridDropTarget()
  386. .getGrid().getDataProvider();
  387. List<T> items = (List<T>) targetDataProvider.getItems();
  388. int index = items.size();
  389. Optional<T> dropTargetRow = event.getDropTargetRow();
  390. if (dropTargetRow.isPresent()) {
  391. index = items.indexOf(dropTargetRow.get())
  392. + (event.getDropLocation() == DropLocation.BELOW ? 1 : 0);
  393. }
  394. return index;
  395. }
  396. private void verifySupportedDataProviders() {
  397. verifySourceDataProvider();
  398. verifyTargetDataProvider();
  399. }
  400. @SuppressWarnings("unchecked")
  401. private void verifySourceDataProvider() {
  402. if (getSourceDataProviderUpdater() != null) {
  403. return; // custom updater is always fine
  404. }
  405. if (!(getSourceDataProvider() instanceof ListDataProvider)) {
  406. throwUnsupportedOperationExceptionForUnsupportedDataProvider(true);
  407. }
  408. if (!(((ListDataProvider<T>) getSourceDataProvider())
  409. .getItems() instanceof List)) {
  410. throwUnsupportedOperationExceptionForUnsupportedCollectionInListDataProvider(
  411. true);
  412. }
  413. }
  414. @SuppressWarnings("unchecked")
  415. private void verifyTargetDataProvider() {
  416. if (getTargetDataProviderUpdater() != null
  417. && getDropIndexCalculator() != null) {
  418. return; // custom updater and calculator is always fine
  419. }
  420. if (!(getTargetDataProvider() instanceof ListDataProvider)) {
  421. throwUnsupportedOperationExceptionForUnsupportedDataProvider(false);
  422. }
  423. if (!(((ListDataProvider<T>) getTargetDataProvider())
  424. .getItems() instanceof List)) {
  425. throwUnsupportedOperationExceptionForUnsupportedCollectionInListDataProvider(
  426. false);
  427. }
  428. }
  429. private DataProvider<T, ?> getSourceDataProvider() {
  430. return getGridDragSource().getGrid().getDataProvider();
  431. }
  432. private DataProvider<T, ?> getTargetDataProvider() {
  433. return getGridDropTarget().getGrid().getDataProvider();
  434. }
  435. private static void throwUnsupportedOperationExceptionForUnsupportedDataProvider(
  436. boolean sourceGrid) {
  437. throw new UnsupportedOperationException(
  438. new StringBuilder().append(sourceGrid ? "Source " : "Target ")
  439. .append("grid does not have a ListDataProvider, cannot automatically ")
  440. .append(sourceGrid ? "remove " : "add ")
  441. .append("items. Use GridRowDragger.set")
  442. .append(sourceGrid ? "Source" : "Target")
  443. .append("DataProviderUpdater(...) ")
  444. .append(sourceGrid ? ""
  445. : "and setDropIndexCalculator(...) "
  446. + "to customize how to handle updating the data provider.")
  447. .toString());
  448. }
  449. private static void throwUnsupportedOperationExceptionForUnsupportedCollectionInListDataProvider(
  450. boolean sourceGrid) {
  451. throw new UnsupportedOperationException(new StringBuilder()
  452. .append(sourceGrid ? "Source " : "Target ")
  453. .append("grid's ListDataProvider is not backed by a List-collection, cannot ")
  454. .append(sourceGrid ? "remove " : "add ")
  455. .append("items. Use a ListDataProvider backed by a List, or use GridRowDragger.set")
  456. .append(sourceGrid ? "Source" : "Target")
  457. .append("DataProviderUpdater(...) ")
  458. .append(sourceGrid ? "" : "and setDropIndexCalculator(...) ")
  459. .append(" to customize how to handle updating the data provider to customize how to handle updating the data provider.")
  460. .toString());
  461. }
  462. }