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.

GridDropTargetExtension.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.GridDropTargetExtensionRpc;
  23. import com.vaadin.shared.ui.grid.GridDropTargetExtensionState;
  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
  32. */
  33. public class GridDropTargetExtension<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 GridDropTargetExtension(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. {@link
  73. * GridDropListener#drop(GridDropEvent)} is called when drop event happens
  74. * 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. @Override
  85. protected void registerDropTargetRpc(Grid<T> target) {
  86. registerRpc(
  87. (GridDropTargetExtensionRpc) (dataTransferText, rowKey, dropLocation) -> {
  88. T dropTargetRow = target.getDataCommunicator()
  89. .getKeyMapper().get(rowKey);
  90. GridDropEvent<T> event = new GridDropEvent<>(target,
  91. dataTransferText, getUI().getActiveDragSource(),
  92. dropTargetRow, dropLocation);
  93. fireEvent(event);
  94. });
  95. }
  96. @Override
  97. protected GridDropTargetExtensionState getState() {
  98. return (GridDropTargetExtensionState) super.getState();
  99. }
  100. @Override
  101. protected GridDropTargetExtensionState getState(boolean markAsDirty) {
  102. return (GridDropTargetExtensionState) super.getState(markAsDirty);
  103. }
  104. }