## Navigation tree with pre-defined shelves.

The <rich:tree> component takes one of the main places in the Photo Album and is tightly bounded with the application logic. It helps to represent and implement inherently the "Shelves - Albums" hierarchy. Shelf is the highest possible level in the tree hierarchy, that is used to group thematic albums and may contain as many albums as needed.

There are two types of navigation tree in the application: for a registered user and for a guest. The difference between them is that the first one has a context menu and drag-and-drop possibility.

### Navigation tree for a guest

Navigation tree for a guest is represented as a simple <rich:tree> component.

There are several ways to implement the <rich:tree> on a page. In the current application the <rich:tree> is designed using a model tag <rich:treeNodesAdaptor>.

The <rich:treeNodesAdaptor> component has a "nodes" attribute that accepts a collection of elements, so <rich:treeNodesAdaptor> iterates over the collection and renders a hierarchical tree structure on a page.

According to the "Shelves - Albums" hierarchy we need two nested <rich:treeNodesAdaptor> components. The first one iterates over the Shelves collection that is returned from the `getPredefinedShelves()` method of `ShelfManager.java` class:

    ...

    public List<Shelf> getPredefinedShelves() {

            if (shelves == null) {

                shelves = shelfAction.getPredefinedShelves();

            }

            return shelves;

        }

    }

    ...

The second <rich:treeNodesAdaptor> component iterates over the Albums collection of the current Shelf which is available via "var" attribute. The "var" attribute is used to get access to the data object of the current collection element Shelf, so it is possible to output any necessary data. Let's see the `src/main/webapp/includes/index/tree.xhtml` file:

    ...

    <rich:tree adviseNodeOpened=" {treeManager.adviseNodeSelected}"

                adviseNodeSelected=" {treeManager.adviseNodeSelected}"

                ajaxSubmitSelection="false" id="PreDefinedTree"

                treeNodeVar="treeNode" switchType="client"

                iconCollapsed="/img/shell/tree_icon_plus.png"

                iconExpanded="/img/shell/tree_icon_minus.png"

                showConnectingLines="false">

          <rich:treeNodesAdaptor nodes=" {shelfManager.getPredefinedShelves()}" var="shelf">

                <rich:treeNode style="cursor:pointer" reRender="treePanel,mainArea" selectedClass="tree-selected-node">

                      <f:facet name="icon">

                            <h:graphicImage style="border: none" value="/img/shell/tree_icon_shelf.png">

                                  <a4j:support reRender="treePanel, mainArea" event="onclick" actionListener=" {controller.showShelf(shelf)}" similarityGroupingId="sel" />

                            </h:graphicImage>

                      </f:facet>

                      <a4j:outputPanel  >

                            <h:outputText style="cursor:pointer" value=" {shelf.name}" />

                            <h:outputText value=" :: " />

                            <strong> {shelf.unvisitedImages.size()}</strong> new

                            <a4j:support reRender="treePanel, mainArea" event="onclick" actionListener=" {controller.showShelf(shelf)}" similarityGroupingId="sel" />

                      </a4j:outputPanel>

                </rich:treeNode>

                <rich:treeNodesAdaptor var="album" nodes=" {shelf.albums}">

                      <rich:treeNode style="cursor:pointer" reRender="treePanel,mainArea" selectedClass="tree-selected-node" icon="img/shell/tree_icon_album.png">

                            <f:facet name="iconLeaf">

                                  <h:graphicImage style="border: none" value="img/shell/tree_icon_album.png">

                                        <a4j:support reRender="treePanel, mainArea" event="onclick" actionListener=" {controller.showAlbum(album)}" similarityGroupingId="sel" />

                                  </h:graphicImage>

                            </f:facet>

                            <a4j:outputPanel>

                                  <h:outputText style="cursor:pointer" value=" {album.name}" />

                                  <h:outputText value=" :: " />

                                  <strong> {album.unvisitedImages.size()}</strong> new

                                  <a4j:support reRender="treePanel, mainArea" event="onclick" actionListener=" {controller.showAlbum(album)}" similarityGroupingId="sel" />

                            </a4j:outputPanel>

                      </rich:treeNode>

                </rich:treeNodesAdaptor>

          </rich:treeNodesAdaptor>

    </rich:tree>

    ...

The image below shows how the navigation tree for a guest is rendered on the page.

Shelves and albums nodes rendered with the help of the <rich:treeNodesAdaptor>