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.

GridDropCriteriaScript.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package com.vaadin.tests.components.grid;
  2. import com.vaadin.annotations.Theme;
  3. import com.vaadin.annotations.Widgetset;
  4. import com.vaadin.server.VaadinRequest;
  5. import com.vaadin.shared.ui.ContentMode;
  6. import com.vaadin.shared.ui.grid.DropLocation;
  7. import com.vaadin.shared.ui.grid.DropMode;
  8. import com.vaadin.tests.components.AbstractTestUI;
  9. import com.vaadin.ui.Grid;
  10. import com.vaadin.ui.Label;
  11. import com.vaadin.ui.components.grid.GridDragSource;
  12. import com.vaadin.ui.components.grid.GridDropTarget;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. @Theme("valo")
  16. @Widgetset("com.vaadin.DefaultWidgetSet")
  17. public class GridDropCriteriaScript extends AbstractTestUI {
  18. class GridItem {
  19. public final String caption;
  20. public final DropLocation dropLocation;
  21. public GridItem(String caption, DropLocation dropLocation) {
  22. this.caption = caption;
  23. this.dropLocation = dropLocation;
  24. }
  25. public String getCaption() {
  26. return caption;
  27. }
  28. public DropLocation getDropLocation() {
  29. return dropLocation;
  30. }
  31. }
  32. @Override
  33. protected void setup(VaadinRequest request) {
  34. getUI().setMobileHtml5DndEnabled(true);
  35. final Label label = new Label("<h1>Test for existance of targetElement "
  36. + "and dropLocation in criteriaScript</h1>"
  37. + "<p>Drag one of the grid items.</p>"
  38. + "<p>While dragging, hints in form of lines show "
  39. + "where the item is allowed to be dropped.</p>"
  40. + "<p>Test passed:" + "<ul>"
  41. + "<li>ABOVE: Only a line above the item is displayed while dragging over the item</li>"
  42. + "<li>BELOW: Only a line below the item is displayed while dragging over the item</li>"
  43. + "<li>ON_TOP: Only a border around the item is displayed while dragging over the item</li>"
  44. + "</ul>" + "</p>" + "<p>Test failed:" + "<ul>"
  45. + "<li>otherwise</li>" + "</ul>" + "</p>", ContentMode.HTML);
  46. final Grid<GridItem> grid = new Grid<>();
  47. grid.addColumn(GridItem::getCaption);
  48. grid.setStyleGenerator(
  49. gridItem -> "dropLocation-" + gridItem.getDropLocation());
  50. new GridDragSource<>(grid);
  51. final GridDropTarget<GridItem> dropTarget = new GridDropTarget<>(grid,
  52. DropMode.ON_TOP_OR_BETWEEN);
  53. dropTarget.setDropCriteriaScript(
  54. "return targetElement.classList.contains('dropLocation-' + dropLocation);");
  55. grid.setItems(createItems());
  56. addComponents(label, grid);
  57. }
  58. private List<GridItem> createItems() {
  59. final ArrayList<GridItem> list = new ArrayList<>();
  60. for (int i = 0; i < 10; i++) {
  61. for (DropLocation dropLocation : new DropLocation[] {
  62. DropLocation.ON_TOP, DropLocation.ABOVE,
  63. DropLocation.BELOW }) {
  64. list.add(new GridItem(i + ": " + dropLocation.name(),
  65. dropLocation));
  66. }
  67. }
  68. return list;
  69. }
  70. }