myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mat...@apache.org
Subject svn commit: r905749 - /myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/SortableModel.java
Date Tue, 02 Feb 2010 19:09:12 GMT
Author: matzew
Date: Tue Feb  2 19:09:07 2010
New Revision: 905749

URL: http://svn.apache.org/viewvc?rev=905749&view=rev
Log:
TRINIDAD-983 - Sortable model for localized text


ported patch from Toma havelka to trinidad/jsf 1.2

Modified:
    myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/SortableModel.java

Modified: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/SortableModel.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/SortableModel.java?rev=905749&r1=905748&r2=905749&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/SortableModel.java
(original)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/model/SortableModel.java
Tue Feb  2 19:09:07 2010
@@ -18,10 +18,13 @@
  */
 package org.apache.myfaces.trinidad.model;
 
+import java.text.Collator;
+
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
+import java.util.Locale;
 
 import javax.el.ELContext;
 import javax.el.ELResolver;
@@ -34,6 +37,7 @@
 import javax.faces.model.DataModel;
 import javax.faces.model.DataModelListener;
 
+import org.apache.myfaces.trinidad.context.RequestContext;
 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
 
 
@@ -285,10 +289,12 @@
     if (_model.isRowAvailable())
     {
       FacesContext context = FacesContext.getCurrentInstance();
+      RequestContext rc = RequestContext.getCurrentInstance();
       ELResolver resolver = _getELResolver(context);
       ELContext elContext = _getELContext(context, resolver);
+      Locale locale = _getLocale(rc, context);
       Comparator<Integer> comp =
-        new Comp(resolver, elContext, property);
+        new Comp(resolver, elContext, locale, property);
       if (!isAscending)
         comp = new Inverter<Integer>(comp);
 
@@ -371,10 +377,20 @@
 
   private final class Comp implements Comparator<Integer>
   {
-    public Comp(ELResolver resolver, ELContext context, String property)
+    public Comp(ELResolver resolver, ELContext context, Locale locale, String property)
     {
       _resolver = resolver;
       _context  = context;
+
+      if (locale != null)
+      {
+        _collator = Collator.getInstance(locale);
+      }
+      else
+      {
+        _collator = null;
+      }
+
       _prop = property;
     }
 
@@ -404,18 +420,38 @@
       // So test before we cast:
       if (value1 instanceof Comparable)
       {
-        return ((Comparable<Object>) value1).compareTo(value2);
+        if ((value1 instanceof String) && (value2 instanceof String)) 
+        {
+          return compare((String) value1, (String) value2);
+        }
+        else 
+        {
+          return ((Comparable<Object>) value1).compareTo(value2);
+        }
       }
       else
       {
         // if the object is not a Comparable, then
         // the best we can do is string comparison:
-        return value1.toString().compareTo(value2.toString());
+        return compare(value1.toString(), value2.toString());
+      }
+    }
+
+    private int compare(String s1, String s2) 
+    {
+      if (_collator != null)
+      {
+        return _collator.compare(s1, s2);
+      }
+      else
+      {
+        return s1.compareTo(s2);
       }
     }
 
     private final ELResolver _resolver;
     private final ELContext  _context;
+    private final Collator _collator;
     private final String _prop;
   }
 
@@ -485,7 +521,7 @@
     // going to have to synthesize one!
     if (context != null)
       return context.getELContext();
-   
+
     return new ELContextImpl(resolver); 
   }
 
@@ -495,7 +531,7 @@
     // get the ELResolver (and the 99.9% scenario)
     if (context != null)
       return context.getApplication().getELResolver();
-    
+
     // If that fails, then we're likely outside of the JSF lifecycle.
     // Look to the ApplicationFactory.
     ApplicationFactory factory = (ApplicationFactory)
@@ -504,6 +540,17 @@
     
   }
 
+  static private Locale _getLocale(RequestContext requestContext, FacesContext facesContext)

+  {
+    if (requestContext != null)
+      return requestContext.getFormattingLocale();
+
+    if (facesContext != null)
+      return facesContext.getViewRoot().getLocale();
+
+    return null;
+  }
+
   private SortCriterion _sortCriterion = null;
 
   private DataModel _model = null;



Mime
View raw message