Introspection: Creating and Using

   

Introspection: Creating and Using BeanInfo Classes

You've already seen in the preceding sections and in the two Beans you designed how to use design patterns to facilitate automatic introspection. You also saw that the automatic introspection mechanism isn't perfect. If you look back at Figure 29.2, you'll see an example of this. Introspection is probably the most important aspect of JavaBeans because without it a container can't do anything with a Bean other than display it. As you become proficient at designing your own Beans, you'll find that you sometimes need to provide additional introspection information for the users of your Beans. In the case of your Beans, this is to hide the parent class's properties to clear up ambiguities .

The java.beans.Introspector class, as discussed earlier in the chapter, does all the pattern analysis to expose the properties, methods , and events that a component has. As a first step, however, this class looks to see whether a BeanInfo class is defined for the Bean it's inspecting. If it finds one, it doesn't do any pattern analysis on the areas of the Bean for which the BeanInfo class supplies information. This means that you can selectively choose which information you want to provide and which information you want to be derived from analysis. To show how this is done, you'll design a BeanInfo class for our TextDisplayer Bean.

The first thing you need to do is define what information you'll provide and what you'll leave up to the Introspector class to analyze. For the sake of example, say that you'll choose to provide the properties of your Bean, and you'll let the Introspector class use analysis to expose the events and methods. Table 29.5 shows the names of the TextDisplayer Bean's properties and the user -friendly names you want to display. With that information defined, you can start working on your BeanInfo class, TextDisplayerBeanInfo.class. Notice how you appended " BeanInfo " to the class name . That's an introspection design pattern; the Introspector class looks for BeanInfo information by appending " BeanInfo " to the class name of the Bean it's currently analyzing.

Table 29.5. The TextDisplayer Bean's Properties and User-Friendly Names
Property Name User-Friendly Name
OutputText "Text String"
BGColor "Background Color"
TextFont "Text Font"
FontColor "Text Color"

All BeanInfo classes must implement the java.beans.BeanInfo interface. At first glance, that seems difficult; there are eight methods in the java.beans.BeanInfo interface! But remember the Introspector class has a set procedure for the way it looks for information. For the sake of clarity, that procedure is shown in the following list:

  1. The Introspector class looks for a BeanInfo class for the Bean it's analyzing.

  2. If a BeanInfo class is present, each method in the BeanInfo class is called to find out whether it can provide any information. The Introspector class will use implicit analysis to expose information for which the BeanInfo class denies any knowledge (returns a null value). If no BeanInfo class is found, the Introspector class will use implicit analysis for all the methods in the java.beans.BeanInfo interface.

  3. The Introspector class then checks to see whether it has obtained explicit information for each of the methods in the BeanInfo interface. If it hasn't, it steps into the parent class (if one exists) and starts the process over for only those methods that it had to use analysis on.

  4. When the Introspector class has information from a BeanInfo class for all the methods in the java.beans.BeanInfo interface, or when there are no more parent classes to explore, the Introspector class returns its results.

To make your life easier as a programmer, Sun has provided a prebuilt class, java.beans.SimpleBeanInfo, which returns a null value for all the BeanInfo methods. That way, you can inherit from that class and only override the methods you choose. Listing 29.7 shows the BeanInfo class for the TextDisplayer Bean. Notice how you only override the getPropertyDescriptors() method. The parent class returns null for all the other methods in the java.beans.BeanInfo interface.

Listing 29.7 TextDisplayerBeanInfo.java ” The Entire BeanInfo Class for the TextDisplayer Bean Showing How to Provide Property Information
 import java.beans.*; public class TextDisplayerBeanInfo extends SimpleBeanInfo {    // override the getPropertyDescriptors method to provide that info.    public PropertyDescriptor[] getPropertyDescriptors() {       PropertyDescriptor[] properties = new PropertyDescriptor[4];       try {          properties[0] = new PropertyDescriptor( "Text String",             BeanClass, "getOutputText", "setOutputText" );          properties[1] = new PropertyDescriptor( "Text Color",             BeanClass, "getFontColor", "setFontColor" );          properties[2] = new PropertyDescriptor( "Text Font",             BeanClass, "getTextFont", "setTextFont" );          properties[3] = new PropertyDescriptor( "Background Color",             BeanClass, "getBGColor", "setBGColor" );       }  catch( IntrospectionException e ) {          return( null ); // exit gracefully if you get an exception.       }       return( properties );    }    private Class BeanClass = TextDisplayer.class; } 

Take a second to look at the try - catch clause in Listing 29.7. Notice how you return a null value if you catch a java.beans.IntrospectionException . If you catch this exception, it usually means that you've provided an incorrect getter or setter method name. You should always return a null value if you catch this exception so that the Introspector class can still analyze your Bean. You should be able to extend this example to override the other methods in the java.beans.BeanInfo interface. Figure 29.3 shows the PropertySheet window of Sun's BeanBox for your TextDisplayer Bean. Notice how the user-friendly names for the properties have been used, and the parent class's properties are gone. Sweet success!

Figure 29.3. The PropertySheet window of Sun's BeanBox shows the user-friendly names for the properties in the TextDisplayer Bean.

graphics/29fig03.gif

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

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