Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » [Databinding] update widget if model changes?
[Databinding] update widget if model changes? [message #19732] Thu, 09 July 2009 13:20 Go to next message
Fabian M. is currently offline Fabian M.Friend
Messages: 4
Registered: July 2009
Junior Member
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 15:19 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
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 #19950 is a reply to message #19762] Fri, 10 July 2009 05:10 Go to previous messageGo to next message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1098
Registered: July 2009
Senior Member

An example can be found here:
http://www.vogella.de/articles/EclipseDataBinding/article.ht ml
Re: [Databinding] update widget if model changes? [message #20102 is a reply to message #19950] Sat, 11 July 2009 09:22 Go to previous message
Fabian M. is currently offline Fabian M.Friend
Messages: 4
Registered: July 2009
Junior Member
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
Previous Topic:[Databinding]
Next Topic:[Databinding] No validation after Wizard/Dialog shows up the first time
Goto Forum:
  


Current Time: Fri Apr 26 14:44:25 GMT 2024

Powered by FUDForum. Page generated in 0.03403 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top