BeanInfo Classes


You have already seen that if you use the standard naming patterns for the methods of your bean class, then a builder tool can use reflection to determine features such as properties and events. This process makes it simple to get started with bean programming, but naming patterns are rather limiting in the end. As your beans become complex, there may be features of your bean that naming patterns will not reveal. Moreover, as we already mentioned, many beans have get/set method pairs that should not correspond to bean properties.

Luckily, the JavaBeans specification allows a far more flexible and powerful mechanism for storing information about your bean for use by a builder. You can define an object that implements the BeanInfo interface to describe your bean. When you implement this interface, builder tool will look to the methods from the BeanInfo interface to tell it about the features that your bean supports.

Even though you can use a BeanInfo class to avoid naming patterns, you need to follow a naming pattern to associate a BeanInfo object to the bean: The name of the bean info class must be formed by adding BeanInfo to the name of the bean. For example, the bean info class associated to the class ImageViewerBean must be named ImageViewerBeanBeanInfo. The bean info class must be part of the same package as the bean itself.

You won't normally write a class that implements all methods of the BeanInfo interface. Instead, you should extend the SimpleBeanInfo convenience class that has default implementations for all the methods in the BeanInfo interface.

The most common reason for supplying a BeanInfo class is to gain control of the bean properties. You construct a PropertyDescriptor for each property by supplying the name of the property and the class of the bean that contains it.

 PropertyDescriptor descriptor = new PropertyDescriptor("fileName", ImageViewerBean.class); 

Then implement the getPropertyDescriptors method of your BeanInfo class to return an array of all property descriptors.

For example, suppose ImageViewerBean wants to hide all properties that it inherits from the JLabel superclass and expose only the fileName property. The following BeanInfo class does just that:

 // bean info class for ImageViewerBean class ImageViewerBeanBeanInfo extends SimpleBeanInfo {    public PropertyDescriptor[] getPropertyDescriptors()    {       return new PropertyDescriptor[]       {          new PropertyDescriptor("fileName", ImageViewerBean.class);       };    } } 

Other methods also return EventSetDescriptor and MethodDescriptor arrays, but they are less commonly used. If one of these methods returns null (as is the case for the SimpleBeanInfo methods), then the standard naming patterns apply. However, if you override a method to return a non-null array, then you must include all properties, events, or methods in your array.

NOTE

Sometimes, you may want to write generic code that discovers properties or events of an arbitrary bean. Call the static getBeanInfo method of the Introspector class. The Introspector constructs a BeanInfo class that completely describes the bean, taking into account the information in BeanInfo companion classes.


Another useful method in the BeanInfo interface is the getIcon method that lets you give your bean a custom icon. Builder tools will display the icon in a palette. Actually, you can specify four separate icon bitmaps. The BeanInfo interface has four constants that cover the standard sizes:

 ICON_COLOR_16x16 ICON_COLOR_32x32 ICON_MONO_16x16 ICON_MONO_32x32 

Here is an example of how you might use the loadImage convenience method in the SimpleBeanInfo class to add an icon to a class:

 public Image getIcon(int iconType) {    String name = "";    if (iconType == BeanInfo.ICON_COLOR_16x16) name = "COLOR_16x16";    else if (iconType == BeanInfo.ICON_COLOR_32x32) name = "COLOR_32x32";    else if (iconType == BeanInfo.ICON_MONO_16x16) name = "MONO_16x16";    else if (iconType == BeanInfo.ICON_MONO_32x32) name = "MONO_32x32";    else return null;    return loadImage("ImageViewerBean_" + name + ".gif"); } 

This works, provided you cleverly name the image files to be

 ImageViewerBean_COLOR_16x16.gif ImageViewerBean_COLOR_32x32.gif 

and so on.


 java.beans.Introspector 1.1 

  • static BeanInfo getBeanInfo(Class<?> beanClass)

    gets the bean information of the given class.


 java.beans.BeanInfo 1.1 

  • EventSetDescriptor[] getEventSetDescriptors()

  • MethodDescriptor[] getMethodDescriptors()

  • PropertyDescriptor[] getPropertyDescriptors()

    return an array of the specified descriptor objects. A return of null signals the builder to use the naming conventions and reflection to find the member. The getPropertyDescriptors method returns a mixture of plain and indexed property descriptors. Use instanceof to check whether a specific PropertyDescriptor is an IndexedPropertyDescriptor.

  • Image getIcon(int iconType)

    returns an image object that can represent the bean in toolboxes, tool bars, and the like. There are four constants, as described earlier, for the standard types of icons.

  • int getDefaultEventIndex()

  • int getDefaultPropertyIndex()

    A bean can have a default event or property. Both of these methods return the array index that specifies which element of the descriptor array to use as that default member, or -1 if no default exists. A bean builder environment can visually enhance the default feature, for example, by placing it first in a list of features or by displaying its name in boldface.

  • BeanInfo[] getAdditionalBeanInfo()

    returns an array of BeanInfo objects or null. Use this method when you want some information about your bean to come from BeanInfo classes for other beans. For example, you might use this method if your bean aggregated lots of other beans. The current BeanInfo class rules in case of conflict.


 java.beans.SimpleBeanInfo 1.1 

  • Image loadImage(String resourceName)

    returns an image object file associated to the resource. Currently only GIFs are supported.

    Parameters:

    resourceName

    A path name (taken relative to the directory containing the current class)



 java.beans.FeatureDescriptor 1.1 

  • String getName()

  • void setName(String name)

    get or set the programmatic name for the feature.

  • String getDisplayName()

  • void setDisplayName(String displayName)

    get or set a display name for the feature. The default value is the value returned by getName. However, currently there is no explicit support for supplying feature names in multiple locales.

  • String getShortDescription()

  • void setShortDescription(String text)

    get or set a string that a builder tool can use to provide a short description for this feature. The default value is the return value of geTDisplayName.

  • Object getValue(String attributeName)

  • void setValue(String attributeName, Object value)

    get or set a named value that is associated with this feature.

  • Enumeration attributeNames()

    returns an enumeration object that contains names of any attributes registered with setValue.

  • boolean isExpert()

  • void setExpert(boolean b)

    get or set an expert flag that a builder can use to determine whether to hide the feature from a naive user. (Not every builder is likely to support this feature.)

  • boolean isHidden()

  • void setHidden(boolean b)

    get or set a flag that a builder tool should hide this feature.


 java.beans.PropertyDescriptor 1.1 

  • PropertyDescriptor(String propertyName, Class<?> beanClass)

  • PropertyDescriptor(String propertyName, Class<?> beanClass, String getMethod, String setMethod)

    construct a PropertyDescriptor object. The methods throw an IntrospectionException if an error occurred during introspection. The first constructor assumes that you follow the standard convention for the names of the get and set methods.

  • Class<?> getPropertyType()

    returns a Class object for the property type.

  • Method getReadMethod()

    returns the get method.

  • Method getWriteMethod()

    returns the set method.

  • boolean isBound()

  • void setBound(boolean b)

    get or set a flag that determines whether this property is a bound property.

  • boolean isConstrained()

  • void setConstrained(boolean b)

    get or set a flag that determines whether this property is a constrained property.


 java.beans.IndexedPropertyDescriptor 1.1 

  • IndexedPropertyDescriptor(String propertyName, Class<?> beanClass)

  • IndexedPropertyDescriptor(String propertyName, Class<?> beanClass, String getMethod, String setMethod, String indexedGetMethod, String indexedSetMethod)

    construct an IndexedPropertyDescriptor for the index property. The methods throw an IntrospectionException if an error occurred during introspection. The first constructor assumes that you follow the standard convention for the names of the get and set methods.

  • Class<?> getIndexedPropertyType()

    returns the class that describes the type of the indexed values of the property, that is, the return type of the indexed get method.

  • Method getIndexedReadMethod()

    returns the indexed get method.

  • Method getIndexedWriteMethod()

    returns the indexed set method.


 java.beans.EventSetDescriptor 1.1 

  • EventSetDescriptor(Class<?> sourceClass, String eventSetName, Class<?> listener, String listenerMethod)

    constructs an EventSetDescriptor. This constructor assumes that you follow the standard pattern for the names of the event class and the names of the methods to add and remove event listeners. Throws an IntrospectionException if an error occurred during introspection.

  • EventSetDescriptor(Class<?> sourceClass, String eventSetName, Class<?> listener, String[] listenerMethods, String addListenerMethod, String removeListenerMethod)

    constructs an EventSetDescriptor with multiple listener methods and custom methods for adding and removing listeners. Throws an IntrospectionException if an error occurred during introspection.

  • Method getAddListenerMethod()

    returns the method used to register the listener.

  • Method getRemoveListenerMethod()

    returns the method used to remove a registered listener for the event.

  • Method[] getListenerMethods()

  • MethodDescriptor[] getListenerMethodDescriptors()

    return an array of Method or MethodDescriptor objects for the methods triggered in the listener interface.

  • Class<?> getListenerType()

    returns the type of the listener interface associated with the event.

  • boolean isUnicast()

  • void setUnicast(boolean b)

    get or set a flag that is TRue if this event can be propagated to only one listener.



    Core JavaT 2 Volume II - Advanced Features
    Building an On Demand Computing Environment with IBM: How to Optimize Your Current Infrastructure for Today and Tomorrow (MaxFacts Guidebook series)
    ISBN: 193164411X
    EAN: 2147483647
    Year: 2003
    Pages: 156
    Authors: Jim Hoskins

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net