PropertyChangeSupport 触发事件未到达

发布于 2024-12-21 05:14:01 字数 3378 浏览 1 评论 0原文

首先,我有一个名为 rit (ride) 的模型类,它扩展了 AbstractModel。 我还有一个 AbstractController。

该模型应该触发抽象控制器应该处理的属性更改。该事件已正确触发,但控制器未捕获该事件。

我有以下代码:

public abstract class AbstractModel {

/**
 * Convenience class that allow others to observe changes to the model properties
 */
protected PropertyChangeSupport propertyChangeSupport;

/**
 * Default constructor. Instantiates the PropertyChangeSupport class.
 */
public AbstractModel() {
    propertyChangeSupport = new PropertyChangeSupport(this);
}

/**
 * Adds a property change listener to the observer list.
 * @param l The property change listener
 */
public void addPropertyChangeListener(PropertyChangeListener l) {
    propertyChangeSupport.addPropertyChangeListener(l);
}
/**
 * Fires an event to all registered listeners informing them that a property in
 * this model has changed.
 * @param propertyName The name of the property
 * @param oldValue The previous value of the property before the change
 * @param newValue The new property value after the change
 */
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
    PropertyChangeListener[] listener = propertyChangeSupport.getPropertyChangeListeners();


    propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
}

.

public class RitAanvraagController extends AbstractController {

//  Properties this controller expects to be stored in one or more registered models

public static final String RITAANVRAAG_WACHTRIJ_PROPERTY = "Ritaanvraag";

/**
 * Voeg een ritaanvraag toe aan het model
 * @param ritAanvraag de nieuwe rit aanvraag.
 */
public void nieuwRitaanvraag(RitAanvraagModel ritAanvraag) {
    alterModelProperty("add", RITAANVRAAG_WACHTRIJ_PROPERTY, ritAanvraag);
}
}

package com.itopia.railcabserver.controller;

public abstract class AbstractController implements PropertyChangeListener {

//  Vectors that hold a list of the registered models and views for this controller.
private ArrayList<AbstractViewPanel> registeredViews;
private ArrayList<AbstractModel> registeredModels;

/** Creates a new instance of Controller */
public AbstractController() {
    registeredViews = new ArrayList<AbstractViewPanel>();
    registeredModels = new ArrayList<AbstractModel>();
}

/**
 * Binds a model to this controller. Once added, the controller will listen for all 
 * model property changes and propogate them on to registered views. In addition,
 * it is also responsible for resetting the model properties when a view changes
 * state.
 * @param model The model to be added
 */
public void addModel(AbstractModel model) {
    registeredModels.add(model);
    model.addPropertyChangeListener(this);
}


public void propertyChange(PropertyChangeEvent evt) {

    for (AbstractViewPanel view : registeredViews) {
        view.modelPropertyChange(evt);
    }
}
}

propertyChange 方法永远不会在抽象控制器中,也永远不会被触发。

但是当我触发这个:propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);在抽象模型中,属性更改永远不会在抽象控制器中被触发。不是当我将某些内容打印到控制台时,也不是当我设置断点时。 当我调试并在方法 propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); 处设置断点时 PropertyChangeListener[] 侦听器确实包含 ritAanvraagController。所以它应该被激发给它,然后由抽象控制器处理。

我希望有人看到我的错误并可以帮助我。

亲切的问候, 里克特

So to start off I have a model class called rit (ride) which extends the AbstractModel.
I also have an AbstractController.

The model is supposed to fire a propertychange which the abstractcontroller should handle. The event is fired correctly however it is not caught by the controller.

I have the following code:

public abstract class AbstractModel {

/**
 * Convenience class that allow others to observe changes to the model properties
 */
protected PropertyChangeSupport propertyChangeSupport;

/**
 * Default constructor. Instantiates the PropertyChangeSupport class.
 */
public AbstractModel() {
    propertyChangeSupport = new PropertyChangeSupport(this);
}

/**
 * Adds a property change listener to the observer list.
 * @param l The property change listener
 */
public void addPropertyChangeListener(PropertyChangeListener l) {
    propertyChangeSupport.addPropertyChangeListener(l);
}
/**
 * Fires an event to all registered listeners informing them that a property in
 * this model has changed.
 * @param propertyName The name of the property
 * @param oldValue The previous value of the property before the change
 * @param newValue The new property value after the change
 */
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
    PropertyChangeListener[] listener = propertyChangeSupport.getPropertyChangeListeners();


    propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
}

.

public class RitAanvraagController extends AbstractController {

//  Properties this controller expects to be stored in one or more registered models

public static final String RITAANVRAAG_WACHTRIJ_PROPERTY = "Ritaanvraag";

/**
 * Voeg een ritaanvraag toe aan het model
 * @param ritAanvraag de nieuwe rit aanvraag.
 */
public void nieuwRitaanvraag(RitAanvraagModel ritAanvraag) {
    alterModelProperty("add", RITAANVRAAG_WACHTRIJ_PROPERTY, ritAanvraag);
}
}

.

package com.itopia.railcabserver.controller;

public abstract class AbstractController implements PropertyChangeListener {

//  Vectors that hold a list of the registered models and views for this controller.
private ArrayList<AbstractViewPanel> registeredViews;
private ArrayList<AbstractModel> registeredModels;

/** Creates a new instance of Controller */
public AbstractController() {
    registeredViews = new ArrayList<AbstractViewPanel>();
    registeredModels = new ArrayList<AbstractModel>();
}

/**
 * Binds a model to this controller. Once added, the controller will listen for all 
 * model property changes and propogate them on to registered views. In addition,
 * it is also responsible for resetting the model properties when a view changes
 * state.
 * @param model The model to be added
 */
public void addModel(AbstractModel model) {
    registeredModels.add(model);
    model.addPropertyChangeListener(this);
}


public void propertyChange(PropertyChangeEvent evt) {

    for (AbstractViewPanel view : registeredViews) {
        view.modelPropertyChange(evt);
    }
}
}

The method propertyChange is never in the abstractcontroller, is never fired.

but when i fire this: propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); in the abstractmodel the propertychange never gets fired in the abstractcontroller. not when i print something to the console nor when i set a breakpoint.
When i debug and set a breakpoint at the method propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
the PropertyChangeListener[] listener does contain the ritAanvraagController. So it should be fired to it, then being handled by the abstractcontroller.

I hope anyone sees my mistake and can help me out.

Kind Regards,
Rikkert

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文