Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

GridDragAndDrop.java 4.0KB

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