views:

265

answers:

1

Hi,

I have been really struggling to get Drag and Drop working in GWT. Last 3 days, I was trying to create a basic drag and drop application and failed. Currently I can drag it around, but I am unable to drop to any location.

  1. How can we solve it? Do we need to modify onDragEnd - I am under the impression that unless I specifically have to do something, I dont have to? I am quite confused.

  2. Also, how do I limit the drop to any single area? I do understand that we can do it using DropController. But I have defined the panels using UiBinder, so how do I get that panel back to link in the DropController? i.e. RootPanel.get() gives me the basic root panel and not the actual panel I want. I tried RootPanel.get("field-id"), but that is showing null even if that id is available. What am I doing wrong?

The code I have written 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 testpanel uibinder looks like the following:

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

If somebody can help me out, I will be very much obliged.

K

P.S: To explain more: I was able to solve the first question by updating onDragEnd as the following:

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

but, I am not sure whether this is the correct solution - since I think I should not be doing the positioning myself.

A: 

If you're new to GWT dnd, why don't you try the working demo ?

There is a lot of examples and all the source code is available.

(And no, you're not supposed to do the positionning yourself)

A.Chatellier