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.

GridDragAndDrop.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.components.grid;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import java.util.stream.IntStream;
  21. import com.vaadin.annotations.Widgetset;
  22. import com.vaadin.event.dnd.DropTargetExtension;
  23. import com.vaadin.server.VaadinRequest;
  24. import com.vaadin.shared.ui.grid.GridDragSourceExtensionState;
  25. import com.vaadin.tests.components.AbstractTestUIWithLog;
  26. import com.vaadin.ui.ComboBox;
  27. import com.vaadin.ui.Grid;
  28. import com.vaadin.ui.GridDragSourceExtension;
  29. import com.vaadin.ui.HorizontalLayout;
  30. import com.vaadin.ui.Label;
  31. import com.vaadin.ui.Layout;
  32. import elemental.json.Json;
  33. import elemental.json.JsonObject;
  34. @Widgetset("com.vaadin.DefaultWidgetSet")
  35. public class GridDragAndDrop extends AbstractTestUIWithLog {
  36. @Override
  37. protected void setup(VaadinRequest request) {
  38. // Drag source
  39. Grid<Bean> dragSourceComponent = new Grid<>();
  40. dragSourceComponent.setItems(createItems(50));
  41. dragSourceComponent.addColumn(Bean::getId).setCaption("ID");
  42. dragSourceComponent.addColumn(Bean::getValue).setCaption("Value");
  43. GridDragSourceExtension<Bean> dragSource = new GridDragSourceExtension<>(
  44. dragSourceComponent);
  45. dragSource.setDragDataGenerator(bean -> {
  46. JsonObject ret = Json.createObject();
  47. ret.put("val", bean.getValue());
  48. return ret;
  49. });
  50. Label dropTargetComponent = new Label("Drop here");
  51. DropTargetExtension<Label> dropTarget = new DropTargetExtension<>(
  52. dropTargetComponent);
  53. dropTarget.addDropListener(event -> {
  54. log(event.getTransferData(
  55. GridDragSourceExtensionState.DATA_TYPE_DRAG_DATA));
  56. });
  57. Layout layout = new HorizontalLayout();
  58. layout.addComponents(dragSourceComponent, dropTargetComponent);
  59. // Selection mode combo box
  60. ComboBox<Grid.SelectionMode> selectionModeSwitch = new ComboBox<>(
  61. "Change selection mode");
  62. selectionModeSwitch.setItems(Arrays.asList(Grid.SelectionMode.SINGLE,
  63. Grid.SelectionMode.MULTI));
  64. selectionModeSwitch.setEmptySelectionAllowed(false);
  65. selectionModeSwitch.addValueChangeListener(event -> dragSourceComponent
  66. .setSelectionMode(event.getValue()));
  67. selectionModeSwitch.setSelectedItem(Grid.SelectionMode.SINGLE);
  68. addComponents(selectionModeSwitch, layout);
  69. }
  70. private List<Bean> createItems(int num) {
  71. List<Bean> items = new ArrayList<>(num);
  72. IntStream.range(0, num)
  73. .forEach(i -> items.add(new Bean("id_" + i, "value_" + i)));
  74. return items;
  75. }
  76. public static class Bean {
  77. private String id;
  78. private String value;
  79. public Bean(String id, String value) {
  80. this.id = id;
  81. this.value = value;
  82. }
  83. public String getId() {
  84. return id;
  85. }
  86. public void setId(String id) {
  87. this.id = id;
  88. }
  89. public String getValue() {
  90. return value;
  91. }
  92. public void setValue(String value) {
  93. this.value = value;
  94. }
  95. }
  96. }