Sunday, 21 September 2014

Bean Properties

Bean Properties can be categorized as follows....
  • Simple Property:  Simple properties are basic, independent, individual properties like width, height, and color.
  • Indexed Property: It is a property that can take on an array of values.
  • Bound Property:  It is a property that alerts other objects when its value changes.
  • Constrained Property: It differs from bound property in that it notifies other objects of an impending change. Constrained properties give the notified objects the power to veto a property change.

Accessor Methods

·                                Simple Property:
o   If, a bean has a property named foo of type fooType that can be read and written, it should have the following accessor methods:
Public fooType getFoo()
{
                        Return foo;
}
Public void setFoo(fooType fooValue)
{
            Foo=fooValue; …..
}

If a property is boolean, getter methods are written using is instead of get eg. isFoo().

·                                Indexed Property:
o   Public widgetType getWidget(int index)
o   Public widgetType[] getWidget()
o   Public void setWidget(int index, widgetType widgetValue)
o   Public void setWidget(widgetType[] widgetValues)

·                                  Bound Property:

Getter and setter methods for bound property are as described above based on whether it is simple or indexed. Bounded properties require certain objects to be notified when they change. The change notification is accomplished through the generation of a PropertyChangeEvent(defined in java.beans). objects that want to be notified of a property change to a bound property must register as listeners. Accordingly, the bean that’s implementing the bound property supplies methods of the form:
Public void addPropertyChangeListener(propertyChangeListener l)
Public void removePropertyChangeListener(propertyChangeListener l)

The preceding listener registration methods do not identify specific bound properties. To register listeners for the PropertyChangeEvent of a specific property, the following methods must be provided:

public void addPropertyNameListener(PropertyChangeListener l)
public void removePropertyNameListener(PropertyChangeListener l)

In the preceding methods, PropertyName is replaced by the name of the bound property.
Objects that implement the PropertyChangeListener interface must implement the PropertyChange() method. This method is invoked by the bean for all registered listeners to inform them of a property change.

Constrained Property:

The previously discussed methods used with simple and indexed properties also apply to the constrained properties. In addition, the following event registration methods provided: 

public void addVetoableChangeListener(VetoableChangeListener l)
public void removeVetoableChangeListener(VetoableChangeListener l)
public void addPropertyNameListener(VetoableChangeListener l)
public void removePropertyNameListener(VetoableChangeListener l)

Objects that implement the VetoableChangeListener interface must implement the vetoableChange(). This method is invoked by the bean for all of its registered listener to inform them of a property change. Any object that does not approve of a property change can throw a PropertyVetoException within its vetoableChange() to inform the bean whose constrained property was changed that the change was not approved.