Java event handler examples
The following lists provide some common examples that illustrate event handlers written in Java. The examples in Using JavaScript to write an event handler can be used as reference as well.
Report‑level events
Report‑level events include initialize, beforeFactory, afterFactory, beforeRender, and afterRender. When these events are called depends on the type of report execution occurring.
The beforeFactory( ) method is often overridden, because this event handler can make changes to a report design. Listing 38‑1 checks a Boolean parameter. If this value is true, the report design drops a table named Mytable.
Listing 38‑1 Using the Report Engine API in beforeFactory
package my.event.handlers;
 
import org.eclipse.birt.report.engine.api.script.IReportContext;
import org.eclipse.birt.report.engine.api.script.element
.IReportDesign;
import org.eclipse.birt.report.engine.api.script.eventadapter
.ReportEventAdapter;
import org.eclipse.birt.report.model.api.*;
import org.eclipse.birt.report.model.api.activity
.SemanticException;
 
 
public class MyReportEvents extends ReportEventAdapter {
@Override
public void beforeFactory(IReportDesign report,
IReportContext reportContext) {
if((Boolean)reportContext.getParameterValue( "DropTable" )){
ReportDesignHandle rdh = ( ReportDesignHandle )
reportContext.getReportRunnable( ).getDesignHandle( );
try{
rdh.findElement( "Mytable" ).drop( );
}catch( SemanticException e ){
e.printStackTrace( );
}
}
}
This example, using the Design Engine API, requires adding modelapi.jar and coreapi.jar to the buildpath and classpath. This example also uses the Design Engine API to add a data source, data set, and table to a report using the beforeFactory event.
In Listing 38‑2, the library mylibrary.rptlibrary, located in the resource folder, opens. The data source named mydatasource, the data set named mydataset, and the table named mytable are all added to the current report design.
Listing 38‑2 Using the Design Engine API in beforeFactory
package my.event.handlers;
 
import org.eclipse.birt.report.engine.api.script.IReportContext;
import org.eclipse.birt.report.engine.api.script.element
.IReportDesign;
import org.eclipse.birt.report.engine.api.script.eventadapter
.ReportEventAdapter;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.LibraryHandle;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.core.DesignSession;
 
public class MyReportAddTableEvent extends ReportEventAdapter {
@Override
public void beforeFactory( IReportDesign report,
IReportContext reportContext ) {
ReportDesignHandle rdh = ( ReportDesignHandle )reportContext
.getReportRunnable( ).getDesignHandle( );
DesignSession ds =rdh.getModule( ).getSession( );
try{
String rsf = ds.getResourceFolder( );
LibraryHandle libhan = ds.openLibrary(
rsf + "/mylibrary.rptlibrary" ).handle( );
DesignElementHandle deh1 =
libhan.findDataSource( "mydatasource" );
DesignElementHandle deh2 =
libhan.findDataSet( "mydataset" );
DesignElementHandle deh3 =
libhan.findElement( "mytable" );
rdh.getDataSources( ).add( deh1 );
rdh.getDataSets( ).add( deh2 );
rdh.getBody( ).add( deh3 );
libhan.close( );
}catch(Exception e){
e.printStackTrace( );
}
}
}
Report item events
Report item events support changing the default behavior of an item. Changes made in the onPrepare event can change the design of the item, changes made in the onCreate event can change the particular instance of an item at generation time, and the onRender event can change properties of an instance of the item at render time. Consider the image item example in Listing 38‑3.
This example illustrates changing image sources for different types of image items. If the image type is a URL image, the output format is checked and the URL for the image changes. If the image type is a file from the resource folder, the filename is searched for the string, up. If this string is found the image is replaced with an image with the name, down. If the image type is an embedded image, the report parameter, SwapImage, is checked. If the value is true, the image is swapped to another embedded image. If the image is a BLOB type image from a database, the bytes for the image are swapped to the bytes read from the local file system.
Listing 38‑3 Changing report item properties in onRender
package my.event.handlers;
 
import org.eclipse.birt.report.engine.api.script.IReportContext;
import org.eclipse.birt.report.engine.api.script.eventadapter
.ImageEventAdapter;
import org.eclipse.birt.report.engine.api.script.instance
.IImageInstance;
import org.eclipse.birt.report.engine.content.IImageContent;
import java.io.*;
 
public class MyImageHandler extends ImageEventAdapter {
public void onRender( IImageInstance image,
IReportContext reportContext ) {
if( image.getImageSource( ) == IImageContent.IMAGE_URL ){
if( reportContext.getOutputFormat( )
.equalsIgnoreCase( "html" )){
image.setURL(
"http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif" );
} else {
image.setURL(
"http://www.google.com/intl/en_ALL/images/logo.gif" );
}
}
 
if( image.getImageSource( ) == IImageContent.IMAGE_FILE ){
String rpl = image.getFile( );
if( rpl.contains( "up" ) ){
String newstr = rpl.replaceAll( "up", "down" );
image.setFile( newstr );
}
}
 
if( image.getImageSource( ) == IImageContent.IMAGE_NAME ){
if( ( Boolean )reportContext
.getParameterValue( "SwapImage" ) ){
if( image.getImageName( ).compareToIgnoreCase(
"tocico.png" ) == 0 ){
image.setImageName( "clientprintico.png" );
}
}
}
 
if( image.getImageSource( ) ==
IImageContent.IMAGE_EXPRESSION){
try{
File myfile = new File( "c:/temp/test.png" );
FileInputStream ist = new FileInputStream( myfile );
long lengthi = myfile.length( );
byte[ ] imageData = new byte[ ( int )lengthi ];
ist.read( imageData );
ist.close( );
image.setData( imageData );
}catch( Exception e ){
e.printStackTrace( );
}
}
}
As stated earlier, onPrepare event handlers can affect the design of a particular report item. In the example shown in Listing 38‑4, an onPrepare event handler sets a hyperlink and a table of contents entry to a data element design. The onCreate event is overridden to modify the hyperlink based on the value of the data item instance.
Note that the example in Listing 38‑4 does not check for null values, so the data element must have a hyperlink added at design time. The onPrepare event replaces any existing hyperlink.
Listing 38‑4 Changing report item properties in onCreate and onPrepare
package my.event.handlers;
 
import org.eclipse.birt.report.engine.api.script.IReportContext;
import org.eclipse.birt.report.engine.api.script.element.IAction;
import org.eclipse.birt.report.engine.api.script.element
.IDataItem;
import org.eclipse.birt.report.engine.api.script.eventadapter
.DataItemEventAdapter;
import org.eclipse.birt.report.engine.api.script.instance
.IActionInstance;
import org.eclipse.birt.report.engine.api.script.instance
.IDataItemInstance;
import org.eclipse.birt.report.model.api.elements
.DesignChoiceConstants;
 
public class MyDataElementEvent extends DataItemEventAdapter {
@Override
public void onCreate( IDataItemInstance data,
IReportContext reportContext ) {
IActionInstance ai =data.getAction( );
if( ( Integer )data.getValue( ) == 10101 ){
ai.setHyperlink( "http://www.yahoo.com","_blank" );
}
}
 
@Override
public void onPrepare(IDataItem dataItemHandle,
IReportContext reportContext) {
IAction act =dataItemHandle.getAction( );
try{
act.setTargetWindow( "_blank" );
act.setURI( "'http://www.google.com'" );
act.setLinkType(
DesignChoiceConstants.ACTION_LINK_TYPE_HYPERLINK );
dataItemHandle.setTocExpression("row[\"ORDERNUMBER\"]");
}catch( Exception e ){
e.printStackTrace( );
}
}
}
Using an onCreate event handler for a row provides access to the bound columns. For example, the code in Listing 38‑5 retrieves the QUANTITYORDERED column for each row of data in a table element. If the value is greater than 40, the background for the row is set to green.
Listing 38‑5 Accessing bound data values in onCreate
package my.event.handlers;
 
import org.eclipse.birt.report.engine.api.script.IReportContext;
import org.eclipse.birt.report.engine.api.script.eventadapter
.RowEventAdapter;
import org.eclipse.birt.report.engine.api.script.instance
.IRowInstance;
 
public class MyRowEvents extends RowEventAdapter {
@Override
public void onCreate( IRowInstance rowInstance,
IReportContext reportContext ) {
try{
Integer qty =
( Integer )rowInstance.getRowData( ).getColumnValue(
"QUANTITYORDERED" );
if( qty > 40 ){
rowInstance.getStyle( ).setBackgroundColor( "green" );
}
}catch( Exception e ){
e.printStackTrace( );
}
}
}