Search in sources :

Example 1 with GridDragStartEvent

use of com.vaadin.flow.component.grid.dnd.GridDragStartEvent in project flow-components by vaadin.

the class GridDnDTest method gridDnD_genericDnD_activeDragComponentAndDataSet.

@Test
public void gridDnD_genericDnD_activeDragComponentAndDataSet() {
    List<String> dragData = Collections.singletonList("2");
    JsonObject object = Json.createObject();
    JsonArray array = Json.createArray();
    JsonObject item = Json.createObject();
    item.put("key", grid.getDataCommunicator().getKeyMapper().key("2"));
    array.set(0, item);
    object.put("draggedItems", array);
    GridDragStartEvent<String> startEvent = new GridDragStartEvent<String>(grid, true, object);
    ComponentUtil.fireEvent(grid, startEvent);
    Assert.assertEquals("No active drag source set", grid, ui.getActiveDragSourceComponent());
    Assert.assertEquals("No drag data set", dragData, ComponentUtil.getData(grid, Grid.DRAG_SOURCE_DATA_KEY));
    AtomicReference<DropEvent<RouterLink>> eventCapture = new AtomicReference<>();
    RouterLink routerLink = new RouterLink() {

        @Override
        public Optional<UI> getUI() {
            return Optional.of(ui);
        }
    };
    DropTarget.create(routerLink).addDropListener(eventCapture::set);
    ComponentUtil.fireEvent(routerLink, new DropEvent<RouterLink>(routerLink, true, EffectAllowed.ALL.getClientPropertyValue()));
    DropEvent<RouterLink> dropEvent = eventCapture.get();
    Assert.assertEquals("Incorrect drag data", dragData, dropEvent.getDragData().get());
    Assert.assertEquals("Incorrect drag source", grid, dropEvent.getDragSourceComponent().get());
    ComponentUtil.fireEvent(grid, new GridDragEndEvent<>(grid, true));
    Assert.assertNull("Active drag source not cleared", ui.getActiveDragSourceComponent());
    Assert.assertNull("Drag data not cleared", ComponentUtil.getData(grid, Grid.DRAG_SOURCE_DATA_KEY));
}
Also used : DropEvent(com.vaadin.flow.component.dnd.DropEvent) GridDragStartEvent(com.vaadin.flow.component.grid.dnd.GridDragStartEvent) JsonObject(elemental.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) JsonArray(elemental.json.JsonArray) RouterLink(com.vaadin.flow.router.RouterLink) UI(com.vaadin.flow.component.UI) Test(org.junit.Test)

Aggregations

UI (com.vaadin.flow.component.UI)1 DropEvent (com.vaadin.flow.component.dnd.DropEvent)1 GridDragStartEvent (com.vaadin.flow.component.grid.dnd.GridDragStartEvent)1 RouterLink (com.vaadin.flow.router.RouterLink)1 JsonArray (elemental.json.JsonArray)1 JsonObject (elemental.json.JsonObject)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1