RE: Re: JSF Component Question

From: Jason Lee <>
Date: Fri, 15 Dec 2006 08:23:31 -0600

I'll lhave to look into facets (I'll admit that I'm not too familiar
with them), but, right now, I can't use them, which I'd like to change.
To get the basic tree working, the tag looks like this:
<risb:tree id="foo" model="#{testBean.tree}" />
With TestBean.getTree() looking like this:
    public TreeNode getTree() {
        TreeNode top = new TextNode ("Top Node!");
        TreeNode category = null;
        TreeNode book = null;
        category = new TextNode("Books for Java Programmers");
        category.add(new TextNode("The Java Tutorial: A Short Course on
the Basics"));
        category.add(new TextNode("The Java Tutorial Continued: The Rest
of the JDK"));
        category.add(new TextNode("The JFC Swing Tutorial: A Guide to
Constructing GUIs"));
        category.add(new MenuNode("Menu Test",
        category = new TextNode("Books for Java Implementers");
        category.add(new TextNode("The Java Virtual Machine
        category.add(new HtmlNode("The Java Language Specification",
"<b>Gilad Brach!</b>"));
        return top;

That's an OK implementation, I guess, but not as useful as I'd like. I
thought about this some (and I'm not kidding: every time I got up to
change my new-born son's diapers this kept running through my head.
Sad. :), and I'd like to see a more flexible solution. Maybe something
like this:
<risb:tree id="testTree">
        <h:outputText value="A giant table!"/>
            <h:dataTable value="#{}" var="data">
                <!-- snip! -->
        <h:outputText value="Another node"/>
            <h:outputText value="And another"/>
                <h:outputText value="And yet another!"/>
which would produce
- A giant table!
    (table data)
- Another node
    - And another
        And yet another!
I think that would be a more practical use, but I haven't had the time
to think through it (clearly) yet, and, if I implement something like,
how would that look from the user's perspective building the tree. I
don't know. These are questions I hope to get hammered out in the
I think, though, that for all practical purpose, the different JS
classes (TextNode, HTMLNode, and MenuNode) can all be rendered using
only HTMLNode, as a TextNode's HTML content can simply be text (or a
styled <span>, etc) and a MenuNode could have a <h:outputLink/> tag
nested in the JSF markup.
All of that is something I'll be pondering today and over the Christmas
Any input (ideas or code) will be greatly appreciated. :)
Jason Lee, SCJP
Programmer/Analyst <>


        From: Ken.Paulsen_at_Sun.COM [mailto:Ken.Paulsen_at_Sun.COM]
        Sent: Thursday, December 14, 2006 6:59 PM
        Subject: Re: JSF Component Question

        Hi Jason,
        Sounds reasonable. You may also consider leveraging JSF's
"facet" capability so that your tree renderer does not have to be so
"smart" when displaying the contents. In my case, I relied on this to
define any TreeNode that was more complicated than plain text or an
        Another piece of advice (which may be obvious and you may be
doing already), when rendering the child tree nodes, don't cast or check
for instanceof on any of the child UIComponents... just render all of
them. Some components I've seen written cast to an specific instance
(i.e. TreeNode) and call their getters... this prevents things like Ed's
AjaxZone from being inserted in the UIComponent tree to re-render
portions of the tree dynamically.
        Good luck!
        Ken Paulsen
        Jason Lee wrote:

                Hmm. For now, it's using the Swing class, but I think I
may create some Java classes that mimic, in terms of nomenclature, the
classes supported by the JS component:
                A TreeNode would be able to hold 0 or more TreeNode
elements for nested levels. This hierarchy would help me identify at
render time what type of node in the JS i need to create for the given
TreeNode on the Java side.
                Anyone see any problems with that?
                Jason Lee, SCJP


                        From: Ken.Paulsen_at_Sun.COM
                        Sent: Wednesday, December 13, 2006 6:03 PM
                        Subject: Re: JSF Component Question

                        Hi Jason,
                        I have had the privilege of writing a JSF tree
component and faced the same issue. There's more than one right
approach (or is that more than one wrong approach?). But I can share
what I did.
                        On the component side, I composed the Tree of
multiple UIComponents. I defined 2 types of components:
                        From an implementation point-of-view, I ended up
having Tree extend TreeNode b/c the root node (represented by Tree) was
just a specialized TreeNode. You could then easily build a tree
staticly in a JSP, Facelets xhtml, or JSFTemplating .jsf file. However,
this doesn't (didn't) address the root of your question.
                        So... on the "data" side, here's what I did. I
defined a factory class for instantiating part of a Tree structure as
described above (or the entire structure if you want). The factory
class required a "TreeAdaptor" implementation. TreeAdaptor" was an
interface that exposed methods for walking a tree structure that that
factory would call. The interface also exposed methods for getting meta
data needed for the UIComponent (image urls, hyperlink, facets, etc.).
The TreeAdaptor implementations could be written to accept any type of
data structure (i.e. swing TreeNode). This allowed me to walk existing
tree structures and "adapt" them to the UIComponent tree structure
required to represent them in JSF.
                        You can see the code here:
                        And for impl. of the TreeAdaptor inteface:
                        I don't know if this type of approach is what
you're looking for or not... but I thought I'd share how I dealt with
                        Good luck!
                        Ken Paulsen
                        Jason Lee wrote:

                                I'm trying to write a JSF component
wrapper for the YUI tree component. My first thought on how to
represent the tree's data to the component, based on discussions I've
overheard around the office is TreeNode, with which I have no
experience. I quickly discovered that that interface's package is
javax.swing.tree. My gut reaction is that it seems a little odd to have
a JSF component using a Swing interface for its data model, but I see no
better, existing interface/class with which to model my tree's data.
Another option is to design a custom class for just this purpose.
Anyone have any thoughts? For now, I'll probably keep plodding along
with TreeNode and see what happens in the interim. :)
                                Jason Lee, SCJP

--------------------------------------------------------------------- To
unsubscribe, e-mail: For
additional commands, e-mail:

--------------------------------------------------------------------- To
unsubscribe, e-mail: For
additional commands, e-mail: