使用 gwt dnd 在 GWT 中拖放

发布于 2024-09-04 20:27:55 字数 2818 浏览 10 评论 0原文

我一直在努力让拖放在 GWT 中工作。过去三天,我试图创建一个基本的拖放应用程序,但失败了。目前我可以拖动它,但无法将其拖放到任何位置。

  1. 如何解决?我们是否需要修改 onDragEnd - 我的印象是,除非我特别必须做某事,否则我不需要这样做?我很困惑。

  2. 另外,如何将掉落限制在单个区域?我确实知道我们可以使用 DropController 来做到这一点。但是我已经使用 UiBinder 定义了面板,那么如何让该面板重新链接到 DropController 中呢?即 RootPanel.get() 为我提供了基本的根面板,而不是我想要的实际面板。我尝试了 RootPanel.get("field-id"),但即使该 id 可用,也显示为 null。我做错了什么?

我编写的代码如下:

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());
}
}

testpanel uibinder 如下所示:

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

如果有人可以帮助我,我将非常感激。

K

P.S:解释更多:我能够通过更新 onDragEnd 来解决第一个问题,如下所示:

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

但是,我不确定这是否是正确的解决方案 - 因为我认为我不应该自己进行定位。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如何视而不见 2024-09-11 20:27:55

如果您是 GWT dnd 的新手,为什么不尝试 工作演示

有很多示例并且所有源代码都可用。

(不,你不应该自己进行定位)

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)

您的好友蓝忘机已上羡 2024-09-11 20:27:55

您必须在放置目标上添加 DragOverHandler:即使它不执行任何操作,也会将组件定义为放置目标。

当然,您仍然需要在此组件上定义 DropHandler(通常还可以选择定义用于视觉反馈的 DragEnterHandler 和 DragLeaveHandler)。

即使未到达目标(在非放置区域中放弃拖动)也会调用 DragEndHandler,它用于更改拖动对象的状态,您可能需要为 DropHandler 设置一种方式来传达放置成功的信息到 DragEndHandler(共享变量、EventBus 等)。

You have to add a DragOverHandler on the drop target(s): even if it does nothing, it defines the component as a drop target.

Of course, you still need to define the DropHandler too on this component (and optionally, DragEnterHandler and DragLeaveHandler for visual feedback, in general).

The DragEndHandler is called even if the target isn't reached (drag abandoned in a non-drop area), it is used to change the state of the dragged object, you might need to set a way for the DropHandler to communicate success on dropping to the DragEndHandler (shared variable, EventBus, etc.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文