Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If you have conversions that should be done the same way in your whole application, you can provide your own converter factory (instance of wicket.util.convert.IConverterFactory). You would use this to force patterns (like always displaying doubles with two fraction digits), or to extend the default set of converters that ships with Wicket with your own types.

In Wicket 1.1

To provide your own IConverterFactory, you need to override getConverterFactory in your application class. For example:

Code Block
/**
 * @see wicket.Application#getConverterFactory()
 */
public IConverterFactory getConverterFactory()
{
  return new IConverterFactory()
  {
    public IConverter newConverter(final Locale locale)
    {
      final Converter converter = new Converter(locale);
      NumberToStringConverter numberToStringConverter = new NumberToStringConverter();
      NumberFormat fmt = NumberFormat.getInstance(locale);
      fmt.setMinimumFractionDigits(2);
      fmt.setMaximumFractionDigits(2);
      numberToStringConverter.setNumberFormat(locale, fmt);
      final StringConverter stringConverter = new StringConverter();
      stringConverter.set(Double.class, numberToStringConverter);
      stringConverter.set(Double.TYPE, numberToStringConverter);
      converter.set(String.class, stringConverter);
      return converter;
    }
  };
}

In Wicket 1.2

Override init() method in Application class to modify IApplicationSettings.

Code Block

protected void init() {
    super.init();
    IApplicationSettings settings = getApplicationSettings();

    settings.setConverterFactory(new IConverterFactory() {
        public IConverter newConverter(final Locale locale) {
	    final Converter converter = new Converter(locale);
	    final StringConverter stringConverter = new StringConverter();
	    final DateToStringConverter dateToStringConverter = new DateToStringConverter();
	    dateToStringConverter.setDateFormat(locale, new SimpleDateFormat("dd/MM/yy"));
	    stringConverter.set(Date.class,dateToStringConverter);
	    final DateConverter dateConverter = new DateConverter(false);
	    // set lenient to false for dateformat so that strict pasrsing is done.
	    final DateFormat format = new SimpleDateFormat("dd/MM/yy");
	    format.setLenient(false);
	    dateConverter.setDateFormat(locale, format);
	    converter.set(Date.class,dateConverter);
	    converter.set(String.class,stringConverter);
	    return converter;
	}
    });
}

Providing a custom converter for specific components

...