Modify key as item is added to GXT tree

73 Views Asked by At

I've been using sencha GXT for a few days, and I'm currently trying to get Drag'n'drop between two trees representing different file-systems to work. This means that a duplicate file may be copied onto one system in several different folders - but since that file has the same key, the treestore breaks.

What I've tried so far, and results:

  • Attempt: Do things in the TreeDropHandler/TreeMoveHandler
  • Result: Turns out these are for when the widget is getting moved

  • Attempt: Override TreeDataStore and override the add methods

  • Result: The add methods aren't even called when dragging and dropping into the tree - it seems they're only there for manually adding things to the treestore

  • Attempt: Just copying the entire treestore and modifying it to work how I want

  • Result: Tree specfically expects the treestore class, and not something that implements some abstract class, unfortunately, so I can't do this

  • Attempt: Override the insert methods in TreeStore

  • Result: These methods are inside a private subclass within the treestore, so I can't directly access them

  • Attempt: Making the modified version of the treestore class extend the original and just hide everything

  • Result: The compiler will tell my I need an @Override infront of a variable to deal with duplicate types, and when I add it, it will tell me there is nothing for it to override (x 100)

I'm a tad stuck on what else I can even try to do at this point. If anybody has any advice or examples, I would be most grateful.

1

There are 1 best solutions below

0
On

I eventually hacked together something that works (for my specific usage case) when dragging and dropping things between two trees - the specific key instance isn't important, mine just keeps the path to the item itself as part of the key - the main part was just modifying appendModel to individually add tree components using inserts instead of addSubTree, so I had a data type I was actually allowed to make instances of.

If anyone can find a better way though, I would love to see it.

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);
            }
        }
    }
}