[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.birt] Re: Dynamic column in dataset

Sly,
The code below dynamically adds a dataset and a table.

Sly wrote:
Hi Anup,

I will explore this way. Do you have a link of a good tutorial ?
Thanx for your answer

Sly

package Rep2DynamicElements;

import org.eclipse.birt.report.engine.api.script.IReportContext;
import org.eclipse.birt.report.engine.api.script.eventadapter.ReportEventAdapter;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.ElementFactory;
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.ScriptDataSetHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.TableHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.structures.ColumnHint;
import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn;
import org.eclipse.birt.report.model.api.elements.structures.ResultSetColumn;


public class ReportAdapter extends ReportEventAdapter {

@Override
public void initialize(IReportContext reportContext) {
//get a reference to the ElementFactory
ElementFactory elementFactory = reportContext.getReportRunnable().getDesignHandle().getElementFactory();
//create a new table with 2 columns
TableHandle hndTable=elementFactory.newTableItem("DynamicTable",2);
try {
hndTable.setWidth("100%");
ReportDesignHandle dHandle=(ReportDesignHandle)reportContext.getReportRunnable().getDesignHandle();
//ScriptDataSetHandle hndDataSet= (ScriptDataSetHandle)dHandle.findDataSet("Employees");

ScriptDataSetHandle hndDataSet=elementFactory.newScriptDataSet("Employees");// (ScriptDataSetHandle)dHandle.findDataSet("Employees");
hndDataSet.setDataSource("Data Source");
hndDataSet.setEventHandlerClass("Rep2DynamicElements.DataSetAdapter");

//any one of the below 2 lines can be used to add the data-set to the report
dHandle.addElement(hndDataSet, dHandle.DATA_SET_SLOT);
//dHandle.getDataSets().add(hndDataSet);

//------Column Hints-----------------------------


			PropertyHandle columnHint =
			hndDataSet.getPropertyHandle(ScriptDataSetHandle.COLUMN_HINTS_PROP);
			
			ColumnHint ch = StructureFactory.createColumnHint();
			ch.setProperty("columnName", "Firstname");
			columnHint.addItem(ch);

			ch=StructureFactory.createColumnHint();
			ch.setProperty("columnName", "Surname");
			columnHint.addItem(ch);
			
			//------Result Set---------------------------------
			PropertyHandle resultSet =
				hndDataSet.getPropertyHandle(ScriptDataSetHandle.RESULT_SET_PROP);

			ResultSetColumn rs = StructureFactory.createResultSetColumn();
			rs.setColumnName("Firstname");
			rs.setPosition(new Integer(1));
			rs.setDataType("string");
			resultSet.addItem(rs);

			rs = StructureFactory.createResultSetColumn();
			rs.setColumnName("Surname");
			rs.setPosition(new Integer(2));
			rs.setDataType("string");
			resultSet.addItem(rs);
			

			
			RowHandle hndNameHeader= (RowHandle)hndTable.getHeader().get(0);
			CellHandle hndNameCell=(CellHandle)hndNameHeader.getCells().get(0);
			LabelHandle lbl=elementFactory.newLabel("L1");	
			lbl.setText("Firstname");
			hndNameCell.getContent().add(lbl);
			CellHandle hndSurNameCell=(CellHandle)hndNameHeader.getCells().get(1);			
			lbl=elementFactory.newLabel("L2");	
			lbl.setText("Surname");
			hndSurNameCell.getContent().add(lbl);

hndTable.setDataSet(hndDataSet);
PropertyHandle boundCols=hndTable.getColumnBindings();
ComputedColumn cs1;
cs1=StructureFactory.createComputedColumn();
cs1.setName("Firstname");
cs1.setExpression("dataSetRow[\"Firstname\"]");
boundCols.addItem(cs1);

cs1=StructureFactory.createComputedColumn();
cs1.setName("Surname");
cs1.setExpression("dataSetRow[\"Surname\"]");
boundCols.addItem(cs1);

//set reference to the first detail row
RowHandle hndDetailRow =(RowHandle) hndTable.getDetail().get(0);

//get references to the first 2 cells
CellHandle hndFirstCell = (CellHandle)hndDetailRow.getCells().get(0);
DataItemHandle hndName=elementFactory.newDataItem(null);
hndName.setResultSetColumn("Firstname");
hndFirstCell.getContent().add(hndName);

CellHandle hndSecondCell = (CellHandle)hndDetailRow.getCells().get(1);
DataItemHandle hndSurName=elementFactory.newDataItem(null);
hndSurName.setResultSetColumn("Surname");
hndSecondCell.getContent().add(hndSurName);

//hndTable.setDataSet(hndDataSet);
((ReportDesignHandle)reportContext.getReportRunnable().getDesignHandle()).getBody().add(hndTable);



}

catch (SemanticException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}





super.initialize(reportContext);
}


}


Regards, Anup