Thanks for the ideas. Here is my implementation. I split it into two
classes since I may want to use the CurrencyConverter separately. In
my initial testing it works well, but I haven't tested
convertToObject(). Let me know if anyone sees any issues or has
suggested improvements:
public class CurrencyConverter extends AbstractNumberConverter {
private static final long serialVersionUID = 1L;
public CurrencyConverter() {
}
@Override
public NumberFormat getNumberFormat(Locale locale) {
return NumberFormat.getCurrencyInstance(locale);
}
@Override
public Class getTargetType() {
return Double.class;
}
public Object convertToObject(String value, Locale locale) {
// string to double.
try {
return getNumberFormat(locale).parse(value).doubleValue();
} catch (ParseException ex) {
throw new WicketRuntimeException("exception when trying to
parse currency value:" + ex.getMessage());
}
}
@Override
public String convertToString(Object value, Locale locale) {
// double to string
return getNumberFormat(locale).format(value);
}
}
public class CurrencyLabel extends Label {
private static final long serialVersionUID = 1L;
public CurrencyLabel(String id) {
this(id,null);
}
public CurrencyLabel(String id, IModel model) {
super(id,model);
}
public IConverter getConverter(Class type) {
return new CurrencyConverter();
}
}
Anyone can feel free to use this or add it to Wicket core.
Tauren
On 10/5/07, Eelco Hillenius <[EMAIL PROTECTED]> wrote:
> > Label dbl = new Label("dbllbl",""+x){
>
> Why pass in a string? Better is to do new Label("foo", new Model(x));
>
> > @Override
> > protected void onComponentTagBody(final
> > MarkupStream markupStream, final
> > ComponentTag openTag)
> > {
> > Object val =
> > getConverter().convert(getModelObjectAsString(),
>
> If you are merely displaying the value, you don't need to convert.
> What this line above does - if you'd pass in a model that produces a
> number) is convert from a number to a string (using the converter,
> this is in getModelObjectAsString) and back again. You can just do
> Object val = getModelObject().
>
> Alternatives:
> 1) Wrap the model (decorator pattern) so that it returns the formatted value.
> 2) Use a custom converter like:
>
> new Label("foo", new Model(x)) {
> public IConverter getConverter(Class type) {
> return new AbstractNumberConverter() {
> public NumberFormat getNumberFormat(Locale locale) {
> return NumberFormat.getCurrencyInstance();
> }
> }
> }
> }
>
>
> Eelco
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]