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
get the treeitem where it is dropped.
populate the dragged text into one column of dropped treeitem
Any comments about approaching a solution is welcome. Thanks a lot.
For a
TreeViewer
you should extendorg.eclipse.jface.viewers.ViewerDropAdapter
and implement theperformDrop
method. UseTreeViewer.addDropSupport
to install the adapter.In
performDrop
a call togetCurrentTarget()
will get you the target object.