我有一个 SWT Tree ,它有图像和其他文本以及多级子级。现在我想拖动这个树节点,但我无法为此获得正确的传输类型。所以一旦我拖动我就没有拖动图像。是否有任何可用的默认传输类型。
以下是我的树-
我使用了以下转移但没有工作-
Transfer[] types = new Transfer[] {FileTransfer.getInstance(),ImageTransfer.getInstance() };
我有一个 SWT Tree ,它有图像和其他文本以及多级子级。现在我想拖动这个树节点,但我无法为此获得正确的传输类型。所以一旦我拖动我就没有拖动图像。是否有任何可用的默认传输类型。
以下是我的树-
我使用了以下转移但没有工作-
Transfer[] types = new Transfer[] {FileTransfer.getInstance(),ImageTransfer.getInstance() };
我仍然不完全确定您要的是什么,但是此代码甚至可以使用Image
s.
如果你的文本不能用作标识符,你可以想出别的东西:
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));
}
}
}
});
}