java - What makes a model change get propagated via JFace databinding? -
in order understand how jface databindings working, have model object 2 properties. changing 1 property should set other same value:
public class model { private double x; private double y; private propertychangesupport changesupport = new propertychangesupport(this); public void addpropertychangelistener(string propertyname, propertychangelistener listener) { propertychangesupport.addpropertychangelistener(propertyname, listener); } public void removepropertychangelistener(propertychangelistener listener) { propertychangesupport.removepropertychangelistener(listener); } public void setx(double x) { propertychangesupport.firepropertychange("x", this.x, this.x = x); } public double getx() { return x; } public void sety(double y) { propertychangesupport.firepropertychange("y", y, this.y = y); setx(y); } public double y() { return y; } }
now in separate class define 2 text widgets, xtext
, ytext
, bound object this:
databindingcontext bindingcontext = new databindingcontext(); bindingcontext.bindvalue(widgetproperties.text(swt.modify).observe(xtext), beanproperties.value(humidityscanparameters.class,"x").observe(getmodel())); bindingcontext.bindvalue(widgetproperties.text(swt.modify).observe(ytext), beanproperties.value(humidityscanparameters.class, "y").observe(getmodel()));
i have found if change text in ytext
, setter automatically called expected, , sets both y
, x
in model. however, xtext
not updated. why this? shouldn't firepropertychange()
call arrange text updated?
thanks, graham.
the compiler optimising away initial value of this.x
, this.y
, led propertychangesupport
instance discard change notification. didn't think had changed. if introduce temporary variable this:
public void setx(double x) { double oldvalue = this.x; this.x = x; propertychangesupport.firepropertychange("x", oldvalue, x); }
then notifications occur might expect.
Comments
Post a Comment