To drop some text onto a treeitem of treeViewer and further populate it into treeitem

217 views Asked by At

I want to drag text from a tableViewer, drop onto a TreeViewer, and populate the dragged text into the selected treeitem. Drag source and drop target is set. The first encountered problem is that the item of DropTargetEvent is null, so I can't know which treeitem text is dropped onto.

Code of drop method fro Drop target is as following:

public void drop(DropTargetEvent event) {
   System.out.println("Droped" + event);
   if (event.data == null) { 
     event.detail = DND.DROP_NONE;
    }
    //TreeItem item = (TreeItem) event.item;
   Tree xmlTree = xmlVisualEditor.getTree();  //to get the target tree
   Point selectedPoint = new Point(event.x, event.y);
   TreeItem item  = xmlTree.getItem(selectedPoint);    #<----- Null pointer exception

   if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
      String dropedData = (String) event.data;
      System.out.println("Dropped Data:" + dropedData);
      item.setText(dropedData); 
   }

}

I print the content of drop target event, it shows as:

DropTargetEvent{DropTarget {} time=141201730 data=students-name x=861 y=393 item=null operations=3 operation=1 feedback=1 dataTypes={ 13 1 } currentDataType=13}

As it can beesen, item is null.

About next to do, I need to

  1. get the treeitem where it is dropped.

  2. populate the dragged text into one column of dropped treeitem

Any comments about approaching a solution is welcome. Thanks a lot.

1

There are 1 answers

0
greg-449 On

For a TreeViewer you should extend org.eclipse.jface.viewers.ViewerDropAdapter and implement the performDrop method. Use TreeViewer.addDropSupport to install the adapter.

In performDrop a call to getCurrentTarget() will get you the target object.