0
votes

I want to handle the drag & drop of hyperlinks in my app. The hyperlink could be from any where, therefore I cannot make it setDragable(true) and setData("link", "the URL") to mark it.

A very similar scenario would be Google Image search by image, where you can drag & drop a link of image to the search box.

The sample code,

Label lblDropLink = new Label("Drop a link here");
lblDropLink.addDragOverHandler(new DragOverHandler() {

    @Override
    public void onDragOver(DragOverEvent event) {
        lblDropLink.setText("Drop here to add the link.");
        lblDropLink.setStyleName("dragOverFade");
    }
});
lblDropLink.addDropHandler(new DropHandler() {

    @Override
    public void onDrop(DropEvent event) {
        event.preventDefault();

        // QUESTION: how to get the link, and even its text?
    }
});

Thanks!

2

2 Answers

0
votes

The only thing you can get when dropping a link is the URL!

You can get it by calling the event.getData(format) method.

format can either be "text" or "url" (see dom-datatransfer-getdata). When testing it "text" and "url" always deliverd the same result.

So the code you need in "// QUESTION: how to get the link, and even its text?" is one of the two

  • event.getData("text")
  • event.getData("url")

Here is a little sample prgramm:

    final Label lblDropLink = new Label(defaultText);
    lblDropLink.addDragOverHandler(new DragOverHandler() {

        @Override
        public void onDragOver(DragOverEvent event) {
            lblDropLink.setText(dragOverText);
        }
    });

    lblDropLink.addDropHandler(new DropHandler() {

        @Override
        public void onDrop(DropEvent event) {
            event.preventDefault();

            RootPanel.get().add(new Label("Dropped source formated with \"text\": " + event.getData("text")));
            RootPanel.get().add(new Label("Dropped source formated with \"url\":  " + event.getData("url")));
        }
    });

    RootPanel.get().add(lblDropLink);
}

That should be all...

0
votes

There is a function called getSource(). Use it to cast the source of the dragged object and get the link property of the source object in the onDrop event.

Example:

 public void onDrop(DropEvent event) {
     event.preventDefault();
     Link link = (Link)event.getSource(); 
 }

Link is the object being dragged. Then you can call Link.getLink() or whatever you use to get the value of the link.

Hpope you get the concept :)