/*******************************************************************************
 * Copyright (c) 2004 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.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.birt.report.designer.core.model.SessionHandleAdapter;
import org.eclipse.birt.report.designer.internal.ui.dialogs.FormatChangeEvent;
import org.eclipse.birt.report.designer.internal.ui.dialogs.FormatDateTimePage;
import org.eclipse.birt.report.designer.internal.ui.dialogs.IFormatChangeListener;
import org.eclipse.birt.report.designer.internal.ui.dialogs.IFormatPage;
import org.eclipse.birt.report.designer.internal.ui.util.ExceptionHandler;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.elements.interfaces.IStyleModel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

/**
 * Format date time attribute page for formatting date and times.
 */

public class FormatDateTimeAttributePage extends AttributePage
{

	IFormatPage formatPage;

	/**
	 * Creates an attribute page for dateTime formatter.
	 * 
	 * @param parent
	 *            The container.
	 * @param style
	 *            The syle of the page.
	 */
	public FormatDateTimeAttributePage( 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( new GridLayout( 1, false ) );
		formatPage = new FormatDateTimePage( this, SWT.NULL,
				IFormatPage.PAGE_ALIGN_VIRTICAL );
		GridData data = new GridData( GridData.FILL_HORIZONTAL );
		//data.widthHint = 400;
		( (Composite) formatPage ).setLayoutData( data );
		formatPage.addFormatChangeListener( new IFormatChangeListener( ) {

			public void formatChange( FormatChangeEvent event )
			{
				if ( formatPage.isDirty( ) && formatPage.isFormatModified( ) )
				{
					doSave( event.getCategory( ), event.getPattern( ) );
					if ( event.getCategory( ) != null
							|| event.getPattern( ) != null )
					{
						doLoad( );
					}
				}
			}
		} );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.page.AttributePage#setInput(java.util.List)
	 */
	public void setInput( List elements )
	{
		super.setInput( elements );
		// load value from model, and set into format page.
		doLoad( );
	}

	private void doLoad( )
	{
		( (Composite) formatPage ).setEnabled( true );

		String[] format = getFormatValue( input );
		if ( format == null )
		{
			formatPage.setInput( null, null );
		}
		else
		{
			formatPage.setInput( format[0], format[1] );
		}
	}

	/**
	 * Saves format value into model.
	 */
	private void doSave( String category, String pattern )
	{
		SessionHandleAdapter.getInstance( ).getCommandStack( )
		.startTrans( Messages.getString("FormatDateTimeAttributePage.Trans.SetDateTimeFormat") );

		for ( Iterator iter = input.iterator( ); iter.hasNext( ); )
		{
			DesignElementHandle element = (DesignElementHandle) iter.next( );
			try
			{
				if ( category == null && pattern == null )
				{
					element.setProperty( IStyleModel.DATE_TIME_FORMAT_PROP,
							null );
				}
				else
				{

					element.getPrivateStyle( ).setDateTimeFormatCategory(
							category );
					element.getPrivateStyle( ).setDateTimeFormat( pattern );
				}
			}
			catch ( SemanticException e )
			{
				ExceptionHandler.handle( e );
				SessionHandleAdapter.getInstance( ).getCommandStack( )
						.rollbackAll( );
			}
		}
		SessionHandleAdapter.getInstance( ).getCommandStack( )
		.commit( );
	}

	private String[] getFormatValue( List input )
	{
		if ( input.isEmpty( ) )
		{
			return null;
		}
		String baseCategory = ( (DesignElementHandle) input.get( 0 ) )
				.getPrivateStyle( ).getDateTimeFormatCategory( );
		String basePattern = ( (DesignElementHandle) input.get( 0 ) )
				.getPrivateStyle( ).getDateTimeFormat( );

		for ( Iterator iter = input.iterator( ); iter.hasNext( ); )
		{
			DesignElementHandle handle = (DesignElementHandle) iter.next( );
			String category = handle.getPrivateStyle( )
					.getDateTimeFormatCategory( );
			String pattern = handle.getPrivateStyle( ).getDateTimeFormat( );

			if ( ( ( baseCategory == null && category == null ) || ( baseCategory != null && baseCategory
					.equals( category ) ) )
					&& ( ( basePattern == null && pattern == null ) || ( basePattern != null && basePattern
							.equals( pattern ) ) ) )
			{
				continue;
			}
			return null;
		}
		return new String[]{baseCategory, basePattern};
	}
	
	protected void refreshValues( Set propertiesSet )
	{
		doLoad();
	}
}