是什么让模型更改通过 JFace 数据绑定传播?

发布于 2024-10-02 04:17:53 字数 1629 浏览 0 评论 0原文

为了了解 JFace 数据绑定是如何工作的,我有一个具有两个属性的模型对象。更改一个属性应将另一个属性设置为相同的值:

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;
    }
}

现在,在一个单独的类中,我定义了两个文本小部件,xTextyText,它们绑定到此对象,如下所示:

    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()));

我发现,如果我更改 yText 中的文本,那么 setter 会按预期自动调用,这会同时设置 yText 和 x模型。但是,xText 未更新。这是为什么呢? firePropertyChange() 调用不应该安排更新文本吗?

谢谢, 格雷厄姆.

In order to understand how JFace databindings is working, I have a model object with two properties. Changing one property should set the other to the 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 a separate class I define two Text widgets, xText and yText, which are bound to this object like 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 that if I change the text in yText, then the setter is automatically called as expected, and this sets both y and x in the model. However, xText is not updated. Why is this? Shouldn't the firePropertyChange() call arrange for the Text to be updated?

Thanks,
Graham.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情空心 2024-10-09 04:17:53

编译器正在优化 this.xthis.y 的初始值,这导致 PropertyChangeSupport 实例丢弃更改通知。它并不认为有什么改变。如果我引入一个像这样的临时变量:

public void setX(double x) {
    double oldValue = this.x;
    this.x = x;
    propertyChangeSupport.firePropertyChange("x", oldValue, x);
}

那么通知就会按照我的预期发生。

The compiler was optimising away the initial value of this.x and this.y, which led the PropertyChangeSupport instance to discard the change notification. It didn't think anything had changed. If I introduce a temporary variable like this:

public void setX(double x) {
    double oldValue = this.x;
    this.x = x;
    propertyChangeSupport.firePropertyChange("x", oldValue, x);
}

then the notifications occur as I might expect.

挽清梦 2024-10-09 04:17:53

我猜这是因为您在正确更新之前将 this.x 添加到您的活动中。

试试这个:


公共无效setX(双x){
这个.x = x; // 更新模型!
propertyChangeSupport.firePropertyChange("x", this.x, x);
}

(参见评论;-))

I guess it's because you put this.x to your event before haveing it updated properly.

Try this:


public void setX(double x) {
this.x = x; // update the model!
propertyChangeSupport.firePropertyChange("x", this.x, x);
}

(see comments ;-) )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文