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

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.ui.views.attributes.providers.ChoiceSetFactory;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.PropertyProcessor;
import org.eclipse.birt.report.designer.util.AlphabeticallyComparator;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.metadata.IChoice;
import org.eclipse.birt.report.model.api.metadata.IChoiceSet;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * ComboPropertyDescriptor manages Combo choice control.
 */
public class ComboPropertyDescriptor extends AbstractPropertyDescriptor
{

	protected Combo combo;

	protected IChoiceSet choiceSet;

	protected String oldValue;

	/**
	 * @param propertyHandle
	 *            the property handle
	 */
	public ComboPropertyDescriptor( PropertyProcessor propertyProcessor )
	{
		super( propertyProcessor );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.attributes.widget.PropertyDescriptor#resetUIData()
	 */
	public void resetUIData( )
	{
		if ( choiceSet == null )
		{
			choiceSet = ChoiceSetFactory.getElementChoiceSet( getPropertyProcessor( ).getElementName( ),
					getPropertyProcessor( ).getProperty( ) );
			String[] displayNames = ChoiceSetFactory.getDisplayNamefromChoiceSet( choiceSet,
					new AlphabeticallyComparator( ) );
			if ( getPropertyProcessor( ).getProperty( )
					.equals( StyleHandle.FONT_FAMILY_PROP ) )
			{
				String[] sysFont = DEUtil.getSystemFontNames( );
				String[] items = new String[displayNames.length
						+ sysFont.length
						+ 1];
				items[0] = ChoiceSetFactory.CHOICE_AUTO;
				System.arraycopy( displayNames,
						0,
						items,
						1,
						displayNames.length );
				System.arraycopy( sysFont,
						0,
						items,
						displayNames.length + 1,
						sysFont.length );
				for ( int i = 0; i < items.length; i++ )
				{
					UIUtil.RemoveQuote( items[i] );
				}
				combo.setItems( items );
			}
			else if ( StyleHandle.TEXT_ALIGN_PROP.equals( getPropertyProcessor( ).getProperty( ) )
					|| StyleHandle.VERTICAL_ALIGN_PROP.equals( getPropertyProcessor( ).getProperty( ) ) )
			{
				String[] items = new String[displayNames.length + 1];
				items[0] = ChoiceSetFactory.CHOICE_AUTO;

				System.arraycopy( displayNames,
						0,
						items,
						1,
						displayNames.length );

				combo.setItems( items );
			}
			else
			{
				combo.setItems( displayNames );
			}
		}

		
		oldValue = getPropertyProcessor( ).getStringValue( getModelList( ) );
		
		if ( getPropertyProcessor( ).getProperty( )
				.equals( StyleHandle.FONT_FAMILY_PROP ) )
		{
			oldValue = UIUtil.RemoveQuote( oldValue );
		}
		
		
		boolean stateFlag = ( ( oldValue == null ) == combo.getEnabled( ) );
		if ( stateFlag )
			combo.setEnabled( oldValue != null );

		if ( getPropertyProcessor( ).isReadOnly( getModelList( ) ) )
		{
			combo.setEnabled( false );
		}

		IChoice choice = null;
		if ( choiceSet != null )
			choice = choiceSet.findChoice( oldValue );
		if ( choice == null )
		{
			combo.deselectAll( );
			if ( oldValue != null )
			{
				combo.setText( oldValue );
			}
			return;
		}
		oldValue = choice.getDisplayName( );
		combo.select( Arrays.asList( combo.getItems( ) ).indexOf( oldValue ) );
	}

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

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.extensions.IPropertyDescriptor#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public Control createControl( Composite parent )
	{
		combo = new Combo( parent, SWT.BORDER | SWT.READ_ONLY );
		combo.addControlListener( new ControlListener( ) {

			public void controlMoved( ControlEvent e )
			{
				combo.clearSelection( );
			}

			public void controlResized( ControlEvent e )
			{
				combo.clearSelection( );
			}
		} );
		combo.addSelectionListener( new SelectionListener( ) {

			public void widgetSelected( SelectionEvent e )
			{
				processAction( );
			}

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

			public void focusGained( FocusEvent e )
			{

			}

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

	/**
	 * Processes the save action.
	 */
	private void processAction( )
	{
		String newValue = combo.getText( );
		if ( "".equals( newValue ) )//$NON-NLS-1$
		{
			newValue = null;
		}
		String pName = getPropertyProcessor( ).getProperty( );
		if ( ChoiceSetFactory.CHOICE_AUTO.equals( newValue )
				&& ( StyleHandle.FONT_FAMILY_PROP.equals( pName )
						|| StyleHandle.TEXT_ALIGN_PROP.equals( pName ) || StyleHandle.VERTICAL_ALIGN_PROP.equals( pName ) ) )
		{
			newValue = null;
		}
		if ( ChoiceSetFactory.CHOICE_NONE.equals( newValue )
				&& ReportDesignHandle.INCLUDE_RESOURCE_PROP.equals( pName ) )
		{
			newValue = null;
		}

		try
		{
			if ( StyleHandle.FONT_FAMILY_PROP.equals( pName ) 
				&&needAddQuote(newValue))
			{

				getPropertyProcessor( ).setStringValue( UIUtil.AddQuote( newValue ),
						getModelList( ) );
			}
			else
			{
				getPropertyProcessor( ).setStringValue( newValue,
						getModelList( ) );
			}

		}
		catch ( SemanticException e )
		{
			combo.setText( oldValue );
			WidgetUtil.processError( combo.getShell( ), e );
		}

	}
	
	private boolean needAddQuote(String value)
	{
		IChoiceSet choice = ChoiceSetFactory.getElementChoiceSet( getPropertyProcessor( ).getElementName( ),
				getPropertyProcessor( ).getProperty( ) );
		String[] displayNames = ChoiceSetFactory.getDisplayNamefromChoiceSet( choice,
				new AlphabeticallyComparator( ) );
		for(int i = 0; i < displayNames.length; i ++)
		{
			if(displayNames[i].equals( value ))
			{
				return false;
			}
		}
		return true;
	}
}