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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.Optional;
  18. import com.vaadin.event.dnd.DragSourceExtension;
  19. import com.vaadin.event.dnd.DropTargetExtension;
  20. import com.vaadin.server.Extension;
  21. import com.vaadin.server.Page;
  22. import com.vaadin.server.VaadinRequest;
  23. import com.vaadin.tests.components.AbstractTestUIWithLog;
  24. import com.vaadin.ui.Component;
  25. import com.vaadin.ui.HorizontalLayout;
  26. import com.vaadin.ui.Label;
  27. public class DragAndDropCardShuffle extends AbstractTestUIWithLog {
  28. // Data type for storing card position
  29. private static final String DATA_INDEX = "index";
  30. // Create cards
  31. private final Label ace = new Label("A");
  32. private final Label jack = new Label("J");
  33. private final Label queen = new Label("Q");
  34. private final Label king = new Label("K");
  35. // Create desk
  36. private final HorizontalLayout desk = new HorizontalLayout();
  37. @Override
  38. protected void setup(VaadinRequest request) {
  39. // Create UI and add extensions
  40. desk.addComponents(ace, jack, queen, king);
  41. ace.setStyleName("card");
  42. addDragSourceExtension(ace);
  43. addDropTargetExtension(ace);
  44. jack.setStyleName("card");
  45. addDragSourceExtension(jack);
  46. addDropTargetExtension(jack);
  47. queen.setStyleName("card");
  48. addDragSourceExtension(queen);
  49. addDropTargetExtension(queen);
  50. king.setStyleName("card");
  51. addDragSourceExtension(king);
  52. addDropTargetExtension(king);
  53. addComponent(desk);
  54. // Add styling
  55. setStyle();
  56. }
  57. private void addDragSourceExtension(Label source) {
  58. // Create and attach extension
  59. DragSourceExtension dragSource = new DragSourceExtension();
  60. dragSource.extend(source);
  61. // Set component position as transfer data
  62. dragSource.setTransferData(DATA_INDEX,
  63. String.valueOf(desk.getComponentIndex(source)));
  64. // Add listeners
  65. dragSource.addDragStartListener(event -> {
  66. event.getComponent().addStyleName("dragged");
  67. log(((Label) event.getComponent()).getValue() + " dragstart");
  68. });
  69. dragSource.addDragEndListener(event -> {
  70. event.getComponent().removeStyleName("dragged");
  71. log(((Label) event.getComponent()).getValue() + " dragend");
  72. });
  73. }
  74. private void addDropTargetExtension(Label target) {
  75. // Create and attach extension
  76. DropTargetExtension dropTarget = new DropTargetExtension();
  77. dropTarget.extend(target);
  78. // Add listener
  79. dropTarget.addDropListener(event -> {
  80. // Retrieve the source's position
  81. int sourceIndex = Integer
  82. .valueOf(event.getTransferData(DATA_INDEX));
  83. // Find source component
  84. Component source = desk.getComponent(sourceIndex);
  85. // Swap source and target components
  86. desk.replaceComponent(target, source);
  87. // Get DragSource extension for target component and set position data
  88. Optional<Extension> targetExt = target.getExtensions().stream()
  89. .filter(e -> e instanceof DragSourceExtension).findFirst();
  90. targetExt.ifPresent(extension -> {
  91. ((DragSourceExtension) extension).setTransferData(DATA_INDEX,
  92. String.valueOf(desk.getComponentIndex(target)));
  93. });
  94. // Get DragSource extension for source component and set position data
  95. Optional<Extension> sourceExt = source.getExtensions().stream()
  96. .filter(e -> e instanceof DragSourceExtension).findFirst();
  97. sourceExt.ifPresent(extension -> {
  98. ((DragSourceExtension) extension).setTransferData(DATA_INDEX,
  99. String.valueOf(desk.getComponentIndex(source)));
  100. });
  101. log(((Label) source).getValue() + " dropped onto " + ((Label) event
  102. .getComponent()).getValue());
  103. });
  104. }
  105. private void setStyle() {
  106. Page.Styles styles = Page.getCurrent().getStyles();
  107. styles.add(".card {"
  108. + "width: 150px;"
  109. + "height: 200px;"
  110. + "border: 1px solid black;"
  111. + "border-radius: 7px;"
  112. + "padding-left: 10px;"
  113. + "color: red;"
  114. + "font-weight: bolder;"
  115. + "font-size: 25px;"
  116. + "background-color: gainsboro;"
  117. + "}");
  118. styles.add(".v-drag-over {border-style: dashed;}");
  119. styles.add(".dragged {opacity: .4;}");
  120. }
  121. @Override
  122. protected String getTestDescription() {
  123. return "Shuffle cards with pure HTML5 drag and drop";
  124. }
  125. }