|
In this section, we cover the basic rules for designing your own beans. First, we want to stress there is no cosmic beans class that you extend to build your beans. Visual beans directly or indirectly extend the Component class, but nonvisual beans don't have to extend any particular superclass. Remember, a bean is simply any class that can be manipulated in a builder tool. The builder tool does not look at the superclass to determine the bean nature of a class, but it analyzes the names of its methods. To enable this analysis, the method names for beans must follow certain patterns. NOTE
Other languages for visual design environments, such as Visual Basic and C#, have special keywords such as "Property" and "Event" to express these concepts directly. The designers of the Java specification decided not to add keywords to the language to support visual programming. Therefore, they needed an alternative so that a builder tool could analyze a bean to learn its properties or events. Actually, there are two alternative mechanisms. If the bean writer uses standard naming patterns for properties and events, then the builder tool can use the reflection mechanism to understand what properties and events the bean is supposed to expose. Alternatively, the bean writer can supply a bean information class that tells the builder tool about the properties and events of the bean. We start out using the naming patterns because they are easy to use. You'll see later in this chapter how to supply a bean information class. NOTE
The naming pattern for properties is simple: Any pair of methods
corresponds to a read/write property. For example, in our ImageViewerBean, there is only one read/write property (for the file name to be viewed), with the following methods: public String getFileName() public void setFileName(String newValue) If you have a get method but not an associated set method, you define a read-only property. Conversely, a set method without an associated get method defines a write-only property. NOTE
NOTE
There is one exception to the get/set naming pattern. Properties that have Boolean values should use an is/set naming pattern, as in the following examples:
For example, an animation might have a property running, with two methods public boolean isRunning() public void setRunning(boolean b) The setRunning method would start and stop the animation. The isRunning method would report its current status. NOTE
Be careful with the capitalization pattern you use for your method names. The designers of the JavaBeans specification decided that the name of the property in our example would be fileName, with a lowercase f, even though the get and set methods contain an uppercase F (getFileName, setFileName). The bean analyzer performs a process called decapitalization to derive the property name. (That is, the first character after get or set is converted to lower case.) The rationale is that this process results in method and property names that are more natural to programmers. However, if the first two letters are uppercase (such as in getURL), then the first letter of the property is not changed to lower case. After all, a property name of uRL would look ridiculous. NOTE
For events, the naming patterns are equally simple. A bean builder environment will infer that your bean generates events when you supply methods to add and remove event listeners. All event class names must end in Event, and the classes must extend the EventObject class. Suppose your bean generates events of type EventNameEvent. The listener interface must be called EventNameListener, and the methods to add and remove a listener must be called
If you look at the code for ImageViewerBean, you'll see that it has no events to expose. However, many Swing components generate events, and they follow this pattern. For example, the AbstractButton class generates ActionEvent objects, and it has the following methods to manage ActionListener objects: public void addActionListener(ActionListener e) public void removeActionListener(ActionListener e) CAUTION
|
|