0

我创建了一个简单的 DragNDrop 编辑器来修改要保存在我的数据库中的树。(使用GXT 2.2.5无法升级)

我有一个用 TreeStore 构建的 TreePanel。TreePanel 既是 TreePanelDragSource 又是 TreePanelDropTarget。

拖放工作正常;作为测试,我使用了现有打开的 Dialog 窗口中的 TreeStore。当我在编辑器中拖放时,另一个窗口会立即显示树的变化。

但是,当我获取 TreeStore 以保存它时,节点不会在 Store 中重新排列。如何获得重组后的树结构?

TIA

4

1 回答 1

0

我的解决方案

Apparently there is no way to directly get at the Tree structure altered by Drag and Drop.
I reasoned that TreePanelView = TreePanel.getView() might have the Drag and Drop chaanges.
By examining TreePanelView in the debugger after a drag and drop I devise this solution:
/*
* These 'My' classes are used to access the internal tree within TreePanelView.
* The internal tree reflects Drag and Drop activity,
* which is NOT reflected in the TreeStore.
*/
private class MyTreePanel<M extends ModelData> extends TreePanel<M> {
    public MyTreePanel(TreeStore<M> ts) {
        super(ts);
        view = new MyView<M>();
        view.bind(this, store);
    }
    public MyView<M> getMyView() {
        return (MyView<M>) getView();
    }
}
private class MyView<M extends ModelData> extends TreePanelView<M> {
    public MyTreeStore<M> getTreeStore() {
        return (MyTreeStore<M>) this.treeStore;
    }
}
private class MyTreeStore<M extends ModelData> extends TreeStore<M> {
    public MyTreeStore() {
        super();
    }
    public Map<M, TreeModel> getModelMap() {
        return modelMap;
    }
}
To extract the tree altered by Drag and Drop:
MyTreePanel<ModelData> myTree; //Initialize the TreeStore appropriately

// After Drag and Drop activity, get the altered tree thusly:
Map<ModelData, TreeModel> viewMap = myTree.getMyView().getTreeStore().getModelMap();

The TreeModel in viewMap is actually a BaseTreeModel.
The ModelData are the objects I originally loaded into TreeStore.
I had to:
1 - Iterate over viewMap, extract "id" from BaseTreeModel and create a reverse map,
    indexed by "id" and containing my ModelData objects.
2 - Fetch root BaseTreeModel node from viewMap using root ModelData of original tree.
3 - Walk the BaseTreeModel tree.
    At each node, fetched ModelData objects by "id" from the reverse map.

In this way I reconstructed the tree altered by Drag and Drop.
于 2012-11-06T14:22:12.953 回答