Simple Design Engine API Example

Virgil Dodson
Virgil Dodson E admin
edited July 28, 2021 in Analytics #1
<p><span style=\"font-size: x-small;\"><span style=\"font-family: Verdana,Geneva,Arial,Helvetica,sans-serif;\">This is a simple example showing how to create a report design from scratch entirely from the API. This example was created for BIRT 2.1.1</span></span></p>
<div class=\"xoopsCode\" style=\"border: 1px solid;\"><span style=\"font-size: x-small;\"><span style=\"font-family: Courier New,Courier,monospace;\"><br />import java.io.IOException;<br /><br />import org.eclipse.birt.core.framework.Platform;<br />import org.eclipse.birt.report.model.api.CellHandle;<br />import org.eclipse.birt.report.model.api.DesignConfig;<br />import org.eclipse.birt.report.model.api.DesignElementHandle;<br />import org.eclipse.birt.report.model.api.ElementFactory;<br />import org.eclipse.birt.report.model.api.GridHandle;<br />import org.eclipse.birt.report.model.api.IDesignEngine;<br />import org.eclipse.birt.report.model.api.IDesignEngineFactory;<br />import org.eclipse.birt.report.model.api.ImageHandle;<br />import org.eclipse.birt.report.model.api.LabelHandle;<br />import org.eclipse.birt.report.model.api.ReportDesignHandle;<br />import org.eclipse.birt.report.model.api.RowHandle;<br />import org.eclipse.birt.report.model.api.SessionHandle;<br />import org.eclipse.birt.report.model.api.activity.SemanticException;<br /><br />import com.ibm.icu.util.ULocale;<br /><br />/**<br /> * Simple BIRT Design Engine API (DEAPI) demo.<br /> */<br /><br />public class SimpleCreate<br />{<br /><br /> public static void main( String[] args )<br /> {<br /> try<br /> {<br /> buildReport( );<br /> }<br /> catch ( IOException e )<br /> {<br /> e.printStackTrace();<br /> }<br /> catch ( SemanticException e )<br /> {<br /> e.printStackTrace();<br /> }<br /> }<br /> <br /> // This function shows how to build a very simple BIRT report with a<br /> // minimal set of content: a simple grid with an image and a label.<br /> static void buildReport( ) throws IOException, SemanticException<br /> {<br /> // Create a session handle. This is used to manage all open designs.<br /> // Your app need create the session only once.<br /> // Configure the Engine and start the Platform<br /> DesignConfig config = new DesignConfig( );<br /><br /> config.setProperty(\"BIRT_HOME\", \"C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine\");<br /> IDesignEngine engine = null;<br /> try{<br /> <br /> Platform.startup( config );<br /> IDesignEngineFactory factory = (IDesignEngineFactory) Platform<br /> .createFactoryObject( IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );<br /> engine = factory.createDesignEngine( config );<br /><br /> }catch( Exception ex){<br /> ex.printStackTrace();<br /> } <br /> <br /> SessionHandle session = engine.newSessionHandle( ULocale.ENGLISH ) ;<br /> <br /> // Create a new report design.<br /> ReportDesignHandle design = session.createDesign( );<br /> <br /> // The element factory creates instances of the various BIRT elements.<br /> ElementFactory efactory = design.getElementFactory( );<br /> <br /> // Create a simple master page that describes how the report will<br /> // appear when printed.<br /> //<br /> // Note: The report will fail to load in the BIRT designer<br /> // unless you create a master page.<br /> <br /> DesignElementHandle element = efactory.newSimpleMasterPage( \"Page Master\" );<br /> design.getMasterPages( ).add( element );<br /> <br /> // Create a grid and add it to the \"body\" slot of the report<br /> // design.<br /> GridHandle grid = efactory.newGridItem( null, 2 /* cols */, 1 /* row */ );<br /> design.getBody( ).add( grid );<br /> <br /> // Note: Set the table width to 100% to prevent the label<br /> // from appearing too narrow in the layout view.<br /> grid.setWidth( \"100%\" );<br /> <br /> // Get the first row.<br /> RowHandle row = (RowHandle) grid.getRows( ).get( 0 );<br /> <br /> // Create an image and add it to the first cell.<br /> ImageHandle image = efactory.newImage( null );<br /> CellHandle cell = (CellHandle) row.getCells( ).get( 0 );<br /> cell.getContent( ).add( image );<br /> image.setURL( \"\"urlofimage\"\" );<br /> <br /> // Create a label and add it to the second cell.<br /> LabelHandle label = efactory.newLabel( null );<br /> cell = (CellHandle) row.getCells( ).get( 1 );<br /> cell.getContent( ).add( label );<br /> label.setText( \"Hello, world!\" );<br /> <br /> // Save the design and close it.<br /> design.saveAs( \"c:/tmp/sample.rptdesign\" );<br /> design.close( );<br /> System.out.println(\"Finished\");<br /> }<br />}<br /><br /></span></span></div>
Warning No formatter is installed for the format ipb