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

わかりました...動作しました。これがコードです。コーディングの合間に他のことをしていたので、1 つまたは 2 つのことを省略している可能性があります。

最初に主要なフィールド (グローバル変数) について言及し、次に関連するメソッドについてのみ説明します。これは、コントローラー、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 に答える