Menu

ManualCustomValueRendering

Remo

Customizing Enumerations and Null Value Representations

Since editing enumeration properties (properties which have a fixed limited range of allowed values e.g. Gender: male, female, unknown)
is very common in information systems, RCPForms offers mechanisms to do this automatically, so individual form programmers just bind
a combo or radio group to an enumeration type property and everything else
(filling the combo, transforming values to strings, creating radio buttons according to enum values) is handled by the system.

How it works

When you bind a property to a combo or radio group using the ValidationManager.bindValue() method, it asks the model adapter for an EnumRangeAdapter calling
ValidationManager.handleRangeAdapter() which asks the ModelAdapter of your model object for an IRangeAdapter implementation.

If an IRangeAdapter implemetation is available, this is used to create a content provider, label provider and input for the combo viewer.
For a radio group the content provider is used to determine the model values associated with the radio buttons, and the label provider is used to create labels for the buttons.
Thus if a suitable IRangeAdapter is provided, enumeration ranges are automatically filled in the user interface.

The default BeanAdapter supports two range adapter which you can check as an example implementation:

  • one for standard java enums: EnumRangeAdapter
  • one for integer ranges using an annotation on the property:!IntegerRangeAdapter with IntegerRangeAdapter.IntRange? annotation

How to customize it

Usually big projects create specific data types for handling enumeration values which are not java enums, but may be read from a database,
may be versioned or extensible by an admin or the user.
To enable the above mechanism for your enumeration implementation you have to do the following:

  • create your own ModelAdapter (if you use beans, derive from BeanAdapter and overwrite method getRangeAdapter(type)), see use your own model for further details
  • make sure your ModelAdapter declares that it can handle your model objects by overriding ModelAdapter.canAdaptClass()
  • register your model adapter using ModelAdapter.registerAdapter(myAdapter)
  • implement a RangeAdapter which provides contentprovider, labelprovider and input for the enum type defined by the passed property and return it in the getRangeAdapter() method

Binding a property of your enum type to a combo should now result in the combo viewer configured with the input, label and content providers from the Range Adapter.

Examples

Change the representation of java enums to a different one by overriding the toString() method of the enum.


Related

Documentation: ManualCustomModel

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.