Components, Ports, and Events

Chapter 7 • Software Components 253 • Methods • Other properties class name, isPrimitive, etc. The reader may wonder why anyone would want to write a program that does this; why not look up the needed information when the program is written? Why wait until run time? The answer is that this capability allows the other applications to discover the way to interact with a bean that was developed by a third party. Reflection is used to gain information about components, but the component developer is allowed to specify more information to help with characterizing the component.

7.2.1 Property Access

Properties define the bean’s appearance and behavior characteristics. For properties that need to be exposed, the developer must provide: • Setter method void setPropertyNameType newvalue write- only property, e.g., password, or • Getter method Type getPropertyName read-only property, or • Both setter and getter read-write property. In addition to naming the accessor methods according to these conventions, the developer may also provide property editors for certain properties. For example, to a property may determine the bean’s background color. The user may enter the value for this property as a hexadecimal number “1b8fc0,” but it is difficult or impossible for the user to visualize how this color 15 looks. Instead, the developer may supply a graphical color chooser for the property editor, which is much more convenient for the user.

7.2.2 Event Firing

The delegation-based event model was introduced with the JavaBeans framework [Sun- JavaBeans]. In this model, there is no central dispatcher of events; every component that generates events dispatches its own events as they happen. The model is a derivative of the Publisher-Subscriber pattern. Events are identified by their class instead of their ID and are either propagated or delegated from an “event source” to an “event listener.” According to the delegation model, whenever an event for which an object declared itself as a source gets generated, the event is multicast to all the registered event listeners. The source object delegates of “fires” the events to the set of listeners by invoking a method on the listeners and passing the corresponding event object. Only objects interested in a particular event need to deal with the event and no super-event handler is required. This is also a better way of passing events to distributed objects. EventSource — Any object can declare itself as a source of certain types of events. An event source has to either follow standard beans design patterns when giving names to the methods or 15 In case you are looking at a black-and-white print, the background color around the text is magenta. Ivan Marsic • Rutgers University 254 use the BeanInfo class to declare itself as a source of certain events. When the source wishes to delegate a specific event type, it must first define a set of methods that enable listeners to register with the source. These methods take the form of setEventTypeListener for unicast andor addEventTypeListener for multicast delegation. [The source must also provide the methods for the listeners de-register.] EventListener — An object can register itself as a listener of a specific type of events originating from an event source. A listener object should implement the EventTypeListener interface for that event type, which inherits from the generic java.util.EventListener interface. The “Listener” interface is typically defined only by few methods, which makes it easy to implement the interface.

7.2.3 Custom Methods

In addition to the information a builder tool can discover from the bean’s class definition through reflection, the bean developer can provide it with explicit additional information. A bean can be customized for a specific application either programmatically, through Java code, or visually, through GUI interfaces hosted by application builder tools. In the former case, the developer specifies additional information by providing a BeanInfo object. In the latter case, the developer can provide customized dialog boxes and editing tools with sophisticated controls. These customization tools are called property editors and customizers, and they are packaged as part of the bean, by providing the PropertyEditor and Customizer classes. The BeanInfo class should be named as BeanNameBeanInfo, for example, for the Foo bean the BeanInfo class should be named FooBeanInfo. class FooBeanInfo extends SimpleBeanInfo { : : with methods: getBeanDescriptor has class and customizer getIcon for displaying the bean in the palette getMethodDescriptors for providing more information than getPropertyDescriptors can be gained through reflection alone A property descriptor can provide a PropertyEditor, in case the developer does not want to use the standard property editor for that property type or there is not one available. In addition, the developer can provide a Customizer in the bean descriptor. Customizers are used to customize the entire bean, not just a property and they are not limited to customizing properties. There is no “design pattern” for Customizers. You must use the BeanInfo to use a customizer and you need to use the BeanDescriptor constructor to specify the customizer class. More information about bean customization is available in [Error Reference source not found.].