You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Todd Patrick <To...@dtn.com> on 2006/07/07 20:53:41 UTC

Tomahawk: Tree2: TreeNodeBase.getChildCount() question - Lazy Loading issue.

I have been unsuccessful in implementing the Lazy Loading technique that
was described in the wiki or as described using AjaxAnywhere by Andrew
R.

"By all means, this is my own fault and no one else, I greatly
appreciate everyone's help!"

Therefore, I am at this point: I really just need the "plus sign"
navigation displayed for every node, no matter if the node has a child
or not.

A user that clicks on the "plus sign" will run a server ActionEvent that
will get the children for that node.

If there is no children, do nothing.

I went back and studied the following comment on Andrew R. blog: "The
tree2 renderer looks only at the getChildCount method, not the
getChildren method when rendering. Meaning that if getChildCount should
return 0, getChildren will never be called unless the node is expanded."

Ok, good... now I should just be able to override the getChildCount
method if I extend the TreeNodeBase class, such as:

public int getChildCount() {
	return 1;
}

So, in combination with the wiki example "Alternative Tree2 Lazy Loading
Method...by jtmille3":

public class LazyTreeNode extends TreeNodeBase {

        private static final long serialVersionUID =
-6870203633601493362L;

        public LazyTreeNode() {
        }

        public LazyTreeNode(String type, String description, boolean
leaf) {
                super(type, description, leaf);
        }

        public LazyTreeNode(String type, String description, String
identifier, boolean leaf) {
                super(type, description, identifier, leaf);
        }

	  public int getChildCount() {
	          return 1;
	  }

        public List getChildren() {
             // if there are no children, try and retrieve them
                if (super.getChildren().size() == 0) {
			 super.getChildren().add(new
LazyTreeNode("document","" + id, "" + id,false));
                }

                return super.getChildren();
        }
}

This doesn't work. What I end up with is the parent value repeated such
as:

- A
|_ A
|_ A
|_ A

Instead, I should have:

- A
|_ B
|_ C
|_ D

The way I build my tree children is in a *for* loop in the following
method:

 public void getTreeTransactBranch(String _transactID, TreeNode
_transactNode) throws NamingException{
        InitialContext ctx = new InitialContext();
        DataSource ds = (javax.sql.DataSource)
ctx.lookup("jdbc/__Pool");
        Connection con;
        List< TransactRow >treeBranch = new LinkedList();
        
        try {
            con = ds.getConnection();
            try {
                TransactRow tree = new TransactRow();
                treeBranch = tree.selectByParentID(con, _transactID);
                
                for (TransactRow row : treeBranch) {
                    childTransactLeafLabel.append(row.getXactTypeID());
                    childTransactLeafLabel.append(spacer);
                    childTransactLeafLabel.append(row.getName());
                    childTransactLeafLabel.append(spacer);
 
childTransactLeafLabel.append(this.getDuration(row.getTCreated()));
                    _transactNode.getChildren().add(new
LazyTreeNode("transactions", childTransactLeafLabel.toString(),
row.getTransactID(), false));
                    childTransactLeafLabel.delete(0,
childTransactLeafLabel.length());
                }
                
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            con.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

When I build my children, I thought the values would be from within the
*for* loop.

I'd appreciate any thoughts or suggestions.

Thanks,

--Todd

Re: Tomahawk: Tree2: TreeNodeBase.getChildCount() question - Lazy Loading issue.

Posted by Andrew Robinson <an...@gmail.com>.
The "magic" of my code is just the fact that the base node reports "1"
for the child count *only if* the node has not loaded the children yet
and the node is not expanded. That is the big "gotcha". If you are not
using TreeModelBase or the default TreeState that it comes with that
base tree node from my blog will not work. My code is also using 1.1.2
but I don't think that should matter.

Your "repeated" A is because you reported a node count of 1, but
returned more or less nodes than 1 as children (that is why I had to
add the expanded code to my node).

What problems were you having with my code? I can try to help that
way, or if it doesn't seem to be working out, I can try during my free
time to build an example ZIP for you with source that does the lazy
loading.

Were you getting errors, or was the tree just not working?

BTW - I am using TreeModelBase and not changing the expansion state of
the model (no overrides or customizations).

-Andrew

On 7/7/06, Todd Patrick <To...@dtn.com> wrote:
> I have been unsuccessful in implementing the Lazy Loading technique that
> was described in the wiki or as described using AjaxAnywhere by Andrew
> R.
>
> "By all means, this is my own fault and no one else, I greatly
> appreciate everyone's help!"
>
> Therefore, I am at this point: I really just need the "plus sign"
> navigation displayed for every node, no matter if the node has a child
> or not.
>
> A user that clicks on the "plus sign" will run a server ActionEvent that
> will get the children for that node.
>
> If there is no children, do nothing.
>
> I went back and studied the following comment on Andrew R. blog: "The
> tree2 renderer looks only at the getChildCount method, not the
> getChildren method when rendering. Meaning that if getChildCount should
> return 0, getChildren will never be called unless the node is expanded."
>
> Ok, good... now I should just be able to override the getChildCount
> method if I extend the TreeNodeBase class, such as:
>
> public int getChildCount() {
>         return 1;
> }
>
> So, in combination with the wiki example "Alternative Tree2 Lazy Loading
> Method...by jtmille3":
>
> public class LazyTreeNode extends TreeNodeBase {
>
>         private static final long serialVersionUID =
> -6870203633601493362L;
>
>         public LazyTreeNode() {
>         }
>
>         public LazyTreeNode(String type, String description, boolean
> leaf) {
>                 super(type, description, leaf);
>         }
>
>         public LazyTreeNode(String type, String description, String
> identifier, boolean leaf) {
>                 super(type, description, identifier, leaf);
>         }
>
>           public int getChildCount() {
>                   return 1;
>           }
>
>         public List getChildren() {
>              // if there are no children, try and retrieve them
>                 if (super.getChildren().size() == 0) {
>                          super.getChildren().add(new
> LazyTreeNode("document","" + id, "" + id,false));
>                 }
>
>                 return super.getChildren();
>         }
> }
>
> This doesn't work. What I end up with is the parent value repeated such
> as:
>
> - A
> |_ A
> |_ A
> |_ A
>
> Instead, I should have:
>
> - A
> |_ B
> |_ C
> |_ D
>
> The way I build my tree children is in a *for* loop in the following
> method:
>
>  public void getTreeTransactBranch(String _transactID, TreeNode
> _transactNode) throws NamingException{
>         InitialContext ctx = new InitialContext();
>         DataSource ds = (javax.sql.DataSource)
> ctx.lookup("jdbc/__Pool");
>         Connection con;
>         List< TransactRow >treeBranch = new LinkedList();
>
>         try {
>             con = ds.getConnection();
>             try {
>                 TransactRow tree = new TransactRow();
>                 treeBranch = tree.selectByParentID(con, _transactID);
>
>                 for (TransactRow row : treeBranch) {
>                     childTransactLeafLabel.append(row.getXactTypeID());
>                     childTransactLeafLabel.append(spacer);
>                     childTransactLeafLabel.append(row.getName());
>                     childTransactLeafLabel.append(spacer);
>
> childTransactLeafLabel.append(this.getDuration(row.getTCreated()));
>                     _transactNode.getChildren().add(new
> LazyTreeNode("transactions", childTransactLeafLabel.toString(),
> row.getTransactID(), false));
>                     childTransactLeafLabel.delete(0,
> childTransactLeafLabel.length());
>                 }
>
>             } catch (SQLException ex) {
>                 ex.printStackTrace();
>             }
>             con.close();
>         } catch (SQLException ex) {
>             ex.printStackTrace();
>         }
>     }
>
> When I build my children, I thought the values would be from within the
> *for* loop.
>
> I'd appreciate any thoughts or suggestions.
>
> Thanks,
>
> --Todd
>