package org.eclipse.birt.report.engine.layout.content;

import org.eclipse.birt.report.engine.content.IBandContent;
import org.eclipse.birt.report.engine.content.IContent;
import org.eclipse.birt.report.engine.content.IGroupContent;
import org.eclipse.birt.report.engine.content.IListBandContent;
import org.eclipse.birt.report.engine.content.IListContent;
import org.eclipse.birt.report.engine.content.IStyle;
import org.eclipse.birt.report.engine.executor.IReportItemExecutor;
import org.eclipse.birt.report.engine.internal.executor.dom.DOMReportItemExecutor;


public class ListContainerExecutor extends BlockStackingExecutor
		implements
			IReportItemExecutor
{
	protected boolean repeat = false;
	
	public ListContainerExecutor(IContent content, IReportItemExecutor executor)
	{
		super(content, null);
		this.executor = new ExecutorList(executor, content);
		if(content instanceof IGroupContent)
		{
			repeat = ((IGroupContent)content).isHeaderRepeat( );
		}
		else
		{
			if(content instanceof IListContent)
			{
				repeat =  ((IListContent)content).isHeaderRepeat( );
			}
		}
	}
	
	protected class ExecutorList implements IReportItemExecutor
	{
		protected IReportItemExecutor currentRunIn = null;
		protected IReportItemExecutor executor = null;
		protected IContent content = null;
		protected IReportItemExecutor childExecutor = null;
		protected IContent childContent = null;
		protected boolean hasNext = false;
		protected boolean needUpdate = true;
		
		public ExecutorList(IReportItemExecutor executor, IContent content)
		{
			this.content = content;
			this.executor = executor;
		}
		
		public void close( )
		{
			if(currentRunIn!=null)
			{
				currentRunIn.close( );
			}
			executor.close( );
		}

		public IContent execute( )
		{
			return content;
		}

		public IReportItemExecutor getNextChild( )
		{
			if(childContent!=null)
			{
				IReportItemExecutor ret = new ItemExecutorWrapper(childExecutor, childContent);
				childContent = null;
				childExecutor = null;
				needUpdate = true;
				return ret;
			}
			if(currentRunIn!=null)
			{
				needUpdate = true;
				IReportItemExecutor runInChild = currentRunIn.getNextChild( );
				if(runInChild!=null)
				{
					IContent runInContent = runInChild.execute( );
					if(runInContent!=null && 
							(runInContent.getChildren( )==null || runInContent.getChildren( ).size( )==0))
					{
						execute(runInChild, runInContent);
						runInChild.close( );
						runInChild = new DOMReportItemExecutor(runInContent);
					}
				}
				return runInChild;
			}
			assert(false);
			return null;
			
		}

		public boolean hasNextChild( )
		{
			if(!needUpdate)
			{
				return hasNext;
			}
			if(currentRunIn!=null)
			{
				if(currentRunIn.hasNextChild( ))
				{
					hasNext = true;
					needUpdate = false;
					return hasNext;
				}
				else
				{
					currentRunIn.close( );
				}
			}
			currentRunIn = null;
			while(executor.hasNextChild( ))
			{
				IReportItemExecutor next = executor.getNextChild( );
				IContent nextContent = next.execute( );
				if(nextContent instanceof IListBandContent)
				{
					IListBandContent band = (IListBandContent)nextContent;
					if(repeat && (band.getBandType( )==IBandContent.BAND_HEADER || band.getBandType( )==IBandContent.BAND_GROUP_HEADER))
					{
						execute(next, nextContent);
						next.close( );
						next = new DOMReportItemExecutor(nextContent);
						next.execute( );
						nextContent.getParent( ).getChildren( ).add( nextContent );
					}
					if(next.hasNextChild( ))
					{
						currentRunIn = next;
						break;
					}
				}
				else
				{
					childExecutor = next;
					childContent = nextContent;
					break;
				}
			}
			if(currentRunIn!=null || childContent!=null)
			{
				hasNext = true;
			}
			else
			{
				hasNext = false;
			}
			needUpdate = false;
			return hasNext;
		}
		
		protected void execute(IReportItemExecutor executor, IContent content)
		{
			while(executor.hasNextChild( ))
			{
				IReportItemExecutor childExecutor = executor.getNextChild( );
				if(childExecutor!=null)
				{
					IContent childContent = childExecutor.execute( );
					removePageBreak(childContent);
					content.getChildren( ).add( childContent );
					execute(childExecutor, childContent);
					childExecutor.close( );
				}
			}
		}
		
		protected void removePageBreak(IContent content)
		{
			IStyle style = content.getStyle( );
			if(style!=null)
			{
				style.setProperty( IStyle.STYLE_PAGE_BREAK_AFTER, IStyle.AUTO_VALUE );
				style.setProperty( IStyle.STYLE_PAGE_BREAK_BEFORE, IStyle.AUTO_VALUE );
			}
		}
		
		
	}
}

@@ -3282,16 +3282,22 @@
 %09execute
+Header
 (next, n
@@ -4101,24 +4101,441 @@
 execute( );%0A
+%09%09%09%09%09content.getChildren( ).add( childContent );%0A%09%09%09%09%09execute(childExecutor, childContent);%0A%09%09%09%09%09childExecutor.close( );%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09protected void executeHeader(IReportItemExecutor executor, IContent content)%0A%09%09%7B%0A%09%09%09while(executor.hasNextChild( ))%0A%09%09%09%7B%0A%09%09%09%09IReportItemExecutor childExecutor = executor.getNextChild( );%0A%09%09%09%09if(childExecutor!=null)%0A%09%09%09%09%7B%0A%09%09%09%09%09IContent childContent = childExecutor.execute( );%0A
 %09%09%09%09%09removeP

Fix Bugzilla Bug 1497062.1: Page Break within nested table or list works in Web Viewer, not PDF