# The <rich:tree> component

The component is designed for hierarchical data presentation and is applied for building a tree structure. The component also uses built-in drag and drop.

The <rich:tree> component in the PhotoAlbum application helps to represent and realize inherently the "Shelves-Albums-Photos" paradigm. Shelf is the highest possible level in the tree hierarchy. Shelfs are used to group thematic albums and may contain as many albums as needed. Photos are not represented in the tree because of their possible big quantities, which can make tree clumsy. Photos by themselves are rendered in the center of the screen (hereinafter referred to as watching area).

Implicitly, the <rich:tree> components takes one of the main position in the PhotoAlbum and bounded with many application logic. There are several ways to realize the <rich:tree>. In the current application the <rich:tree> is realized using model tag <rich:treeNodesAdaptor>.

Using the <rich:treeNodesAdaptor> means that the typical hierarchical tree structure is built on page straight in the component tree. The code snippet below shows the idea of how the tree can be built on the page with the help of adaptors:

    <rich:tree>
    	<rich:treeNodesAdaptor>
    		</rich:treeNode />

    		<rich:treeNodesAdaptor>
    			</rich:treeNode />

    			<rich:treeNodesAdaptor>
    				</rich:treeNode />

    				<rich:treeNodesAdaptor>
    					</rich:treeNode />
    				</rich:treeNodesAdaptor>

    			</rich:treeNodesAdaptor>
    		</rich:treeNodesAdaptor>
    	</rich:treeNodesAdaptor>
    </rich:tree>

The NodesAdaptors completely repeat the tree hierarchy that takes place in an application. Adaptors may be nested one into another as deeply as needed.

When using <rich:treeNodesAdaptor> component there is no need to specify the "value" and "var" attributes for the <rich:tree>. The values for nodes to be rendered are passed directly into the corresponding adaptor and the component do all the necessary iterative job.

The realization of the <rich:tree> in the application is very close to the model shown above. The <rich:treeNodesAdaptor>component has "nodes" attribute. This attribute accepts collections that may include lists, arrays, maps, XML NodeList and NamedNodeMap either as single objects. The top <rich:treeNodesAdaptor> in the application responses for shelfs rendering. It "nodes" attribute refers to getShelfs() method of ShelfManager class and gets the collection of shelfs associated with the current user and all the shared shelfs in the system. Take a look at this method:

    	public List<Shelf> getShelfs(){
    		if(shelfs == null){
    			shelfs = shelfAction.getShelfs(user);
    		}
    		return shelfs;
    	}

After the "nodes" attribute of "shelf" <rich:treeNodesAdaptor> receives the collection of shelfs, it switches to the nested "album" <rich:treeNodesAdaptor>, which "nodes" attribute refers in it turn to albums field of the Shelf class and takes the collection of all albums associated with the current shelf. The <rich:treeNodesAdaptor> renders all the albums that belongs to current iterating shelf and switches back to the "shelf" adaptor to render and iterate the next shelf in the received earlier collection. Here is how it looks in the webapp/includes/index/tree.xhtml file: