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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.server.Page;
  22. import com.vaadin.server.VaadinRequest;
  23. import com.vaadin.shared.ui.grid.DropMode;
  24. import com.vaadin.tests.components.AbstractTestUIWithLog;
  25. import com.vaadin.ui.Grid;
  26. import com.vaadin.ui.GridDragSource;
  27. import com.vaadin.ui.GridDropTarget;
  28. import com.vaadin.ui.HorizontalLayout;
  29. import com.vaadin.ui.Layout;
  30. import com.vaadin.ui.RadioButtonGroup;
  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, "left"));
  39. dragSourceComponent.addColumn(Bean::getId).setCaption("ID");
  40. dragSourceComponent.addColumn(Bean::getValue).setCaption("Value");
  41. GridDragSource<Bean> dragSource = new GridDragSource<>(
  42. dragSourceComponent);
  43. dragSource.setDragDataGenerator(bean -> {
  44. JsonObject ret = Json.createObject();
  45. ret.put("generatedId", bean.getId());
  46. ret.put("generatedValue", bean.getValue());
  47. return ret;
  48. });
  49. // Drop target Grid
  50. Grid<Bean> dropTargetComponent = new Grid<>();
  51. dropTargetComponent.setItems(createItems(5, "right"));
  52. dropTargetComponent.addColumn(Bean::getId).setCaption("ID");
  53. dropTargetComponent.addColumn(Bean::getValue).setCaption("Value");
  54. GridDropTarget<Bean> dropTarget = new GridDropTarget<>(
  55. dropTargetComponent, DropMode.ON_TOP);
  56. dropTarget.addGridDropListener(event -> {
  57. log(event.getDataTransferText() + ", targetId=" + event
  58. .getDropTargetRow().getId() + ", location=" + event
  59. .getDropLocation());
  60. });
  61. // Layout the two grids
  62. Layout grids = new HorizontalLayout();
  63. grids.addComponents(dragSourceComponent, dropTargetComponent);
  64. // Selection modes
  65. List<Grid.SelectionMode> selectionModes = Arrays.asList(
  66. Grid.SelectionMode.SINGLE, Grid.SelectionMode.MULTI);
  67. RadioButtonGroup<Grid.SelectionMode> selectionModeSelect = new RadioButtonGroup<>(
  68. "Selection mode", selectionModes);
  69. selectionModeSelect.setSelectedItem(Grid.SelectionMode.SINGLE);
  70. selectionModeSelect.addValueChangeListener(event -> dragSourceComponent
  71. .setSelectionMode(event.getValue()));
  72. // Drop locations
  73. List<DropMode> dropLocations = Arrays.asList(DropMode.values());
  74. RadioButtonGroup<DropMode> dropLocationSelect = new RadioButtonGroup<>(
  75. "Allowed drop location", dropLocations);
  76. dropLocationSelect.setSelectedItem(DropMode.ON_TOP);
  77. dropLocationSelect.addValueChangeListener(
  78. event -> dropTarget.setDropMode(event.getValue()));
  79. Layout controls = new HorizontalLayout(selectionModeSelect,
  80. dropLocationSelect);
  81. addComponents(controls, grids);
  82. // Set dragover styling
  83. Page.getCurrent().getStyles().add(".v-drag-over {color: red;}");
  84. }
  85. private List<Bean> createItems(int num, String prefix) {
  86. List<Bean> items = new ArrayList<>(num);
  87. IntStream.range(0, num)
  88. .forEach(i -> items
  89. .add(new Bean(prefix + "_" + i, "value_" + i)));
  90. return items;
  91. }
  92. public static class Bean {
  93. private String id;
  94. private String value;
  95. public Bean(String id, String value) {
  96. this.id = id;
  97. this.value = value;
  98. }
  99. public String getId() {
  100. return id;
  101. }
  102. public void setId(String id) {
  103. this.id = id;
  104. }
  105. public String getValue() {
  106. return value;
  107. }
  108. public void setValue(String value) {
  109. this.value = value;
  110. }
  111. }
  112. }