Categories

Duncan Mills

Syndicate this blog

TreeBindings - not just for trees

You may be familiar with the ADF Tree binding which can be used in JSF and Swing to create conventional tree controls. All you have to do is to drag a collection that is a master in a master-detail relationship and drop as Trees|ADF Tree from the context menu.
This generates a ADF Faces tree control and a tree binding in the pageDef. So for the REGIONS > COUNTRY relationship in the HR schema you might get a tree like this:

[More:]

Normal JSF Tree
What's interesting is that you can use the same hierarchical binding that populates the tree component for your own means. Here's a screen shot of the same binding being used to create a somewhat different UI:
Alternative Tree Layouts
There is not difference at all in the ADF binding here but rather than using the single <af:tree> component I've used a couple of <af:forEach> loops to walk through each level in the hierarchy. Here's the code to produce that bit of UI:

  <af:panelHorizontal halign="left" valign="top">
    <af:forEach items="#{bindings.RegionsView1.children}"
                var="regionLevel">
      <af:panelBox text="#{regionLevel.RegionName}">
        <af:panelList>
          <af:forEach items="#{regionLevel.children}"
                      var="countryLevel">
            <af:outputText value="#{countryLevel.CountryName}"/>
          </af:forEach>
        </af:panelList>
      </af:panelBox>
      <af:objectSpacer width="4"/>
    </af:forEach>
  </af:panelHorizontal>

As you can see here the key to understanding this is the attribute children which the tree binding provides and returns a (java.util.)List containing all of the nodes at that level. Each node can then be accessed using the var alias defined in the forEach loop and the attributes defined within the tree binding at that. So the RegionName is accessed via #{regionLevel.RegionName]
To walk down the tree to the children of each node we can use the .children accessor. I use this to create a nested forEach to list the countries in each region within the panelLists.
Knowing how to access the various levels and attributes within a tree binding is a really useful tool that can help you to create some pretty novel hierarchical data representations.

Comments:

Comment from: Michael A. Fons [Visitor]
wow, thank you so much for this example. Once again your trail of bread crumbs has helped me out. I am so glad Shay Schmeltzer pointed out this article in a particular jdev forum that i searched. Thanks very much for laying it out.
Permalink 09/10/07 @ 05:59
Comment from: Mourad [Visitor]
Hi,
thanks for this explication but i didn' t undrstand how to find children attribute when binding.
Permalink 01/11/07 @ 23:07
Comment from: intro [Visitor] · http://bloginii.blogspot.com
thanks for this post it was very usefull
Permalink 10/11/07 @ 20:46

Comments are closed for this post.