Docsity

Customizing JavaBeans with BeanInfo: A Guide to Enhancing Bean Functionality, Lecture notes of Java Programming

Toggle sidebar
bg1
Customizing with BeanInfo
So far, everything NetBeans has known about our beans has been determined by low-level
reflection—that is, by looking at the methods of our classes. The java.Beans.Introspector class
gathers information on a bean using reflection, then analyzes and describes a bean to any tool
that wants to know about it. The introspection process works only if the class follows the
JavaBeans naming conventions for its methods; furthermore, it gives us little control over exactly
what properties and events appear in NetBeans menus. For example, we’ve seen that NetBeans
by default shows all the stuff we inherit from the base Swing component. We can change that by
creating BeanInfo classes for our beans. A BeanInfo class provides the JavaBeans introspector
with explicit information about the properties, methods, and events of a bean; we can even use it
to customize the text that appears in menus in NetBeans (and in other IDEs).
A BeanInfo class implements the BeanInfo interface. That’s a complicated proposition; in most
situations, the introspector’s default behavior is reasonable. Instead of implementing the
BeanInfo interface, we extend the SimpleBeanInfo class, which implements all of BeanInfo’s
methods. We can override specific methods to provide the information we want; when we don’t
override a method, we’ll get the introspector’s default behavior.
In the next few sections, we’ll develop the DialBeanInfo class that provides explicit information
about our Dial bean.
Getting Properties Information
We’ll start out by describing the Dial’s properties. To do so, we must implement the
getPropertyDescriptors() method. This method simply returns an array of PropertyDescriptor
objects—one for each property we want to publicize.
To create a PropertyDescriptor, call its constructor with two arguments: the property’s name and
the class. In the following code, we create descriptors for the Dial’s value, minimum, and
maximum properties. We next call a few methods of the PropertyDescriptor class to provide
additional information about each property. If our methods were bound (generated
PropertyChangeEvents when modified), we’d call the setBound() method of their
PropertyDescriptors. Our code is prepared to catch an IntrospectionException, which can occur if
something goes wrong while creating the property descriptors, such as encountering a
nonexistent method:
//file: DialBeanInfo.java
package magicbeans;
import java.beans.*;
4
public class DialBeanInfo extends SimpleBeanInfo {
4
public PropertyDescriptor[] getPropertyDescriptors() {
try {
pf3
pf4
pf5

Partial preview of the text

Download Customizing JavaBeans with BeanInfo: A Guide to Enhancing Bean Functionality and more Lecture notes Java Programming in PDF only on Docsity!

Customizing with BeanInfo So far, everything NetBeans has known about our beans has been determined by low-level reflection—that is, by looking at the methods of our classes. The java.Beans.Introspector class gathers information on a bean using reflection, then analyzes and describes a bean to any tool that wants to know about it. The introspection process works only if the class follows the JavaBeans naming conventions for its methods; furthermore, it gives us little control over exactly what properties and events appear in NetBeans menus. For example, we’ve seen that NetBeans by default shows all the stuff we inherit from the base Swing component. We can change that by creating BeanInfo classes for our beans. A BeanInfo class provides the JavaBeans introspector with explicit information about the properties, methods, and events of a bean; we can even use it to customize the text that appears in menus in NetBeans (and in other IDEs). A BeanInfo class implements the BeanInfo interface. That’s a complicated proposition; in most situations, the introspector’s default behavior is reasonable. Instead of implementing the BeanInfo interface, we extend the SimpleBeanInfo class, which implements all of BeanInfo’s methods. We can override specific methods to provide the information we want; when we don’t override a method, we’ll get the introspector’s default behavior. In the next few sections, we’ll develop the DialBeanInfo class that provides explicit information about our Dial bean. Getting Properties Information We’ll start out by describing the Dial’s properties. To do so, we must implement the getPropertyDescriptors() method. This method simply returns an array of PropertyDescriptor objects—one for each property we want to publicize. To create a PropertyDescriptor, call its constructor with two arguments: the property’s name and the class. In the following code, we create descriptors for the Dial’s value, minimum, and maximum properties. We next call a few methods of the PropertyDescriptor class to provide additional information about each property. If our methods were bound (generated PropertyChangeEvents when modified), we’d call the setBound() method of their PropertyDescriptors. Our code is prepared to catch an IntrospectionException, which can occur if something goes wrong while creating the property descriptors, such as encountering a nonexistent method: //file: DialBeanInfo.java package magicbeans; import java.beans.*; public class DialBeanInfo extends SimpleBeanInfo { public PropertyDescriptor[] getPropertyDescriptors() { try {

PropertyDescriptor value = new PropertyDescriptor("value", Dial.class); PropertyDescriptor minimum = new PropertyDescriptor("minimum", Dial.class); PropertyDescriptor maximum = new PropertyDescriptor("maximum", Dial.class); return new PropertyDescriptor [] { value, minimum, maximum }; } catch (IntrospectionException e) { return null; } } } Perhaps the most useful thing about DialBeanInfo is that by providing explicit information for our properties, we automatically hide other properties that introspection might find. After compiling DialBeanInfo and packaging it with the Dial, you’ll see that its JComponent properties no longer appear in the NetBeans properties editor. (This has been the case all along if you started with the precompiled example JAR.) A PropertyDescriptor can provide a lot of other information about a property: the names of the accessor methods (if you decide not to use the standard naming convention), information about whether the property is constrained, and a class to use as a property editor (if the standard property editors aren’t sufficient). Getting events information The Dial bean defines its own event: the DialEvent. We’d like to tell development tools about this event so that we can build applications using it. The process for telling the world about our event is similar to what we did previously: we add a method to the DialBeanInfo class called getEventSetDescriptors(), which returns an array of EventSetDescriptors. Events are described in terms of their listener interfaces, not in terms of the event classes themselves, so our getEventSetDescriptors() method creates a descriptor for the DialListener interface. Here’s the code to add to the DialBeanInfo class: public EventSetDescriptor[] getEventSetDescriptors() { try { EventSetDescriptor dial = new EventSetDescriptor( Dial.class, "dialAdjusted", DialListener.class, "dialAdjusted"); dial.setDisplayName("Dial Adjusted"); return new EventSetDescriptor [] { dial }; }

return loadImage("DialIconMono32.gif"); } return null; } This method is called with a constant, indicating what kind of icon is being requested; for example, BeanInfo.ICON_COLOR_16x16 requests a 16 × 16 color image. If an appropriate icon is available, it loads the image and returns an Image object. If the icon isn’t available, it returns null. For convenience, you can package the images in the same JAR file as the bean and its BeanInfo class. Though we haven’t used them here, you can also use a BeanInfo object to provide information about other public methods of your bean (for example, array-valued properties) and other features. Creating customizers and property editors JavaBeans lets you provide a customizer for your beans. Customizers are objects that do advanced customization for a bean as a whole; they let you provide your own GUI for tweaking your bean. We won’t show you how to write a customizer; it’s not too difficult, but it’s beyond the scope of this chapter. Suffice it to say that a customizer must implement the java.beans.Customizer interface and should extend Component (or JComponent) so that it can be displayed. Property editors are a way of giving the properties sheet additional capabilities. For example, you could supply a property editor to let you edit a property type that is specific to your bean. You could provide a property editor that would let you edit an object’s price in dollars and cents. We’ve already seen a couple of property editors: the editor used for Color-valued properties is fundamentally the same as a property editor you might write yourself. In addition, the Molecule bean uses a property editor to specify its moleculeName property. A property editor isn’t quite as fancy as a customizer, but describing it fully is also beyond the scope of this chapter. Again, describing how to write a property editor is also beyond the scope of this chapter. However, it might help you to know that a property editor must implement the PropertyEditor interface; it usually does so by extending the PropertyEditorSupport class, which provides default implementations for most of the methods. Feature Descriptors BeanInfo classes contain descriptors that precisely describe the target Bean's features. The BDK implements the following descriptor classes:  FeatureDescriptor is the base class for the other descriptor classes. It declares the aspects common to all descriptor types.

 BeanDescriptor describes the target Bean's class type and name, and describes the target Bean's customizer class if it exists.  PropertyDescriptor describes the target Bean's properties.  IndexedPropertyDescriptor is a subclass of PropertyDescriptor, and describes the target Bean's indexed properties.  EventSetDescriptor describes the events the target Bean fires.  MethodDescriptor describes the target Bean's methods.  ParameterDescriptor describes method parameters. The BeanInfo interface declares methods that return arrays of the above descriptors. Creating a BeanInfo Class This section uses the ExplicitButtonBeanInfo demo class to illustrate creating a BeanInfo class. Here are the general steps to make a BeanInfo class: Name your BeanInfo class. You must append the string "BeanInfo" to the target class name. If the target class name is ExplicitButton, then its associated Bean information class must be named ExplicitButtonBeanInfo Subclass SimpleBeanInfo. This is a convenience class that implements BeanInfo methods to return null, or an equivalent no-op value. public class ExplicitButtonBeanInfo extends SimpleBeanInfo { Using SimpleBeanInfo saves you from implementing all the BeanInfo methods; you only have to override those methods you need. Override the appropriate methods to return the properties, methods, or events that you want exposed. ExplicitButtonBeanInfo overrides the getPropertyDescriptors() method to return four properties: public PropertyDescriptor[] getPropertyDescriptors() { try { PropertyDescriptor background = new PropertyDescriptor("background", beanClass); PropertyDescriptor foreground = new PropertyDescriptor("foreground", beanClass); PropertyDescriptor font = new PropertyDescriptor("font", beanClass); PropertyDescriptor label = new PropertyDescriptor("label", beanClass); background.setBound(true); foreground.setBound(true);

return new BeanDescriptor(beanClass, customizerClass); } ... private final static Class beanClass = ExplicitButton.class; private final static Class customizerClass = OurButtonCustomizer.class; Keep the BeanInfo class in the same directory as its target class. The BeanBox first searches for a target Bean's BeanInfo class in the target Bean's package path. If no BeanInfo is found, then the Bean information package search path (maintained by the Introspector) is searched. The default Bean information search path is sun.beans.infos. If no BeanInfo class is found, then low-level reflection is used to discover a Bean's features.