[FIXED] Drop dont work after refresh after 14.4.5

Issue

after updating Vaadin from 14.4.4 to any newer version then all my drag&drop components stopped working properly. It is working fine until i refresh browser, then my drag action work but it seems to delete every droptarget ( drop icon is not showing up and drop listeners are never fired). When i change again to version 14.4.4 then drag&drop start working again. video example

    @Route(value = "test1")
@PreserveOnRefresh
@UIScope
public class testMainpanel2 extends Div {
    private static final long serialVersionUID = -4708440555341615506L;
    private List<DragSource<Button>> visCard = new ArrayList<DragSource<Button>>();
    public testMainpanel2() {   
        Button card1 = new Button("card1");
        Button card2 = new Button("card2");     
        addDragDrop(card1);
        addDragDrop(card2);
        add(card1, card2);
    }
    private void addDragDrop(Button comp) {
        DragSource<Button> dragSource = DragSource.create(comp);
        dragSource.setDragData(comp);

        visCard.add(dragSource);
        dragSource.addDragStartListener( event -> getVisibleCards().forEach(target -> {
            Button targetCard = (Button) target.getDragData();
            if (!dragSource.getDragData().equals(targetCard)) {
                targetCard.getElement().getStyle().set("background-color", "red");
            }
        }));
        dragSource.addDragEndListener(event -> {
            getVisibleCards().forEach(target -> ((Button) target.getDragData()).getElement().getStyle().set("background-color", "white"));
        });
        DropTarget<Button> dropTarget = DropTarget.create(comp);

        dropTarget.addDropListener(event -> {
            System.out.println("Dropped target:" + event.getDragData());
        });     
    }
    private List<DragSource<Button>> getVisibleCards() {
        return visCard;
    }
}

Solution

Quick fix for this is to setActive to false and again to true after each UI attach

public interface DropTargetTrans<T extends Component> extends DropTarget<T>{

    @Override
    default void setActive(boolean active) {
        DropTarget.super.setActive(active);
        if(active)
            getElement().addAttachListener( evt ->{
                if(isActive()) {
                    DropTarget.super.setActive(false);
                    DropTarget.super.setActive(true);
                }
            });
    }
}

Answered By – Miłosz Bąbliński

Answer Checked By – Dawn Plyler (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published