You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ck...@apache.org on 2007/01/20 15:49:25 UTC

svn commit: r498116 - /myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/ext/SortableModel.java

Author: ckormos
Date: Sat Jan 20 06:49:24 2007
New Revision: 498116

URL: http://svn.apache.org/viewvc?view=rev&rev=498116
Log:
Fixed TOMAHAWK-864

Modified:
    myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/ext/SortableModel.java

Modified: myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/ext/SortableModel.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/ext/SortableModel.java?view=diff&rev=498116&r1=498115&r2=498116
==============================================================================
--- myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/ext/SortableModel.java (original)
+++ myfaces/tomahawk/trunk/core/src/main/java/org/apache/myfaces/component/html/ext/SortableModel.java Sat Jan 20 06:49:24 2007
@@ -22,11 +22,16 @@
 import java.io.StringReader;
 import java.lang.reflect.Method;
 import java.sql.ResultSet;
+import java.text.CollationKey;
+import java.text.Collator;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+
 import javax.faces.context.FacesContext;
 import javax.faces.el.ReferenceSyntaxException;
 import javax.faces.model.ArrayDataModel;
@@ -39,6 +44,7 @@
 import javax.servlet.jsp.el.FunctionMapper;
 import javax.servlet.jsp.el.VariableResolver;
 import javax.servlet.jsp.jstl.sql.Result;
+
 import org.apache.commons.el.Expression;
 import org.apache.commons.el.ExpressionString;
 import org.apache.commons.el.Logger;
@@ -202,10 +208,13 @@
             {
                 // don't propagate this exception out. This is because it might break
                 // the VE.
-
                 log.warn(e);
                 return false;
             }
+            catch (Exception e) {
+            	log.warn(e);
+                return false;
+            }
         } 
         finally 
         {
@@ -278,6 +287,7 @@
                 comp = new Inverter(comp);
             
             Collections.sort(_baseIndicesList, comp);
+            
             _sortedIndicesList = null;
         }
         
@@ -368,22 +378,35 @@
     {
         private final String _prop;
         
+        private Collator _collator;
+        
+        private Map _collationKeys;
+        
         public Comp(String property) 
         {            
             _prop = property;
-        }
+            _collator = Collator.getInstance(FacesContext.getCurrentInstance().getViewRoot().getLocale()); 
+            _collationKeys = new HashMap();
+        }               
         
         public int compare(Object o1, Object o2) 
         {
             int index1 = ((Integer) o1).intValue();
             int index2 = ((Integer) o2).intValue();
             
-            _model.setRowIndex(index1);
-            Object value1 = getPropertyValue(_prop);
-            
-            _model.setRowIndex(index2);
-            Object value2 = getPropertyValue(_prop);
-            
+            Object value1 = null;
+            Object value2 = null;
+            try {
+            	_model.setRowIndex(index1);
+            	value1 = getPropertyValue(_prop);
+            	
+            	_model.setRowIndex(index2);
+            	value2 = getPropertyValue(_prop);
+            }
+            catch (Exception exc) {	
+            	log.error(exc);
+            }
+                                    
             if (value1 == null)
                 return (value2 == null) ? 0 : -1;
             
@@ -398,13 +421,30 @@
             {
                 return ((Comparable) value1).compareTo(value2);
             } 
+            else if (value1 instanceof String) {
+            	//if the object is a String we best compare locale-sesitive
+            	CollationKey collationKey1 = getCollationKey((String)value1);
+            	CollationKey collationKey2 = getCollationKey((String)value2);
+            	
+            	return collationKey1.compareTo(collationKey2);
+            }
             else 
             {
                 // if the object is not a Comparable, then
                 // the best we can do is string comparison:
                 return value1.toString().compareTo(value2.toString());
             }
-        }               
+        }         
+        
+        private CollationKey getCollationKey(String propertyValue) {
+        	CollationKey key = (CollationKey)_collationKeys.get(propertyValue);
+        	if (key == null) {
+        		key = _collator.getCollationKey(propertyValue);
+        		_collationKeys.put(propertyValue, key);
+        	}
+        		
+        	return key;
+        }
     }
     /**
      *