当在两棵树之间拖放东西时,我最终破解了一些有用的东西(对于我的特定用例) - 特定的键实例并不重要,我的只是将项目本身的路径作为键的一部分 - 主要部分只是修改 appendModel 以使用插入而不是 addSubTree 单独添加树组件,所以我有一个实际上允许创建实例的数据类型。
如果有人能找到更好的方法,我很乐意看到它。
public class MyTreeDropTarget extends TreeDropTarget<FileSystemKey> {
public MyTreeDropTarget(Tree<FileSystemKey, ?> tree) {
super(tree);
}
private void InsertClone(FileSystemKey parent,
TreeStore.TreeNode<FileSystemKey> node, int index) {
FileSystemKey clone;
if (parent == null) { //root of the tree
clone = new FileSystemKey(node.getData());
clone.SetKey("/", null, "/" + clone.key2);
getWidget().getStore().insert(index, clone);
} else { //a child element
clone = new FileSystemKey(node.getData());
clone.SetKey(parent.key1 + parent.key2 + "/", null, parent.key1
+ parent.key2);
getWidget().getStore().insert(parent, index, clone);
}
//if this node had children,
//we then insert the children as children of this node
if (node.getChildren().size() > 0) {
//TODO: rewrite to preserve order
appendModel(clone, node.getChildren(), 0);
}
}
@Override
protected void appendModel(FileSystemKey p, List<?> items, int index) {
if (items.size() == 0)
return;
if (items.get(0) instanceof TreeStore.TreeNode) {
// @SuppressWarnings("unchecked")
List<TreeStore.TreeNode<FileSystemKey>> nodes = (List<TreeStore.TreeNode<FileSystemKey>>) items;
// TODO: replace clone with gets
for (TreeStore.TreeNode<FileSystemKey> key : nodes) {
InsertClone(p, key, index);
}
} else {// no changes here - maybe change
// this if you have non-homogenous key types
@SuppressWarnings("unchecked")
List<FileSystemKey> models = (List<FileSystemKey>) items;
if (p == null) {
getWidget().getStore().insert(index, models);
} else {
getWidget().getStore().insert(p, index, models);
}
}
}
}