0

I followed the instructions given here: Drag and Drop in GWT 2.4

It works well, but I now want to select between many objects which to transfer. How can it be determined which object is dragged?

Thank you in advance!

Community
  • 1
  • 1
girl
  • 19
  • 8
  • Will it be the object which received last mouse down / click event? – appbootup Jan 23 '13 at 14:55
  • Yes. I want to be able to get which object fired the DragOverHandler event. – girl Jan 23 '13 at 15:08
  • DragOverEvent has getSource(), getRelativeElement() api. Try using them. – appbootup Jan 23 '13 at 15:46
  • I tried to get the source and cast it to a Label (I try to drag and drop labels) but I get the following error: Exception caught: a.client.DropAbsolutePanel cannot be cast to a.client.Label – girl Jan 23 '13 at 18:14

1 Answers1

0

I would do the following:

1) Create a map with objects allowed for dragging (Labels in your example)

HashMap <String, Label> draggableWidgetsMap = new HashMap<String, Label>();

draggableWidgetsMap.add("key1", widget1);
...
draggableWidgetsMap.add("keyN", widgetN);

2) Then apply the dragStart event handler for each widget, e.g. for widget1 it would be:

        widget1.addDomHandler(new DragStartHandler() {
            @Override
            public void onDragStart(DragStartEvent event) {
                event.setData("widgetKey", "key1");
            }
        }, DragStartEvent.getType());

3) And then you can check what widget is dragged now in DragOver event handler:

    widget1.addDomHandler(new DragOverHandler() {
                @Override
                public void onDragOver(DragOverEvent event) {
                    String widgetKey = event.getDataTransfer().getData("widgetKey");
                    if (widgetKey != null && widgetKey.length() > 0){
                       if (draggableWidgetsMap.containsKey(widgetKey)){
                          // Print out Label's text to see which one is dragged
                          System.out.println("Dragged label has text: \" " + draggableWidgetsMap.get(widgetKey).getText() + "\"")
                          // do something relevant with this Label
                       }
                    }

                }
            }, DragOverEvent.getType());

Certainly, I would loop through all the widgets to apply both handlers for the widgets you are going to make draggable.

alexp
  • 787
  • 5
  • 26