/***********************************************************************
 * 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.chart.ui.swt.composites;

import org.eclipse.birt.chart.model.data.Trigger;
import org.eclipse.birt.chart.ui.extension.i18n.Messages;
import org.eclipse.birt.chart.ui.swt.wizard.ChartWizardContext;
import org.eclipse.birt.chart.util.LiteralHelper;
import org.eclipse.emf.common.util.EList;
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.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;

/**
 * @author Actuate Corporation
 * 
 */
public class TriggerEditorComposite extends Composite
		implements
			SelectionListener
{

	private transient List lstTriggers = null;

	private transient TriggerDataComposite tdcSelected = null;

	private transient Button btnAdd = null;

	private transient Button btnUpdate = null;

	private transient Button btnRemove = null;

	private static final String TRIGGER_SEPARATOR = " - "; //$NON-NLS-1$

	private transient int iCurrentSelection = -1;

	private transient EList triggers = null;

	private transient ChartWizardContext wizardContext;

	private transient boolean bEnableURLParameters;

	private transient boolean bEnableShowTooltipValue;

	/**
	 * 
	 * @param parent
	 * @param style
	 * @param triggers
	 * @param wizardContext
	 * @param bEnableURLParameters
	 * @param bEnableShowTooltipValue
	 */
	public TriggerEditorComposite( Composite parent, int style, EList triggers,
			ChartWizardContext wizardContext, boolean bEnableURLParameters,
			boolean bEnableShowTooltipValue )
	{
		super( parent, style );
		this.triggers = triggers;
		this.wizardContext = wizardContext;
		this.bEnableURLParameters = bEnableURLParameters;
		this.bEnableShowTooltipValue = bEnableShowTooltipValue;
		init( );
		placeComponents( );
	}

	/**
	 * Invoking this constructor will cause some operation unavailable.
	 * 
	 * @param parent
	 * @param style
	 * @param triggers
	 * @deprecated
	 */
	public TriggerEditorComposite( Composite parent, int style, EList triggers )
	{
		super( parent, style );
		this.triggers = triggers;
		this.bEnableURLParameters = false;
		this.bEnableShowTooltipValue = false;
		init( );
		placeComponents( );
	}

	private void init( )
	{

	}

	private void placeComponents( )
	{
		// Layout for the main composite
		GridLayout glContent = new GridLayout( );
		glContent.numColumns = 2;
		glContent.horizontalSpacing = 5;
		glContent.verticalSpacing = 5;
		glContent.marginHeight = 2;
		glContent.marginWidth = 4;

		this.setLayout( glContent );

		// Layout for the list composite
		GridLayout glCMPList = new GridLayout( );
		glCMPList.numColumns = 2;
		glCMPList.verticalSpacing = 5;
		glCMPList.horizontalSpacing = 5;

		Composite cmpList = new Composite( this, SWT.NONE );
		GridData gdCMPList = new GridData( GridData.FILL_BOTH );
		cmpList.setLayoutData( gdCMPList );
		cmpList.setLayout( glCMPList );

		// Trigger list composite
		lstTriggers = new List( cmpList, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL );
		GridData gdLSTTriggers = new GridData( GridData.FILL_BOTH );
		gdLSTTriggers.horizontalSpan = 2;
		gdLSTTriggers.widthHint = 150;
		gdLSTTriggers.grabExcessVerticalSpace = true;
		lstTriggers.setLayoutData( gdLSTTriggers );
		lstTriggers.addSelectionListener( this );

		// Layout for the list composite
		GridLayout glCMPTrigger = new GridLayout( );
		glCMPTrigger.verticalSpacing = 5;
		glCMPTrigger.horizontalSpacing = 5;

		Composite cmpTrigger = new Composite( this, SWT.NONE );
		GridData gdCMPTrigger = new GridData( GridData.FILL_BOTH );
		cmpTrigger.setLayoutData( gdCMPTrigger );
		cmpTrigger.setLayout( glCMPTrigger );

		// Triger data composite
		tdcSelected = new TriggerDataComposite( cmpTrigger,
				SWT.NONE,
				null,
				wizardContext,
				bEnableURLParameters,
				bEnableShowTooltipValue );
		GridData gdTDCSelected = new GridData( GridData.FILL_HORIZONTAL
				| GridData.VERTICAL_ALIGN_BEGINNING );
		tdcSelected.setLayoutData( gdTDCSelected );

		// Trigger editor buttons
		btnRemove = new Button( cmpList, SWT.PUSH );
		GridData gdBTNRemove = new GridData( GridData.HORIZONTAL_ALIGN_BEGINNING );
		gdBTNRemove.grabExcessVerticalSpace = false;
		btnRemove.setLayoutData( gdBTNRemove );
		btnRemove.setText( Messages.getString( "TriggerEditorComposite.Lbl.Remove" ) ); //$NON-NLS-1$
		btnRemove.addSelectionListener( this );

		btnAdd = new Button( cmpList, SWT.PUSH );
		GridData gdBTNAdd = new GridData( GridData.HORIZONTAL_ALIGN_BEGINNING );
		gdBTNAdd.grabExcessVerticalSpace = false;
		btnAdd.setLayoutData( gdBTNAdd );
		btnAdd.setText( Messages.getString( "TriggerEditorComposite.Lbl.Add" ) ); //$NON-NLS-1$
		btnAdd.addSelectionListener( this );

		btnUpdate = new Button( cmpTrigger, SWT.PUSH );
		GridData gdBTNUpdate = new GridData( GridData.HORIZONTAL_ALIGN_BEGINNING
				| GridData.VERTICAL_ALIGN_BEGINNING );
		gdBTNUpdate.grabExcessVerticalSpace = false;
		btnUpdate.setLayoutData( gdBTNUpdate );
		btnUpdate.setText( Messages.getString( "TriggerEditorComposite.Lbl.Update" ) ); //$NON-NLS-1$
		btnUpdate.addSelectionListener( this );

		populateList( );

		refreshButtons( );
	}

	private void refreshButtons( )
	{
		btnRemove.setEnabled( lstTriggers.getSelectionIndex( ) != -1 );
		btnUpdate.setEnabled( lstTriggers.getSelectionIndex( ) != -1 );
	}

	private void populateList( )
	{
		lstTriggers.removeAll( );
		int i = triggers.size( );
		for ( int iT = 0; iT < i; iT++ )
		{
			lstTriggers.add( getTriggerAsString( (Trigger) triggers.get( iT ) ) );
		}
		if ( i > 0 )
		{
			lstTriggers.select( 0 );
			iCurrentSelection = lstTriggers.getSelectionIndex( );
			tdcSelected.setTrigger( (Trigger) triggers.get( iCurrentSelection ) );
		}
	}

	private String getTriggerAsString( Trigger trigger )
	{
		StringBuffer sbTrigger = new StringBuffer( );
		sbTrigger.append( LiteralHelper.triggerConditionSet.getDisplayNameByName( trigger.getCondition( )
				.getName( ) ) );
		sbTrigger.append( TRIGGER_SEPARATOR );
		sbTrigger.append( LiteralHelper.actionTypeSet.getDisplayNameByName( trigger.getAction( )
				.getType( )
				.getName( ) ) );
		// sbTrigger.append( TRIGGER_SEPARATOR );
		// sbTrigger.append( String.valueOf( trigger.getAction( ).getValue( ) )
		// );
		return sbTrigger.toString( );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	public void widgetSelected( SelectionEvent e )
	{
		if ( e.getSource( ).equals( lstTriggers ) )
		{
			iCurrentSelection = lstTriggers.getSelectionIndex( );
			tdcSelected.setTrigger( (Trigger) triggers.get( iCurrentSelection ) );
			refreshButtons( );
			return;
		}
		if ( e.getSource( ).equals( btnRemove ) )
		{
			if ( iCurrentSelection == -1 )
			{
				refreshButtons( );
				return;
			}
			tdcSelected.clear( );
			triggers.remove( iCurrentSelection );
			iCurrentSelection = -1;
		}
		else if ( e.getSource( ).equals( btnAdd ) )
		{
			triggers.add( tdcSelected.getTrigger( ) );
			tdcSelected.clear( );
			iCurrentSelection = -1;
		}
		else if ( e.getSource( ).equals( btnUpdate ) )
		{
			if ( iCurrentSelection != -1 )
			{
				triggers.set( lstTriggers.getSelectionIndex( ),
						tdcSelected.getTrigger( ) );
				tdcSelected.clear( );
			}
			iCurrentSelection = -1;
		}
		populateList( );

		refreshButtons( );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	public void widgetDefaultSelected( SelectionEvent e )
	{
	}

}