Bean Internals  «Prev  Next»
Lesson 14Constrained properties
ObjectiveLearn about constrained properties.

JavaBeans Constrained Properties

JavaBean constrained properties, are properties that enable an outside party to validate a change in them before accepting the change. When you attempt to change the value of a constrained property, the value is first checked with an outside validation source that accepts or rejects the change. Property change rejections come in the form of PropertyVetoException exceptions, which are handled by the Bean containing the property. When a rejection exception occurs, the Bean is responsible for reverting the property back to its prior value and issuing a new property change notification for the reversion.

Practical Examples of Constrained Properties

A practical example where a constrained property would come in handy is the case of a property that represents a date. An application might want to validate a change in the date property to make sure it stays within a particular range, the current year for instance. If an attempt is made to set the property to a date outside of the current year, the application can reject the change.

  • Veto Constrained Property Value Change:
    An object with constrained properties allows other objects to veto a constrained property value change. Constrained property listeners can veto a change by throwing a PropertyVetoException. The JellyBean class in demo\sunw\demo\jelly\ has a constrained property called PriceInCents.
    public class JellyBean extends Canvas {
     private PropertyChangeSupport changes = new PropertyChangeSupport(this);
     private VetoableChangeSupport vetos =new VetoableChangeSupport(this);
    

     public void setPriceInCents(int newPriceInCents) throws PropertyVetoException {
      int oldPriceInCents = ourPriceInCents;
      vetos.fireVetoableChange("priceInCents",
      new Integer(oldPriceInCents),
      new Integer(newPriceInCents));
      ourPriceInCents = newPriceInCents;
      changes.firePropertyChange("priceInCents",
      new Integer(oldPriceInCents),
      new Integer(newPriceInCents));
    }
    

     public
      void addVetoableChangeListener(VetoableChangeListener l) {
      vetos.addVetoableChangeListener(l);
     }
     public
      void removeVetoableChangeListener(VetoableChangeListener l) {
      vetos.removeVetoableChangeListener(l);
     }
    


JavaBean Constrained Property

Constrained property: a property that enables an interested party to perform a validation on a new property value before accepting the modification. In the next lesson, how constrained properties work will be discussed.

A bean property is constrained if the bean supports the VetoableChangeListener(in the API reference documentation) and PropertyChangeEvent(in the API reference documentation) classes, and if the set method for this property throws a PropertyVetoException(in the API reference documentation). Constrained properties are more complicated than bound properties because they also support property change listeners which happen to be vetoers.
The following operations in the setXXX method for the constrained property must be implemented in this order:
  1. Save the old value in case the change is vetoed.
  2. Notify listeners of the new proposed value, allowing them to veto the change.
  3. If no listener vetoes the change (no exception is thrown), set the property to the new value.
The accessor methods for a constrained property are defined in the same way as those for simple properties, with the addition that the setXXX method throws a PropertyVetoException exception. The syntax is as follows:
public void setPropertyName(PropertyType pt)
throws PropertyVetoException {code}

SEMrush Software