Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Java - Simple Design Engine API (BIRT)"

(Comments)
 
(4 intermediate revisions by 2 users not shown)
Line 35: Line 35:
 
   * Simple BIRT Design Engine API (DEAPI) demo.
 
   * Simple BIRT Design Engine API (DEAPI) demo.
 
   */
 
   */
 
 
  public class SimpleCreate
 
  public class SimpleCreate
 
  {
 
  {
+
    public static void main( String[] args )
  public static void main( String[] args )
+
    {
{
+
        try
try
+
        {
{
+
            buildReport( );
buildReport( );
+
        }
}
+
        catch( IOException e )
catch ( IOException e )
+
        {
{
+
            e.printStackTrace();
// TODO Auto-generated catch block
+
        }
e.printStackTrace();
+
        catch( SemanticException e )
}
+
        {
catch ( SemanticException e )
+
            e.printStackTrace();
{
+
        }
// TODO Auto-generated catch block
+
    }
e.printStackTrace();
+
}
+
}
+
 
 
 
 
// This function shows how to build a very simple BIRT report with a
+
    // This method shows how to build a very simple BIRT report with a
// minimal set of content: a simple grid with an image and a label.
+
    // minimal set of content: a simple grid with an image and a label.
 
  
 
  
static void buildReport( ) throws IOException, SemanticException
+
    static void buildReport() throws IOException, SemanticException
{
+
    {
// Create a session handle. This is used to manage all open designs.
+
        // Create a session handle. This is used to manage all open designs.
// Your app need create the session only once.
+
        // Your app need create the session only once.
 
  
 
  
  //Configure the Engine and start the Platform  
+
        //Configure the Engine and start the Platform  
DesignConfig config = new DesignConfig( );
+
        DesignConfig config = new DesignConfig( );
 
   
 
   
config.setProperty("BIRT_HOME", "C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine");
+
        config.setProperty("BIRT_HOME", "C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine");
IDesignEngine engine = null;
+
        IDesignEngine engine = null;
try{
+
 
+
        try
+
        {
Platform.startup( config );
+
            Platform.startup( config );
IDesignEngineFactory factory = (IDesignEngineFactory) Platform
+
            IDesignEngineFactory factory = (IDesignEngineFactory) Platform.createFactoryObject( IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
.createFactoryObject( IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
+
            engine = factory.createDesignEngine( config );
engine = factory.createDesignEngine( config );
+
        }
+
        catch( Exception ex )
}catch( Exception ex){
+
        {
ex.printStackTrace();
+
            ex.printStackTrace();
}
+
        }
 
 
 
 
 +
        SessionHandle session = engine.newSessionHandle( ULocale.ENGLISH ) ;
 
 
 
 
SessionHandle session = engine.newSessionHandle( ULocale.ENGLISH ) ;
+
        // Create a new report design.
 +
        ReportDesignHandle design = session.createDesign( );
 
 
 
 
// Create a new report design.
+
        // The element factory creates instances of the various BIRT elements.
 +
        ElementFactory efactory = design.getElementFactory( );
 
 
 
 
ReportDesignHandle design = session.createDesign( );
+
        // Create a simple master page that describes how the report will appear when printed.
 +
        //
 +
        // Note: The report will fail to load in the BIRT designer unless you create a master page.
 +
        DesignElementHandle element = efactory.newSimpleMasterPage( "Page Master" );
 +
        design.getMasterPages( ).add( element );
 
 
 
 
// The element factory creates instances of the various BIRT elements.
+
        // Create a grid and add it to the "body" slot of the report design.
 +
        GridHandle grid = efactory.newGridItem( null, 2 /* cols */, 1 /* row */ );
 +
design.getBody( ).add( grid );
 
 
 
 
ElementFactory efactory = design.getElementFactory( );
+
        // Note: Set the table width to 100% to prevent the label
 +
        // from appearing too narrow in the layout view.
 +
        grid.setWidth( "100%" );  
 
 
 
 
// Create a simple master page that describes how the report will
+
        // Get the first row.
// appear when printed.
+
        RowHandle row = (RowHandle) grid.getRows( ).get( 0 );
//
+
// Note: The report will fail to load in the BIRT designer
+
// unless you create a master page.
+
 
 
 
 
DesignElementHandle element = efactory.newSimpleMasterPage( "Page Master" );  
+
        // Create an image and add it to the first cell.
design.getMasterPages( ).add( element );
+
        ImageHandle image = efactory.newImage( null );
 +
        CellHandle cell = (CellHandle) row.getCells( ).get( 0 );
 +
        cell.getContent( ).add( image );
 +
        image.setURL( "\"urlofimage\"" );  
 
 
 
 
// Create a grid and add it to the "body" slot of the report
+
        // Create a label and add it to the second cell.
// design.
+
        LabelHandle label = efactory.newLabel( null );
 +
        cell = (CellHandle) row.getCells( ).get( 1 );
 +
        cell.getContent( ).add( label );
 +
        label.setText( "Hello, world!" );
 
 
 
 
GridHandle grid = efactory.newGridItem( null, 2 /* cols */, 1 /* row */ );
+
        // Save the design and close it.
design.getBody( ).add( grid );
+
        design.saveAs( "c:/tmp/sample.rptdesign" );  
 +
        design.close( );
 +
        System.out.println("Finished");
 
 
 
 
// Note: Set the table width to 100% to prevent the label
+
        // We're done!
// from appearing too narrow in the layout view.
+
    }
+
}
grid.setWidth( "100%" );
+
+
// Get the first row.
+
+
RowHandle row = (RowHandle) grid.getRows( ).get( 0 );
+
+
// Create an image and add it to the first cell.
+
ImageHandle image = efactory.newImage( null );
+
CellHandle cell = (CellHandle) row.getCells( ).get( 0 );
+
cell.getContent( ).add( image );
+
image.setURL( "\"urlofimage\"" );
+
+
// Create a label and add it to the second cell.
+
LabelHandle label = efactory.newLabel( null );
+
cell = (CellHandle) row.getCells( ).get( 1 );
+
cell.getContent( ).add( label );
+
label.setText( "Hello, world!" );
+
+
// Save the design and close it.
+
+
design.saveAs( "c:/tmp/sample.rptdesign" );
+
design.close( );
+
System.out.println("Finished");
+
+
// We're done!
+
}
+
  }
+
 
+
 
----
 
----
  
Line 146: Line 130:
  
 
----
 
----
 +
Thanks for the example. But mine is getting below error.While run as java standard alone program prompting below error. Please advise, much appriciate
 +
 +
I used same example and downloaded birt_runtime C:\birt-runtime-4_4_2\ReportEngine
 +
 +
Error : Error : A JNI error has occurred , please check your installation and try again.
 +
 +
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
  
 
[[Category:BIRT]]
 
[[Category:BIRT]]
 
[[Category:BIRT Example]]
 
[[Category:BIRT Example]]
 
[[Category:BIRT Example Integration]]
 
[[Category:BIRT Example Integration]]

Latest revision as of 15:23, 5 November 2016

< To: Integration Examples (BIRT)

Java - Simple Design Engine API

This example demonstrate using the DE API to create a simple report. Add comments at the bottom of the example. Moved from the old Design Engine API area.

BIRT Design Engine API Return to the BIRT Design Engine API examples

Source

SimpleCreate.java

import java.io.IOException;

import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.GridHandle;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
import org.eclipse.birt.report.model.api.ImageHandle;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException; 

import com.ibm.icu.util.ULocale;

/**
 * Simple BIRT Design Engine API (DEAPI) demo.
 */
public class SimpleCreate
{
   public static void main( String[] args )
   {
       try
       {
           buildReport( );
       }
       catch( IOException e )
       {
           e.printStackTrace();
       }
       catch( SemanticException e )
       {
           e.printStackTrace();
       }
   }
	
   // This method shows how to build a very simple BIRT report with a
   // minimal set of content: a simple grid with an image and a label.
 	
   static void buildReport() throws IOException, SemanticException
   {
       // Create a session handle. This is used to manage all open designs.
       // Your app need create the session only once.
 		
       //Configure the Engine and start the Platform 
       DesignConfig config = new DesignConfig( );

       config.setProperty("BIRT_HOME", "C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine");
       IDesignEngine engine = null;
       try
       {
           Platform.startup( config );
           IDesignEngineFactory factory = (IDesignEngineFactory) Platform.createFactoryObject( IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
           engine = factory.createDesignEngine( config );
       }
       catch( Exception ex )
       {
           ex.printStackTrace();
       }		
				
       SessionHandle session = engine.newSessionHandle( ULocale.ENGLISH ) ;
		
       // Create a new report design.
       ReportDesignHandle design = session.createDesign( );
		
       // The element factory creates instances of the various BIRT elements.
       ElementFactory efactory = design.getElementFactory( );
		
       // Create a simple master page that describes how the report will appear when printed.
       //
       // Note: The report will fail to load in the BIRT designer unless you create a master page.
       DesignElementHandle element = efactory.newSimpleMasterPage( "Page Master" ); 
       design.getMasterPages( ).add( element );
		
       // Create a grid and add it to the "body" slot of the report design.
       GridHandle grid = efactory.newGridItem( null, 2 /* cols */, 1 /* row */ );
	design.getBody( ).add( grid );
		
       // Note: Set the table width to 100% to prevent the label
       // from appearing too narrow in the layout view.
       grid.setWidth( "100%" ); 
		
       // Get the first row.
       RowHandle row = (RowHandle) grid.getRows( ).get( 0 );
		
       // Create an image and add it to the first cell.
       ImageHandle image = efactory.newImage( null );
       CellHandle cell = (CellHandle) row.getCells( ).get( 0 );
       cell.getContent( ).add( image );
       image.setURL( "\"urlofimage\"" ); 
		
       // Create a label and add it to the second cell.
       LabelHandle label = efactory.newLabel( null );
       cell = (CellHandle) row.getCells( ).get( 1 );
       cell.getContent( ).add( label );
       label.setText( "Hello, world!" ); 
		
       // Save the design and close it.
       design.saveAs( "c:/tmp/sample.rptdesign" ); 
       design.close( );
       System.out.println("Finished");
		
       // We're done!
   }

}


Comments

Please enter comments below by selecting the edit icon to the right. You will need a Bugzilla account to add comments.


Thanks for the example. But mine is getting below error.While run as java standard alone program prompting below error. Please advise, much appriciate

I used same example and downloaded birt_runtime C:\birt-runtime-4_4_2\ReportEngine

Error : Error : A JNI error has occurred , please check your installation and try again.

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

Back to the top