According to JavaSoft,
"A Java Bean is a reusable software component that can be manipulated visually in a builder tool." |
Furthermore, two or more Beans should be able to be installed in such a toolbox and caused to communicate with one another without the requirement to recompile either of them.
Depending on the capability of the VBT, it may be possible for the programmer to combine two or more Beans into a working application or applet without the requirement to write any new code (although the VBT will usually write new code on behalf of the programmer).
For example, the BeanBox that we will discuss later allows Beans to be wired together strictly using mouse actions on the Beans themselves and on menus.
Once a new application is created by combining individual Beans, it should be possible to save the new application or applet for later use. This includes saving the current state of all the Beans incorporated into the application.
If you are familiar with Microsoft's Visual Basic or Borland's Delphi, then you are familiar with reusable components similar to Java Beans. The VBX files and OCX files used in these two builder tools serve a purpose similar to that served by Java Beans in Java builder tools.
public void setDelay(int delayIn){myDelay = delayIn;}
public int getDelay(){return myDelay;} |
In this case, according to Beans design patterns, the name of the property is delay and the name of the instance variable used to maintain the property is myDelay. In this case, the methodology used to establish that this is a property is based on design patterns (which will be explained in more detail later).
If the set method exists without a corresponding get method, then the property is a write-only property. Similarly, if the get method exists without a corresponding set method, then the property is a read-only property. (This is probably the more common of these two special cases.)
It is also possible to forego design patterns and provide explicit information regarding various aspects of the interface.
Note that the above methods are declared public. Common jargon has it that this property has been exposed to the builder tool.
Another design pattern used to identify properties has to do with boolean
properties. If the underlying instance variable type is boolean,
an accessor method of the form
public boolean is<PropertyName>() |
There are four kinds of properties:
Exposure of the methods means that the facilities of the VBT can link events generated by other Beans to those methods. Stated differently, this means that those methods can be invoked by the code generated by the VBT as a result of some event possibly involving that Bean or another Bean.
In the general case, if necessary to provide thread-safe operation, methods in a Bean should be synchronized to prevent them from being invoked from two or more threads at the same time.
By default, a Bean can generate any type of event supported by its parent class.
In addition, a Bean can expose the fact that it can generate events
in a multicast sense by providing a pair of public methods similar
to the following (this pair of methods represents another design pattern):
public synchronized void addMouseListener(MouseListener
e){...}
public synchronized void removeMouseListener(MouseListener ml) {...} |
In keeping with the terminology of the JDK 1.1 Delegation Event Model, the Bean that maintains the list is the Source Bean and the objects added to the list (registered) are the Listener objects.
In this case, a VBT might have the ability to cause other Bean objects to be added to the list of registered Listener objects so that they would be automatically notified whenever an event of the specified type occurs on the Source object.
The Introspector class contains methods that can be used to analyze the target Bean's class and superclasses looking either for explicit or implicit information. The information discovered is used to build and return an object of type BeanInfo that describes the target Bean.
As mentioned earlier, the programmer
The methods of the Introspector class use low-level reflection techniques in the analysis of the Bean. Low-level reflection techniques were studied in an earlier lesson.
The primary method of the Introspector class used to analyze a Bean is the getBeanInfo() method. Simply put, this method takes a target Class object as a parameter and returns a BeanInfo object containing information about the target class. The BeanInfo class contains a number of methods that can be used to extract the different elements of information from the BeanInfo object.
There are two versions of the getBeanInfo() method. The method which accepts only one parameter returns information about the target class and all its superclasses.
Another version accepts a second Class object as a parameter and uses that class as a ceiling for introspection up the inheritance hierarchy. For example, if this second class is the direct superclass of the primary target class, only information about the primary target class is returned.
Sometimes it is also desirable for the bean to provide its own property editor to enable a VBT to modify properties whose type is otherwise unknown to the VBT.
Also, in some cases, the Bean may simply be too complex for the default tools in the VBT to handle. In those cases, the Bean can provide a customizer.
The BeanBox is not intended to be a Visual Builder Tool. However, it does resemble VBTs in that it presents a toolbox, a property inspector, and a main form for assembling an application. It also provides menus which allow you to hook Beans together and also to allow you to apply introspection and report on a Bean.
In order to place a new Bean in the toolbox for the BeanBox, you need to create a Java Archive Tool (JAR) file and store that file in a specific directory. Subsequent lessons will contain discussions regarding the use of the BeanBox for testing Beans as well as procedures for creating JAR files and installing them in the toolbox.
-end-