PropertyChangeSupport主要用于监听属性变更。
- 在类里增加监听器
private final PropertyChangeSupport listener = new PropertyChangeSupport(this);
- 补充监听方法
public void addPropertyChangeListener(PropertyChangeListener listener){
this.listener.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener){
this.listener.removePropertyChangeListener(listener);
}
- 在属性set时补充监听
public void setDeployStatusType(TunnelStatusType deployStatusType) {
TunnelStatusType oldValue = this.deployStatusType;
this.deployStatusType = deployStatusType;
this.listener.firePropertyChange("deployStatusType", oldValue, this.deployStatusType);
}
- 在具体的类里补充监听实现
srPolicyInfo.addPropertyChangeListener(evt -> {
if (!"deployStatusType".equals(evt.getPropertyName())) {
return;
}
if (evt.getOldValue() instanceof TunnelStatusType && evt.getNewValue() instanceof TunnelStatusType) {
TunnelStatusType oldValue = (TunnelStatusType) evt.getOldValue();
TunnelStatusType newValue = (TunnelStatusType) evt.getNewValue();
if (TunnelStatusType.DELETE.equals(oldValue) && TunnelStatusType.NORMAL.equals(newValue)) {
removePath(Lists.newArrayList(removeReq));
}
}
});
标签:listener,学习,PropertyChangeSupport,TunnelStatusType,oldValue,evt,deployStatusType
From: https://www.cnblogs.com/liu-im/p/17676541.html