I use the org.eclipse.core.databinding framework to bind some Text fields in an SWT application. I add an update strategy to validate the data and to set the value on the model only when the user click on the save button:
UpdateValueStrategy toModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_CONVERT);
if (validator != null) {
toModel.setAfterGetValidator(validator);
}
UpdateValueStrategy fromModel = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE);
binding = bindingContext.bindValue(SWTObservables.observeText(this, SWT.Modify),
BeansObservables.observeValue(pVO, propertyName), toModel, fromModel);
This piece of code works really well.
But how can I do the same on a TableViewer?
I want it to work so that when I add something in the IHM, the model stay unchanged until I call getBindingContext().updateModels();
You do not need use the JFace Databinding Framework in
TableViewer. Manipulation the structured data is simpler then SWT controls, suchTableViewer,ListViewerandTreeViewer. You can use those viewer in the same way:After the viewer created, just invoke
viewer.setInput(data)to put all the things to your viewer.There are a list of model:
The magic happens in the content provider:
Each model will become a
TableItemand the model in theTableItem(item.getData()).However, a table composed by many columns, you need the
LabelProviderto help you mapping the property of model to theTableItem:The propagation of models to viewer is easy. If you will propagate viewer to the binded model, using the
CellEditoris simple as well.To use
CellEditor, you need set the column properties, cell editors and cell modifier toTableViewer:The CellModifier likes this:
Everything is simple but there are many steps to make all together.