/*******************************************************************************
 * 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.designer.internal.ui.views.attributes.page;

import java.util.Set;

import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.model.api.ListingHandle;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.ReportDesignConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

/**
 * The section attribute page of DE Listing element.
 */
public class ListingSectionPage extends AttributePage
{

	private Button repeatHeaderButton;

	/**
	 * @param parent
	 *            A widget which will be the parent of the new instance (cannot
	 *            be null)
	 * @param style
	 *            The style of widget to construct
	 */
	public ListingSectionPage( Composite parent, int style )
	{
		super( parent, style );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.page.AttributePage#buildUI()
	 */
	protected void buildUI( )
	{
		setLayout( WidgetUtil.createGridLayout( 5 ) );

		String[] properties = {
				StyleHandle.PAGE_BREAK_BEFORE_PROP,
				StyleHandle.PAGE_BREAK_AFTER_PROP,
		// StyleHandle.PAGE_BREAK_INSIDE_PROP,
		};
		for ( int i = 0; i < properties.length; i++ )
		{

			WidgetUtil.buildGridControl( this,
					propertiesMap,
					ReportDesignConstants.STYLE_ELEMENT,
					properties[i],
					1,
					200 );
			if ( properties[i].equals( StyleHandle.PAGE_BREAK_BEFORE_PROP ) )
			{
				WidgetUtil.buildGridControl( this,
						propertiesMap,
						ReportDesignConstants.STYLE_ELEMENT,
						StyleHandle.MASTER_PAGE_PROP,
						1,
						200 );
				WidgetUtil.createGridPlaceholder( this, 1, true );
			}
			else
			{
				WidgetUtil.createGridPlaceholder( this, 3, true );
			}

		}

		WidgetUtil.createHorizontalLine( this, 5 );
		repeatHeaderButton = new Button( this, SWT.CHECK );
		GridData gd = new GridData( );
		gd.horizontalSpan = 2;
		repeatHeaderButton.setLayoutData( gd );
		repeatHeaderButton.setText( Messages.getString( "ListingSectionPage.RepeatHeader" ) ); //$NON-NLS-1$
		repeatHeaderButton.addSelectionListener( new SelectionListener( ) {

			public void widgetSelected( SelectionEvent e )
			{
				setRepeatHeader( repeatHeaderButton.getSelection( ) );
			}

			public void widgetDefaultSelected( SelectionEvent e )
			{
				// TODO Auto-generated method stub

			}
		} );

		WidgetUtil.createHorizontalLine( this, 5 );

		WidgetUtil.buildGridControl( this,
				propertiesMap,
				ReportDesignConstants.LISTING_ITEM,
				ListingHandle.PAGE_BREAK_INTERVAL_PROP,
				1,
				true );

	}

	protected void setRepeatHeader( boolean b )
	{
		if ( input.size( ) == 1 && input.get( 0 ) instanceof ListingHandle )
		{
			ListingHandle listingHandle = (ListingHandle) input.get( 0 );
			try
			{
				listingHandle.setRepeatHeader( b );
			}
			catch ( SemanticException e )
			{
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.page.AttributePage#refreshValues(java.util.Set)
	 */
	protected void refreshValues( Set propertiesSet )
	{
		super.refreshValues( propertiesSet );
		if ( input.size( ) == 1 && input.get( 0 ) instanceof ListingHandle )
		{
			ListingHandle listingHandle = (ListingHandle) input.get( 0 );
			repeatHeaderButton.setSelection( listingHandle.repeatHeader( ) );
		}
	}

}