1

我正在尝试使用这个包来实现对商店类别的 CRUD 操作。我不知道在哪里可以找到有使用这个包经验的人,所以我决定在这里问。

我想添加一个快速搜索选项,用户可以在不浏览树的情况下搜索类别,并且选定的路径和节点在 JTree 上展开。

到目前为止,我已经完成了以下工作:

ArrayList<FpsInventoryCategory> path;

    // recursive method to find the path to the root of a selected node
    private void findTreePath(FpsInventoryCategory currentNode) throws StorageException{
        Object parentId = currentNode.getParentId();
        FpsInventoryCategory parentNode = (FpsInventoryCategory) store.getItem(parentId);
        if (parentNode.isRoot()){
            path.add(0, parentNode);
        } else {
            path.add(0, parentNode);
            findTreePath(parentNode);
        }

    }

FpsInventoryCategory 实现 HierarchialItem。path 是 FpsInventoryCategory 的 ArrayList。这很好用,我能够获得所需的路径。

我遇到的问题是理解包以便确定如何扩展节点和/或 TreePath。我最初尝试通过 TreePath,但后来意识到,由于 JPersistentTree 的整个想法是动态加载数据,因此 JTree 不会有超过第一行子节点的数据。这是一个正确的假设吗?

如果是这样,那么我假设我必须沿着我尝试的路径加载孩子,如下所示:

private void viewTreeNodeAndPath(Long id2) throws StorageException {
        // TODO Auto-generated method stub
gui.getTreeInventoryCategory().getModel().getRoot();
            path = new ArrayList<FpsInventoryCategory>();
            store = gui.getStore();
            FpsInventoryCategory startNode = (FpsInventoryCategory) store.getItem(id);
            path.add(startNode);
            findTreePath(startNode);
            DynamicTreeNode currentTreeNode;
            Iterator it = path.iterator();
            int i = 0;
            FpsInventoryCategory temp;
            while(it.hasNext()){
                temp = (FpsInventoryCategory) it.next();
                System.out.println("temp cat:" + temp.getName());
                currentTreeNode = new DynamicTreeNode(store, temp);
                currentTreeNode.loadChildren();
                gui.getTreeInventoryCategory().expandRow(i);
                i++;
            }

//      selectedTreePath = new TreePath(dynamicNodePath);
//      System.out.println(selectedTreePath);
//      gui.getTreeInventoryCategory().setSelectionPath(selectedTreePath);
//      
//      gui.getTreeInventoryCategory().expandPath(selectedTreePath);

    }

^ 注释掉了路径选择,因为它没有产生结果。这同样行不通。

知道如何让树扩展吗?甚至我可以从哪里获得有关此问题的更多信息?

请随时给我建议或让我知道在哪里可以找到解决此问题的方法。

此致。

4

1 回答 1

1

好的......让它工作。这是代码。在编码之间做一些其他的事情,所以可能会留下一两件事。

我会先提关键字段(全局变量),然后再指出相关方法。这是来自控制器,MVC 模式。

private FpsInventoryCategoryTreeAdapter store;
    ArrayList<FpsInventoryCategory> path;
    TreePath selectedTreePath;
    private Long id;

// expand path and select node in JPersistentTree of category being searched
    private void viewTreeNodeAndPath() throws StorageException {
        // TODO Auto-generated method stub
        path = new ArrayList<FpsInventoryCategory>();
        store = gui.getStore(); // get store from gui (view contains HierarchicalDataStore implementation for JPersistentTree)
        FpsInventoryCategory startNode = (FpsInventoryCategory) store.getItem(id);  // id is the selected node as per quick search
        path.add(startNode);    // add selected node to path
        // find path from root to selected node
        findTreePath(startNode);
        int pathLength = path.size(); 
        TreeModel model = gui.getTreeInventoryCategory().getModel();
        DynamicTreeNode rootNode = (DynamicTreeNode) model.getRoot();
        // traverse through JPersistentTree from root to searched node, returning path to selectedTreePath
        searchThroughTree(model, rootNode, pathLength, 1);
        // expand selectedTreePath (from root to searched node)
        gui.getTreeInventoryCategory().setSelectionPath(selectedTreePath);
        gui.getTreeInventoryCategory().scrollPathToVisible(selectedTreePath);
    }

    // recursive method to find the path to the root of a selected node
    private void findTreePath(FpsInventoryCategory currentNode) throws StorageException{
        Object parentId = currentNode.getParentId();
        FpsInventoryCategory parentNode = (FpsInventoryCategory) store.getItem(parentId);
        if (parentNode.isRoot()){
            path.add(0, parentNode);
        } else {
            path.add(0, parentNode);
            findTreePath(parentNode);
        }

    }

    // recusrive method to get TreePath of searched node
    // pathSize: number of nodes from root to searched path
    // pathPosition: index of current node in path
    private void searchThroughTree(TreeModel model, DynamicTreeNode rootNode, int pathSize, int pathPosition){
        int childCount = model.getChildCount(rootNode);
        for (int i = 0; i < childCount; i++){
            DynamicTreeNode childNode = (DynamicTreeNode) model.getChild(rootNode, i);
            if (model.isLeaf(childNode)){
                if (path.get(pathPosition).getId() == childNode.getUserObject().getId()){
                    selectedTreePath = getPath(childNode);
                    break;
                }
            } else {
                if (path.get(pathPosition).getId() == childNode.getUserObject().getId()){
                    if (pathPosition + 1 == pathSize){
                        selectedTreePath = getPath(childNode);
                        break;
                    } else {
                        pathPosition = pathPosition + 1;
                        searchThroughTree(model, childNode, pathSize, pathPosition);
                    }   
                }
            }
        }
    }

    // find path from root to node
    public TreePath getPath(DynamicTreeNode node){
        List list = new ArrayList();
        // Add all nodes to list
        while (node != null){
            list.add(node);
            node = (DynamicTreeNode) node.getParent();
        }
        Collections.reverse(list);
        // Convert array of DynamicTreeNodes to TreePath
        return new TreePath(list.toArray());
    }

在我的机器上工作正常。

于 2012-08-16T13:44:01.220 回答