Listing 1: Opening a report design for editing

// Create a design engine configuration object.
DesignConfig dConfig = new DesignConfig( );
DesignEngine dEngine = new DesignEngine( dConfig );
// Create a session handle, using the system locale.
SessionHandle session = dEngine.newSessionHandle( null );
// Create a handle for an existing report design.
String name = "./SimpleReport.rptdesign";
ReportDesignHandle design = null;
try {
     design = session.openDesign( name );
} catch (Exception e) {
     System.err.println
          ( "Report " + name + " not opened!\nReason is " + 
               e.toString( ) );
     return null;
}

Listing 2: Finding a report item with a given name

DesignElementHandle logoImage = design.findElement( "Company Logo" );
// Check for the existence of the report item.
if ( logoImage == null) { 
     return null; 
}
// Check that the report item has the expected class.
if ( !( logoImage instanceof ImageHandle ) ) { 
     return null; 
}
// Retrieve the URI of the image.
String imageURI = ( (ImageHandle ) logoImage ).getURI( );
return imageURI;

Listing 3: Navigating the report structure to access a report item

// Instantiate a slot handle and iterator for the body slot.
SlotHandle shBody = design.getBody( );
Iterator slotIterator = shBody.iterator( )
// To retrieve top-level report items, iterate over the body.
while (slotIterator.hasNext( )) {
 Object shContents = slotIterator.next( );
     // To get the contents of the top-level report items,
     // instantiate slot handles.
     if (shContents instanceof GridHandle)      {
          GridHandle grid = ( GridHandle ) shContents;
          SlotHandle grRows = grid.getRows( );
          Iterator rowIterator = grRows.iterator( );
          while (rowIterator.hasNext()) {
               // Get RowHandle objects.
               Object rowSlotContents = rowIterator.next( );
               // To find the image element, iterate over the grid.
               SlotHandle cellSlot = 
                    ( ( RowHandle ) rowSlotContents ).getCells( );
               Iterator cellIterator = cellSlot.iterator( );
               while ( cellIterator.hasNext( ) ) {
                    // Get a CellHandle object.
                    Object cellSlotContents = cellIterator.next( ); 
                    SlotHandle cellContentSlot = 
                         ((CellHandle) cellSlotContents).getContent( );
                    Iterator cellContentIterator = 
                         cellContentSlot.iterator( );
                    while (cellContentIterator.hasNext( )) {
                         // Get a DesignElementHandle object.
                         Object cellContents = 
                              cellContentIterator.next( );
                         // Check that the element is an image.
                         if (cellContents instanceof ImageHandle) {
                              String imageSource = ( ( ImageHandle ) 
                                   cellContents ).getSource( ); 
                              // Check that the image has a URI.
                              if ((imageSource.equals( 
                                   IMAGE_REF_TYPE_URL )) || 
                                        (imageSource.equals
                                             (IMAGE_REF_TYPE_FILE))){
                                   // Retrieve the URI of the image.
                                   String imageURI = ( ( ImageHandle ) 
                                        cellContents ).getURI( );
                              }
                         }
                    }
               }
          }
     }
}

Listing 4: Changing the text property of a label report item

// Access the label by name.
LabelHandle headerLabel = ( LabelHandle ) design.findElement( "Header Label" );
try {
     headerLabel.setText( "Updated " + headerLabel.getText( ) );
} catch ( Exception e ) {
     // Handle the exception
}

Listing 5: Changing a complex property of a report item

// Access the label by name.
LabelHandle headerLabel = ( LabelHandle ) design.findElement( "Header Label" );
try {
     // To prepare to change a style property, get a StyleHandle.
     StyleHandle labelStyle = headerLabel.getPrivateStyle();
     // Update the background color.
     ColorHandle bgColor = labelStyle.getBackgroundColor();
     bgColor.setRGB( 0xFF8888 );
} catch ( Exception e ) {
     // Handle any exception
}

Listing 6:  Adding a container item to the Body slot

// Instantiate an element factory.
ElementFactory factory = design.getElementFactory( );
try {
     // Create a grid element with 2 columns and 1 row.
     GridHandle grid = factory.newGridItem( "New grid", 2, 1 );
     // Set a simple property on the grid, the width.
     grid.setWidth( "50%" );
     // Create a new label and set its properties.
     LabelHandle label = factory.newLabel( "Hello Label" );
     label.setText( "Hello, world!" );
     // Get the first row of the grid.
     RowHandle row = ( RowHandle ) grid.getRows( ).get( 0 );
     // Add the label to the second cell in the row.
     CellHandle cell = ( CellHandle ) row.getCells( ).get( 1 );
     cell.getContent( ).add( label );
     // Get the Body slot. Add the grid to the end of the slot.
     design.getBody( ).add( grid );
} catch ( Exception e ) {
     // Handle any exception
}

Listing 7: Modifying a data set

// Find the data set by name.
DataSetHandle ds = design.findDataSet( "Customers" );
// Find the data source by name.
DataSourceHandle dso = design.findDataSource( "EuropeSales" );
// Check for the existence of the data set and data source.
if (dso == null) || ( ds == null )
{
     System.err.println( "EuropeSales or Customers not found" );
     return;
}
// Change the data source of the data set.
try 
{
     ds.setDataSource( dso );
} catch ( SemanticException e1 ) {
     e1.printStackTrace( );
}

Listing 8: Binding a data set to a report item

// Find the table by name.
TableHandle table = ( TableHandle ) design.findElement( "Report Data" );
// Find the data set by name.
DataSetHandle ds = design.findDataSet( "EuropeanCustomers" );
// Check for the existence of the table and the data set.
if (table == null) || ( ds == null ) {
     System.err.println( "Incorrect report structure" );
     return;
}
// Change the data set for the table.
try {
     table.setDataSet( ds );
} catch (Exception e) {
     System.err.println( "Could not set data set for table" );
}