Drag and Drop in GWT with gwt dnd

I'm really trying to get Drag and Drop to work in GWT. For the last 3 days, I tried to create a basic drag and drop application and failed. Currently, I can drag it, but I can’t get anywhere.

  • How can we solve this? Do I need to modify onDragEnd - I get the impression that if I don’t specifically have to do something, I don’t need to? I am very confused.

  • Also, how can I limit the drop to any particular area? I understand that we can do this with DropController. But I defined the panels using UiBinder, so how can I return this panel for a link in DropController? that is, RootPanel.get () gives me the main panel of the root element, and not the actual panel that I want. I tried RootPanel.get ("field-id"), but it shows zero, even if this identifier is available. What am I doing wrong?

The code I wrote is as follows:

public class TestPanel extends Composite implements
DragHandler, HasMouseDownHandlers, HasMouseUpHandlers, HasMouseMoveHandlers, HasMouseOutHandlers {

interface Binder extends UiBinder<Widget, TestPanel> { }
private static final Binder binder = GWT.create(Binder.class);

@UiField AbsolutePanel absolutePanel;

private PickupDragController TestDragController;

private Image img = new Image("./testicon.png");

public TestPanel(){
    initWidget(binder.createAndBindUi(this));
    absolutePanel.add(img);
    TestDragController = new PickupDragController(RootPanel.get(), false);
    AbsolutePositionDropController dropController = new AbsolutePositionDropController(
                                                          RootPanel.get());
    TestDragController.registerDropController(dropController);
    TestDragController.addDragHandler(this);
    TestDragController.makeDraggable(this, getDragHandle());
}

private Widget getDragHandle() {
    return img;
}

@Override
public void onDragEnd(DragEndEvent event) { }

@Override
public void onDragStart(DragStartEvent event) { }

@Override
public void onPreviewDragEnd(DragEndEvent event) throws VetoDragException { }

@Override
public void onPreviewDragStart(DragStartEvent event) throws VetoDragException { }

@Override
public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
    return addDomHandler(handler, MouseDownEvent.getType());
}

@Override
public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
    return addDomHandler(handler, MouseUpEvent.getType());
}

@Override
public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
    return addDomHandler(handler, MouseMoveEvent.getType());
}

@Override
public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
    return addDomHandler(handler, MouseOutEvent.getType());
}
}

and the uibinder test panel looks like this:

<g:AbsolutePanel ui:field="absolutePanel" styleName="{style.panel}">
</g:AbsolutePanel>

If anyone can help me, I will be very grateful.

TO

PS: To explain more: I was able to resolve the first question by updating onDragEnd as follows:

    @Override
public void onDragEnd(DragEndEvent event) {
    DragContext context = event.getContext();
    RootPanel.get().add(context.draggable, context.desiredDraggableX, context.desiredDraggableY);
}

but I’m not sure if this decision is right, because I think that I should not position myself.

+3
2

GWT dnd, ?

.

( , )

0

DragOverHandler : , .

, DropHandler (, , DragEnterHandler DragLeaveHandler ).

DragEndHandler , ( ), , DropHandler DragEndHandler ( , EventBus ..).

0

Source: https://habr.com/ru/post/1749161/


All Articles