/*******************************************************************************
 * Copyright (c) 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.report.engine.script.internal;

import org.eclipse.birt.report.engine.api.script.IRowData;
import org.eclipse.birt.report.engine.api.script.element.ICell;
import org.eclipse.birt.report.engine.api.script.eventhandler.ICellEventHandler;
import org.eclipse.birt.report.engine.api.script.instance.ICellInstance;
import org.eclipse.birt.report.engine.script.internal.element.Cell;
import org.eclipse.birt.report.engine.script.internal.instance.CellInstance;
import org.eclipse.birt.report.engine.content.impl.CellContent;
import org.eclipse.birt.report.engine.executor.ExecutionContext;
import org.eclipse.birt.report.engine.ir.ReportItemDesign;
import org.eclipse.birt.report.model.api.CellHandle;

public class CellScriptExecutor extends ScriptExecutor
{

	public static void handleOnPrepare( CellHandle cellHandle,
			ExecutionContext context )
	{
		try
		{
			ICell cell = new Cell( cellHandle );
			if ( handleJS( cell, cellHandle.getOnPrepare( ), context ).didRun( ) )
				return;
			ICellEventHandler eh = getEventHandler( cellHandle, context );
			if ( eh != null )
				eh.onPrepare( cell, context.getReportContext( ) );
		} catch ( Exception e )
		{
			addException( context, e );
		}
	}

	public static void handleOnCreate( CellContent content, IRowData rowData,
			ExecutionContext context, boolean fromGrid )
	{
		try
		{
			ReportItemDesign cellDesign = ( ReportItemDesign ) content
					.getGenerateBy( );
			ICellInstance cell = new CellInstance( content, rowData, context,
					fromGrid );
			if ( handleJS( cell, cellDesign.getOnCreate( ), context ).didRun( ) )
				return;
			ICellEventHandler eh = getEventHandler( cellDesign, context );
			if ( eh != null )
				eh.onCreate( cell, context.getReportContext( ) );

		} catch ( Exception e )
		{
			addException( context, e );
		}
	}

	public static void handleOnRender( CellContent content,
			ExecutionContext context )
	{
		try
		{
			ReportItemDesign cellDesign = ( ReportItemDesign ) content
					.getGenerateBy( );
			//fromGrid doesn't matter here since row data is null
			ICellInstance cell = new CellInstance( content, null, context,
					false );
			if ( handleJS( cell, cellDesign.getOnRender( ), context ).didRun( ) )
				return;
			ICellEventHandler eh = getEventHandler( cellDesign, context );
			if ( eh != null )
				eh.onRender( cell, context.getReportContext( ) );
		} catch ( Exception e )
		{
			addException( context, e );
		}
	}

	private static ICellEventHandler getEventHandler( ReportItemDesign design,
			ExecutionContext context )
	{
		CellHandle handle = ( CellHandle ) design.getHandle( );
		if ( handle == null )
			return null;
		return getEventHandler( handle, context );
	}

	private static ICellEventHandler getEventHandler( CellHandle handle,
			ExecutionContext context )
	{
		ICellEventHandler eh = null;
		try
		{
			eh = ( ICellEventHandler ) getInstance( handle, context );
		} catch ( ClassCastException e )
		{
			addClassCastException( context, e, handle.getEventHandlerClass( ),
					ICellEventHandler.class );
		}
		return eh;
	}
}

@@ -1871,32 +1871,132 @@
  )%0A%09%7B%0A%09%09try%0A%09%09%7B%0A
+%09%09%09Object generateBy = content.getGenerateBy( );%0A%09%09%09if ( generateBy == null )%0A%09%09%09%7B%0A%09%09%09%09return;%0A%09%09%09%7D%0A
 %09%09%09ReportItemDes
@@ -2033,34 +2033,17 @@
 esign ) 
-content%0A%09%09%09%09%09.getG
+g
 enerateB
@@ -2039,27 +2039,24 @@
 ) generateBy
-( )
 ;%0A%09%09%09ICellIn
@@ -2527,16 +2527,116 @@
 try%0A%09%09%7B%0A
+%09%09%09Object generateBy = content.getGenerateBy( );%0A%09%09%09if ( generateBy == null )%0A%09%09%09%7B%0A%09%09%09%09return;%0A%09%09%09%7D%0A
 %09%09%09Repor
@@ -2685,26 +2685,9 @@
 n ) 
-content%0A%09%09%09%09%09.getG
+g
 ener
@@ -2687,28 +2687,30 @@
 ) generateBy
-( );
+; %0A%09%09%09
 %0A%09%09%09//fromGr

fix bug 124760