Writing a Java event handler
Most scriptable elements have more than one event for which you can write a handler. If you write an event handler for any event of an element, the event handler class must include methods for all the events for that element. You can leave empty those methods that do not require handler code.
You can give an event handler class any name you choose. Associate the class with a report element in BIRT Report Designer in the Properties view, as explained earlier in this chapter. The Java event handler class can either extend an adapter class or implement an event handler interface. The following sections explain adapter classes and handler interfaces.
Event handler adapter classes
BIRT provides event handler adapter classes for every scriptable report element. An event handler adapter class contains empty methods for every event handler method for the element. If your class extends an adapter class, override only the methods for the events for which you want to provide handler code.
One advantage of using an adapter class instead of implementing an interface is that the class compiles even if methods are added to the interface in a future release. If the signature of an event handler method changes in a future release, however, you must change your implementation of that method to reflect the signature change. The class compiles even if you do not change the method with the changed signature, but the method with the wrong signature is never called.
Event handler interfaces
BIRT provides event handler interfaces for every scriptable report element. If your event handler class extends an adapter class, the adapter class implements the correct interface. If your class does not extend an adapter class, then your class must implement the appropriate interface for the report element.
There are some advantages of specifying an interface instead of extending an adapter class. Eclipse generates stubs for every method the interface specifies. The stubs show the method arguments, so you can see the argument types of the methods to implement. If your class extends an adapter class, there are no generated stubs for you to examine. You also have more freedom in the design of your class structure if you avoid using an adapter class.
For example, you might want two or more event handler classes to extend a single base class. Because Java does not support multiple inheritance, the event handler class cannot extend both the adapter class and the base class. If, however, the event handler class implements an interface instead of extending an adapter class, Java does not prevent the event handler class from extending the base class.
The disadvantage of using an interface over an adapter class is that if additional methods are added to an interface in a future release, a class that implements the interface fails to compile.