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.

GridDropTarget.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 com.vaadin.event.dnd.DropTargetExtension;
  18. import com.vaadin.event.dnd.grid.GridDropEvent;
  19. import com.vaadin.event.dnd.grid.GridDropListener;
  20. import com.vaadin.shared.Registration;
  21. import com.vaadin.shared.ui.grid.DropMode;
  22. import com.vaadin.shared.ui.grid.GridDropTargetRpc;
  23. import com.vaadin.shared.ui.grid.GridDropTargetState;
  24. /**
  25. * Makes the rows of a Grid HTML5 drop targets. This is the server side
  26. * counterpart of GridDropTargetExtensionConnector.
  27. *
  28. * @param <T>
  29. * Type of the Grid bean.
  30. * @author Vaadin Ltd
  31. * @since 8.1
  32. */
  33. public class GridDropTarget<T> extends DropTargetExtension<Grid<T>> {
  34. /**
  35. * Extends a Grid and makes it's rows drop targets for HTML5 drag and drop.
  36. *
  37. * @param target
  38. * Grid to be extended.
  39. * @param dropMode
  40. * Drop mode that describes the allowed drop locations within the
  41. * Grid's row.
  42. * @see GridDropEvent#getDropLocation()
  43. */
  44. public GridDropTarget(Grid<T> target, DropMode dropMode) {
  45. super(target);
  46. setDropMode(dropMode);
  47. }
  48. /**
  49. * Sets the drop mode of this drop target.
  50. *
  51. * @param dropMode
  52. * Drop mode that describes the allowed drop locations within the
  53. * Grid's row.
  54. * @see GridDropEvent#getDropLocation()
  55. */
  56. public void setDropMode(DropMode dropMode) {
  57. if (dropMode == null) {
  58. throw new IllegalArgumentException("Drop mode cannot be null");
  59. }
  60. getState().dropMode = dropMode;
  61. }
  62. /**
  63. * Gets the drop mode of this drop target.
  64. *
  65. * @return Drop mode that describes the allowed drop locations within the
  66. * Grid's row.
  67. */
  68. public DropMode getDropMode() {
  69. return getState(false).dropMode;
  70. }
  71. /**
  72. * Attaches drop listener for the current drop target.
  73. * {@link GridDropListener#drop(GridDropEvent)} is called when drop event
  74. * happens on the client side.
  75. *
  76. * @param listener
  77. * Listener to handle drop event.
  78. * @return Handle to be used to remove this listener.
  79. */
  80. public Registration addGridDropListener(GridDropListener<T> listener) {
  81. return addListener(GridDropEvent.class, listener,
  82. GridDropListener.DROP_METHOD);
  83. }
  84. /**
  85. * Sets the threshold between drop locations from the top and the bottom of
  86. * a row in pixels.
  87. * <p>
  88. * Dropping an element
  89. * <ul>
  90. * <li>within {@code threshold} pixels from the top of a row results in a
  91. * drop event with {@link com.vaadin.shared.ui.grid.DropLocation#ABOVE
  92. * DropLocation.ABOVE}</li>
  93. * <li>within {@code threshold} pixels from the bottom of a row results in a
  94. * drop event with {@link com.vaadin.shared.ui.grid.DropLocation#BELOW
  95. * DropLocation.BELOW}</li>
  96. * <li>anywhere else within the row results in a drop event with
  97. * {@link com.vaadin.shared.ui.grid.DropLocation#ON_TOP
  98. * DropLocation.ON_TOP}</li>
  99. * </ul>
  100. * The value only has an effect when drop mode is set to
  101. * {@link DropMode#ON_TOP_OR_BETWEEN}.
  102. * <p>
  103. * Default is 5 pixels.
  104. *
  105. * @param threshold
  106. * The threshold from the top and bottom of the row in pixels.
  107. */
  108. public void setDropThreshold(int threshold) {
  109. getState().dropThreshold = threshold;
  110. }
  111. /**
  112. * Gets the threshold between drop locations from the top and the bottom of
  113. * the row.
  114. *
  115. * @return The threshold in pixels.
  116. */
  117. public int getDropThreshold() {
  118. return getState(false).dropThreshold;
  119. }
  120. @Override
  121. protected void registerDropTargetRpc(Grid<T> target) {
  122. registerRpc((GridDropTargetRpc) (dataTransferText, rowKey,
  123. dropLocation) -> {
  124. T dropTargetRow = target.getDataCommunicator().getKeyMapper()
  125. .get(rowKey);
  126. GridDropEvent<T> event = new GridDropEvent<>(target,
  127. dataTransferText, getUI().getActiveDragSource(),
  128. dropTargetRow, dropLocation);
  129. fireEvent(event);
  130. });
  131. }
  132. @Override
  133. protected GridDropTargetState getState() {
  134. return (GridDropTargetState) super.getState();
  135. }
  136. @Override
  137. protected GridDropTargetState getState(boolean markAsDirty) {
  138. return (GridDropTargetState) super.getState(markAsDirty);
  139. }
  140. }