You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2010/08/19 07:11:31 UTC
svn commit: r987019 - in /myfaces/tomahawk/trunk/core20/src:
main/java/org/apache/myfaces/component/html/ext/
test/java/org/apache/myfaces/component/html/ext/
Author: lu4242
Date: Thu Aug 19 05:11:31 2010
New Revision: 987019
URL: http://svn.apache.org/viewvc?rev=987019&view=rev
Log:
TOMAHAWK-1533 Add preserveRowComponentState to t:dataTable
Added:
myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/_DetailStampFacetAndChildrenIterator.java
myfaces/tomahawk/trunk/core20/src/test/java/org/apache/myfaces/component/html/ext/HtmlDataTablePreserveRowComponentStateTest.java
Modified:
myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/AbstractHtmlDataTable.java
myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/HtmlDataTableHack.java
Modified: myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/AbstractHtmlDataTable.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/AbstractHtmlDataTable.java?rev=987019&r1=987018&r2=987019&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/AbstractHtmlDataTable.java (original)
+++ myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/AbstractHtmlDataTable.java Thu Aug 19 05:11:31 2010
@@ -453,11 +453,11 @@ public abstract class AbstractHtmlDataTa
UIComponent facet = getFacet(HtmlTableRenderer.DETAIL_STAMP_FACET_NAME);
/*Just for obtaining an iterator which must be passed to saveDescendantComponentStates()*/
- Set set = new HashSet();
- set.add(facet);
+ ArrayList<UIComponent> detailStampList = new ArrayList<UIComponent>(1);
+ detailStampList.add(facet);
if (getRowIndex() != -1 && facet != null)
{
- _detailRowStates.put(getContainerClientId(facesContext), saveDescendantComponentStates(set.iterator(), false));
+ _detailRowStates.put(getContainerClientId(facesContext), saveDescendantComponentStates(detailStampList.iterator(), false));
}
String rowIndexVar = getRowIndexVar();
@@ -525,7 +525,7 @@ public abstract class AbstractHtmlDataTa
{
Object rowState = _detailRowStates.get(getContainerClientId(facesContext));
- restoreDescendantComponentStates(set.iterator(),
+ restoreDescendantComponentStates(detailStampList.iterator(),
rowState, false);
}
@@ -536,6 +536,67 @@ public abstract class AbstractHtmlDataTa
requestMap.put(getStateHelper().get(PropertyKeys.varDetailToggler), this);
}
}
+
+ @Override
+ protected void restoreFullDescendantComponentDeltaStates(FacesContext facesContext,
+ Map<String, Object> rowState, Object initialState)
+ {
+ if (getFacetCount() > 0)
+ {
+ UIComponent detailStampFacet = getFacet(DETAIL_STAMP_FACET_NAME);
+ if (detailStampFacet != null)
+ {
+ restoreFullDescendantComponentDeltaStates(facesContext, new _DetailStampFacetAndChildrenIterator(detailStampFacet, getChildren()), rowState, initialState, false, getContainerClientId(facesContext));
+ return;
+ }
+ }
+ restoreFullDescendantComponentDeltaStates(facesContext, getChildren().iterator(), rowState, initialState, false, getContainerClientId(facesContext));
+ }
+
+ @Override
+ protected void restoreFullDescendantComponentStates(FacesContext facesContext,
+ Object initialState)
+ {
+ if (getFacetCount() > 0)
+ {
+ UIComponent detailStampFacet = getFacet(DETAIL_STAMP_FACET_NAME);
+ if (detailStampFacet != null)
+ {
+ restoreFullDescendantComponentStates(facesContext, new _DetailStampFacetAndChildrenIterator(detailStampFacet, getChildren()), initialState, false);
+ return;
+ }
+ }
+ restoreFullDescendantComponentStates(facesContext, getChildren().iterator(), initialState, false);
+ }
+
+ @Override
+ protected Collection<Object[]> saveDescendantInitialComponentStates(
+ FacesContext facesContext)
+ {
+ if (getFacetCount() > 0)
+ {
+ UIComponent detailStampFacet = getFacet(DETAIL_STAMP_FACET_NAME);
+ if (detailStampFacet != null)
+ {
+ return saveDescendantInitialComponentStates(facesContext, new _DetailStampFacetAndChildrenIterator(detailStampFacet, getChildren()), false);
+ }
+ }
+ return saveDescendantInitialComponentStates(facesContext, getChildren().iterator(), false);
+ }
+
+ @Override
+ protected Map<String,Object> saveFullDescendantComponentStates(FacesContext facesContext)
+ {
+ if (getFacetCount() > 0)
+ {
+ UIComponent detailStampFacet = getFacet(DETAIL_STAMP_FACET_NAME);
+ if (detailStampFacet != null)
+ {
+ return saveFullDescendantComponentStates(facesContext, null, new _DetailStampFacetAndChildrenIterator(detailStampFacet, getChildren()), false, getContainerClientId(facesContext));
+ }
+ }
+ return saveFullDescendantComponentStates(facesContext, null, getChildren().iterator(), false, getContainerClientId(facesContext));
+ }
public void processDecodes(FacesContext context)
{
@@ -725,11 +786,11 @@ public abstract class AbstractHtmlDataTa
if ( rowIndex == (last - 1) )
{
- Set set = new HashSet();
- set.add(facet);
+ ArrayList<UIComponent> detailStampList = new ArrayList<UIComponent>(1);
+ detailStampList.add(facet);
_detailRowStates.put(
getContainerClientId(FacesContext.getCurrentInstance()),
- saveDescendantComponentStates(set.iterator(),false));
+ saveDescendantComponentStates(detailStampList.iterator(),false));
}
}
}
@@ -886,6 +947,19 @@ public abstract class AbstractHtmlDataTa
}
_preservedDataModel = null;
}
+
+ public void markInitialState()
+ {
+ if (isPreserveRowComponentState())
+ {
+ if (getFacesContext().getAttributes().containsKey("org.apache.myfaces.MARK_INITIAL_STATE"))
+ {
+ //Since we re
+ replaceColumnsWithCommandSortHeadersIfNeeded(getFacesContext());
+ }
+ }
+ super.markInitialState();
+ }
public void encodeBegin(FacesContext context) throws IOException
{
@@ -907,7 +981,16 @@ public abstract class AbstractHtmlDataTa
((UIColumns) component).encodeTableBegin(context);
}
}
+
+ replaceColumnsWithCommandSortHeadersIfNeeded(context);
+ // Now invoke the superclass encodeBegin, which will eventually
+ // execute the encodeBegin for the associated renderer.
+ super.encodeBegin(context);
+ }
+
+ private void replaceColumnsWithCommandSortHeadersIfNeeded(FacesContext context)
+ {
//replace facet header content component of the columns, with a new command sort header component
//if sortable=true, replace it for all, if not just for the columns marked as sortable
for (Iterator iter = getChildren().iterator(); iter.hasNext();)
@@ -1020,10 +1103,6 @@ public abstract class AbstractHtmlDataTa
}
}
}
-
- // Now invoke the superclass encodeBegin, which will eventually
- // execute the encodeBegin for the associated renderer.
- super.encodeBegin(context);
}
/**
Modified: myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/HtmlDataTableHack.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/HtmlDataTableHack.java?rev=987019&r1=987018&r2=987019&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/HtmlDataTableHack.java (original)
+++ myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/HtmlDataTableHack.java Thu Aug 19 05:11:31 2010
@@ -31,6 +31,7 @@ import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.component.EditableValueHolder;
import javax.faces.component.UIComponent;
+import javax.faces.component.UIComponentBase;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
@@ -43,6 +44,7 @@ import javax.faces.model.ScalarDataModel
import javax.servlet.jsp.jstl.sql.Result;
import org.apache.commons.lang.StringUtils;
+import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
import org.apache.myfaces.component.html.util.HtmlComponentUtils;
import org.apache.myfaces.custom.ExtendedComponentBase;
@@ -69,10 +71,15 @@ public abstract class HtmlDataTableHack
// holds for each row the states of the child components of this UIData
private Map<String, Object> _rowStates = new HashMap<String, Object>();
+
+ // holds delta state information
+ private Map<String, Map<String, Object> > _rowDeltaStates = new HashMap<String, Map<String, Object> >();
// contains the initial row state which is used to initialize each row
private Object _initialDescendantComponentState = null;
+ private Object _initialDescendantFullComponentState = null;
+
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Every field and method from here is identical to UIData !!!!!!!!!
// EXCEPTION: we can create a DataModel from a Collection
@@ -279,6 +286,18 @@ public abstract class HtmlDataTableHack
@SuppressWarnings("unchecked")
public void setRowIndex(int rowIndex)
{
+ if (isPreserveRowComponentState() && _initialDescendantComponentState != null)
+ {
+ setRowIndexPreserveComponentState(rowIndex);
+ }
+ else
+ {
+ setRowIndexWithoutPreserveComponentState(rowIndex);
+ }
+ }
+
+ private void setRowIndexWithoutPreserveComponentState(int rowIndex)
+ {
if (rowIndex < -1)
{
throw new IllegalArgumentException("rowIndex is less than -1");
@@ -295,15 +314,13 @@ public abstract class HtmlDataTableHack
{
if (_initialDescendantComponentState == null)
{
- _initialDescendantComponentState = saveDescendantComponentStates(getChildren()
- .iterator(), false);
+ _initialDescendantComponentState = saveDescendantComponentStates();
}
}
else
{
_rowStates.put(getContainerClientId(facesContext),
- saveDescendantComponentStates(getChildren()
- .iterator(), false));
+ saveDescendantComponentStates());
}
_rowIndex = rowIndex;
@@ -339,25 +356,99 @@ public abstract class HtmlDataTableHack
if (_rowIndex == -1)
{
- restoreDescendantComponentStates(getChildren().iterator(),
- _initialDescendantComponentState, false);
+ restoreDescendantComponentStates(_initialDescendantComponentState);
}
else
{
Object rowState = _rowStates.get(getContainerClientId(facesContext));
if (rowState == null)
{
- restoreDescendantComponentStates(getChildren().iterator(),
- _initialDescendantComponentState, false);
+ restoreDescendantComponentStates(_initialDescendantComponentState);
}
else
{
- restoreDescendantComponentStates(getChildren().iterator(),
- rowState, false);
+ restoreDescendantComponentStates(rowState);
}
}
}
+
+ private void setRowIndexPreserveComponentState(int rowIndex)
+ {
+ if (rowIndex < -1)
+ {
+ throw new IllegalArgumentException("rowIndex is less than -1");
+ }
+ if (_rowIndex == rowIndex)
+ {
+ return;
+ }
+
+ FacesContext facesContext = getFacesContext();
+
+ if (_initialDescendantFullComponentState != null)
+ {
+ //Just save the row
+ Map<String, Object> sm = saveFullDescendantComponentStates(facesContext);
+ if (sm != null && !sm.isEmpty())
+ {
+ _rowDeltaStates.put(getContainerClientId(facesContext), sm);
+ }
+ }
+
+ _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 (_initialDescendantFullComponentState != null)
+ {
+ Map<String, Object> rowState = _rowDeltaStates.get(getContainerClientId(facesContext));
+ if (rowState == null)
+ {
+ //Restore as original
+ restoreFullDescendantComponentStates(facesContext, _initialDescendantFullComponentState);
+ }
+ else
+ {
+ //Restore first original and then delta
+ restoreFullDescendantComponentDeltaStates(facesContext, rowState, _initialDescendantFullComponentState);
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ protected void restoreDescendantComponentStates(Object state)
+ {
+ restoreDescendantComponentStates(getChildren().iterator(), state, false);
+ }
+
@SuppressWarnings("unchecked")
protected void restoreDescendantComponentStates(Iterator<UIComponent> childIterator,
Object state, boolean restoreChildFacets)
@@ -404,6 +495,12 @@ public abstract class HtmlDataTableHack
}
@SuppressWarnings("unchecked")
+ protected Object saveDescendantComponentStates()
+ {
+ return saveDescendantComponentStates(getChildren().iterator(), false);
+ }
+
+ @SuppressWarnings("unchecked")
protected Object saveDescendantComponentStates(Iterator<UIComponent> childIterator,
boolean saveChildFacets)
{
@@ -439,6 +536,307 @@ public abstract class HtmlDataTableHack
}
return childStates;
}
+
+
+
+
+ @Override
+ public void markInitialState()
+ {
+ if (isPreserveRowComponentState())
+ {
+ if (getFacesContext().getAttributes().containsKey("org.apache.myfaces.MARK_INITIAL_STATE"))
+ {
+ _initialDescendantFullComponentState = saveDescendantInitialComponentStates(getFacesContext());
+ }
+ }
+ super.markInitialState();
+ }
+
+ protected void restoreFullDescendantComponentStates(FacesContext facesContext, Object initialState)
+ {
+ restoreFullDescendantComponentStates(facesContext, getChildren().iterator(), initialState, false);
+ }
+
+ protected void restoreFullDescendantComponentStates(FacesContext facesContext,
+ Iterator<UIComponent> childIterator, Object initialState,
+ boolean restoreChildFacets)
+ {
+ Iterator<? extends Object[]> descendantStateIterator = null;
+ while (childIterator.hasNext())
+ {
+ if (descendantStateIterator == null && initialState != null)
+ {
+ descendantStateIterator = ((Collection<? extends Object[]>) initialState)
+ .iterator();
+ }
+ UIComponent component = childIterator.next();
+
+ // reset the client id (see spec 3.1.6)
+ component.setId(component.getId());
+ if (!component.isTransient())
+ {
+ Object childState = null;
+ Object descendantState = null;
+ String childId = null;
+ if (descendantStateIterator != null
+ && descendantStateIterator.hasNext())
+ {
+ do
+ {
+ Object[] object = descendantStateIterator.next();
+ childState = object[0];
+ descendantState = object[1];
+ childId = (String) object[2];
+ }
+ while(descendantStateIterator.hasNext() && !component.getId().equals(childId));
+
+ if (!component.getId().equals(childId))
+ {
+ // cannot apply initial state to components correctly.
+ throw new IllegalStateException("Cannot restore row correctly.");
+ }
+ }
+
+ component.clearInitialState();
+ component.restoreState(facesContext, childState);
+ component.markInitialState();
+
+ Iterator<UIComponent> childsIterator;
+ if (restoreChildFacets)
+ {
+ childsIterator = component.getFacetsAndChildren();
+ }
+ else
+ {
+ childsIterator = component.getChildren().iterator();
+ }
+ restoreFullDescendantComponentStates(facesContext, childsIterator,
+ descendantState, true);
+ }
+ }
+ }
+
+ protected Collection<Object[]> saveDescendantInitialComponentStates(FacesContext facesContext)
+ {
+ return saveDescendantInitialComponentStates(facesContext, getChildren().iterator(), false);
+ }
+
+ protected Collection<Object[]> saveDescendantInitialComponentStates(FacesContext facesContext,
+ Iterator<UIComponent> childIterator, boolean saveChildFacets)
+ {
+ Collection<Object[]> childStates = null;
+ while (childIterator.hasNext())
+ {
+ if (childStates == null)
+ {
+ childStates = new ArrayList<Object[]>();
+ }
+
+ UIComponent child = childIterator.next();
+ if (!child.isTransient())
+ {
+ // Add an entry to the collection, being an array of two
+ // elements. The first element is the state of the children
+ // of this component; the second is the state of the current
+ // child itself.
+
+ Iterator<UIComponent> childsIterator;
+ if (saveChildFacets)
+ {
+ childsIterator = child.getFacetsAndChildren();
+ }
+ else
+ {
+ childsIterator = child.getChildren().iterator();
+ }
+ Object descendantState = saveDescendantInitialComponentStates(
+ facesContext, childsIterator, true);
+ Object state = null;
+ if (child.initialStateMarked())
+ {
+ child.clearInitialState();
+ state = child.saveState(facesContext);
+ child.markInitialState();
+ }
+ else
+ {
+ state = child.saveState(facesContext);
+ }
+
+ childStates.add(new Object[] { state, descendantState, child.getId()});
+ }
+ }
+ return childStates;
+ }
+
+ protected Map<String,Object> saveFullDescendantComponentStates(FacesContext facesContext)
+ {
+ return saveFullDescendantComponentStates(facesContext, null, getChildren().iterator(), false, getContainerClientId(facesContext));
+ }
+
+ protected Map<String,Object> saveFullDescendantComponentStates(FacesContext facesContext, Map<String,Object> stateMap,
+ Iterator<UIComponent> childIterator, boolean saveChildFacets, String containerClientId)
+ {
+ while (childIterator.hasNext())
+ {
+ UIComponent child = childIterator.next();
+ if (!child.isTransient())
+ {
+ // Add an entry to the collection, being an array of two
+ // elements. The first element is the state of the children
+ // of this component; the second is the state of the current
+ // child itself.
+
+ Iterator<UIComponent> childsIterator;
+ if (saveChildFacets)
+ {
+ childsIterator = child.getFacetsAndChildren();
+ }
+ else
+ {
+ childsIterator = child.getChildren().iterator();
+ }
+ stateMap = saveFullDescendantComponentStates(facesContext, stateMap,
+ childsIterator, true, containerClientId);
+ Object state = child.saveState(facesContext);
+ if (state != null)
+ {
+ if (stateMap == null)
+ {
+ stateMap = new HashMap<String,Object>();
+ }
+ stateMap.put(child.getClientId(facesContext).substring(containerClientId.length()+1), state);
+ }
+ }
+ }
+ return stateMap;
+ }
+
+ protected void restoreFullDescendantComponentDeltaStates(FacesContext facesContext,
+ Map<String, Object> rowState, Object initialState)
+ {
+ restoreFullDescendantComponentDeltaStates(facesContext, getChildren().iterator(), rowState, initialState, false, getContainerClientId(facesContext));
+ }
+
+ protected void restoreFullDescendantComponentDeltaStates(FacesContext facesContext,
+ Iterator<UIComponent> childIterator, Map<String, Object> state, Object initialState,
+ boolean restoreChildFacets, String containerClientId)
+ {
+ Iterator<? extends Object[]> descendantFullStateIterator = null;
+ while (childIterator.hasNext())
+ {
+ if (descendantFullStateIterator == null && initialState != null)
+ {
+ descendantFullStateIterator = ((Collection<? extends Object[]>) initialState).iterator();
+ }
+ UIComponent component = childIterator.next();
+
+ // reset the client id (see spec 3.1.6)
+ component.setId(component.getId());
+ if (!component.isTransient())
+ {
+ Object childInitialState = null;
+ Object descendantInitialState = null;
+ Object childState = null;
+ String childId = null;
+ childState = (state == null) ? null : state.get(component.getClientId(facesContext).substring(containerClientId.length()+1));
+ if (descendantFullStateIterator != null
+ && descendantFullStateIterator.hasNext())
+ {
+ do
+ {
+ Object[] object = descendantFullStateIterator.next();
+ childInitialState = object[0];
+ descendantInitialState = object[1];
+ childId = (String) object[2];
+ }while(descendantFullStateIterator.hasNext() && !component.getId().equals(childId));
+
+ if (!component.getId().equals(childId))
+ {
+ // cannot apply initial state to components correctly. State is corrupt
+ throw new IllegalStateException("Cannot restore row correctly.");
+ }
+ }
+
+ component.clearInitialState();
+ if (childInitialState != null)
+ {
+ component.restoreState(facesContext, childInitialState);
+ component.markInitialState();
+ component.restoreState(facesContext, childState);
+ }
+ else
+ {
+ component.restoreState(facesContext, childState);
+ component.markInitialState();
+ }
+
+ Iterator<UIComponent> childsIterator;
+ if (restoreChildFacets)
+ {
+ childsIterator = component.getFacetsAndChildren();
+ }
+ else
+ {
+ childsIterator = component.getChildren().iterator();
+ }
+ restoreFullDescendantComponentDeltaStates(facesContext, childsIterator,
+ state, descendantInitialState , true, containerClientId);
+ }
+ }
+ }
+
+ @Override
+ public void restoreState(FacesContext context, Object state)
+ {
+ if (state == null)
+ {
+ return;
+ }
+
+ Object values[] = (Object[]) state;
+ super.restoreState(context, values[0]);
+ Object restoredRowStates = UIComponentBase.restoreAttachedState(context, values[1]);
+ if (restoredRowStates == null)
+ {
+ if (!_rowDeltaStates.isEmpty())
+ {
+ _rowDeltaStates.clear();
+ }
+ }
+ else
+ {
+ _rowDeltaStates = (Map<String, Map<String, Object> >) restoredRowStates;
+ }
+ }
+
+ @Override
+ public Object saveState(FacesContext context)
+ {
+ if (initialStateMarked())
+ {
+ Object parentSaved = super.saveState(context);
+ if (parentSaved == null &&_rowDeltaStates.isEmpty())
+ {
+ return null;
+ }
+ else
+ {
+ Object values[] = new Object[2];
+ values[0] = super.saveState(context);
+ values[1] = UIComponentBase.saveAttachedState(context, _rowDeltaStates);
+ return values;
+ }
+ }
+ else
+ {
+ Object values[] = new Object[2];
+ values[0] = super.saveState(context);
+ values[1] = UIComponentBase.saveAttachedState(context, _rowDeltaStates);
+ return values;
+ }
+ }
public void setValueBinding(String name, ValueBinding binding)
{
@@ -484,6 +882,7 @@ public abstract class HtmlDataTableHack
super.setValue(value);
_dataModelMap.clear();
_rowStates.clear();
+ _rowDeltaStates.clear();
_isValidChilds = true;
}
@@ -689,7 +1088,14 @@ public abstract class HtmlDataTableHack
*/
public void clearRowStates()
{
- _rowStates.clear();
+ if (isPreserveRowComponentState())
+ {
+ _rowDeltaStates.clear();
+ }
+ else
+ {
+ _rowStates.clear();
+ }
}
/**
@@ -707,34 +1113,85 @@ public abstract class HtmlDataTableHack
// copy next rowstate to current row for each row from deleted row onward.
int rowCount = getRowCount();
- for (int index = deletedIndex + 1; index < rowCount; ++index)
+ if (isPreserveRowComponentState())
{
- setRowIndex(index);
- String nextRowStateKey = getContainerClientId(facesContext);
-
- Object nextRowState = _rowStates.get(nextRowStateKey);
- if (nextRowState == null)
+ for (int index = deletedIndex + 1; index < rowCount; ++index)
{
- _rowStates.remove(currentRowStateKey);
+ setRowIndex(index);
+ String nextRowStateKey = getContainerClientId(facesContext);
+
+ Map<String, Object> nextRowState = _rowDeltaStates.get(nextRowStateKey);
+ if (nextRowState == null)
+ {
+ _rowDeltaStates.remove(currentRowStateKey);
+ }
+ else
+ {
+ _rowDeltaStates.put(currentRowStateKey, nextRowState);
+ }
+ currentRowStateKey = nextRowStateKey;
}
- else
+
+ // Remove last row
+ _rowDeltaStates.remove(currentRowStateKey);
+ }
+ else
+ {
+ for (int index = deletedIndex + 1; index < rowCount; ++index)
{
- _rowStates.put(currentRowStateKey, nextRowState);
+ setRowIndex(index);
+ String nextRowStateKey = getContainerClientId(facesContext);
+
+ Object nextRowState = _rowStates.get(nextRowStateKey);
+ if (nextRowState == null)
+ {
+ _rowStates.remove(currentRowStateKey);
+ }
+ else
+ {
+ _rowStates.put(currentRowStateKey, nextRowState);
+ }
+ currentRowStateKey = nextRowStateKey;
}
- currentRowStateKey = nextRowStateKey;
+
+ // Remove last row
+ _rowStates.remove(currentRowStateKey);
}
- // Remove last row
- _rowStates.remove(currentRowStateKey);
-
// restore saved row index
setRowIndex(savedRowIndex);
}
+
+ /**
+ * Indicates whether the state for a component in each row should not be
+ * discarded before the datatable is rendered again.
+ *
+ * This property is similar to tomahawk t:dataTable preserveRowStates
+ *
+ * This will only work reliable if the datamodel of the
+ * datatable did not change either by sorting, removing or
+ * adding rows. Default: false
+ *
+ * @return
+ */
+ @JSFProperty(faceletsOnly=true, literalOnly=true)
+ public boolean isPreserveRowComponentState()
+ {
+ Boolean b = (Boolean) getStateHelper().get(PropertyKeys.preserveRowComponentState);
+ return b == null ? false : b.booleanValue();
+ }
+
+ public void setPreserveRowComponentState(boolean preserveComponentState)
+ {
+ getStateHelper().put(PropertyKeys.preserveRowComponentState, preserveComponentState);
+ }
+
protected enum PropertyKeys
{
preserveRowStates
, forceId
, forceIdIndex
+ , preserveRowComponentState
}
}
Added: myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/_DetailStampFacetAndChildrenIterator.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/_DetailStampFacetAndChildrenIterator.java?rev=987019&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/_DetailStampFacetAndChildrenIterator.java (added)
+++ myfaces/tomahawk/trunk/core20/src/main/java/org/apache/myfaces/component/html/ext/_DetailStampFacetAndChildrenIterator.java Thu Aug 19 05:11:31 2010
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.component.html.ext;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import javax.faces.component.UIComponent;
+
+/**
+ * @author Leonardo Uribe
+ * @since 1.1.10
+ */
+class _DetailStampFacetAndChildrenIterator implements Iterator<UIComponent>
+{
+ private UIComponent _detailStamp;
+ private Iterator<UIComponent> _childrenIterator;
+
+ _DetailStampFacetAndChildrenIterator(UIComponent detailStamp, List<UIComponent> childrenList)
+ {
+ _detailStamp = detailStamp;
+ _childrenIterator = childrenList != null ? childrenList.iterator() : null;
+ }
+
+ public boolean hasNext()
+ {
+ boolean hasNext = (_detailStamp != null)
+ || (_childrenIterator != null && _childrenIterator.hasNext());
+ if (!hasNext)
+ {
+ _detailStamp = null;
+ _childrenIterator = null;
+ }
+
+ return hasNext;
+ }
+
+ public UIComponent next()
+ {
+ if (_detailStamp != null)
+ {
+ UIComponent detailStamp = _detailStamp;
+ _detailStamp = null;
+ return detailStamp;
+ }
+ else if (_childrenIterator != null && _childrenIterator.hasNext())
+ {
+ return _childrenIterator.next();
+ }
+ else
+ {
+ throw new NoSuchElementException();
+ }
+ }
+
+ public void remove()
+ {
+ throw new UnsupportedOperationException(this.getClass().getName() + " UnsupportedOperationException");
+ }
+}
Added: myfaces/tomahawk/trunk/core20/src/test/java/org/apache/myfaces/component/html/ext/HtmlDataTablePreserveRowComponentStateTest.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core20/src/test/java/org/apache/myfaces/component/html/ext/HtmlDataTablePreserveRowComponentStateTest.java?rev=987019&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/core20/src/test/java/org/apache/myfaces/component/html/ext/HtmlDataTablePreserveRowComponentStateTest.java (added)
+++ myfaces/tomahawk/trunk/core20/src/test/java/org/apache/myfaces/component/html/ext/HtmlDataTablePreserveRowComponentStateTest.java Thu Aug 19 05:11:31 2010
@@ -0,0 +1,295 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.component.html.ext;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.faces.component.UIColumn;
+import javax.faces.component.UIOutput;
+import javax.faces.component.UIPanel;
+import javax.faces.component.UIViewRoot;
+
+import org.apache.myfaces.test.base.AbstractJsfTestCase;
+import org.apache.myfaces.test.utils.TestUtils;
+
+public class HtmlDataTablePreserveRowComponentStateTest extends AbstractJsfTestCase
+{
+
+ public HtmlDataTablePreserveRowComponentStateTest(String name)
+ {
+ super(name);
+ }
+
+ @Override
+ protected void setUpRenderKit() throws Exception
+ {
+ super.setUpRenderKit();
+ TestUtils.addDefaultRenderers(facesContext);
+ }
+
+ public static class RowData
+ {
+ private String text;
+
+ public RowData(String text, String style)
+ {
+ super();
+ this.text = text;
+ this.style = style;
+ }
+
+ private String style;
+
+ public String getText()
+ {
+ return text;
+ }
+
+ public void setText(String text)
+ {
+ this.text = text;
+ }
+
+ public String getStyle()
+ {
+ return style;
+ }
+
+ public void setStyle(String style)
+ {
+ this.style = style;
+ }
+ }
+
+ public void testPreserveRowComponentState1() throws Exception
+ {
+ List<RowData> model = new ArrayList<RowData>();
+ model.add(new RowData("text1","style1"));
+ model.add(new RowData("text1","style2"));
+ model.add(new RowData("text1","style3"));
+ model.add(new RowData("text1","style4"));
+
+ //Put on request map to be resolved later
+ request.setAttribute("list", model);
+
+ UIViewRoot root = facesContext.getViewRoot();
+ HtmlDataTable table = new HtmlDataTable();
+ UIColumn column = new UIColumn();
+ UIOutput text = new UIOutput();
+
+ //This is only required if markInitiaState fix is not used
+ root.setId("root");
+ table.setId("table");
+ column.setId("column");
+ text.setId("text");
+
+ table.setVar("row");
+ table.setPreserveRowComponentState(true);
+ table.setValueExpression("value", application.
+ getExpressionFactory().createValueExpression(
+ facesContext.getELContext(),"#{list}",List.class));
+
+ text.setValueExpression("value", application.
+ getExpressionFactory().createValueExpression(
+ facesContext.getELContext(),"#{row.text}",String.class));
+
+ root.getChildren().add(table);
+ table.getChildren().add(column);
+ column.getChildren().add(text);
+
+ //Simulate markInitialState call.
+ facesContext.getAttributes().put("org.apache.myfaces.MARK_INITIAL_STATE", Boolean.TRUE);
+ root.markInitialState();
+ table.markInitialState();
+ column.markInitialState();
+ text.markInitialState();
+ facesContext.getAttributes().remove("org.apache.myfaces.MARK_INITIAL_STATE");
+
+ //Check the value expressions are working and change the component state
+ for (int i = 0; i < model.size(); i++)
+ {
+ RowData rowData = model.get(i);
+ table.setRowIndex(i);
+ assertEquals(rowData.getText(), text.getValue());
+ text.getAttributes().put("style", rowData.getStyle());
+ }
+
+ //Reset row index
+ table.setRowIndex(-1);
+
+ //Check the values were not lost
+ for (int i = 0; i < model.size(); i++)
+ {
+ table.setRowIndex(i);
+ assertEquals(model.get(i).getStyle(), text.getAttributes().get("style"));
+ }
+
+ }
+
+ public void testPreserveRowComponentState2() throws Exception
+ {
+ List<RowData> model = new ArrayList<RowData>();
+ model.add(new RowData("text1","style1"));
+ model.add(new RowData("text1","style2"));
+ model.add(new RowData("text1","style3"));
+ model.add(new RowData("text1","style4"));
+
+ //Put on request map to be resolved later
+ request.setAttribute("list", model);
+
+ UIViewRoot root = facesContext.getViewRoot();
+ HtmlDataTable table = new HtmlDataTable();
+ UIColumn column = new UIColumn();
+ UIOutput text = new UIOutput();
+
+ //This is only required if markInitiaState fix is not used
+ root.setId("root");
+ table.setId("table");
+ column.setId("column");
+ text.setId("text");
+
+ table.setVar("row");
+ table.setPreserveRowComponentState(true);
+ table.setValueExpression("value", application.
+ getExpressionFactory().createValueExpression(
+ facesContext.getELContext(),"#{list}",List.class));
+
+ text.setValueExpression("value", application.
+ getExpressionFactory().createValueExpression(
+ facesContext.getELContext(),"#{row.text}",String.class));
+
+ root.getChildren().add(table);
+ table.getChildren().add(column);
+ column.getChildren().add(text);
+
+ //Simulate markInitialState call.
+ facesContext.getAttributes().put("org.apache.myfaces.MARK_INITIAL_STATE", Boolean.TRUE);
+ root.markInitialState();
+ table.markInitialState();
+ column.markInitialState();
+ text.markInitialState();
+ facesContext.getAttributes().remove("org.apache.myfaces.MARK_INITIAL_STATE");
+
+ //Check the value expressions are working and change the component state
+ for (int i = 0; i < model.size(); i++)
+ {
+ RowData rowData = model.get(i);
+ table.setRowIndex(i);
+ assertEquals(rowData.getText(), text.getValue());
+ text.getAttributes().put("style", rowData.getStyle());
+ }
+
+ //Reset row index
+ table.setRowIndex(-1);
+
+ //Remove a row
+ table.deleteRowStateForRow(1);
+ model.remove(1);
+
+ //Check the values were not lost
+ for (int i = 0; i < model.size(); i++)
+ {
+ table.setRowIndex(i);
+ assertEquals(model.get(i).getStyle(), text.getAttributes().get("style"));
+ }
+
+ }
+
+ public void testPreserveRowComponentStateDetailStamp1() throws Exception
+ {
+ List<RowData> model = new ArrayList<RowData>();
+ model.add(new RowData("text1","style1"));
+ model.add(new RowData("text1","style2"));
+ model.add(new RowData("text1","style3"));
+ model.add(new RowData("text1","style4"));
+
+ //Put on request map to be resolved later
+ request.setAttribute("list", model);
+
+ UIViewRoot root = facesContext.getViewRoot();
+ HtmlDataTable table = new HtmlDataTable();
+ UIColumn column = new UIColumn();
+ UIPanel detailStampPanel = new UIPanel();
+ UIOutput text = new UIOutput();
+ UIOutput detailStampText = new UIOutput();
+
+ //This is only required if markInitiaState fix is not used
+ root.setId("root");
+ table.setId("table");
+ detailStampPanel.setId("detailStamp");
+ column.setId("column");
+ text.setId("text");
+ detailStampText.setId("detailStampText");
+
+ table.setVar("row");
+ table.setPreserveRowComponentState(true);
+ table.setValueExpression("value", application.
+ getExpressionFactory().createValueExpression(
+ facesContext.getELContext(),"#{list}",List.class));
+
+ text.setValueExpression("value", application.
+ getExpressionFactory().createValueExpression(
+ facesContext.getELContext(),"#{row.text}",String.class));
+
+ detailStampText.setValueExpression("value", application.
+ getExpressionFactory().createValueExpression(
+ facesContext.getELContext(),"#{row.text}",String.class));
+
+ root.getChildren().add(table);
+ table.getChildren().add(column);
+ table.getFacets().put(AbstractHtmlDataTable.DETAIL_STAMP_FACET_NAME, detailStampPanel);
+ column.getChildren().add(text);
+ detailStampPanel.getChildren().add(detailStampText);
+
+ //Simulate markInitialState call.
+ facesContext.getAttributes().put("org.apache.myfaces.MARK_INITIAL_STATE", Boolean.TRUE);
+ root.markInitialState();
+ table.markInitialState();
+ detailStampPanel.markInitialState();
+ detailStampText.markInitialState();
+ column.markInitialState();
+ text.markInitialState();
+ facesContext.getAttributes().remove("org.apache.myfaces.MARK_INITIAL_STATE");
+
+ //Check the value expressions are working and change the component state
+ for (int i = 0; i < model.size(); i++)
+ {
+ RowData rowData = model.get(i);
+ table.setRowIndex(i);
+ assertEquals(rowData.getText(), text.getValue());
+ assertEquals(rowData.getText(), detailStampText.getValue());
+ text.getAttributes().put("style", rowData.getStyle());
+ detailStampText.getAttributes().put("style", rowData.getStyle());
+ }
+
+ //Reset row index
+ table.setRowIndex(-1);
+
+ //Check the values were not lost
+ for (int i = 0; i < model.size(); i++)
+ {
+ table.setRowIndex(i);
+ assertEquals(model.get(i).getStyle(), text.getAttributes().get("style"));
+ assertEquals(model.get(i).getStyle(), detailStampText.getAttributes().get("style"));
+ }
+
+ }
+
+}