swt/jface databinding: PojoProperties vs PojoObservable

2.4k views Asked by At

I'm writing a JFace dialog, and I'd like to use databing to a model object.

Looking at code I can see that there are times when I find a PojoProperties used to build the binding, while other time it is used a PojoObservables.

Looking at the Javadoc I can read:

PojoObservables: A factory for creating observable objects for POJOs (plain old java objects) that conform to idea of an object with getters and setters but does not provide property change events on change.

PojoProperties: A factory for creating properties for POJOs (plain old Java objects) that conform to idea of an object with getters and setters but does not provide property change events on change.

The same question applies to the difference that exists between BeansObservables and BeansProperties

The (obvious) difference sems to be that the observable allows to observe objects and the properties allows to observe properties, but since a Pojo has a getter and a setter for its data, what is the difference between them? And which of them should I choose for my dialog?

Here follows a code excerpt:

The POJO:

public class DataObject {
  private String m_value;
  public String getValue() {
    return m_value;
  }
  public void setValue(String i_value) {
    m_value = i_value;
  }
}

The DIALOG (relevant part):

@Override
protected Control createDialogArea(Composite parent) {
    Composite container = (Composite) super.createDialogArea(parent);

    m_combo = new Combo(container, SWT.BORDER);
    m_comboViewer = new ComboViewer(container, SWT.NONE);

}

The BINDING (relevant part):

    // using PojoObservable
    IObservableValue observeValue = PojoObservables.observeValue(m_dataObject, "value");
    IObservableValue observeWidget = SWTObservables.observeSelection(m_combo);

    // using PojoProperties
    IObservableValue observeValue = PojoProperties.value("value").observe(m_dataObject);
    IObservableValue observeWidget = ViewerProperties.singleSelection().observe(m_comboViewer);

I understand that one time I'm using a combo and another I'm using a ComboViewer, but I can get the combo from the viewer and bind the other way if I need...

Also, can I mix the two, for example use the observeValue with the ViewerProperties?

    IObservableValue observeValue = PojoObservables.observeValue(m_dataObject, "value");
    IObservableValue observeWidget = ViewerProperties.singleSelection().observe(m_comboViewer);
2

There are 2 answers

0
Juraj Martinka On

I am playing around a little with JFace viewers (especially ComboViewer) & databinding and discovered that if I use

SWTObservables.observeSelection(comboViewer.getCombo());

then databinding is not working correctly.

However, if I use

ViewersObservables.observeSingleSelection(comboViewer);

Then everything is working as expected.

Maybe this is a special for my case, so to get it a better overview I'll describe my set up in following paragraph.

I have modelObject with field named selectedEntity and entities and bind this ComboViewer to the modelObject.

  • I want to display all "entities" in model object, if I add any entity to the modelObject.entities collection then I want to this entity be added to combo automatically.
  • If user selects some item in combo I want to modelObject.selectedEntity be set automatically.
  • If I set modelObject.selectedEntity I want to combo selection be set automatically.

Source code can be found at: https://gist.github.com/3938502

0
Loomchild On

Since Eclipse Mars, PojoObservables is deprecated in favor of PojoProperties and BeansObservables is deprecated in favor of BeanProperties so the answer to which one should be used has now become evident.