Tree, TreeNode

UI Components for JSF
Post Reply
kiler
Posts: 4
Joined: 17 Nov 2010, 13:56
Location: Warsaw

24 Mar 2011, 16:58

Hi All,

I am new in java and JSF and I have (IMHO) big problem. I want to list disk folder in Tree control, but I do not know how to do this.
I wrote somthing like below, but it's return just subfolders from parent. I do not know how to read sub...sub...folders. I could build new path and add to tree (or treeNode), but I do not know it is good idea, may be there is simple way.
Could someone show example code ?

Code: Select all

        String realPath = "somePath";
        File rootDir = new File(realPath);
        TreeNode rootNode = new DefaultTreeNode("Root", null);

        File[] subDirs = rootDir.listFiles(new FileFilter() {

            @Override
            public boolean accept(File rootDir) {
                return rootDir.isDirectory();
            }
        });

        for (int i = 0; i < subDirs.length; i++) {
            if (!(subDirs[i].getName().toUpperCase().equals("META-INF")
                    || subDirs[i].getName().toUpperCase().equals("WEB-INF"))) {

                rootNode.addChild(new DefaultTreeNode(subDirs[i].getName(), null));
            }
        }
Thx for your answer !
Last edited by kiler on 24 Mar 2011, 19:54, edited 2 times in total.

gcameo
Posts: 63
Joined: 02 Mar 2011, 07:38

24 Mar 2011, 17:53

Well, this is not actually a primefaces question but because am almost doing what you are doing, maybe you can learn from mine.

what you need to do is recursion.

this is how mine is. Not the repeated call to

Code: Select all

populateTree
if the current child has children, the call the method again.

Code: Select all

    private TreeNode populateTree( Set<HierarchyItem> children, TreeNode parentNode )
    {
        if ( !Utils.isNullOrEmpty( children ) )
        {
            for ( HierarchyItem site : children )
            {
                TreeNode node = new DefaultTreeNode( site , parentNode );
                if ( !Utils.isNullOrEmpty( site.getChildren() ) )
                {
                    populateTree( site.getChildren(), node );
                }
            }
        }
        return parentNode;
    }
When you become a guru like optimus, dont forget to help someone else
Primefaces version: 3.0-SNAPSHOT

JSF implementation: Mojarra 2.0.4

Server: Glassfish 3.1-beta

kiler
Posts: 4
Joined: 17 Nov 2010, 13:56
Location: Warsaw

24 Mar 2011, 19:52

Thank You Very Much !!!

I know that is not issue related with primefaces, but primefaces community should help eachother :)

Can I one more question ? Class/library

Code: Select all

HierarchyItem
where can I find ? or this is yours implementation ?


PS.
I always try help if I only can.

gcameo
Posts: 63
Joined: 02 Mar 2011, 07:38

24 Mar 2011, 21:34

Can I one more question ? Class/library

Code:
HierarchyItem

where can I find ? or this is yours implementation ?
That is the model i store in the tree so when a tree is selected, and it fires the tree event handler, i retreive that from TreeNode via treeNode.getData().

dobra noc
Primefaces version: 3.0-SNAPSHOT

JSF implementation: Mojarra 2.0.4

Server: Glassfish 3.1-beta

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 48 guests