Search code examples
javaswingwicketjtree

DefaultTreeModel and Wicket Tree: setAsksAllowsChildren doesn't work


I'm using Wicket's Tree component in a web app. But empty folders are shown in a file-way. Just like this: enter image description here

Bellow is where I use the DefaultTreeModel and Tree:

PDMinterface pdmI = new PDMinterface(); 
DefaultMutabletreeNode rootTreeNode = pdmI.getDocTree();            //文档树根结点,由PDM接口提供
DefaultTreeModel treeModel = new DefaultTreeModel(rootTreeNode);
treeModel.setAsksAllowsChildren(true);

and I'm sure that folder5 is set to allow children:

public DefaultMutableTreeNode getDocTree(){  
    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    root.setAllowsChildren(true);
    FolderNode rootFolder = new FolderNode(0, "root", "Jiajun", true);
    root.setUserObject(rootFolder);

    for(int i=0; i < 5; i++){
        DefaultMutableTreeNode newnode = new DefaultMutableTreeNode();
        newnode.setAllowsChildren(true);
        FolderNode newFolder = new FolderNode(i+1, "Folder" + Integer.toString(i+1), "Jiajun", false);
        newnode.setUserObject(newFolder);
        root.add(newnode);
        if(i < 4){
            for(int j=0; j < 5; j++){
                DefaultMutableTreeNode newdocNode = new DefaultMutableTreeNode();
                newdocNode.setAllowsChildren(false);
                DocNode newDoc = new DocNode(10*(i+1) + j, "Document" + Integer.toString(10*(i+1)+j), "Jiajun");
                newdocNode.setUserObject(newDoc);
                newnode.add(newdocNode);
            }
        }
    }

Solution

  • To be more helpful than in my comment, I found this code in the AbstractTreeClass which will control what image it assigns to the node:

    /**
     * Returns the resource reference for icon of specified tree node.
     *
     * @param node
     *            The node
     * @return The package resource reference
     */
    protected ResourceReference getNodeIcon(TreeNode node)
    {
        if (node.isLeaf() == true)
        {
            return getItem();
        }
        else
        {
            if (isNodeExpanded(node))
            {
                return getFolderOpen();
            }
            else
            {
                return getFolderClosed();
            }
        }
    }
    

    So, the whole thing comes to the question of what does the isLeaf() method return. I found this in the DefaultMutableTreeNode class:

    public boolean isLeaf()
    {
        return children.size() == 0;
    }
    

    So, it seems that your combination would treat all elements without children as leafs and not as folders. Maybe you could overwrite the getNodeIcon method using the getAllowsChildren, making the necessary type adjustments...

    Another idea is to overwrite the isLeaf() method of DefaultMutableTreeNode, but then you might get other unexpected issues if it is called somewhere you cannot control...

    This is just some insight on how you could do it... Hope it is helpful...