Programming using the BIRT Reporting APIs : Programming the structure of a report design : Using a report item in a report design : Understanding property structure objects
 
Understanding property structure objects
Complex property structures represent many optional report element features within BIRT. For example, computed columns, highlight rules, sort keys, and table of contents entries are all optional complex properties of report elements. The classes for these properties all derive directly or indirectly from org.eclipse.birt.report.model.core.Structure. An application can access existing structure property objects on a report element or create new ones.
Using an existing property structure object
Use the property handle, which is an object of class PropertyHandle, to apply a new structure object to a report element’s property or to modify an existing property structure. To access the property handle call the method, DesignElementHandle.getPropertyHandle( ), which is available on all report elements. This method takes a String argument that defines the property to access. Class fields having names of the form XXX_PROP define the available property structures for a report element class. The property handle object provides access to one or more property structure objects. Use the PropertyHandle getter methods to access settings on existing property structures.
Typically, the property structure class provides setter methods only for a few key values. Set other values by calling the property structure object’s setProperty( ) method. This method takes two arguments: a String that identifies the property and an object that specifies the value. Class fields defined on the property structure class or on the DesignChoiceConstants class provide the values for the property identifier.
How to set values on an existing property structure object
The code sample in Listing 12‑21 shows how to change the sort direction value on a table item’s first sort key.
Listing 12‑21 Changing the sort direction on a table
void modSortKey( TableHandle th ) {
try {
SortKeyHandle sk;
PropertyHandle ph =
th.getPropertyHandle( TableHandle.SORT_PROP );
if ( ph.isSet( ) ) {
sk = ( SortKeyHandle ) ph.get( 0 );
sk.setDirection
( DesignChoiceConstants.SORT_DIRECTION_DESC );
}
} catch ( Exception e ) {
e.printStackTrace( );
}
}
Creating a property structure object
The design engine class, StructureFactory, provides static methods for creating property structure objects. Most StructureFactory methods take the form createXXX, where XXX is the structure to create. After creating a new structure object, set its simple or complex properties using its setter methods. Report element handles provide methods to add key structure properties to their property handles. For example, add a filter condition to a data set object by using the addFilter( ) method. To add other properties to a report element, use the method, PropertyHandle.addItem( ).
How to create a complex property object
The code sample in Listing 12‑22 shows how to create a highlight rule object and apply that rule to a row handle. The variable, rh, is a RowHandle object. Because RowHandle does not have a specific method to add a highlight rule property, the code uses the PropertyHandle.addItem( ) method.
Listing 12‑22 Adding a highlight rule to a table or grid row
try {
HighlightRule hr = StructureFactory.createHighlightRule( );
hr.setOperator( DesignChoiceConstants.MAP_OPERATOR_GT );
hr.setTestExpression( "row[\"CustomerCreditLimit\"]" );
hr.setValue1( "100000" );
hr.setProperty
( HighlightRule.BACKGROUND_COLOR_MEMBER, "blue" );
 
PropertyHandle ph =
rh.getPropertyHandle( StyleHandle.HIGHLIGHT_RULES_PROP );
ph.addItem( hr );
} catch ( Exception e  ){ e.printStackTrace(); }
The code samples in Listing 12‑23 provide further examples of using the structure factory to create complex properties.
Listing 12‑23 Various structure factory examples
// Use the structure factory to add a sort key to a table.
void addSortKey( TableHandle th ) {
try {
SortKey sk = StructureFactory.createSortKey( );
sk.setKey( "row[\"CustomerName\"]" );
sk.setDirection
( DesignChoiceConstants.SORT_DIRECTION_ASC );
 
PropertyHandle ph =
th.getPropertyHandle( TableHandle.SORT_PROP );
ph.addItem( sk );
} catch ( Exception e ) { e.printStackTrace( ); }
}
 
// Add a column binding to a table.
void addColumnBinding( TableHandle th ) {
try {
ComputedColumn cs1;
cs1 = StructureFactory.createComputedColumn( );
cs1.setName( "CustomerName" );
cs1.setExpression( "dataSetRow[\"CUSTOMERNAME\"]" );
th.addColumnBinding( cs1, false );
} catch ( Exception e ) { e.printStackTrace( ); }
}
 
// Add a filter condition to a table.
void addFilterCondition( TableHandle th ) {
try {
FilterCondition fc = StructureFactory.createFilterCond();
fc.setExpr( "row[\"CustomerCountry\"]" );
fc.setOperator(DesignChoiceConstants.MAP_OPERATOR_EQ);
fc.setValue1("'USA'");
PropertyHandle ph =
th.getPropertyHandle( TableHandle.FILTER_PROP );
ph.addItem( fc );
} catch ( Exception e ) { e.printStackTrace( ); }
}
 
// Add a filter condition to a data set.
void addFilterCondition( OdaDataSetHandle dh ) {
try {
FilterCondition fc = StructureFactory.createFilterCond();
fc.setExpr( "row[\"COUNTRY\"]" );
fc.setOperator( DesignChoiceConstants.MAP_OPERATOR_EQ );
fc.setValue1( "'USA'" );
// Add the filter to the data set.
dh.addFilter( fc );
} catch ( Exception e ) { e.printStackTrace( ); }
}
 
// Add a hyperlink to a label.
void addHyperlink( LabelHandle lh ) {
try {
Action ac = StructureFactory.createAction( );
ActionHandle actionHandle = lh.setAction( ac );
actionHandle.setURI( "'http://www.google.com'" );
actionHandle.setLinkType
( DesignChoiceConstants.ACTION_LINK_TYPE_HYPERLINK );
} catch ( Exception e ) { e.printStackTrace( ); }
}
 
// Add a table of contents entry to a data item.
void addToc( DataItemHandle dh ) {
try {
TOC myToc = StructureFactory.createTOC
( "row[\"CustomerName\"]" );
dh.addTOC( myToc );
} catch ( Exception e ) { e.printStackTrace( ); }
}
 
// Add an embedded image to the report design.
void addImage( ) {
try {
EmbeddedImage image =
StructureFactory.createEmbeddedImage( );
image.setType
( DesignChoiceConstants.IMAGE_TYPE_IMAGE_JPEG );
image.setData( load( "logo3.jpg" ));
image.setName( "mylogo" );
designHandle.addImage( image );
} catch ( Exception e ) { e.printStackTrace( ); }
}
 
// Load the embedded image from a file on disk.
public byte[ ] load( String fileName ) throws IOException
{
InputStream is = null;
is = new BufferedInputStream
( this.getClass( ).getResourceAsStream( fileName ));
byte data[ ] = null;
if ( is != null ) {
try {
data = new byte[is.available( )];
is.read( data );
}
catch ( IOException e1 ) {
throw e1;
}
}
return data;
}