/*******************************************************************************
 * 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 org.eclipse.birt.report.designer.internal.ui.views.attributes.page.WidgetUtil;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.PropertyProcessor;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.birt.report.model.api.ScalarParameterHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.elements.ReportDesignConstants;
import org.eclipse.birt.report.model.api.metadata.IChoiceSet;
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.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;

/**
 * PropertyDescriptor manages label tip & input text control.
 */
public class TextPropertyDescriptor extends AbstractPropertyDescriptor
{

	private static final IChoiceSet DATA_TYPE_CHOICE_SET = DEUtil.getMetaDataDictionary( )
			.getElement( ReportDesignConstants.SCALAR_PARAMETER_ELEMENT )
			.getProperty( ScalarParameterHandle.DATA_TYPE_PROP )
			.getAllowedChoices( );
	private static final IChoiceSet CONTROL_TYPE_CHOICE_SET = DEUtil.getMetaDataDictionary( )
			.getChoiceSet( DesignChoiceConstants.CHOICE_PARAM_CONTROL );

	protected Text text;

	private int style = SWT.BORDER;

	private String deValue;

	/**
	 * constructor
	 * 
	 * @param propertyProcessor
	 *            The model instance
	 */
	public TextPropertyDescriptor( PropertyProcessor propertyProcessor )
	{
		super( propertyProcessor );
	}

	/**
	 * Sets the style of Text control
	 * 
	 * @param style
	 *            The style of Text control to construct
	 */
	public void setStyle( int style )
	{
		this.style = style;
	}

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

		if ( ScalarParameterHandle.DATA_TYPE_PROP.equals( getPropertyProcessor( ).getProperty( ) ) )
		{
			String displayName = DATA_TYPE_CHOICE_SET.findChoice( deValue )
					.getDisplayName( );
			if ( displayName != null )
			{
				deValue = displayName;
			}
		}
		else if ( ScalarParameterHandle.CONTROL_TYPE_PROP.equals( getPropertyProcessor( ).getProperty( ) ) )
		{
			String displayName = CONTROL_TYPE_CHOICE_SET.findChoice( deValue )
					.getDisplayName( );
			if ( displayName != null )
			{
				deValue = displayName;
			}
		}

		if ( getModelList( ).size( ) > 1 )
		{
			text.setText( "" ); //$NON-NLS-1$
			text.setEnabled( false );
			return;
		}

		boolean stateFlag = ( ( deValue == null ) == text.getEnabled( ) );
		if ( stateFlag )
		{
			text.setEnabled( deValue != null );
		}

		if ( getPropertyProcessor( ).isReadOnly( getModelList( ) ) )
		{
			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 text;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.extensions.IPropertyDescriptor#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public Control createControl( Composite parent )
	{
		text = new Text( parent, style );
		text.addSelectionListener( new SelectionAdapter( ) {

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

			public void focusGained( FocusEvent e )
			{

			}

			public void focusLost( FocusEvent e )
			{
				processAction( );
			}
		} );
		return text;
	}

	/**
	 * Processes the save action.
	 */
	private void processAction( )
	{
		String value = text.getText( );
		if ( !value.equals( deValue ) )
		{
			if ( ScalarParameterHandle.CONTROL_TYPE_PROP.equals( getPropertyProcessor( ).getProperty( ) )
					|| ScalarParameterHandle.DATA_TYPE_PROP.equals( getPropertyProcessor( ).getProperty( ) ) )
			{
				return;
			}

			try
			{
				getPropertyProcessor( ).setStringValue( value, getModelList( ) );
			}
			catch ( SemanticException e1 )
			{
				text.setText( deValue );
				WidgetUtil.processError( text.getShell( ), e1 );

			}
		}
	}
}
