Set constraint properties
The steps to set the constraint attribute of the source bean are as follows:
To achieve a mechanism, you can register or log out your interest when you accept the monitor object of the VetoableChangeListener interface. These listening objects are not notifications to accept attributes that have changed, but are accepted in the proposed attribute change notification. BE, by using inheritance or instantiation of the VETOABECHANGESUPPORT tool class, which contains the VETOABLECHANGELISTENER object to the listening list, and the method of moving the object from the listening list, but also contains a transmission attribute change The method of events, and also captures and responds to events. FireveToableChange (
AddVETOABLECHANGELISTENER (Listener);
RemoveveToableChangeListener (Listener);
Send the proposed attribute change event to the listener of interest. Since these attributes change may be veto, the source bean sends the proposed event before the attribute value changes. Source Beans Send PropertyChangeeevent by calling the listener's VETOABLECHANGE method. Providing a way for listening objects such that once the listener rejoits the proposed attribute changes, the original value can also be maintained. To accomplish this, the source bean calls VETOABLECHANGE, first pass the initial value of the attribute to the listener.
Anatical example
The Javabean named JellyBean implemented a constraint attribute, the latter called PriceNCents. When this bean is initialized, the two new object VETOABECHANGESUPPORT objects (for maintaining the vetese listener list), the PropertyChangeSupport object (to store the property change listener list) is instantiated. code show as below:
Private VetoableChangeSupport Vetos = New VetoableChangeSupport (this);
Private PropertyChangeSupport changes = new propertychangesupport (this);
After that, JellyBean implements the VETOABECHANGESUPPORT interface method for registration and logout constraint attribute change listeners:
public void addVetoableChangeListener (VetoableChangeListener l) {vetos.addVetoableChangeListener (l);} public void removeVetoableChangeListener (VetoableChangeListener l) {vetos.removeVetoableChangeListener (l);}
The method of adding and removing the listener is applicable to all constraint properties in the bean. Specifies that the listener is also possible to associate only specific constraint attributes in the bean. If so, the above two methods can include an additional string parameter containing the attribute name. The first appearance is the parameter name in the parameter table, followed by the listener object. For example, add a monitoring bean for the specified attribute.
Public void addvetoablechangelistener (String propertyName);
Referring to the following example, by specifying a specific attribute name, you can register and log out of the veto listener for each property: Void Add
JellyBean also defines a method to get current prices, and define another method to set up new prices. In setting a new price method, JellyBean calls method FireveToableChange to notify the registration attribute change listener.
Public void setPriceinCents (int newpriceinities)
THROWS PropertyvetoException {
Vetos.fireveToableChange ("PriceinCents",
New Integer (OldpriceInCents),
New Integer (NewPriceInCents));
// IF VETOED (Propertyvetoexception Thrown)
// Don't catch
// Exception Here so routine exits.
// if not vetoed, Make the Property
// change
Ourpriceincents = newpriceincents;
Changes.firepropertychange
"Price",
New Integer (OldpriceInCents),
New Integer (NewPriceInCents));
}
Once the bean performs the FirevetoableChange method, it will notify the listener of interest: PriceINCents will change and inform them the current price and new prices. If the method is successfully performed, it means there is no exception to occur, indicating that there is no listener to reject this proposed change. The SETPRICEINCENTS method then executes the FirePropertyChange method to send an attribute change event to notify the listener attribute has changed.
However, if the FireveToableChange method throws a PropertyVetoException exception, the listener has rejected the proposed changes. The proposed attribute change will not take effect. The SetPRICEINCENTS method is not captured, so that after exiting the method, it can make exceptions can be processed at a higher level level.