Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for 
change notification.

The "Tapestry5OutputLocaleNumber" page has been changed by MarcusVeloso.
The comment on this change is: google code project myt5lib is updated..
http://wiki.apache.org/tapestry/Tapestry5OutputLocaleNumber?action=diff&rev1=4&rev2=5

--------------------------------------------------

- 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.
+ This content was moved to the myt5lib project. 
(http://code.google.com/p/myt5lib/wiki/OutputLocale)
  
- === OutputLocale.java ===
- {{{
- import java.text.DateFormat;
- import java.text.DecimalFormat;
- import java.text.Format;
- import java.text.NumberFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- 
- import org.apache.tapestry.ComponentResources;
- import org.apache.tapestry.MarkupWriter;
- import org.apache.tapestry.annotations.Inject;
- import org.apache.tapestry.annotations.Parameter;
- import org.apache.tapestry.annotations.Service;
- import org.apache.tapestry.ioc.internal.util.InternalUtils;
- import org.apache.tapestry.ioc.services.ThreadLocale;
- 
- /** component that formats a value and outputs it. */
- public class OutputLocale {
-       @Parameter(required = true)
-       private Object _value;
- 
-       @Parameter(required = true)
-       private Object _format;
- 
-       @Parameter("componentResources.elementName")
-       private String _elementName;
- 
-       @Inject
-       private ComponentResources _resources;
-       
-       @Inject
-       private RequestGlobals _requestGlobals;
- 
-       boolean beginRender(MarkupWriter writer) throws Exception {
-               String formatted = null;
- 
-               if (_format instanceof String) 
-               {
-                       if (_format.toString().equalsIgnoreCase("integer")) // 
Syntax: format="literal:integer"
- 
-                               formatted = integer().format(_value);
- 
-                       else if 
(_format.toString().equalsIgnoreCase("decimal")) // Syntax: 
format="literal:decimal"
- 
-                               formatted = decimal().format(_value);
- 
-                       else if 
(_format.toString().equalsIgnoreCase("currency")) // Syntax: 
format="literal:currency"
- 
-                               formatted = currency().format(_value);
- 
-                       // 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) ||
-                                               
(_format.toString().indexOf(')') < _format.toString().indexOf('(')
-                                               || 
_format.toString().indexOf(')') != _format.toString().length() - 1))
-                                               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)
-                                               formatted = 
"?literal:"+_format.toString()+"?";
-                                       else
-                                               formatted = 
date().format(_value);
-                               }
- 
-                       else
-                               
-                               formatted = "?literal:"+_format.toString()+"?"; 
 // 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() {
-               NumberFormat fmt = NumberFormat.getInstance(
-                       _requestGlobals.getRequest().getLocale());
-               fmt.setMaximumFractionDigits(0);
-               return fmt;
-       }
- 
-       public DecimalFormat decimal() {
-               return (DecimalFormat) NumberFormat.getInstance(
-                       _requestGlobals.getRequest().getLocale());
-       }
- 
-       public DecimalFormat currency() {
-               DecimalFormat fmt = (DecimalFormat) 
NumberFormat.getCurrencyInstance(
-                       _requestGlobals.getRequest().getLocale());
-               fmt.setDecimalSeparatorAlwaysShown(true);
-               fmt.setMaximumFractionDigits(2);
-               fmt.setMinimumFractionDigits(2);
-               return fmt;
-       }
- 
-       public DateFormat date() {
-               return (DateFormat) 
DateFormat.getDateInstance(DateFormat.MEDIUM,
-                       _requestGlobals.getRequest().getLocale());
-       }
- 
-       public String formatDate(Date d, String pattern) {
-               SimpleDateFormat sdf = new SimpleDateFormat(pattern, 
-                       _requestGlobals.getRequest().getLocale());
-               return sdf.format(d);
-       }
- 
- }
- }}}
- 
- <<BR>>
- Testing...
- 
- === TestOutputLocale.java ===
- {{{
- package org.example.hilo.pages;
- 
- public class TestOutputLocale
- {
-       private float _valor=12345.6789f;
- 
-       public float getValor() {
-               return _valor;
-       }
- 
-         private Date _someDate = new Date();
- 
-       public Date getSomeDate() {
-               return _someDate;
-       }
- 
- }
- }}}
- 
- === TestOutputLocale.tml ===
- {{{
- <html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd";>
-       <head>
-               <title>Output Locale</title>
-       </head>
- 
-       <body>
-       <center>
- 
-       <h1>Output Locale Number</h1>
- 
-       <form t:type="Form">
- 
-               <p>Number is 12345.6789</p>
- 
-               <p>using expansion results<br/>
-                       ${valor}</p>
- 
-               <p>using format="literal:integer" results<br/>
-                       <span t:type="OutputLocale" value="valor" 
format="literal:integer"/></p>
- 
-               <p>using format="literal:decimal" results<br/>
-                       <span t:type="OutputLocale" value="valor" 
format="literal:decimal"/></p>
- 
-               <p>using format="literal:currency" results<br/>
-                       <span t:type="OutputLocale" value="valor" 
format="literal:currency"/></p>
- 
-                 <p>using format="literal:date" results<br/>
-                       <span t:type="OutputLocale" value="someDate" 
format="literal:date"/></p>
- 
-                 <p>using format="literal:date(yyyy-MM-dd)" results<br/>
-                       <span t:type="OutputLocale" value="someDate" 
format="literal:date(yyyy-MM-dd)"/></p>
- 
-               <p>using format="someFormat" acts exactly like in Output 
component</p>
- 
-       </form>
-       
-       </center>
-       </body>
- </html>
- }}}
- 
- <<BR>>
- We can extend components and learn a little bit about Tapestry 5. Thank you 
Howard!
- <<BR>><<BR>>
- Marcus Veloso
- <<BR>>
- 

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to