You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sv...@apache.org on 2005/08/06 23:27:17 UTC

svn commit: r230591 - in /myfaces: forrest/trunk/content/xdocs/tomahawk/ tomahawk/trunk/src/java/org/apache/myfaces/component/html/ext/ tomahawk/trunk/src/java/org/apache/myfaces/taglib/html/ext/ tomahawk/trunk/tld/

Author: svieujot
Date: Sat Aug  6 14:27:07 2005
New Revision: 230591

URL: http://svn.apache.org/viewcvs?rev=230591&view=rev
Log:
Added forceIdIndexFormula attribute to x:dataTable .

Modified:
    myfaces/forrest/trunk/content/xdocs/tomahawk/extDataTable.xml
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/ext/HtmlDataTable.java
    myfaces/tomahawk/trunk/src/java/org/apache/myfaces/taglib/html/ext/HtmlDataTableTag.java
    myfaces/tomahawk/trunk/tld/myfaces_ext.tld

Modified: myfaces/forrest/trunk/content/xdocs/tomahawk/extDataTable.xml
URL: http://svn.apache.org/viewcvs/myfaces/forrest/trunk/content/xdocs/tomahawk/extDataTable.xml?rev=230591&r1=230590&r2=230591&view=diff
==============================================================================
--- myfaces/forrest/trunk/content/xdocs/tomahawk/extDataTable.xml (original)
+++ myfaces/forrest/trunk/content/xdocs/tomahawk/extDataTable.xml Sat Aug  6 14:27:07 2005
@@ -67,7 +67,8 @@
             <title>Usage</title>
             <source>
 &lt;x:dataTable [ all standard dataTable attributes allowed ]
-                [ preserveDataModel="{true|false}" ]
+                [ preserveDataModel="{true|false}" ]
+                [ forceIdIndexFormula="value-binding" ]
                 [ sortColumn="value-binding" ]
                 [ sortAscending="value-binding" ]
                 [ preserveSort="{true|false}" ] &gt;
@@ -82,7 +83,8 @@
             <title>Syntax</title>
             <note label="&lt;x:dataTable&gt;">
                 <code>all standard dataTable attributes allowed</code><br/>
-                <code>preserveDataModel="{true|false}"</code><br/>
+                <code>preserveDataModel="{true|false}"</code><br/>
+                <code>forceIdIndexFormula="value-binding"</code><br/>
                 <code>sortColumn="value-binding"</code><br/>
                 <code>sortAscending="value-binding"</code><br/>
                 <code>preserveSort="{true|false}"</code><br/>
@@ -122,7 +124,27 @@
             <warning label="Attention">
                 To minimize the effort for saving the state of the DataModel only the visible rows are saved and restored.
                 During the update model phase the setter will be called with an Array or List, that contains only these restored rows.
-            </warning>
+            </warning>
+            <p>
+                <strong>Working with a changing data - the forceIdIndexFormula attribute</strong><br/>
+                The default table assumes that your backing data collection is stable between 2 requests.
+                This assumption can be false in several cases: concurrent accesses, unstable backing data collection order, ...
+    			If you have components within that table that update your data, you will get unintended behaviours.<br/>
+    			This attribute is meant to fix that problem.<br/>
+                The table's components and the backing data objects are linked by the table's components' ids.
+                By default the changing part of those ids is the row number.
+                So if the backing data positions in the list is changed between the 2 requests
+                (like an element inserted in the head of the list), you will update the wrong element in the
+                backing data's collection.<br/>
+                To fix this, you can set the forceIdIndexFormula to an EL that will be unique and stable for each row.
+                That way, even if your backing data list changes between 2 requests, you always update the intended
+                element in the list.<br/>
+                <br/>
+                Example : &lt;x:dataTable value="#{mailDAO.userInbox}" var="email" forceIdIndexFormula="#{email.primaryKey}" ...&gt; ...
+                <br/>
+                <br/>
+                Warning : make sure that the value-binding evaluates to a unique value for each row.
+            </p>
         </section>        
 
     </body>

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/ext/HtmlDataTable.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/ext/HtmlDataTable.java?rev=230591&r1=230590&r2=230591&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/ext/HtmlDataTable.java (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/component/html/ext/HtmlDataTable.java Sat Aug  6 14:27:07 2005
@@ -50,6 +50,7 @@
 
     private _SerializableDataModel _preservedDataModel;
 
+    private String _forceIdIndexFormula = null;
     private String _sortColumn = null;
     private Boolean _sortAscending = null;
     private String _rowOnClick = null;
@@ -62,9 +63,35 @@
     private String _rowOnKeyPress = null;
     private String _rowOnKeyDown = null;
     private String _rowOnKeyUp = null;
-    
 
     private boolean _isValidChilds = true;
+    
+    public String getClientId(FacesContext context)
+    {
+        String standardClientId = super.getClientId(context);
+        int rowIndex = getRowIndex();
+        if (rowIndex == -1)
+        {
+            return standardClientId;
+        }
+        
+        String forcedIdIndex = getForceIdIndexFormula();
+        if( forcedIdIndex == null || forcedIdIndex.length() == 0 )
+        	return standardClientId;
+        
+        // Trick : Remove the last part starting with '_' that contains the rowIndex.
+        // It would be best to not resort to String manipulation,
+        // but we can't get super.super.getClientId() :-(
+        int indexLast_ = standardClientId.lastIndexOf('_');
+        if( indexLast_ == -1 ){
+        	log.warn("Could not parse super.getClientId. forcedIdIndex will contain the rowIndex.");
+        	return standardClientId+'_'+forcedIdIndex;
+        }
+        
+        String parsedForcedClientId = standardClientId.substring(0, indexLast_+1)+forcedIdIndex;
+
+		return parsedForcedClientId;
+    }
 
 	public void setRowIndex(int rowIndex)
 	{
@@ -361,7 +388,7 @@
     public Object saveState(FacesContext context)
     {
         boolean preserveSort = isPreserveSort();
-    	Object values[] = new Object[21];
+    	Object values[] = new Object[22];
         values[0] = super.saveState(context);
         values[1] = _preserveDataModel;
         if (isPreserveDataModel())
@@ -373,25 +400,26 @@
             values[2] = null;
         }
         values[3] = _preserveSort;
-        values[4] = _sortColumn;
-        values[5] = _sortAscending;
-        values[6] = _renderedIfEmpty;
-        values[7] = _rowCountVar;
-        values[8] = _rowIndexVar;
-
-        values[9] = _rowOnClick;
-        values[10] = _rowOnDblClick;
-        values[11] = _rowOnMouseDown;
-        values[12] = _rowOnMouseUp;
-        values[13] = _rowOnMouseOver;
-        values[14] = _rowOnMouseMove;
-        values[15] = _rowOnMouseOut;
-        values[16] = _rowOnKeyPress;
-        values[17] = _rowOnKeyDown;
-        values[18] = _rowOnKeyUp;
+        values[4] = _forceIdIndexFormula;
+        values[5] = _sortColumn;
+        values[6] = _sortAscending;
+        values[7] = _renderedIfEmpty;
+        values[8] = _rowCountVar;
+        values[9] = _rowIndexVar;
+
+        values[10] = _rowOnClick;
+        values[11] = _rowOnDblClick;
+        values[12] = _rowOnMouseDown;
+        values[13] = _rowOnMouseUp;
+        values[14] = _rowOnMouseOver;
+        values[15] = _rowOnMouseMove;
+        values[16] = _rowOnMouseOut;
+        values[17] = _rowOnKeyPress;
+        values[18] = _rowOnKeyDown;
+        values[19] = _rowOnKeyUp;
 
-        values[19] = preserveSort ? getSortColumn() : null;
-        values[20] = preserveSort ? Boolean.valueOf(isSortAscending()) : null;
+        values[20] = preserveSort ? getSortColumn() : null;
+        values[21] = preserveSort ? Boolean.valueOf(isSortAscending()) : null;
         return values;
     }
     
@@ -422,27 +450,28 @@
                 _preservedDataModel = null;
         }
         _preserveSort = (Boolean) values[3];
-        _sortColumn = (String) values[4];
-        _sortAscending = (Boolean) values[5];
-        _renderedIfEmpty = (Boolean) values[6];
-        _rowCountVar = (String) values[7];
-        _rowIndexVar = (String) values[8];
-
-        _rowOnClick = (String) values[9];
-        _rowOnDblClick = (String) values[10];
-        _rowOnMouseDown = (String) values[11];
-        _rowOnMouseUp = (String) values[12];
-        _rowOnMouseOver = (String) values[13];
-        _rowOnMouseMove = (String) values[14];
-        _rowOnMouseOut = (String) values[15];
-        _rowOnKeyPress = (String) values[16];
-        _rowOnKeyDown = (String) values[17];
-        _rowOnKeyUp = (String) values[18];
+        _forceIdIndexFormula = (String) values[4];
+        _sortColumn = (String) values[5];
+        _sortAscending = (Boolean) values[6];
+        _renderedIfEmpty = (Boolean) values[7];
+        _rowCountVar = (String) values[8];
+        _rowIndexVar = (String) values[9];
+
+        _rowOnClick = (String) values[10];
+        _rowOnDblClick = (String) values[11];
+        _rowOnMouseDown = (String) values[12];
+        _rowOnMouseUp = (String) values[13];
+        _rowOnMouseOver = (String) values[14];
+        _rowOnMouseMove = (String) values[15];
+        _rowOnMouseOut = (String) values[16];
+        _rowOnKeyPress = (String) values[17];
+        _rowOnKeyDown = (String) values[18];
+        _rowOnKeyUp = (String) values[19];
 
         if (isPreserveSort())
         {
-            String sortColumn = (String) values[19];
-            Boolean sortAscending = (Boolean) values[20];
+            String sortColumn = (String) values[20];
+            Boolean sortAscending = (Boolean) values[21];
             if (sortColumn != null && sortAscending != null)
             {
                 ValueBinding vb = getValueBinding("sortColumn");
@@ -514,25 +543,45 @@
             return super.isRendered();
     }
     
+    
+    public void setForceIdIndexFormula(String forceIdIndexFormula)
+    {
+    	_forceIdIndexFormula = forceIdIndexFormula;
+        ValueBinding vb = getValueBinding("forceIdIndexFormula");
+        if (vb != null)
+        {
+            vb.setValue(getFacesContext(), _forceIdIndexFormula);
+            _forceIdIndexFormula = null;
+        }
+    }
+    
+    public String getForceIdIndexFormula()
+    {
+        if (_forceIdIndexFormula != null)
+            return _forceIdIndexFormula;
+        ValueBinding vb = getValueBinding("forceIdIndexFormula");
+        return vb != null ? (String) vb.getValue(getFacesContext()) : null;
+    }
+    
     public void setSortColumn(String sortColumn)
     {
-            _sortColumn = sortColumn;
-            // update model is necessary here, because processUpdates is never called
-            // reason: HtmlCommandSortHeader.isImmediate() == true
-            ValueBinding vb = getValueBinding("sortColumn");
-            if (vb != null)
-            {
-                    vb.setValue(getFacesContext(), _sortColumn);
-                    _sortColumn = null;
-            }
+        _sortColumn = sortColumn;
+        // update model is necessary here, because processUpdates is never called
+        // reason: HtmlCommandSortHeader.isImmediate() == true
+        ValueBinding vb = getValueBinding("sortColumn");
+        if (vb != null)
+        {
+            vb.setValue(getFacesContext(), _sortColumn);
+            _sortColumn = null;
+        }
     }
     
     public String getSortColumn()
     {
-            if (_sortColumn != null)
-                    return _sortColumn;
-            ValueBinding vb = getValueBinding("sortColumn");
-            return vb != null ? (String) vb.getValue(getFacesContext()) : null;
+        if (_sortColumn != null)
+            return _sortColumn;
+        ValueBinding vb = getValueBinding("sortColumn");
+        return vb != null ? (String) vb.getValue(getFacesContext()) : null;
     }
     
     public void setSortAscending(boolean sortAscending)

Modified: myfaces/tomahawk/trunk/src/java/org/apache/myfaces/taglib/html/ext/HtmlDataTableTag.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/src/java/org/apache/myfaces/taglib/html/ext/HtmlDataTableTag.java?rev=230591&r1=230590&r2=230591&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/src/java/org/apache/myfaces/taglib/html/ext/HtmlDataTableTag.java (original)
+++ myfaces/tomahawk/trunk/src/java/org/apache/myfaces/taglib/html/ext/HtmlDataTableTag.java Sat Aug  6 14:27:07 2005
@@ -41,6 +41,7 @@
     }
 
     private String _preserveDataModel;
+    private String _forceIdIndexFormula;
     private String _sortColumn;
     private String _sortAscending;
     private String _preserveSort;
@@ -65,6 +66,7 @@
         super.release();
 
         _preserveDataModel=null;
+        _forceIdIndexFormula=null;
         _sortColumn=null;
         _sortAscending=null;
         _preserveSort=null;
@@ -92,6 +94,7 @@
         super.setProperties(component);
 
         setBooleanProperty(component, "preserveDataModel", _preserveDataModel);
+        setValueBinding(component, "forceIdIndexFormula", _forceIdIndexFormula);
         setValueBinding(component, "sortColumn", _sortColumn);
         setValueBinding(component, "sortAscending", _sortAscending);
         setBooleanProperty(component, "preserveSort", _preserveSort);
@@ -118,6 +121,11 @@
         _preserveDataModel = preserveDataModel;
     }
 
+    public void setForceIdIndexFormula(String forceIdIndexFormula)
+    {
+    	_forceIdIndexFormula = forceIdIndexFormula;
+    }
+    
     public void setSortColumn(String sortColumn)
     {
         _sortColumn = sortColumn;

Modified: myfaces/tomahawk/trunk/tld/myfaces_ext.tld
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/tld/myfaces_ext.tld?rev=230591&r1=230590&r2=230591&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/tld/myfaces_ext.tld (original)
+++ myfaces/tomahawk/trunk/tld/myfaces_ext.tld Sat Aug  6 14:27:07 2005
@@ -137,6 +137,17 @@
             </description>
         </attribute>
         <attribute>
+            <name>forceIdIndexFormula</name>
+            <required>false</required>
+            <rtexprvalue>false</rtexprvalue>
+            <description>
+				A formula that overrides the defaut row index in the construction of table's
+				body components.
+				Example : #{myRowVar.key}
+				Warning, the EL should evaluate to a unique value for each row !
+            </description>
+        </attribute>
+        <attribute>
             <name>sortColumn</name>
             <required>false</required>
             <rtexprvalue>false</rtexprvalue>