You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChangingTheDefaultConvertersForAnApplication.asciidoc 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ---
  2. title: Changing The Default Converters For An Application
  3. order: 6
  4. layout: page
  5. ---
  6. [[changing-the-default-converters-for-an-application]]
  7. = Changing the default converters for an application
  8. Each Vaadin session instance has a `ConverterFactory` that provides
  9. converters to Fields and Table. The defaults might not be ideal for your
  10. case so it is possible for you to change the defaults by providing your
  11. own ConverterFactory. If you, for instance, want to format all (or most)
  12. doubles from your data model with 3 decimals and no thousand separator
  13. (but still allow the user to input with any number of decimals) you can
  14. do this by first creating your own Converter:
  15. [source,java]
  16. ....
  17. public class MyStringToDoubleConverter extends StringToDoubleConverter {
  18. @Override
  19. protected NumberFormat getFormat(Locale locale) {
  20. NumberFormat format = super.getFormat(locale);
  21. format.setGroupingUsed(false);
  22. format.setMaximumFractionDigits(3);
  23. format.setMinimumFractionDigits(3);
  24. return format;
  25. }
  26. }
  27. ....
  28. and then extending the default converter factory to use your converter
  29. for all `Double` <-> `String` conversions.
  30. [source,java]
  31. ....
  32. public class MyConverterFactory extends DefaultConverterFactory {
  33. @Override
  34. protected <PRESENTATION, MODEL> Converter<PRESENTATION, MODEL> findConverter(
  35. Class<PRESENTATION> presentationType, Class<MODEL> modelType) {
  36. // Handle String <-> Double
  37. if (presentationType == String.class && modelType == Double.class) {
  38. return (Converter<PRESENTATION, MODEL>) new MyStringToDoubleConverter();
  39. }
  40. // Let default factory handle the rest
  41. return super.findConverter(presentationType, modelType);
  42. }
  43. }
  44. ....
  45. You still need to tell your application to always use
  46. `MyConverterFactory`:
  47. [source,java]
  48. ....
  49. VaadinSession.getCurrent().setConverterFactory(new MyConverterFactory());
  50. ....
  51. Now we can test it using
  52. [source,java]
  53. ....
  54. public class MyUI extends UI {
  55. public void init(VaadinRequest request) {
  56. TextField tf = new TextField("This is my double field");
  57. tf.setImmediate(true);
  58. tf.setConverter(Double.class);
  59. setContent(tf);
  60. tf.setConvertedValue(50.1);
  61. }
  62. }
  63. ....
  64. This will not enforce the contents of the field to the format specified
  65. by the converter. Only data from the data source is formatted to adhere
  66. to the format set in the converter.
  67. If you want to force the user to enter data with a given number of
  68. decimals you need to create your own converter instead of only
  69. overriding the format for `StringToDoubleConverter`.