JBoss.orgCommunity Documentation
The component is designed for hierarchical data presentation and is applied for building a tree structure with a drag-and-drop capability.
Highly customizable look-and-feel
Built-in drag and drop capability, than enable relocating tree nodes within the tree
Built-in Ajax processing
Possibility to define a visual representation by node type
Support of several root elements in a tree
As it has been mentioned above the <rich:tree> component allows rendering any tree-like data model.
You can build your
<rich:tree>
using model (org.richfaces.model.TreeNode
or
javax.swing.tree.TreeNode
). In this case the
<rich:tree>
component interacts with data model via
"TreeNode"
interface ( org.richfaces.model.TreeNode ) that is used for
the
<rich:tree>
nodes representation.
Actually you can develop and use your own implementation of the
"TreeNode"
interface or use a
default one, which is defined with a default class
"TreeNodeImpl"
( org.richfaces.model.TreeNodeImpl ).
The "value" attribute of the <rich:tree> component contains a nodes structure defined in a bean property.
When the
<rich:tree>
component is being rendered it iterates over the model
nodes and renders them using one of its immediate
<rich:treeNode>
children. Data property of the current model
TreeNode
is exposed using
"var"
attribute, so if
var="station"
you can refer
to that data using #{station}
syntax.
In the following example the
<rich:tree>
is built from a simple
org.richfaces.model.TreeNode
model:
...
private TreeNodeImpl<String> stationRoot = new TreeNodeImpl<String>();
private TreeNodeImpl<String> stationNodes = new TreeNodeImpl<String>();
private String[] kickRadioFeed = { "Hall & Oates - Kiss On My List",
"David Bowie - Let's Dance", "Lyn Collins - Think (About It)",
"Kim Carnes - Bette Davis Eyes",
"KC & the Sunshine Band - Give It Up" };
...
stationRoot.setData("KickRadio");
stationNodes.addChild(0, stationRoot);
for (int i = 0; i < kickRadioFeed.length; i++){
TreeNodeImpl<String> child = new TreeNodeImpl<String>();
child.setData(kickRadioFeed[i]);
stationRoot.addChild(i, child);
}
...
As it is mentioned before you need to pass
#{stations.stationNodes}
property to the
"value"
attribute and define the
"var"
attribute in order to refer to the data:
...
<rich:tree value="#{stations.stationNodes}" var="station">
<rich:treeNode>
<h:outputText value="#{station}" />
</rich:treeNode>
</rich:tree>
...
This is a result:
Implementation of the
<rich:tree>
component provides another way to build a tree. This
approach implies using a
"XmlTreeDataBuilder"
class
( org.richfaces.component.xml.XmlTreeDataBuilder )
that allows to transform XML into structures of objects containing
"XmlNodeData"
( org.richfaces.component.xml.XmlNodeData )
instances as data, which could be represented by the
<rich:tree>
component.
Let's try to build a simple <rich:tree> from a local XML file. In the following example a simple XML file (stations.xml) is used as a radio station playlist:
<?xml version="1.0"?>
<station name="KickRadio">
<feed date="today">
<song time="07:00">Hall & Oates - Kiss On My List</song>
<song time="07:03">David Bowie - Let's Dance</song>
<song time="07:06">Lyn Collins - Think (About It)</song>
<song time="07:10">Kim Carnes - Bette Davis Eyes</song>
<song time="07:15">KC & the Sunshine Band - Give It Up</song>
</feed>
</station>
Now you need to create a bean that holds a model nodes:
...
private TreeNode data;
...
FacesContext context = FacesContext.getCurrentInstance();
data = XmlTreeDataBuilder.build(new InputSource(getClass().getResourceAsStream("stations.xml")));
...
Finally you should set the
"value"
attribute to the data
bean property and
define the
"var"
attribute in order to refer to the data of nodes:
...
<rich:tree id="treeXML" value="#{stations.data}" var="vardata">
<rich:treeNode>
<h:outputText value="#{vardata.attributes['name']}" />
<h:outputText value="#{vardata.attributes['date']}" />
<h:outputText value="#{vardata.attributes['time']}" />
<h:outputText value=" #{vardata.text}" />
</rich:treeNode>
</rich:tree>
...
This is a result:
It's possible to define a visual representation of a node data model (to define a node icon) and its behavior in correspondence with the data contained in this node (with a value of the "var" attribute). The node behavior is defined by the components nested into the <rich:treeNode> (e.g. links or buttons). For these purposes you should use "nodeFace" attribute. For each tree node a value of "nodeFace" attribute is evaluated and <rich:treeNode> with a value of "type" attribute equal to a value of "nodeFace" is used for node representation. See an example below.
...
<h:form>
<rich:tree style="width:300px" value="#{library.data}" var="item" nodeFace="#{item.type}">
<rich:treeNode type="artist" iconLeaf="/images/tree/singer.png" icon="/images/tree/singer.png">
<h:outputText value="#{item.name}" />
</rich:treeNode>
<rich:treeNode type="album" iconLeaf="/images/tree/disc.png" icon="/images/tree/disc.png">
<h:outputText value="#{item.title}" />
</rich:treeNode>
<rich:treeNode type="song" iconLeaf="/images/tree/song.png" icon="/images/tree/song.png">
<h:outputText value="#{item.title}" />
</rich:treeNode>
</rich:tree>
</h:form>
...
This is a result:
In the example above, when each node of data model is processed, data
contained in the "data"
property of
"TreeNode" interface
is assigned to a request scope variable, which name is defined with
"var"
attribute. The value of the
"nodeFace"
attribute is evaluated in correspondence with the data
assigned to the
"var"
attribute. The corresponding
<rich:treeNode>
component (with a value of
"type"
attribute equal to a value of
"nodeFace"
) is used for the node representation. For example, during
data model processing, an object with a name "Chris
Rea" was inserted in the
"var"
attribute. Then the value of
"nodeFace"
attribute was evaluated as
"artist"
. Thus, for the node
representation the
<rich:treeNode>
with
"type"
equal to "artist
" was
used.
You can also assign an EL-expression as value of the "nodeFace" attribute. See an example below:
Example:
nodeFace="#{data.name != 'param-value' ? 'artist' : 'album'}"
There are some essential points in a "nodeFace" attribute usage: you need to define notions for typeless and a default nodes.
The typeless node is the first <rich:treeNode> component (from all children nodes nested to the <rich:tree> component) with not defined "type" attribute and defined "rendered" attribute. The typeless node is used for representation when "nodeFace" attribute is null.
Default node has the following interior presentation:
Example:
...
<h:outputText value="#{varAttributeName}">
...
"varAttributeName" is a value for "var" attribute.
Default node is used in the following cases:
"nodeFace" attribute is defined, but its value isn't equal to any "type" attribute value from all children nodes;
"nodeFace" attribute is defined and its value is equal to a value of some "type" attribute from all children nodes, but the value of "rendered" attribute for this node is "false".
There is also one thing that has to be remembered using "type" and "rendered" attributes: it's possible to define several <rich:treeNode> components with equal values of "type" attribute and different values of "rendered" attribute. It provides a possibility to define different representation styles for the same node types. In the example with artists and their albums (see above) it's possible to represent albums that are available for sale and albums that are not available. Please study the example below:
Example:
...
<h:form>
<rich:tree style="width:300px" value="#{library.data}" var="item" nodeFace="#{item.type}">
...
<rich:treeNode type="album" iconLeaf="/images/tree/album.gif" icon="/images/tree/album.gif"
rendered="#{item.exist}">
<h:outputText value="#{item.name}" />
</rich:treeNode>
<rich:treeNode type="album" iconLeaf="/images/tree/album_absent.gif" icon="/images/tree/album_absent.gif"
rendered="#{not item.exist}">
<h:outputText value="#{item.name}" />
</rich:treeNode>
...
</rich:tree>
</h:form>
...
This is a result of the code:
In the example the <rich:treeNode> components has equal values of the "type" attribute. Depending on value of the "rendered" attribute the corresponding <rich:treeNode> component is selected for node representation. If an album is available for sale the value of the "rendered" for the first <rich:treeNode> component is "true", for the second one is "false". Thus, the first <rich:treeNode> is selected for node representation.
Tree node can be run in tree modes. Modes can be specified with "switchType" attribute for <rich:tree> component.
Ajax
(default value) - Ajax submission is
used performing the functionality. Note, that for
collapse/expand operations an Ajax request is sent
to the server and it can cause a short
delay.
Server
- regular form of submission
request is used.
Client
– all operations are performed
totally on the client; no interaction with a
server is involved. Full page content is reloaded
after every action.
The "icon" , "iconCollapsed" , "iconExpanded" , "iconLeaf" attributes set the icons' images for the component. You can also define icons using facets with the same names. If the facets are defined, the corresponding attributes are ignored and facets' content is used as icons. By default the width of a rendered facet area is 16px.
Example:
...
<rich:tree value="#{library.data}" var="item">
...
<f:facet name="icon">
<h:graphicImage value="/images/tree/singer.png "/>
</f:facet>
<f:facet name="iconCollapsed">
<h:graphicImage value="/images/tree/singer.png" />
</f:facet>
<f:facet name="iconExpanded">
<h:graphicImage value="/images/tree/singer.png" />
</f:facet>
<f:facet name="iconLeaf">
<h:graphicImage value="/images/tree/song.png" />
</f:facet>
...
</rich:tree>
...
The <rich: tree> component can be used together with <rich: treeNodeAdaptor> . In this case there is no need to specify the attributes "value" and "var" . Besides, visual representation shouldn't be defined right in the tree. In this case a <rich: tree> tag is applied mainly for defining common attributes such as "ajaxSubmitSelection" etc.
Information about the "process" attribute usage you can find in the "Decide what to process" guide section.
As it's mentioned before, the <rich:tree> component uses a data model to represent the tree-like nodes structure on the page. To identify a particular node during a client request, the model provides a set of unique keys for tree nodes. The <rich:tree> can use strings as keys values which may contain special characters not allowed by browsers, such as the left angle bracket (<), ampersand (&), ant etc. Thus, to have a possibility to use unallowed characters in the tree nodes keys, the following converters are provided:
org.richfaces.TreeRowKeyConverter
that is
used for "TreeNode" based trees.
The key should be of a
java.lang.String
type.
org.richfaces.TreeAdaptorRowKeyConverter
that is used for adaptor-based trees (see
<rich:treeNodesAdaptor>
,
<rich:recursiveTreeNodesAdaptor>
). The key should be of a
java.lang.String
type.
org.richfaces.TreeAdaptorIntegerRowKeyConverter
which is provided for adaptor-based trees. The key
should be of a java.lang.Integer
type.
The converters can be extended in order to have a possibility for implementing custom converters.
To apply a converter to the <rich:tree> component, you should define it as a value of the "rowKeyConverter" attribute.
Have a look at the example of a tree which contains the RichFaces
components as its nodes and the components attributes as the nodes
child elements. As the components have unallowed characters (<
and >) in their names, the org.richfaces.TreeRowKeyConverter
is used here.
Example:
...
<rich:tree value="#{treeBean.data}" var="node" switchType="ajax" rowKeyConverter="org.richfaces.TreeRowKeyConverter">
<rich:treeNode ajaxSingle="true">
<h:outputText value="#{node}"/>
</rich:treeNode>
</rich:tree>
...
In the example the tree uses the following data model:
...
String[ ] components = {"< a4j:ajaxListener >", "< a4j:keepAlive >", "< a4j:actionparam >" };
String[ ][ ] attributes = {{"type"},
{"ajaxOnly", "beanName"},
{"actionListener", "assignTo", "binding", "converter", "id", "name", "noEscape", "value"}};
data = new TreeNodeImpl<String>();
for (int i = 0; i < components.length; i++) {
TreeNode<String> child = new TreeNodeImpl<String>();
child.setData(components[i]);
data.addChild(components[i], child);
for (int j = 0; j < attributes[i].length; j++) {
TreeNode<String> grandChild = new TreeNodeImpl<String>();
grandChild.setData(attributes[i][j]);
child.addChild(attributes[i][j], grandChild);
}
}
...
Words "built-in" in this context mean, that <rich:tree> component has its own attributes, that provide drag-and-drop capability. These attributes can be divided into two groups: those ones which provide drag and those which provide drop operations (see the tables below).
Table 6.91. Drag group
Attribute Name | Description |
---|---|
dragValue | Element value drag passed into processing after a Drop event |
dragListener | A listener that processes a Drag event |
dragIndicator | Id of a component that is used as a drag pointer during the drag operation |
dragType | Defines a drag zone type that is used for definition of a dragged element, which can be accepted by a drop zone |
Table 6.92. Drop group
Attribute Name | Description |
---|---|
dropValue | Element value drop passed into processing after Drop events |
dropListener | A listener that processes a Drop event. |
acceptedTypes | Drag zone names are allowed to be processed with a Drop zone |
typeMapping | Drag zones names mapping on the corresponding drop zone parameters |
Consider drag-and-drop inside a tree. All zones, which are assumed to be dragged, must be marked. In terms of <rich:tree> these zones completely correspond to tree nodes. So, all dragging nodes should be marked with "dragType" attribute. Then, to mark zone(-s), where the dragging node could be dropped, pass the type of dragging node to the "acceptedTypes" attribute of the drop zone. It would be good to itemize, that each tree node in the <rich:tree> component’s structure has its own key. Depending on how the component is used, these keys can be generated by the component itself or can be taken from the component’s data model. Keys help to identify each node in a tree; key is what exactly being passing from one node to another in drag-and-drop operations. Finally, the method binding, that will process drag-and-drop operation, should be pointed via "dropListener" attribute of the <rich:tree> .
Chapters "6.40 <dragIndicator>" and "6.39 <dndParam>" describes how to apply visual element, that show some additional information (e.g. dragging item name) while operating with drag-and-drop.
Page code, that describes a tree with built in drag-and-drop in the way it is considered, is shown below.
Example:
...
<h:form>
<rich:tree style="width:300px" value="#{libraryAjaxTree.data}" nodeFace="#{item.type}" var="item" dragIndicator=":treeDragIndicator" dropListener="#{libraryAjaxTree.processDrop}">
<rich:treeNode type="artist" icon="/images/tree/group.png" iconLeaf="/images/tree/group.png" acceptedTypes="album">
<h:outputText value="#{item.name}" />
</rich:treeNode>
<rich:treeNode type="album" icon="/images/tree/cd.png" iconLeaf="/images/tree/cd.png" dragType="album" acceptedTypes="song">
<h:outputText value="#{item.title}" />
<rich:dndParam name="label" type="drag" value="Album: #{item.title}" />
</rich:treeNode>
<rich:treeNode type="song" icon="/images/tree/music.png" iconLeaf="/images/tree/music.png" dragType="song">
<h:outputText value="#{item.title}" />
<rich:dndParam name="label" type="drag" value="Song: #{item.title}" />
</rich:treeNode>
</rich:tree>
</h:form>
...
This code renders following tree:
Listeners classes that process events on the server side are defined with the help of:
changeExpandListener processes expand/collapse event of a treeNode
dropListener processes a Drop event
dragListener processes a Drag event
nodeSelectListener is called during request sending on a node selecting event (if request sending on this event is defined)
Listener methods can be defined using the following attributes or using nested tags.
Client event attributes are:
"onexpand" is a script expression to invoke when a node is expanded
"oncollapse" is a script expression to invoke when a node is collapsed
"ondragexit" is a script expression to invoke when an element passing out from a tree zone
"ondragstart" is a script expression to invoke when dragging starts
"ondragend" is a script expression to invoke when dragging ends (a drop event)
"ondragenter" is a script expression to invoke when a dragged element appears on a tree
They can be used to add some JavaScript effects.
Standart HTML event attributes like "onclick" , "onmousedown" , "onmouseover" etc. can be also used. Event handlers of a <rich:tree> component capture events occured on any tree part. But event handlers of treeNode capture events occured on treeNode only, except for children events.
Table of <rich:tree> attributes.
Table 6.93. Component Identification Parameters
Name | Value |
---|---|
component-type | org.richfaces.Tree |
component-class | org.richfaces.component.html.HtmlTree |
component-family | org.richfaces.Tree |
renderer-type | org.richfaces.TreeRenderer |
tag-class | org.richfaces.taglib.TreeTag |
Table 6.94. Facets
Facet name | Description |
---|---|
icon | Redefines the icon for node. Related attribute is "icon" |
iconCollapsed | Redefines the icon for collapsed node. Related attribute is "iconCollapsed" |
iconExpanded | Redefines the icon for expanded node. Related attribute is "iconExpanded" |
iconLeaf | Redefines the icon for component leaves. Related attribute is "iconLeaf" |
Table 6.95. Classes names that define a component appearance
Class name | Description |
---|---|
rich-tree | Defines styles for a wrapper <div> element of a tree |
On the component LiveDemo page you can see the example of <rich:tree> usage and sources for the given example.
How to Expand/Collapse Tree Nodes from code, see in thiswiki article.
Read RichFaces Tree FAQ to know how to avoid problem with showing only two levels of node when tree actually contains more.