Mahmoud Saleh
Mahmoud Saleh

Reputation: 33605

JSF/ICEfaces Dynamic hierarchy tree example

i have list of departments and each department might have a parent or not,department domain object is as follows:

- departmentId
- parentDepartmentId (null if current department has no parent i,e should be under root directly, and have value if current department have parent).
.
.
.

looking at icefaces tutorial code for creating basic tree:

// create root node with its children expanded
    DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
    IceUserObject rootObject = new IceUserObject(rootTreeNode);
    rootObject.setText("Root Node");
    rootObject.setExpanded(true);
    rootTreeNode.setUserObject(rootObject);

    // model is accessed by by the ice:tree component via a getter method, this object is what's needed in the view to display the tree
    model = new DefaultTreeModel(rootTreeNode);

    // add some child nodes
    for (int i = 0; i <3; i++) {
        DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
        IceUserObject branchObject = new IceUserObject(branchNode);
        branchObject.setText("node-" + i);
        branchNode.setUserObject(branchObject);
        rootTreeNode.add(branchNode);
    }

it's all about constructing basic node, and adding childs.

my case is complex that child A which is under root may have child nodes B,C,D and D have for example child nodes and so on so on.

so i am thinking of a best practice about how to accomplish something like that, i need a sample code or hints if anyone can help.

Upvotes: 1

Views: 2832

Answers (3)

carlsLobato
carlsLobato

Reputation: 11

Check http://click.avoka.com/click-examples/tree/checkbox-tree-page.htm

The latter is done via the Apache Click framework. Right now I'm developing a project where this data structure (hierarchy tree) is heavily used. You can set the root node or if you need to have several starting points, you can create a wildcard root node that won't affect the functionality, the subclasses, like others have commented, need to be created recursively.

Upvotes: 0

Mahmoud Saleh
Mahmoud Saleh

Reputation: 33605

finally i was able to do it as follows:

List<Department> departmentList = getAllDepartments();
            // create root node with its children expanded
            DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
            IceUserObject rootObject = new IceUserObject(rootTreeNode);
            rootObject.setText("Root");
            rootObject.setExpanded(true); 
            rootTreeNode.setUserObject(rootObject);

            HashMap<Department, DefaultMutableTreeNode> createdNodesMap = new HashMap<Department, DefaultMutableTreeNode>(
                    0);

            for (Department department : departmentList) {

                DefaultMutableTreeNode currentNode = null;

                if (createdNodesMap.get(department) == null) {
                    log.debug("############ CREATING NODE "
                            + department.getName());
                    currentNode = new DefaultMutableTreeNode();
                    IceUserObject currentObject = new IceUserObject(currentNode);
                    currentObject.setText(department.getName());
                    currentObject.setExpanded(true);
                    currentNode.setUserObject(currentObject);

                    if (department.getParentDepartment() == null) {
                        rootTreeNode.add(currentNode);
                        log.debug("######### NODE " + department.getName()
                                + " ADDED UNDER ROOT");
                    }

                    createdNodesMap.put(department, currentNode);
                } else {
                    log.debug("############ GETTING CREATED NODE "
                            + department.getName());
                    currentNode = createdNodesMap.get(department);
                }

                if (department.getChildren().size() > 0)
                    log.debug("############ NODE " + department.getName()
                            + " HAVE " + department.getChildren().size()
                            + " CHILDREN");
                else
                    log.debug("############ NODE " + department.getName()
                            + " DOES NOT HAVE CHILDREN");

                for (Department department2 : department.getChildren()) {

                    log.debug("############ CREATING CHILD "
                            + department2.getName() + " FOR PARENT "
                            + department.getName());
                    DefaultMutableTreeNode branchNode;
                    if (createdNodesMap.get(department2) == null) {
                        branchNode = new DefaultMutableTreeNode();
                        IceUserObject branchObject = new IceUserObject(
                                branchNode);
                        branchObject.setText(department2.getName());
                        branchObject.setExpanded(true);
                        branchNode.setUserObject(branchObject);
                    } else
                        branchNode = createdNodesMap.get(department2);

                    createdNodesMap.put(department2, branchNode);
                    currentNode.add(branchNode);
                }

            }

            model = new DefaultTreeModel(rootTreeNode);

Upvotes: 0

r0ast3d
r0ast3d

Reputation: 2635

You would need a recursive method to construct the tree from your model.

public void buildRecursiveTreeNode(DefaultMutableTreeNode parentTreeNode,
            String treeId, String treeName, GenericTreeVo modelVo) 
    {
            // if the database model contains more children. 
            // add the current nodes first and pass in this nodes tree id and name to construct the children for this parent nodes.


    }

Updated answer to include recursion example.

http://www.danzig.us/java_class/recursion.html

just added a recursion link, all I am saying is when you iterate the data from the database, you would see if you have any child records, if you have child records you would call the same method by passing the DefaultMutableTreeNode and that would become the parent.

Upvotes: 1

Related Questions