Project: SWT
Bug 178735 [DND] Stops working by simply adding an EraseItem Listener in Tree
Reported: 2007-03-22 04:00:40 | Status: resolved fixed

Tested with 3.3 M5 and I20070321-0010 (seems to fail on Vista too). Consider this snippet: import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.DND; import org.eclipse.swt.dnd.DragSource; import org.eclipse.swt.dnd.DragSourceEvent; import org.eclipse.swt.dnd.DragSourceListener; import org.eclipse.swt.dnd.DropTarget; import org.eclipse.swt.dnd.DropTargetAdapter; import org.eclipse.swt.dnd.DropTargetEvent; import org.eclipse.swt.dnd.TextTransfer; import org.eclipse.swt.dnd.Transfer; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeColumn; import org.eclipse.swt.widgets.TreeItem; public class Main { public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final Tree tree = new Tree(shell, SWT.FULL_SELECTION); tree.setHeaderVisible(true); TreeColumn col1 = new TreeColumn(tree, SWT.None); col1.setWidth(100); TreeColumn col2 = new TreeColumn(tree, SWT.None); col2.setWidth(100); for (int k = 0; k < 3; k++) { TreeItem subsubItem = new TreeItem(tree, SWT.NONE); subsubItem.setText(1, "item " + " " + k); } tree.addListener(SWT.EraseItem, new Listener() { public void handleEvent(Event event) { } }); Transfer[] types = new Transfer[] { TextTransfer.getInstance() }; int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK; final DragSource source = new DragSource(tree, operations); source.setTransfer(types); final TreeItem[] dragSourceItem = new TreeItem[1]; source.addDragListener(new DragSourceListener() { public void dragStart(DragSourceEvent event) { TreeItem[] selection = tree.getSelection(); if (selection.length > 0 && selection[0].getItemCount() == 0) { event.doit = true; dragSourceItem[0] = selection[0]; } else { event.doit = false; } }; public void dragSetData(DragSourceEvent event) { event.data = dragSourceItem[0].getText(1); } public void dragFinished(DragSourceEvent event) { if (event.detail == DND.DROP_MOVE) dragSourceItem[0].dispose(); dragSourceItem[0] = null; } }); DropTarget target = new DropTarget(tree, operations); target.setTransfer(types); target.addDropListener(new DropTargetAdapter() { public void dragOver(DropTargetEvent event) { event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL | DND.FEEDBACK_SELECT; } public void drop(DropTargetEvent event) {} }); shell.setSize(400, 400); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } Change the listener being set in line 38 to SWT.Selection and notice the DND working now. Tested with Table too and seems to work (though the selection feedback looks weird then). Ben

Files changed in fix:
bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java