org.apache.myfaces.trinidad.model
Class CollectionModel

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

public abstract class CollectionModel
extends javax.faces.model.DataModel
implements RowKeyIndex

The data model that is used by the Trinidad Table and Iterator components. This extends the Faces DataModel class and adds on support for rowKeys and sorting. Ordinary DataModels are still supported, and will automatically be wrapped into CollectionModels, but without the added functionality.

Row key support

In the Faces DataModel, rows are identified entirely by index. This causes major problems if the underlying data changes from one request to the next - a user request to delete one row may delete a different row because a row got added by another user, etc. To work around this, CollectionModel is based around row keys instead of indices. An implementation of CollectionModel must implement getRowKey() and setRowKey(), and handle conversion from integer indices to row keys. A trivial implementation might simply use Integer objects as the row keys, but a better version could use a unique ID in the row.


Constructor Summary
CollectionModel()
           
 
Method Summary
 java.lang.Object getRowData(int rowIndex)
          Gets the rowData at the given index.
abstract  java.lang.Object getRowKey()
          Gets the rowKey of the current row.
 java.util.List<SortCriterion> getSortCriteria()
          Gets the criteria that this collection is sorted by.
 boolean isRowAvailable(int rowIndex)
          Checks to see if the row at the given index is available.
 boolean isSortable(java.lang.String property)
          Return true if this collection is sortable by the given property.
abstract  void setRowKey(java.lang.Object key)
          Finds the row with the matching key and makes it current
 void setSortCriteria(java.util.List<SortCriterion> criteria)
          Sorts this collection by the given criteria.
 
Methods inherited from class javax.faces.model.DataModel
addDataModelListener, getDataModelListeners, getRowCount, getRowData, getRowIndex, getWrappedData, isRowAvailable, removeDataModelListener, setRowIndex, setWrappedData
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.myfaces.trinidad.model.RowKeyIndex
getRowCount, getRowData, getRowIndex, isRowAvailable, setRowIndex
 

Constructor Detail

CollectionModel

public CollectionModel()
Method Detail

getRowKey

public abstract java.lang.Object getRowKey()
Gets the rowKey of the current row. rowKeys are safer to use than row indices because rowKeys are unaffected by mutations to this collection. rowKeys should have efficient implementations of Object.equals(java.lang.Object) and Object.hashCode() as they will be used as keys in hashtables. rowKeys should also be Serializable, so that the application can run under all JSF state-saving schemes.

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

setRowKey

public abstract void setRowKey(java.lang.Object key)
Finds the row with the matching key and makes it current

Specified by:
setRowKey in interface RowKeyIndex
Parameters:
key - the rowKey, previously obtained from getRowKey().

isRowAvailable

public boolean isRowAvailable(int rowIndex)
Checks to see if the row at the given index is available. This method makes the given row current and calls DataModel.isRowAvailable(). Finally, the row that was current before this method was called is made current again.

Specified by:
isRowAvailable in interface RowKeyIndex
Parameters:
rowIndex - the index of the row to check.
Returns:
true if data for the row exists.

getRowData

public java.lang.Object getRowData(int rowIndex)
Gets the rowData at the given index. This method makes the given row current and calls DataModel.getRowData(). Finally, the row that was current before this method was called is made current again.

Specified by:
getRowData in interface RowKeyIndex
Parameters:
rowIndex - the index of the row to get data from.
Returns:
the data for the given row.

isSortable

public boolean isSortable(java.lang.String property)
Return true if this collection is sortable by the given property. This implementation always returns false;


getSortCriteria

public java.util.List<SortCriterion> getSortCriteria()
Gets the criteria that this collection is sorted by. This method should never return null. This implementation always returns an empty List.

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(java.util.List<SortCriterion> criteria)
Sorts this collection by the given criteria.

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


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