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.2KB

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