Wednesday 29 March 2017

Using Bound Properties & Using the BeanInfo Interface - Java Tutorials

Using Bound Properties

A Bean that has a bound property generates an event when the property is changed. The event is of type PropertyChangeEvent and is sent to objects that previously registered an interest in receiving such notifications.

The TickTock Bean is supplied with the BDK. It generates a property change event every N seconds. N is a property of the Bean that can be changed via the Properties window of the BDK. The next example builds an application that uses the TickTock Bean to automatically control the Colors Bean. 


Steps

For this example, start the BDK and create an instance of the Colors Bean in the BeanBox window.

Create an instance of the TickTock Bean. The Properties window should show one property for this component. It is “Interval” and its initial value is 5. This represents the number of seconds that elapse between property change events generated by the TickTock Bean. Change the value to 1.

Now you need to map events generated by the TickTock Bean into method calls on the Colors Bean. Follow these steps:
  1. Go to the menu bar of the BeanBox and select Edit | Events | propertyChange | propertyChange. You should now see a line extending from the button to the cursor.
  2. Move the cursor so that it is inside the Colors Bean display area, and click the left mouse button. You should see the Event Target Dialog dialog box.
  3. The dialog box allows you to choose a method that should be invoked when this event occurs. Select the entry labeled “change” and click the OK button. You should see a message box appear very briefly, stating that the tool is “Generating and compiling adaptor class.”


You should now see the color of your component change every second.




Using the BeanInfo Interface

In our previous examples, design patterns were used to determine the information that was provided to a Bean user. This section describes how a developer can use the BeanInfo interface to explicitly control this process.

This interface defines several methods, including these:

      PropertyDescriptor[ ] getPropertyDescriptors( )
      EventSetDescriptor[ ] getEventSetDescriptors( )
      MethodDescriptor[ ] getMethodDescriptors( )

They return arrays of objects that provide information about the properties, events, and methods of a Bean. By implementing these methods, a developer can designate exactly what is presented to a user.

SimpleBeanInfo is a class that provides default implementations of the BeanInfo interface, including the three methods just shown. You may extend this class and override one or more of them. The following listing shows how this is done for the Colors Bean that was developed earlier. ColorsBeanInfo is a subclass of SimpleBeanInfo. It overrides getPropertyDescriptors( ) in order to designate which properties are presented to a Bean user. This method creates a PropertyDescriptor object for the rectangular property. The PropertyDescriptor constructor that is used is shown here:

      PropertyDescriptor(String property, Class beanCls)
            throws IntrospectionException

Here, the first argument is the name of the property, and the second argument is the class of the Bean.

  // A Bean information class.
  package sunw.demo.colors;
  import java.beans.*;
  public class ColorsBeanInfo extends SimpleBeanInfo {
    public PropertyDescriptor[] getPropertyDescriptors() {
      try {
        PropertyDescriptor rectangular = new
          PropertyDescriptor("rectangular", Colors.class);
        PropertyDescriptor pd[] = {rectangular};
        return pd;
      }
      catch(Exception e) {
      }
      return null;
    }
  }

You must compile this file from the BDK\demo directory or set CLASSPATH so that it includes c:\bdk\demo. If you don’t, the compiler won’t find the Colors.class file properly. After this file is successfully compiled, the colors.mft file can be updated, as shown here:

  Name: sunw/demo/colors/ColorsBeanInfo.class
  Name: sunw/demo/colors/Colors.class
  Java-Bean: True

Use the JAR tool to create a new colors.jar file. Restart the BDK and create an instance of the Colors Bean in the BeanBox.

The introspection facilities are designed to look for a BeanInfo class. If it exists, its behavior explicitly determines the information that is presented to a Bean user Otherwise, design patterns are used to infer this information.

You can see that the properties inherited from Component are no longer presented for the Colors Bean. Only the rectangular property appears.


Constrained Properties

A Bean that has a constrained property generates an event when an attempt is made to change its value. The event is of type PropertyChangeEvent. It is sent to objects that previously registered an interest in receiving such notifications. Those other objects have the ability to veto the proposed change. This capability allows a Bean to operate differently according to its run-time environment. A full discussion of constrained properties is beyond the scope of this book.


Persistence

Persistence is the ability to save a Bean to nonvolatile storage and retrieve it at a later time. The information that is particularly important are the configuration settings.

Let us first see how the BDK allows you to save a set of Beans that have been configured and connected together to form an application. Recall our previous example involving both the Colors and TickTock Beans. The rectangular property of the Colors Bean was changed to true, and the interval property of the TickTock Bean was changed to one second. These changes can be saved.

To save the application, go to the menu bar of the BeanBox and select File | Save. A dialog box should appear, allowing you to specify the name of a file to which the Beans and their configuration parameters should be saved. Supply a filename and click the OK button on that dialog box. Exit from the BDK.

Start the BDK again. To restore the application, go to the menu bar of the BeanBox and select File | Load. A dialog box should appear, allowing you to specify the name of the file from which an application should be restored. Supply the name of the file in which the application was saved, and click the OK button. Your application should now be functioning. Confirm that the rectangular property of the Colors Bean is true and that the interval property for the TickTock Bean is equal to one second.

The object serialization capabilities provided by the Java class libraries are used to provide persistence for Beans. If a Bean inherits directly or indirectly from java.awt.Component, it is automatically serializable, because that class implements the java.io.Serializable interface. If a Bean does not inherit an implementation of the Serializable interface, you must provide this yourself. Otherwise, containers cannot save the configuration of your component.

The transient keyword can be used to designate data members of a Bean that should not be serialized. The color variable of the Colors class is an example of such an item.


Customizers

The Properties window of the BDK allows a developer to modify the properties of a Bean. However, this may not be the best user interface for a complex component with many interrelated properties. Therefore, a Bean developer can provide a customizer that helps another developer configure this software. A customizer can provide a step-by-step guide through the process that must be followed to use the component in a specific context. Online documentation can also be provided. A Bean developer has great flexibility to develop a customizer that can differentiate his or her product in the marketplace.

No comments:

Post a Comment