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.

DropEvent.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.event.dnd;
  17. import java.util.Optional;
  18. import com.vaadin.shared.ui.dnd.DropEffect;
  19. import com.vaadin.ui.AbstractComponent;
  20. import com.vaadin.ui.Component;
  21. /**
  22. * Server side drop event. Fired when an HTML5 drop happens.
  23. *
  24. * @param <T>
  25. * Type of the drop target component.
  26. * @author Vaadin Ltd
  27. * @see DropTargetExtension#addDropListener(DropListener)
  28. * @since 8.1
  29. */
  30. public class DropEvent<T extends AbstractComponent> extends Component.Event {
  31. private final String dataTransferText;
  32. private final DropEffect dropEffect;
  33. private final DragSourceExtension<? extends AbstractComponent> dragSourceExtension;
  34. private final AbstractComponent dragSource;
  35. /**
  36. * Creates a server side drop event.
  37. *
  38. * @param target
  39. * Component that received the drop.
  40. * @param dataTransferText
  41. * Data of type {@code "text"} from the {@code DataTransfer}
  42. * object.
  43. * @param dropEffect
  44. * Drop effect from {@code DataTransfer.dropEffect} object.
  45. * @param dragSourceExtension
  46. * Drag source extension of the component that initiated the drop
  47. * event.
  48. */
  49. public DropEvent(T target, String dataTransferText, DropEffect dropEffect,
  50. DragSourceExtension<? extends AbstractComponent> dragSourceExtension) {
  51. super(target);
  52. this.dataTransferText = dataTransferText;
  53. this.dropEffect = dropEffect;
  54. this.dragSourceExtension = dragSourceExtension;
  55. this.dragSource = Optional.ofNullable(dragSourceExtension)
  56. .map(DragSourceExtension::getParent).orElse(null);
  57. }
  58. /**
  59. * Get data of type {@code "text"} from the client side {@code DataTransfer}
  60. * object.
  61. *
  62. * @return Data of type {@code "text"} if exists in the client side {@code
  63. * DataTransfer} object, otherwise {@literal null}.
  64. */
  65. public String getDataTransferText() {
  66. return dataTransferText;
  67. }
  68. /**
  69. * Get drop effect set for the current drop target.
  70. *
  71. * @return {@code dropEffect} parameter set for the current drop target.
  72. */
  73. public DropEffect getDropEffect() {
  74. return dropEffect;
  75. }
  76. /**
  77. * Returns the drag source component if the drag originated from a
  78. * component in the same UI as the drop target component, or an empty
  79. * optional.
  80. *
  81. * @return Drag source component or an empty optional.
  82. */
  83. public Optional<AbstractComponent> getDragSourceComponent() {
  84. return Optional.ofNullable(dragSource);
  85. }
  86. /**
  87. * Returns the extension of the drag source component if the drag
  88. * originated from a component in the same UI as the drop target component,
  89. * or an empty optional.
  90. *
  91. * @return Drag source extension or an empty optional
  92. */
  93. public Optional<DragSourceExtension<? extends AbstractComponent>> getDragSourceExtension() {
  94. return Optional.ofNullable(dragSourceExtension);
  95. }
  96. /**
  97. * Gets the server side drag data. This data can be set during the drag
  98. * start event on the server side and can be used to transfer data between
  99. * drag source and drop target when they are in the same UI.
  100. *
  101. * @return Optional server side drag data if set and the drag source and the
  102. * drop target are in the same UI, otherwise empty {@code Optional}.
  103. * @see DragSourceExtension#setDragData(Object)
  104. */
  105. public Optional<Object> getDragData() {
  106. return getDragSourceExtension().map(DragSourceExtension::getDragData);
  107. }
  108. /**
  109. * Returns the drop target component where the drop event occurred.
  110. *
  111. * @return Component on which a drag source was dropped.
  112. */
  113. @Override
  114. @SuppressWarnings("unchecked")
  115. public T getComponent() {
  116. return (T) super.getComponent();
  117. }
  118. }