Reimplement all UIData functionality to be able to have (protected) access
the internal DataModel.
| Method from org.apache.myfaces.component.html.ext.HtmlDataTableHack Detail: |
protected DataModel createDataModel() {
Object value = getValue();
if (value == null)
{
return EMPTY_DATA_MODEL;
}
else if (value instanceof DataModel)
{
return (DataModel) value;
}
else if (value instanceof List)
{
return new ListDataModel((List) value);
}
// accept a Collection is not supported in the Spec
else if (value instanceof Collection)
{
return new ListDataModel(new ArrayList((Collection) value));
}
else if (OBJECT_ARRAY_CLASS.isAssignableFrom(value.getClass()))
{
return new ArrayDataModel((Object[]) value);
}
else if (value instanceof ResultSet)
{
return new ResultSetDataModel((ResultSet) value);
}
else if (value instanceof Result)
{
return new ResultDataModel((Result) value);
}
else
{
return new ScalarDataModel(value);
}
}
Creates a new DataModel around the current value. |
public void encodeBegin(FacesContext context) throws IOException {
_initialDescendantComponentState = null;
if (_isValidChilds && !hasErrorMessages(context))
{
//Refresh DataModel for rendering:
_dataModelMap.clear();
if (!isPreserveRowStates())
{
_rowStates.clear();
}
}
super.encodeBegin(context);
}
|
public void encodeEnd(FacesContext context) throws IOException {
setRowIndex(-1);
super.encodeEnd(context);
}
|
public String getClientId(FacesContext context) {
String clientId = super.getClientId(context);
int rowIndex = getRowIndex();
if (rowIndex == -1)
{
return clientId;
}
// the following code tries to avoid rowindex to be twice in the client id
int index = clientId.lastIndexOf(NamingContainer.SEPARATOR_CHAR);
if(index != -1)
{
String rowIndexString = clientId.substring(index + 1);
try
{
if(Integer.parseInt(rowIndexString) == rowIndex)
{
return clientId;
}
}
catch(NumberFormatException e)
{
return clientId + NamingContainer.SEPARATOR_CHAR + rowIndex;
}
}
return clientId + NamingContainer.SEPARATOR_CHAR + rowIndex;
}
Hack since RI does not call getRowIndex() |
protected DataModel getDataModel() {
DataModel dataModel = null;
String clientID = "";
UIComponent parent = getParent();
if (parent != null)
{
clientID = parent.getClientId(getFacesContext());
}
dataModel = (DataModel) _dataModelMap.get(clientID);
if (dataModel == null)
{
dataModel = createDataModel();
_dataModelMap.put(clientID, dataModel);
}
return dataModel;
}
|
public int getRowCount() {
return getDataModel().getRowCount();
}
|
public Object getRowData() {
return getDataModel().getRowData();
}
|
public int getRowIndex() {
return _rowIndex;
}
|
protected boolean hasErrorMessages(FacesContext context) {
for(Iterator iter = context.getMessages(); iter.hasNext();)
{
FacesMessage message = (FacesMessage) iter.next();
if(FacesMessage.SEVERITY_ERROR.compareTo(message.getSeverity()) < = 0)
{
return true;
}
}
return false;
}
|
public boolean isForceId() {
if (_forceId != null) return _forceId.booleanValue();
ValueBinding vb = getValueBinding("forceId");
return vb != null && booleanFromObject(vb.getValue(getFacesContext()), false);
}
|
public boolean isPreserveRowStates() {
if (_preserveRowStates != null)
return _preserveRowStates.booleanValue();
ValueBinding vb = getValueBinding("preserveRowStates");
Boolean v = vb != null ? (Boolean) vb.getValue(getFacesContext()) : null;
return v != null ? v.booleanValue() : DEFAULT_PRESERVEROWSTATES;
}
|
public boolean isRowAvailable() {
return getDataModel().isRowAvailable();
}
|
public void processUpdates(FacesContext context) {
super.processUpdates(context);
// check if a update model error forces the render response for our data
if (context.getRenderResponse())
{
_isValidChilds = false;
}
}
|
public void processValidators(FacesContext context) {
super.processValidators(context);
// check if a validation error forces the render response for our data
if (context.getRenderResponse())
{
_isValidChilds = false;
}
}
|
protected void restoreDescendantComponentStates(Iterator childIterator,
Object state,
boolean restoreChildFacets) {
Iterator descendantStateIterator = null;
while (childIterator.hasNext())
{
if (descendantStateIterator == null && state != null)
{
descendantStateIterator = ((Collection) state).iterator();
}
UIComponent component = (UIComponent) childIterator.next();
// reset the client id (see spec 3.1.6)
component.setId(component.getId());
if(!component.isTransient())
{
Object childState = null;
Object descendantState = null;
if (descendantStateIterator != null
&& descendantStateIterator.hasNext())
{
Object[] object = (Object[]) descendantStateIterator.next();
childState = object[0];
descendantState = object[1];
}
if (childState != null && component instanceof EditableValueHolder)
{
((EditableValueHolderState) childState)
.restoreState((EditableValueHolder) component);
}
Iterator childsIterator;
if (restoreChildFacets)
{
childsIterator = component.getFacetsAndChildren();
}
else
{
childsIterator = component.getChildren().iterator();
}
restoreDescendantComponentStates(childsIterator, descendantState,
true);
}
}
}
|
public void restoreState(FacesContext context,
Object state) {
Object[] values = (Object[])state;
super.restoreState(context, values[0]);
_preserveRowStates = (Boolean) values[1];
}
|
protected Object saveDescendantComponentStates(Iterator childIterator,
boolean saveChildFacets) {
Collection childStates = null;
while (childIterator.hasNext())
{
if (childStates == null)
{
childStates = new ArrayList();
}
UIComponent child = (UIComponent) childIterator.next();
if(!child.isTransient())
{
Iterator childsIterator;
if (saveChildFacets)
{
childsIterator = child.getFacetsAndChildren();
}
else
{
childsIterator = child.getChildren().iterator();
}
Object descendantState = saveDescendantComponentStates(
childsIterator, true);
Object state = null;
if (child instanceof EditableValueHolder)
{
state = new EditableValueHolderState(
(EditableValueHolder) child);
}
childStates.add(new Object[] { state, descendantState });
}
}
return childStates;
}
|
public Object saveState(FacesContext context) {
Object[] values = new Object[2];
values[0] = super.saveState(context);
values[1] = _preserveRowStates;
return values;
}
|
protected void setDataModel(DataModel datamodel) {
UIComponent parent = getParent();
String clientID = "";
if(parent != null)
{
clientID = parent.getClientId(getFacesContext());
}
_dataModelMap.put(clientID, datamodel);
}
|
public void setForceId(boolean b) {
_forceId = Boolean.valueOf(b);
}
|
public void setPreserveRowStates(boolean preserveRowStates) {
_preserveRowStates = Boolean.valueOf(preserveRowStates);
}
|
public void setRowIndex(int rowIndex) {
if (rowIndex < -1)
{
throw new IllegalArgumentException("rowIndex is less than -1");
}
if (_rowIndex == rowIndex)
{
return;
}
FacesContext facesContext = getFacesContext();
if (_rowIndex == -1)
{
if (_initialDescendantComponentState == null)
{
_initialDescendantComponentState = saveDescendantComponentStates(getChildren()
.iterator(), false);
}
}
else
{
_rowStates.put(getClientId(facesContext),
saveDescendantComponentStates(getChildren()
.iterator(), false));
}
_rowIndex = rowIndex;
DataModel dataModel = getDataModel();
dataModel.setRowIndex(rowIndex);
String var = getVar();
if (rowIndex == -1)
{
if (var != null)
{
facesContext.getExternalContext().getRequestMap().remove(var);
}
}
else
{
if (var != null)
{
if (isRowAvailable())
{
Object rowData = dataModel.getRowData();
facesContext.getExternalContext().getRequestMap().put(var,
rowData);
}
else
{
facesContext.getExternalContext().getRequestMap().remove(
var);
}
}
}
if (_rowIndex == -1)
{
restoreDescendantComponentStates(getChildren().iterator(),
_initialDescendantComponentState, false);
}
else
{
Object rowState = _rowStates.get(getClientId(facesContext));
if (rowState == null)
{
restoreDescendantComponentStates(getChildren().iterator(),
_initialDescendantComponentState, false);
}
else
{
restoreDescendantComponentStates(getChildren().iterator(),
rowState, false);
}
}
}
|
public void setValue(Object value) {
super.setValue(value);
_dataModelMap.clear();
_rowStates.clear();
_isValidChilds = true;
}
|
public void setValueBinding(String name,
ValueBinding binding) {
if (name == null)
{
throw new NullPointerException("name");
}
else if (name.equals("value"))
{
_dataModelMap.clear();
}
else if (name.equals("var") || name.equals("rowIndex"))
{
throw new IllegalArgumentException(
"You can never set the 'rowIndex' or the 'var' attribute as a value-binding. Set the property directly instead. Name " + name);
}
super.setValueBinding(name, binding);
}
|