Multilevel Master/Detail Databinding with EMF and RCP
I made a model with EMF, representing the settings of a device, and a RCP GUI. In the GUI I have a list to select different devices of the type of the model (Master).
The model has a List of Objects of a small class which should be displayed in a table (Detail).
The tableItems itself need to be edited so I have a small GUI part with checkboxes etc. to change the settings. H开发者_如何转开发ere the tableitem is master and all fields shown in the GUI are details.
Observable for the List of devices:
IObservableValue selection = ViewersObservables.observeSingleSelection(availableDevicesList);
Table:
IObservableList list = EMFObservables.observeDetailList(Realm.getDefault(), selection,DevicePackage.Literals.LIST);
TableViewer tableViewer = new TableViewer(parent, SWT.SINGLE | SWT.FULL_SELECTION);
tableViewer.setInput(list);
IObservableValue tableSelection = ViewersObservables.observeSingleSelection(tableViewer);
Editing:
Spinner field1 = new Spinner(parent, SWT.BORDER);
dbc.bindValue(SWTObservables.observeSelection(field1), EMFObservables.observeDetailValue(Realm.getDefault(), tableSelection, DevicePackage.Literals.Value1));
When changing the device selection the tableitems get replaced. But the tableSelection seems to have a problem with this. Sometimes it still contains values of a a tableitem from a different device and sometimes it just contains null. I also have a button which gets enabled/disabled according the validation status of all fields. When the tableSelection puts null into these fields the validation does not work and the button is disabled until a entry in the table is selected.
I tried to manually set the selection empty with a listener on the list and:
tableViewer.setSelection(StructuredSelection.EMPTY, true);
but this does not do the full job. At least all "old" values are replaced but the null problem still occurs.
All I need is to set the tableSelection to an empty state as after the launch of the application, when no tableitem was selected yet.
I found the solution by myself. The problem was actually the spinner itself. It threw a nullpointer when the selection was empty and there was no value.
I solved it by giving it a custom converter (from int to int...) where I return a default value if the source is null. Now the validation works fine and my button's enable state is set correct.
精彩评论