You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Leon Derks <le...@aurumeurope.com> on 2011/10/12 11:02:43 UTC

NumberFormatter

I have a TextField that shows a double value.

But I want to format the value in the Textfield as a decimal with 4 decimals (i.e. 0.1234)

In Tapestry there is a NumericFormatter and a BigDecimalNumericFormatter. But I can't find any documentation on how to use these on a TextField.
Can someone give me an example how I can format my double value into a decimal value with 4 decimals?

regards,
Leon



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: NumberFormatter

Posted by Leon Derks <le...@aurumeurope.com>.
Hello all,
Thanks for your support.

It was quite some work, but I managed to get it working..
For everybody else who is struggling with this, here is my code for Tapestry 5.2.6

----Page.tml
<input type="text" t:type="textfield" t:id="currencyInput" t:validate="required" value="price" translate="currency" />

----CurrencyTranslator.java
import java.text.DecimalFormat;

import org.apache.tapestry5.Field;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.ValidationException;
import org.apache.tapestry5.internal.translator.AbstractTranslator;
import org.apache.tapestry5.services.FormSupport;

public class CurrencyTranslator extends AbstractTranslator<Double> {

	private static DecimalFormat decimalFormat = new DecimalFormat("0.0000");
	
	public CurrencyTranslator() {
		super("currency", Double.class, "currency-format-exception");
	}

	@Override
	public String toClient(Double value) {
		return decimalFormat.format(value);
	}

	@Override
	public Double parseClient(Field field, String clientValue, String message) throws ValidationException {
		Double returnValue = null;
		
		if(clientValue != null) {
			try{
				returnValue =  Double.valueOf(clientValue);
			}
			catch(Exception e) {
				throw new ValidationException(message);
			}
		}
		
		return returnValue;
	}

	@Override
	public void render(Field field, String message, MarkupWriter writer, FormSupport formSupport) {
	}
	
}

----AppModule.java
public static void contributeTranslatorSource(MappedConfiguration<Class, Translator> configuration) {
	configuration.override(Double.class, new CurrencyTranslator());
 }


On Oct 12, 2011, at 12:34 PM, Muhammad Gelbana wrote:

> When you need to "contribute" a configuration or anything to tapestry, first
> check this page:
> http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/TapestryModule.html
> 
> Find the method likely to satisfy your need, like in your case here:
> http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/TapestryModule.html#contributeTranslatorSource(org.apache.tapestry5.ioc.MappedConfiguration,
> org.apache.tapestry5.internal.translator.NumericTranslatorSupport)
> 
> Google it to see it in action:
> http://www.google.com.eg/search?gcx=w&sourceid=chrome&ie=UTF-8&q=contributeTranslatorSource#sclient=psy-ab&hl=en&source=hp&q=tapestry+%22contributeTranslatorSource%22&pbx=1&oq=tapestry+%22contributeTranslatorSource%22&aq=f&aqi=&aql=&gs_sm=e&gs_upl=13088l14418l1l14680l2l3l0l0l0l0l0l0ll1l0&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=127c72f3b5eca502&biw=1366&bih=643
> 
> Check those links too:
> http://tapestryjava.blogspot.com/2008/09/tapestry-5-ioc-introducing-service.html
> http://jumpstart.doublenegative.com.au/jumpstart/examples/input/translators1
> http://jumpstart.doublenegative.com.au/jumpstart/examples/input/creatingtranslators1
> 
> 
> Then love tapestry even more :)
> Good luck.
> 
> On Wed, Oct 12, 2011 at 12:04 PM, Leon Derks <le...@aurumeurope.com>wrote:
> 
>> Hello Chris,
>> 
>> Thank you. Yes I have already seen that page, but I couldn't find an
>> example with a FieldTranslator.
>> Does anyone have an example how to do this?
>> 
>> regards,
>> Leon
>> 
>> 
>> On Oct 12, 2011, at 11:57 AM, derkoe wrote:
>> 
>>> 
>>> Leon Derks wrote:
>>>> 
>>>> I have a TextField that shows a double value.
>>>> 
>>>> But I want to format the value in the Textfield as a decimal with 4
>>>> decimals (i.e. 0.1234)
>>>> 
>>>> In Tapestry there is a NumericFormatter and a
>> BigDecimalNumericFormatter.
>>>> But I can't find any documentation on how to use these on a TextField.
>>>> Can someone give me an example how I can format my double value into a
>>>> decimal value with 4 decimals?
>>>> 
>>>> regards,
>>>> Leo
>>>> 
>>> 
>>> For input fields (such as TextField) you can add a parameter "translate"
>> of
>>> type FieldTranslator which translates between the object (e.g.
>> BigDecimal)
>>> and the String representation in the input.
>>> See:
>>> 
>> http://tapestry.apache.org/current/tapestry-core/ref/org/apache/tapestry5/corelib/components/TextField.html
>>> 
>>> You can also define your translator globally by contributing to
>>> TranslatorSource.
>>> 
>>> --
>>> Chris
>>> 
>>> --
>>> View this message in context:
>> http://tapestry.1045711.n5.nabble.com/NumberFormatter-tp4894812p4894939.html
>>> Sent from the Tapestry - User mailing list archive at Nabble.com.
>>> 
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>> 
>> 
> 
> 
> -- 
> *Regards,*
> *Muhammad Gelbana
> Java Developer*


Re: NumberFormatter

Posted by Muhammad Gelbana <m....@gmail.com>.
When you need to "contribute" a configuration or anything to tapestry, first
check this page:
http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/TapestryModule.html

Find the method likely to satisfy your need, like in your case here:
http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/TapestryModule.html#contributeTranslatorSource(org.apache.tapestry5.ioc.MappedConfiguration,
org.apache.tapestry5.internal.translator.NumericTranslatorSupport)

Google it to see it in action:
http://www.google.com.eg/search?gcx=w&sourceid=chrome&ie=UTF-8&q=contributeTranslatorSource#sclient=psy-ab&hl=en&source=hp&q=tapestry+%22contributeTranslatorSource%22&pbx=1&oq=tapestry+%22contributeTranslatorSource%22&aq=f&aqi=&aql=&gs_sm=e&gs_upl=13088l14418l1l14680l2l3l0l0l0l0l0l0ll1l0&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=127c72f3b5eca502&biw=1366&bih=643

Check those links too:
http://tapestryjava.blogspot.com/2008/09/tapestry-5-ioc-introducing-service.html
http://jumpstart.doublenegative.com.au/jumpstart/examples/input/translators1
http://jumpstart.doublenegative.com.au/jumpstart/examples/input/creatingtranslators1


Then love tapestry even more :)
 Good luck.

On Wed, Oct 12, 2011 at 12:04 PM, Leon Derks <le...@aurumeurope.com>wrote:

> Hello Chris,
>
> Thank you. Yes I have already seen that page, but I couldn't find an
> example with a FieldTranslator.
> Does anyone have an example how to do this?
>
> regards,
> Leon
>
>
> On Oct 12, 2011, at 11:57 AM, derkoe wrote:
>
> >
> > Leon Derks wrote:
> >>
> >> I have a TextField that shows a double value.
> >>
> >> But I want to format the value in the Textfield as a decimal with 4
> >> decimals (i.e. 0.1234)
> >>
> >> In Tapestry there is a NumericFormatter and a
> BigDecimalNumericFormatter.
> >> But I can't find any documentation on how to use these on a TextField.
> >> Can someone give me an example how I can format my double value into a
> >> decimal value with 4 decimals?
> >>
> >> regards,
> >> Leo
> >>
> >
> > For input fields (such as TextField) you can add a parameter "translate"
> of
> > type FieldTranslator which translates between the object (e.g.
> BigDecimal)
> > and the String representation in the input.
> > See:
> >
> http://tapestry.apache.org/current/tapestry-core/ref/org/apache/tapestry5/corelib/components/TextField.html
> >
> > You can also define your translator globally by contributing to
> > TranslatorSource.
> >
> > --
> > Chris
> >
> > --
> > View this message in context:
> http://tapestry.1045711.n5.nabble.com/NumberFormatter-tp4894812p4894939.html
> > Sent from the Tapestry - User mailing list archive at Nabble.com.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
*Regards,*
*Muhammad Gelbana
Java Developer*

Re: NumberFormatter

Posted by Leon Derks <le...@aurumeurope.com>.
Hello Chris,

Thank you. Yes I have already seen that page, but I couldn't find an example with a FieldTranslator.
Does anyone have an example how to do this?

regards,
Leon


On Oct 12, 2011, at 11:57 AM, derkoe wrote:

> 
> Leon Derks wrote:
>> 
>> I have a TextField that shows a double value.
>> 
>> But I want to format the value in the Textfield as a decimal with 4
>> decimals (i.e. 0.1234)
>> 
>> In Tapestry there is a NumericFormatter and a BigDecimalNumericFormatter.
>> But I can't find any documentation on how to use these on a TextField.
>> Can someone give me an example how I can format my double value into a
>> decimal value with 4 decimals?
>> 
>> regards,
>> Leo
>> 
> 
> For input fields (such as TextField) you can add a parameter "translate" of
> type FieldTranslator which translates between the object (e.g. BigDecimal)
> and the String representation in the input.
> See:
> http://tapestry.apache.org/current/tapestry-core/ref/org/apache/tapestry5/corelib/components/TextField.html
> 
> You can also define your translator globally by contributing to
> TranslatorSource.
> 
> -- 
> Chris
> 
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/NumberFormatter-tp4894812p4894939.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: NumberFormatter

Posted by derkoe <ta...@gmail.com>.
Leon Derks wrote:
> 
> I have a TextField that shows a double value.
> 
> But I want to format the value in the Textfield as a decimal with 4
> decimals (i.e. 0.1234)
> 
> In Tapestry there is a NumericFormatter and a BigDecimalNumericFormatter.
> But I can't find any documentation on how to use these on a TextField.
> Can someone give me an example how I can format my double value into a
> decimal value with 4 decimals?
> 
> regards,
> Leo
> 

For input fields (such as TextField) you can add a parameter "translate" of
type FieldTranslator which translates between the object (e.g. BigDecimal)
and the String representation in the input.
See:
http://tapestry.apache.org/current/tapestry-core/ref/org/apache/tapestry5/corelib/components/TextField.html

You can also define your translator globally by contributing to
TranslatorSource.

-- 
Chris

--
View this message in context: http://tapestry.1045711.n5.nabble.com/NumberFormatter-tp4894812p4894939.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org