Freitag, 6. September 2013

CDI - ComboBoxCell - How to reduce 80% of code

The full code and demo jUnit you will find under : BitBucket Repo - RapidPM - Modules - javafx-filtered-tableview 
Please use the develop branch and a commit after aec4f95 

Today I had a TableView with the following function.
- In one column there was a ComboBoxCell.
- The values of this ComboBox was calculated / loaded at the moment the combobox was used.
- Each ComboBox will have different values based on the context of this row.

The first version was something like this:
A ComboBoxCell:
and the code called from the controller:

This is to much boilerplate code. So I started to reduce it.

First step was reducing the generics..
This is shorter, but not better... 

The solution is the abstract GenericComboBoxCell. (part of the javafx-filtered-tableview module)

Iterating over the columns are now part of the GenericComboBoxCell, and here will be the typesave code.

The developer must extend this class and has to implement the following methods only:
- protected abstract GenericComboBoxCell getComboBoxCellRef(); 
- public abstract boolean disableComboBox(final RT row); (optional) 
- public abstract List createComboBoxValues(final RT row); 
- public abstract void workOnRowItself(final RT row); (optional) 

A demo implementation is shown here: 

The controlle will need a piece of code like this: 

With this you can test the context logic indepentend from the javafx stuff. 
All initializations inside a ComboBoxCell must be done in a PostConstruct - method. 

With the GenericComboBoxCell you can activate/deactivate the combobox, modify the row cell values, and create the combobox values. 

This implementation is based on the JavaFX/CDI bootstrap from cdi-commons-fx. (blog entry JavaFX/CDI bootstrap )

UML of this demo:

Full CDI dependicies