How can I bid tree2 component?

Hi,

How can I bind my tomahawk tree2 in the bean, I have a tree in a panelGrid

and I want to expand and collapse the tree on click of a command button but

I dont want to use HtmlTree tree = (HtmlTree)pGrid.findComponent("clientTree"); since I want it to be reusable.

I tried binding it but that didnt work.

Can anyone suggest any idea please?

Thanks,

Sanjeev.

[409 byte] By [SanjeevGoura] at [2007-11-26 16:31:57]
# 1
> I tried binding it but that didnt work.Can you please elaborate "didnt work" ?
BalusCa at 2007-7-8 22:56:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thank you for you concern.

Well, I tried binding the tree with the HtmlTree in my backing bean. After that i tried using the expandAll() method on the tree instance I created in the backing bean but the tree in my jsp which has the binding with this backing bean tree instance, didn't expand its node.

That's what I think. Have I done anything wrong?

SanjeevGoura at 2007-7-8 22:56:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Show the code you've tried. This should just work.
BalusCa at 2007-7-8 22:56:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

THis is my jsp:

<t:tree2 id="clientTree" binding="#{LibraryTreeModel.tree}" value="#{LibraryTreeModel.treeData}"

var="node" varNodeToggler="t" showRootNode="false"

clientSideToggle="true" preserveToggle="false">

<f:facet name="folder">

<h:panelGroup>

<f:facet name="expand">

<t:graphicImage value="images/obook.gif" rendered="#{t.nodeExpanded}" border="0"/>

</f:facet>

<f:facet name="collapse">

<t:graphicImage value="images/cbook.gif" rendered="#{!t.nodeExpanded}" border="0"/>

</f:facet>

<h:commandLink action="nodeClicked"

styleClass="#{t.nodeSelected ? 'documentSelected':'document'}"

actionListener="#{LibraryTreeHandler.processAction}"

immediate="true">

<h:outputText value="#{node.description}"/>

</h:commandLink>

</h:panelGroup>

</f:facet>

<f:facet name="document">

<h:panelGroup>

<t:graphicImage value="images/topic.gif" border="0"/>

<h:commandLink action="nodeClicked"

styleClass="#{t.nodeSelected ? 'documentSelected':'document'}"

actionListener="#{LibraryTreeHandler.processAction}"

immediate="true">

<h:outputText value="#{node.description}"/>

</h:commandLink>

</h:panelGroup>

</f:facet>

</t:tree2>

And this is the class having a method which returns the data model:

public class LibraryTreeModel {

private TreeNode treeData;

private HtmlTree tree;

.......

.......

public HtmlTree getTree() {

return tree;

}

/**

* @param tree The tree to set.

*/

public void setTree(HtmlTree tree) {

this.tree = tree;

}

ANd this is the class which handles tree events;

public class LibraryHandler {

private LibraryTreeModel treeModel;

public LibraryHandler() {

treeModel = new LibraryTreeModel();

}

public LibraryTreeModel getTreeModel() {

return treeModel;

}

public void expandAll(ActionEvent event) throws AbortProcessingException {

UIComponent component = (UIComponent)event.getSource();

HtmlTree tree = getTreeModel().getTree();

tree.expandAll();

}

public void processAction(ActionEvent event) throws AbortProcessingException {

UIComponent component = (UIComponent)event.getSource();

while (!(component != null && component instanceof HtmlTree)) {

component = component.getParent();

}

if (component != null) {

HtmlTree tree = (HtmlTree)component;

tree.setNodeSelected(event);

authorBio = tree.getNode().getIdentifier()+" "+tree.getNode().getDescription();

}

}

The processAction method just works fine, but expandAll() doesn't work.

Can u please look into this?

Thnx.

SanjeevGoura at 2007-7-8 22:56:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Sorry, I forgot to paste the command button line:<h:commandLink action="expand" actionListener="#{LibraryTreeHandler.expandAll}">On click of this it gives null pointer exception in the line tree.expandAll(); in expandAll method.
SanjeevGoura at 2007-7-8 22:56:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

It is null? Just do something likeif (tree == null) {

tree = new HtmlTree();

}

or

private HtmlTree tree = new HtmlTree();

BalusCa at 2007-7-8 22:56:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Hi,

Putting the expandAll() method in LibraryTreeModel instead of LibraryTreeHandler worked. Now the binding works fine.

Now a ltitle more of your precious time and advice I need :).

Could you please suggest me some way to iterate though the tree nodes and collapse the tree using tree.getDataModel().getTreeState().collapsePath(strings);

I am not getting how to form this array 'strings' to collapse it completely.

Thanks,

Sanjeev.

SanjeevGoura at 2007-7-8 22:56:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...