public void dndBuilder() {
Transfer[] type = new Transfer[] { TextTransfer.getInstance() };
int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK
| DND.DROP_NONE;
final DragSource source = new DragSource(tree, operations);
source.setTransfer(type);
final TreeItem[] dragSourceItem = new TreeItem[1];
source.addDragListener(new DragSourceListener() {
public void dragStart(DragSourceEvent event) {
TreeItem[] selection = tree.getSelection();
if (selection.length > 0) {
event.doit = true;
dragSourceItem[0] = selection[0];
} else {
event.doit = false;
}
};
public void dragSetData(DragSourceEvent event) {
event.data = dragSourceItem[0].getText();
}
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(type);
target.addDropListener(new DropTargetAdapter() {
private TreeItem initTreeItem(TreeItem sitem, TreeItem ditem) {
ditem.setText(sitem.getText());
ditem.setData(sitem.getData());
for (int i = 0; i < sitem.getItemCount(); i++) {
TreeItem subitem = new TreeItem(ditem, SWT.NONE);
try {
subitem = initTreeItem(sitem.getItem(i), subitem);
} catch (Exception e) {
// e.printStackTrace();
}
}
return ditem;
}
private boolean checkValidate(TreeItem sitem, TreeItem ditem) {
if (ditem.equals(sitem))
return false;
TreeItem titem = ditem.getParentItem();
while (titem != null) {
if (sitem.equals(titem))
return false;
titem = titem.getParentItem();
}
return true;
}
public void dragOver(DropTargetEvent event) {
event.feedback = DND.FEEDBACK_NONE | DND.FEEDBACK_SCROLL;
if (event.item != null) {
TreeItem item = (TreeItem) event.item;
if (!checkValidate(dragSourceItem[0], item)) {
event.detail = DND.DROP_NONE;
return;
} else {
event.detail = DND.DROP_MOVE;
}
Point pt = shell.getDisplay().map(null, tree, event.x,
event.y);
Rectangle bounds = item.getBounds();
if (pt.y < bounds.y + bounds.height / 3) {
event.feedback |= DND.FEEDBACK_INSERT_BEFORE;
} else if (pt.y > bounds.y + 2 * bounds.height / 3) {
event.feedback |= DND.FEEDBACK_INSERT_AFTER;
} else {
event.feedback |= DND.FEEDBACK_SELECT;
}
}
}
public void drop(DropTargetEvent event) {
if (event.data == null) {
event.detail = DND.DROP_NONE;
return;
}
if (event.item == null) {
TreeItem item = new TreeItem(tree, SWT.NONE);
item = initTreeItem(dragSourceItem[0], item);
} else {
TreeItem item = (TreeItem) event.item;
Point pt = shell.getDisplay().map(null, tree, event.x,
event.y);
Rectangle bounds = item.getBounds();
TreeItem parent = item.getParentItem();
if (parent != null) {
TreeItem[] items = parent.getItems();
int index = 0;
for (int i = 0; i < items.length; i++) {
if (items[i] == item) {
index = i;
break;
}
}
if (pt.y < bounds.y + bounds.height / 3) {
TreeItem newItem = new TreeItem(parent, SWT.NONE,
index);
newItem = initTreeItem(dragSourceItem[0], newItem);
} else if (pt.y > bounds.y + 2 * bounds.height / 3) {
TreeItem newItem = new TreeItem(parent, SWT.NONE,
index + 1);
newItem = initTreeItem(dragSourceItem[0], newItem);
} else {
TreeItem newItem = new TreeItem(item, SWT.NONE);
newItem = initTreeItem(dragSourceItem[0], newItem);
}
} else {
TreeItem[] items = tree.getItems();
int index = 0;
for (int i = 0; i < items.length; i++) {
if (items[i] == item) {
index = i;
break;
}
}
if (pt.y < bounds.y + bounds.height / 3) {
TreeItem newItem = new TreeItem(tree, SWT.NONE,
index);
newItem = initTreeItem(dragSourceItem[0], newItem);
} else if (pt.y > bounds.y + 2 * bounds.height / 3) {
TreeItem newItem = new TreeItem(tree, SWT.NONE,
index + 1);
newItem = initTreeItem(dragSourceItem[0], newItem);
} else {
TreeItem newItem = new TreeItem(item, SWT.NONE);
newItem = initTreeItem(dragSourceItem[0], newItem);
}
}
}
tree.redraw();
}
});
}