Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » convert ArrayList to table in DEAPI
convert ArrayList to table in DEAPI [message #368172] Fri, 08 May 2009 12:05 Go to next message
Conor O'Mahony is currently offline Conor O'MahonyFriend
Messages: 108
Registered: July 2009
Senior Member
Hi,

I have a list of lists of strings, which corresponds to data that I want to insert into a Table
using the Design Engine API.

ArrayList<List<String>> tableParameters;

I have created a table as follows -
TableHandle table = factory.newTableItem( null, tableParameters.get(0).size());
table.setWidth( "100%" );
table.setRepeatHeader(true);


The first list of strings in the tableParameters list correspond to the table headings, so I can add
the table heading to the created table from this.

List<String> headings = parameters.get(0);

// table header
RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );

LabelHandle label;
int i = 0;
for( String item : headings)
{
label= factory.newLabel( null );
label.setText(item);
CellHandle cell = (CellHandle) tableheader.getCells( ).get( i );
cell.getContent( ).add( label);
i++;
}

However I am not sure what is the best way to fill the table detail. Should I try and create a
dataset/datasource or is there a way of iterating over each table detail row directly and filling it
with the list data?
Re: convert ArrayList to table in DEAPI [message #368177 is a reply to message #368172] Fri, 08 May 2009 14:32 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jasonweathersby.alltel.net

Conor,

If you already have the data why not use a grid and insert labels or
text fields that you can paste the data into?

Jason

Conor O'Mahony wrote:
> Hi,
>
> I have a list of lists of strings, which corresponds to data that I want
> to insert into a Table using the Design Engine API.
>
> ArrayList<List<String>> tableParameters;
>
> I have created a table as follows -
> TableHandle table = factory.newTableItem( null,
> tableParameters.get(0).size());
> table.setWidth( "100%" );
> table.setRepeatHeader(true);
>
>
> The first list of strings in the tableParameters list correspond to the
> table headings, so I can add the table heading to the created table from
> this.
>
> List<String> headings = parameters.get(0);
>
> // table header
> RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );
>
> LabelHandle label;
> int i = 0;
> for( String item : headings)
> {
> label= factory.newLabel( null );
> label.setText(item);
> CellHandle cell = (CellHandle) tableheader.getCells( ).get( i );
> cell.getContent( ).add( label);
> i++;
> }
>
> However I am not sure what is the best way to fill the table detail.
> Should I try and create a dataset/datasource or is there a way of
> iterating over each table detail row directly and filling it with the
> list data?
Re: convert ArrayList to table in DEAPI [message #368181 is a reply to message #368177] Fri, 08 May 2009 15:23 Go to previous messageGo to next message
Conor O'Mahony is currently offline Conor O'MahonyFriend
Messages: 108
Registered: July 2009
Senior Member
Hi Jason,

I did think of that but thought a table would be better as the data may contain a large number of
lines and I wanted to use setRepeatHeader() to repeat the headers on the top of each page... is
there a way to do that with a grid?

Many thanks,
Conor

Jason Weathersby wrote:
> Conor,
>
> If you already have the data why not use a grid and insert labels or
> text fields that you can paste the data into?
>
> Jason
>
> Conor O'Mahony wrote:
>> Hi,
>>
>> I have a list of lists of strings, which corresponds to data that I
>> want to insert into a Table using the Design Engine API.
>>
>> ArrayList<List<String>> tableParameters;
>>
>> I have created a table as follows -
>> TableHandle table = factory.newTableItem( null,
>> tableParameters.get(0).size());
>> table.setWidth( "100%" );
>> table.setRepeatHeader(true);
>>
>>
>> The first list of strings in the tableParameters list correspond to
>> the table headings, so I can add the table heading to the created
>> table from this.
>>
>> List<String> headings = parameters.get(0);
>>
>> // table header
>> RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );
>>
>> LabelHandle label;
>> int i = 0;
>> for( String item : headings)
>> {
>> label= factory.newLabel( null );
>> label.setText(item);
>> CellHandle cell = (CellHandle) tableheader.getCells( ).get( i );
>> cell.getContent( ).add( label);
>> i++;
>> }
>>
>> However I am not sure what is the best way to fill the table detail.
>> Should I try and create a dataset/datasource or is there a way of
>> iterating over each table detail row directly and filling it with the
>> list data?
Re: convert ArrayList to table in DEAPI [message #368183 is a reply to message #368181] Fri, 08 May 2009 17:22 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jasonweathersby.alltel.net

Conor,

I do not think so. If you have to use a table it is best that you add a
dataset then create the binding on the table and fill out the detail
row. If you need some sample de api code to do this let me know.

Jason

Conor O'Mahony wrote:
> Hi Jason,
>
> I did think of that but thought a table would be better as the data may
> contain a large number of lines and I wanted to use setRepeatHeader() to
> repeat the headers on the top of each page... is there a way to do that
> with a grid?
>
> Many thanks,
> Conor
>
> Jason Weathersby wrote:
>> Conor,
>>
>> If you already have the data why not use a grid and insert labels or
>> text fields that you can paste the data into?
>>
>> Jason
>>
>> Conor O'Mahony wrote:
>>> Hi,
>>>
>>> I have a list of lists of strings, which corresponds to data that I
>>> want to insert into a Table using the Design Engine API.
>>>
>>> ArrayList<List<String>> tableParameters;
>>>
>>> I have created a table as follows -
>>> TableHandle table = factory.newTableItem( null,
>>> tableParameters.get(0).size());
>>> table.setWidth( "100%" );
>>> table.setRepeatHeader(true);
>>>
>>>
>>> The first list of strings in the tableParameters list correspond to
>>> the table headings, so I can add the table heading to the created
>>> table from this.
>>>
>>> List<String> headings = parameters.get(0);
>>>
>>> // table header
>>> RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );
>>>
>>> LabelHandle label;
>>> int i = 0;
>>> for( String item : headings)
>>> {
>>> label= factory.newLabel( null );
>>> label.setText(item);
>>> CellHandle cell = (CellHandle) tableheader.getCells( ).get(
>>> i );
>>> cell.getContent( ).add( label);
>>> i++;
>>> }
>>>
>>> However I am not sure what is the best way to fill the table detail.
>>> Should I try and create a dataset/datasource or is there a way of
>>> iterating over each table detail row directly and filling it with the
>>> list data?
Re: convert ArrayList to table in DEAPI [message #368188 is a reply to message #368183] Mon, 11 May 2009 08:55 Go to previous messageGo to next message
Conor O'Mahony is currently offline Conor O'MahonyFriend
Messages: 108
Registered: July 2009
Senior Member
Hi Jason,

Thanks for your reply. I have seen some sample deapi code for creating dataset and tables (e.g.
http://wiki.eclipse.org/Java_-_Build_Dynamic_Table_(BIRT) ) but if you could point me to some code
that might be more suitable when I already have the data in an array then that would be great.

Many thanks,
Conor

Jason Weathersby wrote:
> Conor,
>
> I do not think so. If you have to use a table it is best that you add a
> dataset then create the binding on the table and fill out the detail
> row. If you need some sample de api code to do this let me know.
>
> Jason
>
> Conor O'Mahony wrote:
>> Hi Jason,
>>
>> I did think of that but thought a table would be better as the data
>> may contain a large number of lines and I wanted to use
>> setRepeatHeader() to repeat the headers on the top of each page... is
>> there a way to do that with a grid?
>>
>> Many thanks,
>> Conor
>>
>> Jason Weathersby wrote:
>>> Conor,
>>>
>>> If you already have the data why not use a grid and insert labels or
>>> text fields that you can paste the data into?
>>>
>>> Jason
>>>
>>> Conor O'Mahony wrote:
>>>> Hi,
>>>>
>>>> I have a list of lists of strings, which corresponds to data that I
>>>> want to insert into a Table using the Design Engine API.
>>>>
>>>> ArrayList<List<String>> tableParameters;
>>>>
>>>> I have created a table as follows -
>>>> TableHandle table = factory.newTableItem( null,
>>>> tableParameters.get(0).size());
>>>> table.setWidth( "100%" );
>>>> table.setRepeatHeader(true);
>>>>
>>>>
>>>> The first list of strings in the tableParameters list correspond to
>>>> the table headings, so I can add the table heading to the created
>>>> table from this.
>>>>
>>>> List<String> headings = parameters.get(0);
>>>>
>>>> // table header
>>>> RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );
>>>>
>>>> LabelHandle label;
>>>> int i = 0;
>>>> for( String item : headings)
>>>> {
>>>> label= factory.newLabel( null );
>>>> label.setText(item);
>>>> CellHandle cell = (CellHandle) tableheader.getCells( ).get(
>>>> i );
>>>> cell.getContent( ).add( label);
>>>> i++;
>>>> }
>>>>
>>>> However I am not sure what is the best way to fill the table detail.
>>>> Should I try and create a dataset/datasource or is there a way of
>>>> iterating over each table detail row directly and filling it with
>>>> the list data?
Re: convert ArrayList to table in DEAPI [message #368194 is a reply to message #368188] Mon, 11 May 2009 14:44 Go to previous message
Eclipse UserFriend
Originally posted by: jasonweathersby.alltel.net

Conor,

Here is an example of doing a scripted data set within a report. You
will need to modify it to use your object.

Jason

package DEAPI;


import java.io.File;
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.DataItemHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.DesignEngine;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.TableGroupHandle;
import org.eclipse.birt.report.model.api.TableHandle;
import org.eclipse.birt.report.model.api.ColumnHandle;

import org.eclipse.birt.report.model.api.activity.SemanticException ;
import org.eclipse.birt.report.model.api.command.ContentException;
import org.eclipse.birt.report.model.api.command.NameException;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConst ants;
import org.eclipse.birt.report.model.api.elements.structures.Comput edColumn;
import org.eclipse.birt.report.model.api.metadata.IMetaDataDictiona ry;
import org.eclipse.birt.report.model.elements.interfaces.IReportIte mModel;
import org.eclipse.birt.report.model.elements.interfaces.IStyleMode l;

import org.eclipse.birt.report.model.api.ScriptDataSetHandle;
import org.eclipse.birt.report.model.api.ScriptDataSourceHandle;
import com.ibm.icu.util.ULocale;
import
org.eclipse.birt.report.model.api.elements.structures.Result SetColumn;
import java.util.Iterator;
import java.util.List;
import org.eclipse.birt.report.model.api.DataSetHandle;

public class ScriptedDS
{

ReportDesignHandle reportDesignHandle = null;

ElementFactory elementFactory = null;

IMetaDataDictionary dict = null;

ComputedColumn cs1, cs2, cs3, cs4, cs5 = null;

public static void main( String[] args ) throws SemanticException,
IOException
{
new ScriptedDS( ).createReport( );
}

void createReport( ) throws SemanticException, IOException
{
//Configure the Engine and start the Platform
DesignConfig config = new DesignConfig( );

config.setBIRTHome("C:/birt/birt-runtime-2_3_2/birt-runtime-2_3_2/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
reportDesignHandle = session.createDesign( );

// Element factory is used to create instances of BIRT elements.
elementFactory = reportDesignHandle.getElementFactory( );

dict = new DesignEngine( null ).getMetaData( );

createMasterPages( );
createDataSources( );
createDataSets( );
createBody( );


reportDesignHandle.saveAs("output/desample/SDS.rptdesign"
);//$NON-NLS-1$//$NON-NLS-2$
reportDesignHandle.close();

System.out.println("finished");

Platform.shutdown();
}

//Scripted Data Set

private void createDataSources( ) throws SemanticException
{
ScriptDataSourceHandle dataSourceHandle =
elementFactory.newScriptDataSource( "Data Source" );
reportDesignHandle.getDataSources( ).add( dataSourceHandle );
}

private void createDataSets( ) throws SemanticException
{



ScriptDataSetHandle dataSetHandle = elementFactory.newScriptDataSet(
"Data Set" );//$NON-NLS-1$
dataSetHandle.setDataSource( "Data Source" );//$NON-NLS-1$

// Set open( ) in code
dataSetHandle.setOpen( "i=0;"
);

// Set fetch( ) in code
dataSetHandle.setFetch( "if ( i < 20 ){"
+ "row[\"Month\"] = 1;"
+ "row[\"Product\"] = 'My Product' + parseInt(i/3);"
+ "row[\"Amount\"] = i;"
+ "i++;"
+ "return true;}" + "else return false;" );



PropertyHandle computedSet =
dataSetHandle.getPropertyHandle(ScriptDataSetHandle.RESULT_S ET_PROP);

// Since this is a Scripted Data Source you need to tell the
// DataSet what the columns are. For this example, we will just
// hard code the known resultColumn values
ResultSetColumn resultColumn = StructureFactory.createResultSetColumn();
resultColumn.setPosition(1);
resultColumn.setColumnName("Month");
resultColumn.setDataType("integer");
computedSet.addItem(resultColumn);

resultColumn = StructureFactory.createResultSetColumn();
resultColumn.setPosition(2);
resultColumn.setColumnName("Product");
resultColumn.setDataType("string");
computedSet.addItem(resultColumn);

resultColumn = StructureFactory.createResultSetColumn();
resultColumn.setPosition(3);
resultColumn.setColumnName("Amount");
resultColumn.setDataType("integer");

computedSet.addItem(resultColumn);


reportDesignHandle.getDataSets( ).add( dataSetHandle );


}






private void createMasterPages( ) throws ContentException, NameException
{
DesignElementHandle simpleMasterPage =
elementFactory.newSimpleMasterPage( "Master Page" );//$NON-NLS-1$
reportDesignHandle.getMasterPages( ).add( simpleMasterPage );
}



private void createBody( ) throws SemanticException
{


TableHandle table = elementFactory.newTableItem( null, 3, 1, 1, 1 );
table.setProperty( IStyleModel.TEXT_ALIGN_PROP,
DesignChoiceConstants.TEXT_ALIGN_CENTER );
table.setWidth( "80%" );//$NON-NLS-1$
table.setProperty( IReportItemModel.DATA_SET_PROP, "Data Set"
);//$NON-NLS-1$

ColumnHandle ch = (ColumnHandle)table.getColumns().get(0);
//ch.setProperty("Width", "3in");
ch.getWidth().setStringValue("3in");


// bind the data set columns to the table
DataSetHandle dataSetHandle = (DataSetHandle)
reportDesignHandle.getDataSets().get(0);
List resultSetCols =
dataSetHandle.getListProperty(DataSetHandle.RESULT_SET_PROP) ;
PropertyHandle boundCols = table.getColumnBindings();
for (Iterator iterator = resultSetCols.iterator(); iterator.hasNext();) {
ResultSetColumn rsHandle = (ResultSetColumn) iterator.next();
ComputedColumn col = StructureFactory.createComputedColumn();
col.setName(rsHandle.getColumnName());
col.setExpression("dataSetRow[\"" + rsHandle.getColumnName() + "\"]");
boundCols.addItem(col);
}


// Header
RowHandle header = (RowHandle) table.getHeader( ).get( 0 );

CellHandle tcell = (CellHandle) header.getCells( ).get( 0 );
tcell.getWidth().setStringValue("3in");
LabelHandle label = elementFactory.newLabel( null );
label.setText( "Order" );//$NON-NLS-1$
tcell.getContent( ).add( label );

tcell = (CellHandle) header.getCells( ).get( 1 );
label = elementFactory.newLabel( null );
label.setText( "Product" );//$NON-NLS-1$
tcell.getContent( ).add( label );

tcell = (CellHandle) header.getCells( ).get( 2 );
label = elementFactory.newLabel( null );
label.setText( "Amount" );//$NON-NLS-1$
tcell.getContent( ).add( label );

RowHandle detail = (RowHandle) table.getDetail( ).get( 0 );
DataItemHandle data = elementFactory.newDataItem( null );

tcell = (CellHandle) detail.getCells( ).get( 1 );
data = elementFactory.newDataItem( null );
data.setResultSetColumn("Product");
tcell.getContent( ).add( data );

tcell = (CellHandle) detail.getCells( ).get( 2 );
data = elementFactory.newDataItem( null );
data.setResultSetColumn( "Amount" );
tcell.getContent( ).add( data );
reportDesignHandle.getBody( ).add( table );
}
}

Conor O'Mahony wrote:
> Hi Jason,
>
> Thanks for your reply. I have seen some sample deapi code for creating
> dataset and tables (e.g.
> http://wiki.eclipse.org/Java_-_Build_Dynamic_Table_(BIRT) ) but if you
> could point me to some code that might be more suitable when I already
> have the data in an array then that would be great.
>
> Many thanks,
> Conor
>
> Jason Weathersby wrote:
>> Conor,
>>
>> I do not think so. If you have to use a table it is best that you add
>> a dataset then create the binding on the table and fill out the detail
>> row. If you need some sample de api code to do this let me know.
>>
>> Jason
>>
>> Conor O'Mahony wrote:
>>> Hi Jason,
>>>
>>> I did think of that but thought a table would be better as the data
>>> may contain a large number of lines and I wanted to use
>>> setRepeatHeader() to repeat the headers on the top of each page... is
>>> there a way to do that with a grid?
>>>
>>> Many thanks,
>>> Conor
>>>
>>> Jason Weathersby wrote:
>>>> Conor,
>>>>
>>>> If you already have the data why not use a grid and insert labels or
>>>> text fields that you can paste the data into?
>>>>
>>>> Jason
>>>>
>>>> Conor O'Mahony wrote:
>>>>> Hi,
>>>>>
>>>>> I have a list of lists of strings, which corresponds to data that I
>>>>> want to insert into a Table using the Design Engine API.
>>>>>
>>>>> ArrayList<List<String>> tableParameters;
>>>>>
>>>>> I have created a table as follows -
>>>>> TableHandle table = factory.newTableItem( null,
>>>>> tableParameters.get(0).size());
>>>>> table.setWidth( "100%" );
>>>>> table.setRepeatHeader(true);
>>>>>
>>>>>
>>>>> The first list of strings in the tableParameters list correspond to
>>>>> the table headings, so I can add the table heading to the created
>>>>> table from this.
>>>>>
>>>>> List<String> headings = parameters.get(0);
>>>>>
>>>>> // table header
>>>>> RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );
>>>>>
>>>>> LabelHandle label;
>>>>> int i = 0;
>>>>> for( String item : headings)
>>>>> {
>>>>> label= factory.newLabel( null );
>>>>> label.setText(item);
>>>>> CellHandle cell = (CellHandle) tableheader.getCells(
>>>>> ).get( i );
>>>>> cell.getContent( ).add( label);
>>>>> i++;
>>>>> }
>>>>>
>>>>> However I am not sure what is the best way to fill the table
>>>>> detail. Should I try and create a dataset/datasource or is there a
>>>>> way of iterating over each table detail row directly and filling it
>>>>> with the list data?
Previous Topic:Adding items to toolbar and BIRT Main Menu Bar
Next Topic:p2 enabled update site for BIRT?
Goto Forum:
  


Current Time: Fri Apr 26 19:35:19 GMT 2024

Powered by FUDForum. Page generated in 0.03432 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top