[Databinding] update widget if model changes? [message #19732] |
Thu, 09 July 2009 09:20  |
Eclipse User |
|
|
|
hello newsgroup,
i'm working on a rcp-project and want to use eclipse databinding.
i already implemented the update-function of my model if the value in the
gui-widget is changed.
how can a do the opposite way? i want to update the gui, if the model
changes.
my sample-View:
public void createPartControl(Composite parent) {
FormToolkit toolkit = new FormToolkit(parent.getDisplay());
ScrolledForm form = toolkit.createScrolledForm(parent);
form.setText("Databinding-Test");
GridLayout layout = new GridLayout();
layout.numColumns = 2;
form.getBody().setLayout(layout);
// TestModel has an attribute "testValue" with a getter and a setter
(eclipse-generated)
final TestModel testModel = new TestModel();
Text testText = toolkit.createText(form.getBody(), "");
DataBindingContext context = new DataBindingContext();
context.bindValue(SWTObservables.observeText(testText, SWT.Modify),
PojoObservables.observeValue(testModel, "testValue"), null,
null);
...}
my test-code is:
Button button = toolkit.createButton(form.getBody(), "Test it!",
SWT.PUSH);
button.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
System.out.println("Button clicked");
testModel.setTestValue("newValue");
}
});
what is my mistake?
thanks,
fabian
|
|
|
Re: [Databinding] update widget if model changes? [message #19762 is a reply to message #19732] |
Thu, 09 July 2009 11:19   |
Eclipse User |
|
|
|
You have to implement the JavaBeans specification in order for changes
to the model to propagate back to the GUI. Otherwise the binding has no
way to know when you model changes.
In practice this is very easy to introduce into your classes. Add the
following methods to your bean class, or if possible, put them in a base
class e.g. ModelObject and extend your beans from that:
private PropertyChangeChange changeSupport =
new PropertyChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(
String propertyName, PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(
PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
public void removePropertyChangeListener(
String propertyName, PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(propertyName, listener);
}
protected void firePropertyChange(
String propertyName, Object oldValue, Object newValue) {
changeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
Then inside you setter methods, fire a property change:
public void setName(String name) {
firePropertyChange("name", this.name, this.name = name);
}
Once you've done that, switch from using PojoObservables to
BeansObservables and that should do it.
Matthew
Fabian M. wrote:
> hello newsgroup,
>
> i'm working on a rcp-project and want to use eclipse databinding.
> i already implemented the update-function of my model if the value in
> the gui-widget is changed.
> how can a do the opposite way? i want to update the gui, if the model
> changes.
>
>
> my sample-View:
>
> public void createPartControl(Composite parent) {
>
> FormToolkit toolkit = new FormToolkit(parent.getDisplay());
> ScrolledForm form = toolkit.createScrolledForm(parent);
> form.setText("Databinding-Test");
>
> GridLayout layout = new GridLayout();
> layout.numColumns = 2;
> form.getBody().setLayout(layout);
>
> // TestModel has an attribute "testValue" with a getter and a setter
> (eclipse-generated)
> final TestModel testModel = new TestModel();
>
> Text testText = toolkit.createText(form.getBody(), "");
>
> DataBindingContext context = new DataBindingContext();
> context.bindValue(SWTObservables.observeText(testText, SWT.Modify),
> PojoObservables.observeValue(testModel, "testValue"), null,
> null);
> ..}
>
> my test-code is:
>
> Button button = toolkit.createButton(form.getBody(), "Test it!",
> SWT.PUSH);
> button.addSelectionListener(new SelectionListener() {
>
> public void widgetDefaultSelected(SelectionEvent e) {
>
> }
>
> public void widgetSelected(SelectionEvent e) {
> System.out.println("Button clicked");
> testModel.setTestValue("newValue");
>
> }
>
> });
>
> what is my mistake?
> thanks,
> fabian
>
|
|
|
|
Re: [Databinding] update widget if model changes? [message #20102 is a reply to message #19950] |
Sat, 11 July 2009 05:22  |
Eclipse User |
|
|
|
hello!
i did/do not know how the PojoObservables-class works. i thought it can
observe pojos - using some magic reflection-stuff ;)
since i changed to BeansObservables and implemented the interface in my
models, my problem is solved.
thank you very much!
fabian
|
|
|
Powered by
FUDForum. Page generated in 0.30630 seconds