/*******************************************************************************
 * 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.widget;

import java.util.List;

import org.eclipse.birt.report.designer.internal.ui.util.UIUtil;
import org.eclipse.birt.report.designer.internal.ui.views.attributes.page.WidgetUtil;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.designer.ui.dialogs.ExpressionBuilder;
import org.eclipse.birt.report.designer.ui.dialogs.ExpressionProvider;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.PropertyProcessor;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Control;
import org.eclipse.swt.widgets.Text;

/**
 * Property Descriptor for value expression property.
 */

public class ExpressionPropertyDescriptor extends AbstractPropertyDescriptor
{

	protected Text text;

	protected Button button;

	private Composite containerPane;

	private String deValue;

	private String newValue;

	/**
	 * The constructor.
	 */
	public ExpressionPropertyDescriptor( )
	{
		super( );
	}

	/**
	 * The constructor.
	 * 
	 * @param processor
	 */
	public ExpressionPropertyDescriptor( PropertyProcessor processor )
	{
		super( processor );
	}

	/**
	 * After selection changed, re-sets UI data.
	 */
	public void resetUIData( )
	{
		deValue = getPropertyProcessor( ).getStringValue( getModelList( ) );

		List lst = getModelList( );

		if ( lst == null )
		{
			button.setEnabled( false );
			text.setEnabled( false );
		}

		if ( deValue == null )
		{
			deValue = ""; //$NON-NLS-1$
		}
		if ( !text.getText( ).equals( deValue ) )
		{
			text.setText( deValue );
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.widget.PropertyDescriptor#getControl()
	 */
	public Control getControl( )
	{
		return containerPane;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.extensions.IPropertyDescriptor#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public Control createControl( Composite parent )
	{
		containerPane = new Composite( parent, SWT.NONE );
		containerPane.setLayout( new GridLayout( ) );

		text = new Text( containerPane, SWT.MULTI
				| SWT.WRAP
				| SWT.BORDER
				| SWT.H_SCROLL
				| SWT.V_SCROLL );
		text.setLayoutData( new GridData( GridData.FILL_BOTH ) );
		text.addSelectionListener( new SelectionAdapter( ) {

			public void widgetDefaultSelected( SelectionEvent e )
			{
				newValue = text.getText( );
				processAction( );
			}
		} );
		text.addFocusListener( new FocusListener( ) {

			public void focusGained( FocusEvent e )
			{

			}

			public void focusLost( FocusEvent e )
			{
				newValue = text.getText( );
				processAction( );
			}
		} );

		button = new Button( containerPane, SWT.PUSH );
		button.setText( Messages.getString( "ExpressionPropertyDescriptor.text.Edit" ) ); //$NON-NLS-1$
		button.addSelectionListener( new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				ExpressionBuilder builder = new ExpressionBuilder( button.getShell( ),
						deValue );

				// TODO find shared datasets between the model list.
				List lst = getModelList( );
				if ( lst != null && lst.size( ) == 1 )
				{
					DesignElementHandle elementHandle = (DesignElementHandle) lst.get( 0 );
					builder.setExpressionProvier( new ExpressionProvider( elementHandle ) );
				}
				if ( builder.open( ) == Window.OK )
				{
					newValue = builder.getResult( );
					processAction( );
				}
			}
		} );

		return containerPane;
	}

	/**
	 * Processes the save action.
	 */
	private void processAction( )
	{
		String value = newValue;
		if ( value != null && value.length( ) == 0 )
		{
			value = null;
		}

		if ( ( value == null && deValue != null )
				|| ( value != null && !value.equals( deValue ) ) )
		{
			try
			{
				getPropertyProcessor( ).setStringValue( value, getModelList( ) );
				text.setText( UIUtil.convertToGUIString( value ) );
			}
			catch ( SemanticException e1 )
			{
				text.setText( UIUtil.convertToGUIString( deValue ) );
				WidgetUtil.processError( text.getShell( ), e1 );

			}
		}
	}

}