我有一个SWT树,它有图像和其他文本以及多级子项。 现在我想拖动这个树节点,但我无法为此获得正确的传输类型。 所以一旦我拖动我就不会得到拖动图像。 是否有可用的默认传输类型。
以下是我的树 -
我使用了以下转移但没有工作 -
Transfer[] types = new Transfer[] {FileTransfer.getInstance(),ImageTransfer.getInstance() };
答案 0 :(得分:0)
我仍然不完全确定你要求的是什么,但这段代码甚至适用于Image
。
如果您的文字不能用作标识符,您可以提出其他内容:
private static Map<String, Image> mapping = new HashMap<String, Image>();
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
Tree tree = new Tree(shell, SWT.NONE);
Image[] images = new Image[] { display.getSystemImage(SWT.ICON_WARNING),
display.getSystemImage(SWT.ICON_ERROR), display.getSystemImage(SWT.ICON_INFORMATION),
display.getSystemImage(SWT.ICON_QUESTION) };
for (int i = 0; i < 10; i++)
{
TreeItem item = new TreeItem(tree, SWT.NONE);
item.setText("Item " + i);
item.setImage(images[i % images.length]);
mapping.put("Item " + i, images[i % images.length]);
}
initDnD(display, tree);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void initDnD(final Display display, final Tree tree)
{
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();
}
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_SCROLL;
if (event.item != null)
{
TreeItem item = (TreeItem) event.item;
Point pt = display.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;
}
}
}
public void drop(DropTargetEvent event)
{
if (event.data == null)
{
event.detail = DND.DROP_NONE;
return;
}
String text = (String) event.data;
if (event.item != null)
{
TreeItem item = (TreeItem) event.item;
Point pt = display.map(null, tree, event.x, event.y);
Rectangle bounds = item.getBounds();
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.setText(text);
newItem.setImage(mapping.get(text));
}
else if (pt.y > bounds.y + 2 * bounds.height / 3)
{
TreeItem newItem = new TreeItem(tree, SWT.NONE, index + 1);
newItem.setText(text);
newItem.setImage(mapping.get(text));
}
else
{
TreeItem newItem = new TreeItem(item, SWT.NONE);
newItem.setText(text);
newItem.setImage(mapping.get(text));
}
}
}
});
}