Search code examples
javatapestry

Translator functionality in Tapestry5 Grid Componenet


Today I will have question regarding formatting numbers in Tapestry5 grid. I have couple of fields in Grid component that I need to format due to some rules that concerns decimal mark and thousands separator. For textfields I'm using translator defined for whole application (like in this example) but I have no idea how to get same functionality for grid (or even if it is possible at all).I would like to have same mechanism available for all grids in my whole applications.

Of course not all numeric grid field will be currencies so there should be some kind of possibility to configure them.

I know that with grid I can use ValueEncoder but I think that this won't work in that case because it encodes whole object displayed in grid, not only some defined fields.

One more thing: I know that I can define each field to get value from method and there do all functionality, but I'm looking for something more flexible - like mentioned earlier "translator" for textfields.


Solution

  • This can be done by the combination of a few things

    1. A DataTypeAnalyzer which produces a String datatype given a PropertyAdapter
    2. A DisplayBlockContribution which maps the datatype to a block (see also EditBlockContribution).
    3. Contribute the DisplayBlockContribution to the BeanBlockSource

    Note that this will not only affect the grid, it will also affect all bean based components (eg BeanDisplay).

    Jumpstart example here