|[DataBinding] Avoid setting a feature of previously selected element when viewer selection changes [message #1734465]
||Wed, 08 June 2016 14:33
| Stephan Hildebrandt
Registered: July 2015
I have the following Xtend code, which sets up a Jface editor:
* The editor consists of a list viewer, which shows all elements in the model, and a combo viewer,
* which shows the current value of the feature "dummyEnum" of the model element selected in the list
* viewer. The items of the combo viewer are retrieved from the item property descriptor's getChoiceOfValue()
//This is the feature, whose value is selected in the combo viewer. Its type is an enumeration.
val feature = EPackage.Registry.INSTANCE.getEPackage("http://dummydsl/1.0").eAllContents.filter(EAttribute).
name == "dummyEnum"
val listViewer = new ListViewer(composite)
GridDataFactory.fillDefaults.align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(listViewer.control)
listViewer.contentProvider = new ObservableListContentProvider
* AllModelElementsObservableList returns all model elements in the editing domain.
listViewer.input = new AllModelElementsObservableList(editingDomain)
* create combo viewer
val comboViewer = new ComboViewer(composite)
GridDataFactory.fillDefaults.align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(comboViewer.control)
comboViewer.contentProvider = new ObservableListContentProvider
* FeatureChoiceOfValuesObservableList retrieves the item property descriptor for the "dummyEnum" feature of the currently selected model element
* in the list viewer, and returns the result of itemPropertyDescriptor.getChoiceOfValues().
comboViewer.input = new FeatureChoiceOfValuesObservableList(
* Bind the selection of the combo viewer to the value of the "dummyEnum" feature of the model element selected in the list viewer.
Now, the problem is that only model elements of type Dummy have the "dummyEnum" feature, all other elements do not. If the list viewer's selection changes from a Dummy element to another model element, a selection change event is fired, which causes FeatureChoiceOfValuesObservableList to retrieve the item property descriptor for the selected element. However, now there is no item property descriptor because the selected element does not have the dummyEnum feature. Therefore, FeatureChoiceOfValues returns an empty list and fires a list change event. Then, the combo viewer's list of selectable items is cleared and the combo viewer selection becomes "null". This causes another change event to be fired, which is processed by the value binding (last seven lines above), setting the value of the dummyEnum feature of the PREVIOUSLY selected element to null.
What is causing this behavior? I do not fully understand what is happening the data binding layer. How can keep the databinding framework from accidentally modifying the model?
Powered by FUDForum
. Page generated in 0.02305 seconds