Reputation: 381
A background thread shall update a simple text input field with some value.
I created the POJO with getter/setter in advance, called the JFACE databinding wizard to generate a shell and let it generate the code.
I'm entirely relying on the generated code, just added an inline thread that sets the value data by calling the setter.
While it is working perfectly from the UI to the data POJO, the other way round, no way.
I tried POJOs as well as beans applying PropertyChangeSupport and firePropertyChange(), no way.
Can please somebody shed a light on this or point to some web ressources ? (Sure I googled and applied search here ...)
(For simplicity I've changed some elements to 'static' in this sample as well ommitted some proper thread handling.)
Best regards Gerd
Code:
public class Gui extends Shell {
private DataBindingContext m_bindingContext;
private static com.gsi.MyDataClass myDataClass = new com.gsi.MyDataClass();
private Text myStringText;
/**
* Launch the application.
* @param args
*/
public static void main(String args[]) {
Display display = new Display();
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
try {
Display display = Display.getDefault();
Gui shell = new Gui(display, SWT.SHELL_TRIM);
shell.open();
shell.layout();
// here is the thread -----------------------------------------
new Thread () {
public void run () {
while (true) {
try { Thread.sleep(1000); } catch (InterruptedException e) { }
myDataClass.setMyString("Date1:" + new Date().toString());
}
}
} .start();
// the rest is generated code ------------------------------------------------------------
while (!shell.isDisposed()) { if (!display.readAndDispatch()) {
display.sleep(); }
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the shell.
* @param display
* @param style
*/
public Gui(Display display, int style) {
super(display, style);
createContents();
}
/**
* Create contents of the window.
*/
protected void createContents() {
setText("SWT Application");
setSize(242, 99);
setLayout(new GridLayout(2, false));
new Label(this, SWT.NONE).setText("MyString:");
myStringText = new Text(this, SWT.BORDER | SWT.SINGLE);
myStringText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
false));
if (myDataClass != null) {
m_bindingContext = initDataBindings();
}
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
public com.gsi.MyDataClass getMyDataClass() {
return myDataClass;
}
public void setMyDataClass(com.gsi.MyDataClass newMyDataClass) {
setMyDataClass(newMyDataClass, true);
}
public void setMyDataClass(com.gsi.MyDataClass newMyDataClass,
boolean update) {
myDataClass = newMyDataClass;
if (update) {
if (m_bindingContext != null) {
m_bindingContext.dispose();
m_bindingContext = null;
}
if (myDataClass != null) {
m_bindingContext = initDataBindings();
}
}
}
protected DataBindingContext initDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
//
IObservableValue myStringObserveWidget = SWTObservables.observeText(myStringText, SWT.Modify);
IObservableValue myStringObserveValue = PojoObservables.observeValue(myDataClass, "myString");
bindingContext.bindValue(myStringObserveWidget, myStringObserveValue, null, null);
// bindingContext.bindValue(myStringObserveWidget, myStringObserveValue,
// new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE), new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE));
//
return bindingContext;
}
}
Upvotes: 1
Views: 1019
Reputation: 81
Use
BeanProperties.value(myDataClass, "myString");
Instead of
PojoObservables.observeValue(myDataClass, "myString");
And your bean setter method must call fireProperyChage(....)
Upvotes: 0
Reputation: 305
PojoObservable is a simple wrap around POJO to use them in databindings. They have no way to check if one of their property change, so there isn't any way they can change the UI in response to change in their internal state.
Upvotes: 1