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

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