Using the BeanInfo Interface
Using the BeanInfo Interface
Design patterns were used to determine the information that was provided to a Bean user. This section describes how a developer can use the BeanInfo interface to explicitly control this process.
This interface defines several methods, including these:
PropertyDescriptor[ ] getPropertyDescriptors( )
EventSetDescriptor[ ] getEventSetDescriptors( )
MethodDescriptor[ ] getMethodDescriptors( )
They return arrays of objects that provide information about the properties, events, and methods of a Bean. By implementing these methods, a developer can designate exactly what is presented to a user. SimpleBeanInfo is a class that provides default implementations of the BeanInfo interface, including the three methods just shown. You may extend this class and override one or more of them. The following listing shows how this is done for the Colors Bean that was developed earlier. ColorsBeanInfo is a subclass of SimpleBeanInfo.
It overrides getPropertyDescriptors( ) in order to designate which properties are presented to a Bean user. This method creates a PropertyDescriptor object for the rectangular property. The PropertyDescriptor constructor that is used is shown here:
PropertyDescriptor(String property, Class beanCls) throws IntrospectionException
Here, the first argument is the name of the property, and the second argument is the class of the Bean.
// A Bean information class.
package sunw.demo.colors;
import java.beans.*;
public class ColorsBeanInfo extends SimpleBeanInfo
{
public PropertyDescriptor[] getPropertyDescriptors()
{
try
{
PropertyDescriptor rectangular = new
PropertyDescriptor("rectangular", Colors.class);
PropertyDescriptor pd[] = {rectangular};
return pd;
}
catch(Exception e)
{
}
return null;
}
}
You must compile this file from the BDK\demo directory or set CLASSPATH so that it includes c:\bdk\demo. If you don’t, the compiler won’t find the Colors.class file properly. After this file is successfully compiled, the colors.mft file can be updated, as shown here:
Name: sunw/demo/colors/ColorsBeanInfo.class
Name: sunw/demo/colors/Colors.class
Java-Bean: True
Use the JAR tool to create a new colors.jar file. Restart the BDK and create an instance of the Colors Bean in the BeanBox.
The introspection facilities are designed to look for a BeanInfo class. If it exists, its behavior explicitly determines the information that is presented to a Bean user. Otherwise, design patterns are used to infer this information.
Labels: Advanced Java ( Unit 2 )
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home