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.

DragAndDropCardShuffle.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.tests.dnd;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import com.vaadin.annotations.Theme;
  20. import com.vaadin.annotations.Widgetset;
  21. import com.vaadin.server.Page;
  22. import com.vaadin.server.VaadinRequest;
  23. import com.vaadin.shared.ui.dnd.criteria.ComparisonOperator;
  24. import com.vaadin.shared.ui.dnd.DropEffect;
  25. import com.vaadin.shared.ui.dnd.EffectAllowed;
  26. import com.vaadin.tests.components.AbstractTestUIWithLog;
  27. import com.vaadin.ui.CheckBox;
  28. import com.vaadin.ui.HorizontalLayout;
  29. import com.vaadin.ui.Label;
  30. import com.vaadin.ui.NativeSelect;
  31. import com.vaadin.ui.dnd.DragSourceExtension;
  32. import com.vaadin.ui.dnd.DropTargetExtension;
  33. @Theme("valo")
  34. @Widgetset("com.vaadin.DefaultWidgetSet")
  35. public class DragAndDropCardShuffle extends AbstractTestUIWithLog {
  36. // Create cards
  37. private final Label ace = new Label("A");
  38. private final Label jack = new Label("J");
  39. private final Label queen = new Label("Q");
  40. private final Label king = new Label("K");
  41. // Create desk
  42. private HorizontalLayout desk = new HorizontalLayout();
  43. private final List<DragSourceExtension<Label>> sources = new ArrayList<>();
  44. private final List<DropTargetExtension<Label>> targets = new ArrayList<>();
  45. @Override
  46. protected void setup(VaadinRequest request) {
  47. NativeSelect<EffectAllowed> effectAllowed = new NativeSelect<>(
  48. "Effect Allowed (source)");
  49. effectAllowed.setItems(EffectAllowed.values());
  50. effectAllowed.setValue(EffectAllowed.UNINITIALIZED);
  51. effectAllowed.setEmptySelectionAllowed(false);
  52. effectAllowed.addValueChangeListener(event -> sources
  53. .forEach(source -> source.setEffectAllowed(event.getValue())));
  54. NativeSelect<DropEffect> dropEffect = new NativeSelect<>(
  55. "Drop Effect (target)");
  56. dropEffect.setItems(DropEffect.values());
  57. dropEffect.addValueChangeListener(event -> targets
  58. .forEach(target -> target.setDropEffect(event.getValue())));
  59. CheckBox enableMobileSupport = new CheckBox("Mobile Support", false);
  60. enableMobileSupport.addValueChangeListener(event -> {
  61. setMobileHtml5DndEnabled(event.getValue());
  62. removeExtensions();
  63. setupExtensions();
  64. });
  65. setupExtensions();
  66. desk.addComponents(ace, jack, queen, king);
  67. addComponents(new HorizontalLayout(effectAllowed, dropEffect,
  68. enableMobileSupport), desk);
  69. // Add styling
  70. setStyle();
  71. }
  72. private void setupExtensions() {
  73. // Create UI and add extensions
  74. ace.setStyleName("card");
  75. addDragSourceExtension(ace, 14);
  76. addDropTargetExtension(ace, 14);
  77. jack.setStyleName("card");
  78. addDragSourceExtension(jack, 11);
  79. addDropTargetExtension(jack, 11);
  80. queen.setStyleName("card");
  81. addDragSourceExtension(queen, 12);
  82. addDropTargetExtension(queen, 12);
  83. king.setStyleName("card");
  84. addDragSourceExtension(king, 13);
  85. addDropTargetExtension(king, 13);
  86. }
  87. private void removeExtensions() {
  88. ace.removeExtension(ace.getExtensions().iterator().next());
  89. ace.removeExtension(ace.getExtensions().iterator().next());
  90. jack.removeExtension(jack.getExtensions().iterator().next());
  91. jack.removeExtension(jack.getExtensions().iterator().next());
  92. queen.removeExtension(queen.getExtensions().iterator().next());
  93. queen.removeExtension(queen.getExtensions().iterator().next());
  94. king.removeExtension(king.getExtensions().iterator().next());
  95. king.removeExtension(king.getExtensions().iterator().next());
  96. }
  97. private void addDragSourceExtension(Label source, int cardValue) {
  98. // Create and attach extension
  99. DragSourceExtension<Label> dragSource = new DragSourceExtension<>(
  100. source);
  101. // Set card value via criteria API for acceptance criteria
  102. dragSource.setPayload("card_value", cardValue);
  103. // Add listeners
  104. dragSource.addDragStartListener(event -> {
  105. log(event.getComponent().getValue() + " dragstart, effectsAllowed="
  106. + event.getEffectAllowed());
  107. });
  108. dragSource.addDragEndListener(event -> {
  109. log(event.getComponent().getValue() + " dragend, dropEffect="
  110. + event.getDropEffect());
  111. });
  112. sources.add(dragSource);
  113. }
  114. private void addDropTargetExtension(Label target, int cardValue) {
  115. // Create and attach extension
  116. DropTargetExtension<Label> dropTarget = new DropTargetExtension<>(
  117. target);
  118. // Cards can be dropped onto others with smaller value
  119. dropTarget.setDropCriterion("card_value", ComparisonOperator.SMALLER_THAN,
  120. cardValue);
  121. // Add listener
  122. dropTarget.addDropListener(event -> {
  123. event.getDragSourceExtension().ifPresent(dragSource -> {
  124. if (dragSource.getParent() instanceof Label) {
  125. Label source = (Label) dragSource.getParent();
  126. // Swap source and target components
  127. desk.replaceComponent(target, source);
  128. log(event.getComponent().getValue() + " drop received "
  129. + source.getValue() + ", dropEffect="
  130. + event.getDropEffect() + ", mouseEventDetails="
  131. + event.getMouseEventDetails());
  132. } else {
  133. log(event.getComponent().getValue()
  134. + " drop received something else than card");
  135. }
  136. });
  137. });
  138. targets.add(dropTarget);
  139. }
  140. private void setStyle() {
  141. Page.Styles styles = Page.getCurrent().getStyles();
  142. styles.add(".card {" + "width: 150px;" + "height: 200px;"
  143. + "border: 1px solid black;" + "border-radius: 7px;"
  144. + "padding-left: 10px;" + "color: red;" + "font-weight: bolder;"
  145. + "font-size: 25px;" + "background-color: gainsboro;" + "}");
  146. styles.add(".v-label-drag-center {border-style: dashed;}");
  147. styles.add(".v-label-dragged {opacity: .4;}");
  148. }
  149. @Override
  150. protected String getTestDescription() {
  151. return "Shuffle cards with pure HTML5 drag and drop";
  152. }
  153. }