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.

VAbstractDropHandler.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.client.ui.dd;
  17. import java.util.Iterator;
  18. import com.google.gwt.user.client.Command;
  19. import com.vaadin.client.ComponentConnector;
  20. import com.vaadin.client.UIDL;
  21. import com.vaadin.client.extensions.DropTargetExtensionConnector;
  22. import com.vaadin.event.Transferable;
  23. import com.vaadin.event.dd.DropTarget;
  24. import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
  25. /**
  26. *
  27. * @author Vaadin Ltd
  28. * @deprecated Replaced in 8.1 with {@link DropTargetExtensionConnector}
  29. */
  30. @Deprecated
  31. public abstract class VAbstractDropHandler implements VDropHandler {
  32. private UIDL criterioUIDL;
  33. private VAcceptCriterion acceptCriteria = new VAcceptAll();
  34. /**
  35. * Implementor/user of {@link VAbstractDropHandler} must pass the UIDL
  36. * painted by {@link AcceptCriterion} to this method. Practically the
  37. * details about {@link AcceptCriterion} are saved.
  38. *
  39. * @param uidl
  40. */
  41. public void updateAcceptRules(UIDL uidl) {
  42. criterioUIDL = uidl;
  43. /*
  44. * supports updating the accept rule root directly or so that it is
  45. * contained in given uidl node
  46. */
  47. if (!uidl.getTag().equals("-ac")) {
  48. Iterator<Object> childIterator = uidl.iterator();
  49. while (!uidl.getTag().equals("-ac") && childIterator.hasNext()) {
  50. uidl = (UIDL) childIterator.next();
  51. }
  52. }
  53. acceptCriteria = VAcceptCriteria.get(uidl.getStringAttribute("name"));
  54. if (acceptCriteria == null) {
  55. throw new IllegalArgumentException(
  56. "No accept criteria found with given name "
  57. + uidl.getStringAttribute("name"));
  58. }
  59. }
  60. /**
  61. * Default implementation does nothing.
  62. */
  63. @Override
  64. public void dragOver(VDragEvent drag) {
  65. }
  66. /**
  67. * Default implementation does nothing. Implementors should clean possible
  68. * emphasis or drag icons here.
  69. */
  70. @Override
  71. public void dragLeave(VDragEvent drag) {
  72. }
  73. /**
  74. * The default implementation in {@link VAbstractDropHandler} checks if the
  75. * Transferable is accepted.
  76. * <p>
  77. * If transferable is accepted (either via server visit or client side
  78. * rules) the default implementation calls abstract
  79. * {@link #dragAccepted(VDragEvent)} method.
  80. * <p>
  81. * If drop handler has distinct places where some parts may accept the
  82. * {@link Transferable} and others don't, one should use similar validation
  83. * logic in dragOver method and replace this method with empty
  84. * implementation.
  85. *
  86. */
  87. @Override
  88. public void dragEnter(final VDragEvent drag) {
  89. validate(event -> dragAccepted(drag), drag);
  90. }
  91. /**
  92. * This method is called when a valid drop location was found with
  93. * {@link AcceptCriterion} either via client or server side check.
  94. * <p>
  95. * Implementations can set some hints for users here to highlight that the
  96. * drag is on a valid drop location.
  97. *
  98. * @param drag
  99. */
  100. protected abstract void dragAccepted(VDragEvent drag);
  101. protected void validate(final VAcceptCallback cb, final VDragEvent event) {
  102. Command checkCriteria = () -> acceptCriteria.accept(event, criterioUIDL,
  103. cb);
  104. VDragAndDropManager.get().executeWhenReady(checkCriteria);
  105. }
  106. boolean validated = false;
  107. /**
  108. * The default implemmentation visits server if {@link AcceptCriterion}
  109. * can't be verified on client or if {@link AcceptCriterion} are met on
  110. * client.
  111. */
  112. @Override
  113. public boolean drop(VDragEvent drag) {
  114. if (acceptCriteria.needsServerSideCheck(drag, criterioUIDL)) {
  115. return true;
  116. } else {
  117. validated = false;
  118. acceptCriteria.accept(drag, criterioUIDL,
  119. event -> validated = true);
  120. return validated;
  121. }
  122. }
  123. /**
  124. * Returns the Paintable who owns this {@link VAbstractDropHandler}. Server
  125. * side counterpart of the Paintable is expected to implement
  126. * {@link DropTarget} interface.
  127. */
  128. @Override
  129. public abstract ComponentConnector getConnector();
  130. }