Tuesday, April 21, 2009

File drag functionality for a tree in SWT

Below is a small snippet of impementing filedrag functionality for a tree in SWT

int operations = DND.DROP_DEFAULT | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_MOVE;
DragSource dragSource = new DragSource(tree, operations);
Transfer[] types = new Transfer[] { FileTransfer.getInstance() };
dragSource.setTransfer(types);
dragSource.addDragListener(new DragSourceListener() {

TreeItem[] dndSelection = null;
String[] sourceNames = null;

public void dragStart(DragSourceEvent event) {
dndSelection = ExporttargetTree.getSelection();
sourceNames = null;
event.doit = dndSelection.length > 0;
}

public void dragFinished(DragSourceEvent event) {
dndSelection = null;
sourceNames = null;
}

public void dragSetData(DragSourceEvent event) {
if (dndSelection == null || dndSelection.length == 0)
return;
if (!FileTransfer.getInstance().isSupportedType(event.dataType))
return;
for (int i = 0; i <>
File file = (File) dndSelection[i].getData();
sourceNames[i] = file.getAbsolutePath();
}
event.data = sourceNames;
}
});

No comments:

Post a Comment