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 MarceloLotif: http://wiki.apache.org/tapestry/Tapestry5OutputLocaleNumber The comment on the change is: Added date formatting to the output component ------------------------------------------------------------------------------ - Creating a new component for formatting (numbers) according to locale, using literals (integer, decimal or currency). It's based on Output component. + 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. === MyOutput.java === {{{ + import java.text.DateFormat; - package org.example.hilo.components; - import java.text.DecimalFormat; import java.text.Format; import java.text.NumberFormat; + import java.text.SimpleDateFormat; + import java.util.Date; import java.util.Locale; import org.apache.tapestry.ComponentResources; @@ -20, +21 @@ import org.apache.tapestry.ioc.services.ThreadLocale; /** component that formats a value and outputs it. */ - public class MyOutput + public class MyOutput { - { - @Parameter(required = true) + @Parameter(required = true) - private Object _value; + private Object _value; - @Parameter(required = true) + @Parameter(required = true) - private Object _format; + private Object _format; - @Parameter("componentResources.elementName") + @Parameter("componentResources.elementName") - private String _elementName; + private String _elementName; - @Inject + @Inject - private ComponentResources _resources; + private ComponentResources _resources; - @Inject + @Inject - @Service("ThreadLocale") + @Service("ThreadLocale") - private ThreadLocale _threadLocale; + private ThreadLocale _threadLocale; + boolean beginRender(MarkupWriter writer) throws Exception { + String formatted = null; + + if (_format instanceof String) { + if (_format.toString().equalsIgnoreCase("integer")) + + formatted = integer().format(_value); + + else if (_format.toString().equalsIgnoreCase("decimal")) + + formatted = decimal().format(_value); + + else if (_format.toString().equalsIgnoreCase("currency")) + + formatted = currency().format(_value); + + else if (_format.toString().substring(0, 4) + .equalsIgnoreCase("date")) + + 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().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(')'))); + + } else { + if (_format.toString().length() > 4) + throw new Exception("Syntax error! The correct syntax is format=\"literal:date\" or format=\"literal:date(stringPattern)\""); + + formatted = date().format(_value); + + } + + else + + throw new Exception("Unknown literal format"); + } + + else { + + formatted = ((Format) _format).format(_value); + } + + if (InternalUtils.isNonBlank(formatted)) { + if (_elementName != null) { + writer.element(_elementName); + + _resources.renderInformalParameters(writer); + } + + writer.write(formatted); + + if (_elementName != null) + writer.end(); + } + + return false; + } + - private Locale _localeDefault = _threadLocale.getLocale().getDefault(); + private Locale _localeDefault = _threadLocale.getLocale().getDefault(); - - boolean beginRender(MarkupWriter writer) - { - String formatted = null; - - if (_format instanceof String) - { - if (_format.toString().equalsIgnoreCase("integer")) - formatted = integer().format(_value); - - else if (_format.toString().equalsIgnoreCase("decimal")) - - formatted = decimal().format(_value); - - else if (_format.toString().equalsIgnoreCase("currency")) - - formatted = currency().format(_value); - - else - - formatted = "unknown literal format"; - } - - else { - - formatted = ((Format)_format).format(_value); - } - - if (InternalUtils.isNonBlank(formatted)) - { - if (_elementName != null) - { - writer.element(_elementName); - - _resources.renderInformalParameters(writer); - } - - writer.write(formatted); - - if (_elementName != null) writer.end(); - } - - return false; - } - - public NumberFormat integer() + public NumberFormat integer() { - { - NumberFormat fmt = NumberFormat.getInstance(_localeDefault); + NumberFormat fmt = NumberFormat.getInstance(_localeDefault); - fmt.setMaximumFractionDigits(0); + fmt.setMaximumFractionDigits(0); - return fmt; - } - + return fmt; + } + - public DecimalFormat decimal() + public DecimalFormat decimal() { - { - return (DecimalFormat)NumberFormat.getInstance(_localeDefault); + return (DecimalFormat) NumberFormat.getInstance(_localeDefault); - } - + } + - public DecimalFormat currency() + public DecimalFormat currency() { + DecimalFormat fmt = (DecimalFormat) NumberFormat.getCurrencyInstance(_localeDefault); - { - DecimalFormat fmt = (DecimalFormat) - NumberFormat.getCurrencyInstance(_localeDefault); - fmt.setDecimalSeparatorAlwaysShown(true); + fmt.setDecimalSeparatorAlwaysShown(true); - fmt.setMaximumFractionDigits(2); + fmt.setMaximumFractionDigits(2); - fmt.setMinimumFractionDigits(2); + fmt.setMinimumFractionDigits(2); - return fmt; - } + return fmt; + } + + public DateFormat date() { + return (DateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM, + _localeDefault); + } + + public String formatDate(Date d, String pattern) { + SimpleDateFormat sdf = new SimpleDateFormat(pattern, _localeDefault); + return (d==null?"":sdf.format(d)); + } + } }}} @@ -122, +148 @@ public float getValor() { return _valor; + } + + private Date _someDate = new Date(); + + public Date getSomeDate() { + return _someDate; } } @@ -155, +187 @@ <p>using format="literal:currency" results<br/> <span t:type="MyOutput" value="valor" format="literal:currency"/></p> + <p>using format="literal:date" results<br/> + <span t:type="MyOutput" value="someDate" format="literal:date"/></p> + + <p>using format="literal:date(yyyy-MM-dd)" results<br/> + <span t:type="MyOutput" value="someDate" format="literal:date(yyyy-MM-dd)"/></p> + <p>using format="someFormat" acts exactly like in Output component</p> </form> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
