Dear Wiki user, You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification. The following page has been changed by MarcusVeloso: http://wiki.apache.org/tapestry/Tapestry5OutputLocaleNumber The comment on the change is: ------------------------------------------------------------------------------ - Creating a new component for formatting numbers and dates according to locale, using literals (date, integer, decimal or currency). It's based on Output component. + A component to display formatted numbers and dates according to locale, using literals (integer, decimal, currency, date e date(MM/yyyy)). It's based on Tapestry Output component. - === MyOutput.java === + === OutputLocale.java === {{{ import java.text.DateFormat; import java.text.DecimalFormat; @@ -10, +10 @@ import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.Date; - import java.util.Locale; import org.apache.tapestry.ComponentResources; import org.apache.tapestry.MarkupWriter; @@ -21, +20 @@ import org.apache.tapestry.ioc.services.ThreadLocale; /** component that formats a value and outputs it. */ - public class MyOutput { + public class OutputLocale { @Parameter(required = true) private Object _value; @@ -33, +32 @@ @Inject private ComponentResources _resources; - + @Inject + private RequestGlobals _requestGlobals; - @Service("ThreadLocale") - private ThreadLocale _threadLocale; boolean beginRender(MarkupWriter writer) throws Exception { String formatted = null; - if (_format instanceof String) { + if (_format instanceof String) + { - if (_format.toString().equalsIgnoreCase("integer")) + if (_format.toString().equalsIgnoreCase("integer")) // Syntax: format="literal:integer" formatted = integer().format(_value); - else if (_format.toString().equalsIgnoreCase("decimal")) + else if (_format.toString().equalsIgnoreCase("decimal")) // Syntax: format="literal:decimal" formatted = decimal().format(_value); - else if (_format.toString().equalsIgnoreCase("currency")) + else if (_format.toString().equalsIgnoreCase("currency")) // Syntax: format="literal:currency" formatted = currency().format(_value); - else if (_format.toString().substring(0, 4) - .equalsIgnoreCase("date")) + // Null date. + else if (_format.toString().substring(0, 4).equalsIgnoreCase("date") && _value == null) + formatted = ""; + + else if (_format.toString().substring(0, 4).equalsIgnoreCase("date")) // Syntax: format="literal:date" or format="literal:date(MM/dd/yyyy)" if (_format.toString().indexOf('(') >= 0) { - if (_format.toString().indexOf(')') < 0) + if ((_format.toString().indexOf(')') < 0) || - throw new Exception( - "Closing parenthesis does not exists"); - if (_format.toString().indexOf(')') < _format.toString().indexOf('(') + (_format.toString().indexOf(')') < _format.toString().indexOf('(') - || _format.toString().indexOf(')') != _format.toString().length() - 1) + || _format.toString().indexOf(')') != _format.toString().length() - 1)) - throw new Exception("Syntax error! The correct syntax is format=\"literal:date\" or format=\"literal:date(stringPattern)\""); - - formatted = formatDate((Date) _value, _format.toString().substring(_format.toString().indexOf('(') + 1, _format.toString().indexOf(')'))); + formatted = "?literal:"+_format.toString()+"?"; + else { + try { + formatted = formatDate((Date) _value, _format.toString().substring( + _format.toString().indexOf('(') + 1, + _format.toString().indexOf(')'))); + } + catch (Exception e) { + formatted = "?literal:"+_format.toString()+"?"; } + } } else { if (_format.toString().length() > 4) - throw new Exception("Syntax error! The correct syntax is format=\"literal:date\" or format=\"literal:date(stringPattern)\""); - + formatted = "?literal:"+_format.toString()+"?"; + else - formatted = date().format(_value); + formatted = date().format(_value); - } else - - throw new Exception("Unknown literal format"); + + formatted = "?literal:"+_format.toString()+"?"; // Unknown literal format. } else { @@ -102, +108 @@ return false; } - private Locale _localeDefault = _threadLocale.getLocale().getDefault(); - public NumberFormat integer() { - NumberFormat fmt = NumberFormat.getInstance(_localeDefault); + NumberFormat fmt = NumberFormat.getInstance( + _requestGlobals.getRequest().getLocale()); fmt.setMaximumFractionDigits(0); return fmt; } public DecimalFormat decimal() { - return (DecimalFormat) NumberFormat.getInstance(_localeDefault); + return (DecimalFormat) NumberFormat.getInstance( + _requestGlobals.getRequest().getLocale()); } public DecimalFormat currency() { - DecimalFormat fmt = (DecimalFormat) NumberFormat.getCurrencyInstance(_localeDefault); + DecimalFormat fmt = (DecimalFormat) NumberFormat.getCurrencyInstance( + _requestGlobals.getRequest().getLocale()); fmt.setDecimalSeparatorAlwaysShown(true); fmt.setMaximumFractionDigits(2); fmt.setMinimumFractionDigits(2); @@ -124, +131 @@ public DateFormat date() { return (DateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM, - _localeDefault); + _requestGlobals.getRequest().getLocale()); } public String formatDate(Date d, String pattern) { - SimpleDateFormat sdf = new SimpleDateFormat(pattern, _localeDefault); + SimpleDateFormat sdf = new SimpleDateFormat(pattern, + _requestGlobals.getRequest().getLocale()); - return (d==null?"":sdf.format(d)); + return sdf.format(d); } } }}} [[BR]] - Done. Now we need some page for test, something like: + Testing... - === TestOutputLocaleNumber.java === + === TestOutputLocale.java === {{{ package org.example.hilo.pages; - public class TestOutputLocaleNumber + public class TestOutputLocale { private float _valor=12345.6789f; @@ -159, +167 @@ } }}} - === TestOutputLocaleNumber.html === + === TestOutputLocale.tml === {{{ - Output Locale Number + Output Locale @@ -179, +187 @@ ${valor}

using format="literal:integer" results
-

+

using format="literal:decimal" results
-

+

using format="literal:currency" results
-

+

using format="literal:date" results
-

+

using format="literal:date(yyyy-MM-dd)" results
-

+

using format="someFormat" acts exactly like in Output component

@@ -203, +211 @@ }}} [[BR]] - As you can see here, we can extend components and learn a little more about Tapestry 5. Thank you Howard! + We can extend components and learn a little bit about Tapestry 5. Thank you Howard! + [[BR]][[BR]] + Marcus Veloso [[BR]] --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org For additional commands, e-mail: dev-help@tapestry.apache.org