Created
October 23, 2012 12:33
-
-
Save jumarko/3938502 to your computer and use it in GitHub Desktop.
JFace ComboViewer databinding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected Control createDialogArea(Composite parent) { | |
... | |
createEntityCombo(); | |
... | |
initBindings(); | |
} | |
private void createEntityCombo(Composite parent) { | |
final ComboViewer entityCombo = new ComboViewer(parent, SWT.VERTICAL | SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY); | |
final GridData entityComboLayoutData = new GridData(SWT.END, SWT.CENTER, false, false); | |
entityCombo.getCombo().setLayoutData(entityComboLayoutData); | |
final ObservableListContentProvider entityComboContentProvider = new ObservableListContentProvider(); | |
entityCombo.setContentProvider(entityComboContentProvider); | |
entityCombo.setLabelProvider( | |
new ObservableMapLabelProvider(BeansObservables.observeMap(entityComboContentProvider.getKnownElements(), | |
EventStoreWriterFieldMapping.ENTITY_NAME_PROPERTY)) { | |
}); | |
entityCombo.setInput(modelObject.getEntitiesObservable()); | |
setInitialSelection(entityCombo); | |
} | |
private void setInitialSelection(ComboViewer entityCombo) { | |
if (modelObject.getSelectedEntity() != null) { | |
entityCombo.setSelection(new StructuredSelection(modelObject.getSelectedEntity())); | |
} | |
} | |
private void initBindings() { | |
final DataBindingContext bindingContext = new DataBindingContext(); | |
// selected entity binding: [entity selected in entities combo -> modelObject.selectedEntity] | |
final IObservableValue entityComboObservable = ViewersObservables.observeSingleSelection(entityCombo); | |
final IObservableValue modelSelectedEntityObservable = BeansObservables.observeValue(modelObject, | |
EventStoreWriterFieldMapping.SELECTED_ENTITY_PROPERTY); | |
bindingContext.bindValue(modelSelectedEntityObservable, entityComboObservable); | |
// selected entity binding: [modelObject.selectedEntity -> entity selected in entities combo] | |
bindingContext.bindValue(entityComboObservable, modelSelectedEntityObservable); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ModelObject { | |
static final String SELECTED_ENTITY_PROPERTY = "selectedEntity"; | |
/** | |
* Observable list of labels <DatasetItem>s, labels are also instances of this class. | |
* Unfortunately, {@link IObservableCollection} is not generic. | |
*/ | |
private final IObservableCollection entities = new WritableList(); | |
private Entity selectedEntity; | |
/** Used to allow automatic data binding */ | |
private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); | |
public ModelObject(Collection<Entity> entities) { | |
if (CollectionUtils.isNotEmpty(entities)) { | |
this.entities.addAll(entities); | |
} | |
} | |
/** | |
* @return unmodifiable collection of all known Eventstore entities, or empty collection if no such entities exist | |
*/ | |
@SuppressWarnings("unchecked") | |
public Collection<Entity> getEntities() { | |
if (CollectionUtils.isEmpty(entities)) { | |
return Collections.<Entity>emptyList(); | |
} | |
return Collections.unmodifiableCollection(entities); | |
} | |
public IObservableCollection getEntitiesObservable() { | |
return entities; | |
} | |
/** | |
* Selected entity represents entity which user selected from list of all entities. | |
* Entities fields mappings can be retrieved via {@link com.gooddata.service.model.eventstore.Entity#getAttributes()}. | |
* @return selected entity | |
*/ | |
public Entity getSelectedEntity() { | |
return selectedEntity; | |
} | |
public void setSelectedEntity(Entity selectedEntity) { | |
final Entity oldSelectedEntity = this.selectedEntity; | |
this.selectedEntity = selectedEntity; | |
propertyChangeSupport.firePropertyChange(SELECTED_ENTITY_PROPERTY, oldSelectedEntity, selectedEntity); | |
} | |
/** | |
* Add a listener to be notified about property changes | |
*/ | |
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { | |
propertyChangeSupport.addPropertyChangeListener(propertyName, listener); | |
} | |
/** | |
* Remove attached listener. | |
*/ | |
public void removePropertyChangeListener(PropertyChangeListener listener) { | |
propertyChangeSupport.removePropertyChangeListener(listener); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment