The mechanism by which a constrained property is connected with a validation source is very similar to the listener registration mechanism for bound properties.
A Bean supporting constrained properties must implement two event listener registration methods, addVetoableChangeListener() and removeVetoableChangeListener(). These methods are used to register listeners with the Bean containing properties they want to validate, or constrain.
Following are the definitions for these methods:
public void addVetoableChangeListener (VetoableChangeListener l);
public void removeVetoableChangeListener (VetoableChangeListener l);
Similarly to the registration of listeners for bound properties, when an interested party wants to register itself as validating a property, it must call
addVetoableChangeListener() and pass in an object implementing the
VetoableChangeListener interface.
The
VetoableChangeListener interface supports the
vetoableChange()method, which is used to validate the constrained property. Constrained property listeners can be removed by calling the
removeVetoableChangeListener() method. Both of these registration methods are implemented in the
VetoableChangeSupportclass, which is a utility class used in Beans that support constrained properties.
Note that constrained properties are similar to bound properties in that they support listener registration on a per-property basis.
To this point we have assumed that all the property changes that take place are acceptable. Often this will not be the case; it is possible that an attempt to set a property to a given value will be unacceptable and therefore rejected. The simple case is when the object that owns the property wants to reject the change, but there may be times when another object wants a chance to voice its disapproval.
Properties that go through this approval process are known as constrained properties.
The design pattern for setting and getting constrained properties is similar to the design pattern for
properties that are not constrained. The difference is that the set method declares that it throws the
exception java.beans.PropertyVetoException. The method signatures look as follows:
public <PropertyType> get<PropertyName>();
public void set <PropertyName>(<PropertyType> value)
throws java.beans.PropertyVetoException;
In the next lesson, I will explain how to create a JavaBean property.