// A vetoable property change event is fired when a constrained property is changed.
// A listener can veto the change by throwing PropertyVetoException and preventing the change.
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
public class Main {
public static void main(String[] argv) throws Exception {
MyBean bean = new MyBean();
bean.addVetoableChangeListener(new MyVetoableChangeListener());
}
}
class MyVetoableChangeListener implements VetoableChangeListener {
public void vetoableChange(PropertyChangeEvent evt)
throws PropertyVetoException {
Object oldValue = evt.getOldValue();
Object newValue = evt.getNewValue();
boolean veto = false;
if (veto) {
throw new PropertyVetoException("the reason for the veto", evt);
}
}
}
class MyBean {
VetoableChangeSupport vceListeners = new VetoableChangeSupport(this);
int myProperty;
public int getMyProperty() {
return myProperty;
}
public void setMyProperty(int newValue) throws PropertyVetoException {
try {
vceListeners.fireVetoableChange("myProperty", new Integer(myProperty),
new Integer(newValue));
myProperty = newValue;
} catch (PropertyVetoException e) {
throw e;
}
}
public synchronized void addVetoableChangeListener(
VetoableChangeListener listener) {
vceListeners.addVetoableChangeListener(listener);
}
public synchronized void removeVetoableChangeListener(
VetoableChangeListener listener) {
vceListeners.removeVetoableChangeListener(listener);
}
}
|