org.apache.myfaces.trinidad.model
Class ChildPropertyTreeModel

java.lang.Object
  extended by javax.faces.model.DataModel
      extended by org.apache.myfaces.trinidad.model.CollectionModel
          extended by org.apache.myfaces.trinidad.model.TreeModel
              extended by org.apache.myfaces.trinidad.model.ChildPropertyTreeModel
All Implemented Interfaces:
Iterable, LocalRowKeyIndex, RowKeyIndex, TreeLocalRowKeyIndex
Direct Known Subclasses:
RowKeyPropertyTreeModel

public class ChildPropertyTreeModel
extends TreeModel

Creates a TreeModel from a List of beans. To use this class you must have a tree of beans (or Maps). The structure of your tree must be a List (or array) of root beans, and each bean must have a getter method (or Map property) that returns the children List. All elements of your tree must be the same type.

Suppose you have a bean called EmpBean that contains the data for a particular employee. Suppose this bean has a method called getDirectReports() that returns a List of EmpBeans which are the direct reports of the employee. Suppose there is a List called "founders" which is the root list of EmpBeans. Now you can construct a TreeModel by calling

 TreeModel model = new ChildPropertyTreeModel(founders, "directReports");
 
Bean rules will be used to find an appropriate getter method for the "directReports" property. java.util.Maps are also supported instead of beans.

Example: Given the following class:

 public class Person
 {
    public Person(String name)
    {
      _name = name;
    }
    
    public String getName()
    {
      return _name;
    }
    
    public List getKids()
    {
      return _kids;
    }
    
    private final String _name;
    private final List _kids = new ArrayList();
 }
 
You can construct a tree by:
 Person john = new Person("John Smith");
 Person kim = new Person("Kim Smith");
 Person tom = new Person("Tom Smith");
 Person ira = new Person("Ira Wickrememsinghe");
 Person mallika = new Person("Mallika Wickremesinghe");
 
 john.getKids().add(kim);
 john.getKids().add(tom);
 ira.getKids().add(mallika);
 
 // create the list of root nodes:
 List people = new ArrayList();
 people.add(john);
 people.add(ira);
 
Now you can construct a TreeModel by:
 TreeModel model = new ChildPropertyTreeModel(people, "kids");
 


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.apache.myfaces.trinidad.model.LocalRowKeyIndex
LocalRowKeyIndex.Confidence, LocalRowKeyIndex.LocalCachingStrategy
 
Constructor Summary
ChildPropertyTreeModel()
          No-arg constructor for use with managed-beans.
ChildPropertyTreeModel(Object instance, String childProperty)
          Creates a TreeModel
 
Method Summary
protected  CollectionModel createChildModel(Object childData)
          Converts childData into a CollectionModel.
 void enterContainer()
          This Collection changes to reflect the children of the current rowData, and the current rowData changes to be null.
 void exitContainer()
          Pops back up to the parent collection.
protected  Object getChildData(Object parentData)
          Gets the child data for a node.
 String getChildProperty()
          Gets the property name used to fetch the children.
 Object getContainerRowKey(Object childKey)
          Gets the rowKey of a given child row's container row.
 int getRowCount()
          Gets the number of values in this collection
 Object getRowData()
          Gets the current value identified by the current index or rowKey.
 int getRowIndex()
          Gets the index of the current value.
 Object getRowKey()
          Gets the rowKey of the current row.
 List<SortCriterion> getSortCriteria()
          Gets the criteria that this collection is sorted by.
 Object getWrappedData()
          Gets the instance being wrapped by this TreeModel.
 boolean isContainer()
          Tests to see if the row identified by getRowData() is a container element.
 boolean isRowAvailable()
          Checks to make sure a value exists for the current index or rowKey.
 boolean isSortable(String property)
          Return true if this collection is sortable by the given property.
 void setChildProperty(String childProperty)
          Sets the property name used to fetch the children.
 void setRowIndex(int rowIndex)
          Sets up a value at a particular index to be the current value.
 void setRowKey(Object rowKey)
          Selects a new current row.
 void setSortCriteria(List<SortCriterion> criteria)
          Sorts this collection by the given criteria.
 void setWrappedData(Object data)
          Sets the instance being wrapped by this TreeModel.
 
Methods inherited from class org.apache.myfaces.trinidad.model.TreeModel
areRowsLocallyAvailable, areRowsLocallyAvailable, areRowsLocallyAvailable, getAllAncestorContainerRowKeys, getContainerRowKey, getDepth, getDepth, isChildCollectionLocallyAvailable, isChildCollectionLocallyAvailable, isChildCollectionLocallyAvailable, isContainerEmpty
 
Methods inherited from class org.apache.myfaces.trinidad.model.CollectionModel
addRowKeyChangeListener, areRowsAvailable, areRowsAvailable, areRowsAvailable, areRowsLocallyAvailable, areRowsLocallyAvailable, areRowsLocallyAvailable, clearCachedRow, clearCachedRow, clearCachedRows, clearCachedRows, clearLocalCache, fireRowKeyChange, getCachingStrategy, getEstimatedRowCount, getEstimatedRowCountConfidence, getRowData, getRowData, isRowAvailable, isRowAvailable, isRowLocallyAvailable, isRowLocallyAvailable, removeRowKeyChangeListener
 
Methods inherited from class javax.faces.model.DataModel
addDataModelListener, getDataModelListeners, iterator, removeDataModelListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ChildPropertyTreeModel

public ChildPropertyTreeModel(Object instance,
                              String childProperty)
Creates a TreeModel

Parameters:
instance - The Collection of root nodes of this tree. This can be a List or array of beans (or Maps). This instance is first converted into a CollectionModel (see ModelUtils.toCollectionModel(java.lang.Object)).
childProperty - This property will be used to get at the child Lists from each bean (or Map). Bean rules will be used to find a getter method that matches this property. If each node is a Map, this property will be passed in to the Map's get method to get the child List.

ChildPropertyTreeModel

public ChildPropertyTreeModel()
No-arg constructor for use with managed-beans. Must call the setChildProperty(java.lang.String) and setWrappedData(java.lang.Object) methods after constructing this instance.

Method Detail

getRowKey

public Object getRowKey()
Gets the rowKey of the current row.

Specified by:
getRowKey in interface RowKeyIndex
Specified by:
getRowKey in class CollectionModel
Returns:
this key should be Serializable and immutable.
See Also:
CollectionModel.setRowKey(java.lang.Object)

setRowKey

public void setRowKey(Object rowKey)
Selects a new current row. The row that matches the given rowKey is made current.

Specified by:
setRowKey in interface RowKeyIndex
Specified by:
setRowKey in class CollectionModel
Parameters:
rowKey - use null to access the root collection

getContainerRowKey

public Object getContainerRowKey(Object childKey)
Description copied from class: TreeModel
Gets the rowKey of a given child row's container row.
 |-Root1 (rowKey="r1", containerRowKey=null)
 |  |-Folder1 (rowKey="r1f1", containerRowKey="r1")
 |  |  |-Node1 (rowKey="r1f1n1", containerRowKey="r1f1")
 |  |  |-Node2 (rowKey="r1f1n2", containerRowKey="r1f1")
 

Specified by:
getContainerRowKey in class TreeModel
Parameters:
childKey - the rowKey of the child row.
Returns:
the rowKey of the container, or null if the child is a root row.

getRowCount

public int getRowCount()
Description copied from interface: RowKeyIndex
Gets the number of values in this collection

Specified by:
getRowCount in interface RowKeyIndex
Specified by:
getRowCount in class javax.faces.model.DataModel
Returns:
-1 if the number of values is not known.

getRowData

public Object getRowData()
Description copied from interface: RowKeyIndex
Gets the current value identified by the current index or rowKey.

Specified by:
getRowData in interface RowKeyIndex
Specified by:
getRowData in class javax.faces.model.DataModel
Returns:
null if the current value has been cleared.
See Also:
RowKeyIndex.getRowKey(), RowKeyIndex.getRowIndex()

isRowAvailable

public boolean isRowAvailable()
Description copied from interface: RowKeyIndex
Checks to make sure a value exists for the current index or rowKey. This is useful if the number of values in this collection is not known (See RowKeyIndex.getRowCount()).

Specified by:
isRowAvailable in interface RowKeyIndex
Specified by:
isRowAvailable in class javax.faces.model.DataModel
Returns:
true if a value exists; false otherwise.
See Also:
RowKeyIndex.getRowKey(), RowKeyIndex.getRowIndex()

isContainer

public boolean isContainer()
Description copied from class: TreeModel
Tests to see if the row identified by getRowData() is a container element. Use TreeModel.isContainerEmpty() to see if the current container element actually has children, or is an empty container.

Specified by:
isContainer in class TreeModel
Returns:
true if the current element may contain children.

enterContainer

public void enterContainer()
Description copied from class: TreeModel
This Collection changes to reflect the children of the current rowData, and the current rowData changes to be null. The current rowIndex becomes -1. This method should only be called if TreeModel.isContainer() returns true. DataModel.getRowCount() can be used to get the number of children.

Specified by:
enterContainer in class TreeModel

exitContainer

public void exitContainer()
Description copied from class: TreeModel
Pops back up to the parent collection. The current rowData becomes the rowData of the parent. This Collection will change to include the children of the new rowData.

Specified by:
exitContainer in class TreeModel

getWrappedData

public Object getWrappedData()
Gets the instance being wrapped by this TreeModel.

Specified by:
getWrappedData in class javax.faces.model.DataModel

setWrappedData

public void setWrappedData(Object data)
Sets the instance being wrapped by this TreeModel. Calling this method sets the path to empty.

Specified by:
setWrappedData in class javax.faces.model.DataModel

getChildProperty

public final String getChildProperty()
Gets the property name used to fetch the children.


setChildProperty

public final void setChildProperty(String childProperty)
Sets the property name used to fetch the children.


getRowIndex

public int getRowIndex()
Description copied from interface: RowKeyIndex
Gets the index of the current value. The current value is returned by calling {link #getRowData}

Specified by:
getRowIndex in interface RowKeyIndex
Specified by:
getRowIndex in class javax.faces.model.DataModel
Returns:
the zero-based index of the current value, or -1 if there is no current value

setRowIndex

public void setRowIndex(int rowIndex)
Description copied from interface: RowKeyIndex
Sets up a value at a particular index to be the current value. The current value is returned by calling {link #getRowData}

Specified by:
setRowIndex in interface RowKeyIndex
Specified by:
setRowIndex in class javax.faces.model.DataModel
Parameters:
rowIndex - the zero-based index of the value to make current. Use -1 to clear the current value

isSortable

public boolean isSortable(String property)
Description copied from class: CollectionModel
Return true if this collection is sortable by the given property. This implementation always returns false;

Overrides:
isSortable in class CollectionModel

getSortCriteria

public List<SortCriterion> getSortCriteria()
Description copied from class: CollectionModel
Gets the criteria that this collection is sorted by. This method should never return null. This implementation always returns an empty List.

Overrides:
getSortCriteria in class CollectionModel
Returns:
each element in this List is of type SortCriterion. An empty list is returned if this collection is not sorted.
See Also:
SortCriterion

setSortCriteria

public void setSortCriteria(List<SortCriterion> criteria)
Description copied from class: CollectionModel
Sorts this collection by the given criteria.

Overrides:
setSortCriteria in class CollectionModel
Parameters:
criteria - Each element in this List must be of type SortCriterion. The empty list may be used to cancel any sort order. null should be treated the same as an empty list.
See Also:
SortCriterion

getChildData

protected Object getChildData(Object parentData)
Gets the child data for a node. This child data will be converted into a CollectionModel by calling createChildModel(java.lang.Object).

Parameters:
parentData - the node to get the child data from
Returns:
the List or array that is the child data. must return null for a leaf node.

createChildModel

protected CollectionModel createChildModel(Object childData)
Converts childData into a CollectionModel. This method calls ModelUtils.toCollectionModel(java.lang.Object)

Parameters:
childData - the data to convert. This can be a List or array.


Copyright © 2001-2012 The Apache Software Foundation. All Rights Reserved.