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: Initial changes. New page: Creating a new component for formatting (numbers) according to locale, using literals (integer, decimal or currency). It's based on Output component. === MyOutput.java === {{{ package org.example.hilo.components; import java.text.DecimalFormat; import java.text.Format; import java.text.NumberFormat; import java.util.Locale; 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 MyOutput { @Parameter(required = true) private Object _value; @Parameter(required = true) private Object _format; @Parameter("componentResources.elementName") private String _elementName; @Inject private ComponentResources _resources; @Inject @Service("ThreadLocale") private ThreadLocale _threadLocale; 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() { NumberFormat fmt = NumberFormat.getInstance(_localeDefault); fmt.setMaximumFractionDigits(0); return fmt; } public DecimalFormat decimal() { return (DecimalFormat)NumberFormat.getInstance(_localeDefault); } public DecimalFormat currency() { DecimalFormat fmt = (DecimalFormat) NumberFormat.getCurrencyInstance(_localeDefault); fmt.setDecimalSeparatorAlwaysShown(true); fmt.setMaximumFractionDigits(2); fmt.setMinimumFractionDigits(2); return fmt; } } }}} [[BR]] Done. Now we need some page for test, something like: === TestOutputLocaleNumber.java === {{{ package org.example.hilo.pages; public class TestOutputLocaleNumber { private float _valor=12345.6789f; public float getValor() { return _valor; } } }}} === TestOutputLocaleNumber.html === {{{ <html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd"> <head> <title>Output Locale Number</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="MyOutput" value="valor" format="literal:integer"/></p> <p>using format="literal:decimal" results<br/> <span t:type="MyOutput" value="valor" format="literal:decimal"/></p> <p>using format="literal:currency" results<br/> <span t:type="MyOutput" value="valor" format="literal:currency"/></p> <p>using format="someFormat" acts exactly like in Output component</p> </form> </center> </body> </html> }}} [[BR]] As you can see here, we can extend components and learn a little more about Tapestry 5. Thank you Howard! [[BR]] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
