You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by jw...@apache.org on 2009/01/30 01:25:51 UTC

svn commit: r739100 - in /myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main: java/org/apache/myfaces/trinidadinternal/convert/ javascript/META-INF/adf/jsLibs/

Author: jwaldman
Date: Fri Jan 30 00:25:50 2009
New Revision: 739100

URL: http://svn.apache.org/viewvc?rev=739100&view=rev
Log:
TRINIDAD-1380 convertNumber should support 'locale' attribute on client
applied patch for Cale Scholl to 1.2.10.1-branch

Modified:
    myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/convert/NumberConverter.java
    myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js
    myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js

Modified: myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/convert/NumberConverter.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/convert/NumberConverter.java?rev=739100&r1=739099&r2=739100&view=diff
==============================================================================
--- myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/convert/NumberConverter.java (original)
+++ myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/convert/NumberConverter.java Fri Jan 30 00:25:50 2009
@@ -18,16 +18,20 @@
  */
 package org.apache.myfaces.trinidadinternal.convert;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
 import javax.faces.convert.ConverterException;
 
+import org.apache.myfaces.trinidad.context.RenderingContext;
 import org.apache.myfaces.trinidad.convert.ClientConverter;
+import org.apache.myfaces.trinidad.logging.TrinidadLogger;
 import org.apache.myfaces.trinidad.util.IntegerUtils;
 import org.apache.myfaces.trinidadinternal.util.JsonUtils;
 
@@ -96,6 +100,21 @@
 
   public Collection<String> getClientImportNames()
   {
+    // Load the library for the different locale, so that the locale elements
+    // are available for the client converter and validator.
+    if (_isDifferentLocale())
+    {
+      ArrayList<String> names = new ArrayList<String>(2);
+      names.add("TrNumberConverter()");
+      // Load NamedLocaleInfoScriptlet  "LocaleInfo_<locale>"
+      String sloc = getLocale().toString();
+      StringBuffer sb = new StringBuffer(11 + sloc.length());
+      sb.append("LocaleInfo_");
+      sb.append(sloc);
+      names.add(sb.toString());
+      return names;
+    }
+    
     return _IMPORT_NAMES;
   }
 
@@ -204,5 +223,28 @@
       }
       return outBuffer.toString();
     }
+  
+  /*
+   * This method returns true if the locale specified for NumberConverter is
+   * different from the locale specified in Adf-faces-config or the client
+   * locale. If both are same or if no locale is specified for
+   * NumberConverter, then this returns false.
+   */
+  private boolean _isDifferentLocale()
+  {
+    Locale converterLocale = getLocale();
+    if (converterLocale != null)
+    {
+      Locale defaultLocale = RenderingContext.getCurrentInstance()
+          .getLocaleContext().getFormattingLocale();
+      return !converterLocale.equals(defaultLocale);
+    }
+
+    return false;
+  }
+  
+  private static final TrinidadLogger _LOG = TrinidadLogger
+      .createTrinidadLogger(NumberConverter.class);
+  
   private static final Collection<String> _IMPORT_NAMES = Collections.singletonList( "TrNumberConverter()" );
-}
\ No newline at end of file
+}

Modified: myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js?rev=739100&r1=739099&r2=739100&view=diff
==============================================================================
--- myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js (original)
+++ myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberConverter.js Fri Jan 30 00:25:50 2009
@@ -58,7 +58,7 @@
     this._groupingUsed = true;
     
   //init the TrNumberFormat
-  this._initNumberFormat();
+  this._initNumberFormat(locale);
   
   // for debugging
   this._class = "TrNumberConverter";
@@ -234,15 +234,17 @@
     var parsedValue;
     if(this._type=="percent" || this._type=="currency")
     {
+      var localeSymbols = getLocaleSymbols(this._locale);
+      
       // TODO matzew - see TRINIDAD-682
       // Remove the thousands separator - which Javascript doesn't want to see
-      var groupingSeparator = getLocaleSymbols().getGroupingSeparator();
+      var groupingSeparator = localeSymbols.getGroupingSeparator();
       var grouping = new RegExp("\\" + groupingSeparator, "g");
       numberString = numberString.replace(grouping, "");
 
       // Then change the decimal separator into a period, the only
       // decimal separator allowed by JS
-      var decimalSeparator = getLocaleSymbols().getDecimalSeparator();
+      var decimalSeparator = localeSymbols.getDecimalSeparator();
       var decimal = new RegExp("\\" + decimalSeparator, "g");
       numberString = numberString.replace(decimal, ".");
       
@@ -279,8 +281,9 @@
       }
       
       // to be able to pass the _decimalParse, we replace the decimal separator...
+      // Note that _decimalParse uses the page locale.
       var jsSeparator = new RegExp("\\" + ".",  "g");
-      numberString = numberString.replace(jsSeparator, decimalSeparator);
+      numberString = numberString.replace(jsSeparator, getLocaleSymbols().getDecimalSeparator());
     }
     
     parsedValue = _decimalParse(numberString, 
@@ -317,34 +320,28 @@
  */
 TrNumberConverter.prototype._isConvertible = function()
 {
-  if((this._pattern == null) && (this._locale == null))
-  {
-    return true;
-  }
-  else
-  {
-    return false;
-  }
+  // The locale attribute is now supported on convertNumber.
+  return (this._pattern == null);
 }
 
 /**
  * runs the creation of the used TrNumberFormat class
  */
-TrNumberConverter.prototype._initNumberFormat = function()
+TrNumberConverter.prototype._initNumberFormat = function(locale)
 {
   if(this._type=="percent")
   {
     this._example = 0.3423;
-    this._numberFormat = TrNumberFormat.getPercentInstance();
+    this._numberFormat = TrNumberFormat.getPercentInstance(locale);
   }
   else if(this._type=="currency")
   {
     this._example = 10250;
-    this._numberFormat = TrNumberFormat.getCurrencyInstance();
+    this._numberFormat = TrNumberFormat.getCurrencyInstance(locale);
   }
   else if(this._type=="number")
   {
-  	this._numberFormat = TrNumberFormat.getNumberInstance();
+  	this._numberFormat = TrNumberFormat.getNumberInstance(locale);
   }
 
   this._numberFormat.setGroupingUsed(this.isGroupingUsed());

Modified: myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js
URL: http://svn.apache.org/viewvc/myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js?rev=739100&r1=739099&r2=739100&view=diff
==============================================================================
--- myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js (original)
+++ myfaces/trinidad/branches/1.2.10.1-branch/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/NumberFormat.js Fri Jan 30 00:25:50 2009
@@ -20,15 +20,17 @@
 /**
  * constructor for TrNumberFormat.
  */
- function TrNumberFormat(type)
+ function TrNumberFormat(type, locale)
 {
   if(!type)
     alert("type for TrNumberFormat not defined!");
   this._type = type;
-  this._pPre = getLocaleSymbols().getPositivePrefix();
-  this._pSuf = getLocaleSymbols().getPositiveSuffix();
-  this._nPre = getLocaleSymbols().getNegativePrefix();
-  this._nSuf = getLocaleSymbols().getNegativeSuffix();
+  
+  this._localeSymbols = getLocaleSymbols(locale);
+  this._pPre = this._localeSymbols.getPositivePrefix();
+  this._pSuf = this._localeSymbols.getPositiveSuffix();
+  this._nPre = this._localeSymbols.getNegativePrefix();
+  this._nSuf = this._localeSymbols.getNegativeSuffix();
 
   //default values, similar to JDK (values from Apache Harmony)
   this._maxFractionDigits = 3;
@@ -45,25 +47,25 @@
 /**
  * Returns a number formater.
  */
-TrNumberFormat.getNumberInstance = function()
+TrNumberFormat.getNumberInstance = function(locale)
 {
-  return new TrNumberFormat("number");
+  return new TrNumberFormat("number", locale);
 }
 
 /**
  * Returns a currency formater.
  */
-TrNumberFormat.getCurrencyInstance = function()
+TrNumberFormat.getCurrencyInstance = function(locale)
 {
-  return new TrNumberFormat("currency");
+  return new TrNumberFormat("currency", locale);
 }
 
 /**
  * Returns a percent formater.
  */
-TrNumberFormat.getPercentInstance = function()
+TrNumberFormat.getPercentInstance = function(locale)
 {
-  return new TrNumberFormat("percent");
+  return new TrNumberFormat("percent", locale);
 }
 
 /**
@@ -347,7 +349,7 @@
   ints  = this._formatIntegers(ints);
   fracs = this._formatFractions(fracs)
   
-  var decimalSeparator = getLocaleSymbols().getDecimalSeparator();
+  var decimalSeparator = this._localeSymbols.getDecimalSeparator();
 
   if(fracs!="")
     numberString = (ints+decimalSeparator+fracs);
@@ -398,7 +400,7 @@
   // wrong, we should be notified by the following exception. If any changes need to be made, you 
   // should start by looking at _getPercentData() in:
   // maven-i18n-plugin\src\main\java\org\apache\myfaces\trinidadbuild\plugin\i18n\uixtools\JSLocaleElementsGenerator.java
-  var suffix = getLocaleSymbols().getPercentSuffix();
+  var suffix = this._localeSymbols.getPercentSuffix();
   if (!suffix || suffix == "")
   {
     throw new TrParseException("percent suffix undefined or empty");
@@ -585,7 +587,7 @@
     toFormat = ints;
   }
 
-  var groupingSeparator = getLocaleSymbols().getGroupingSeparator();
+  var groupingSeparator = this._localeSymbols.getGroupingSeparator();
   for(i=0; i < toFormat.length; i++)
   {
     if(i%3==0 && i!=0)