/*******************************************************************************
 * 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.ChoiceSetFactory;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.PropertyProcessor;
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.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * SpinnerPropertyDescriptor manages Spinner choice control.
 */
public class SpinnerPropertyDescriptor extends AbstractPropertyDescriptor
{

	protected Spinner spinner;

	protected Combo combo;

	protected Composite container;

	protected IChoiceSet choiceSet;

	/**
	 * @param propertyProcessor
	 *            The property handle
	 */
	public SpinnerPropertyDescriptor( PropertyProcessor propertyProcessor )
	{
		super( propertyProcessor );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.widget.PropertyDescriptor#resetUIData()
	 */
	public void resetUIData( )
	{
		String value = getPropertyProcessor( ).getStringValue( getModelList( ) );

		boolean stateFlag = ( ( value == null ) == spinner.getEnabled( ) );
		if ( stateFlag )
		{
			spinner.setEnabled( value != null );
			combo.setEnabled( value != null );
		}
		if ( value == null )
			return;
		String spinnerValue = getPropertyProcessor( ).getMeasureValue( getModelList( ) );
		double doubleValue = Double.parseDouble( spinnerValue );
		if ( Math.abs( spinner.getSelection( ) - doubleValue ) > 0.01 )
		{
			spinner.setSelection( doubleValue );
		}

		String[] displayNames = null;
		if ( choiceSet == null )
		{
			choiceSet = ChoiceSetFactory.getElementChoiceSet( getPropertyProcessor( ).getElementName( ),
					getPropertyProcessor( ).getProperty( ) );
			displayNames = ChoiceSetFactory.getDisplayNamefromChoiceSet( choiceSet );
			combo.setItems( displayNames );
		}
		if ( displayNames == null )
			displayNames = ChoiceSetFactory.getDisplayNamefromChoiceSet( choiceSet );
		String comboValue = getPropertyProcessor( ).getUnit( getModelList( ) );
		IChoice choice = choiceSet.findChoice( comboValue );
		if ( choice == null )
		{
			combo.deselectAll( );
			return;
		}
		if ( !choice.getDisplayName( ).equals( combo.getText( ) ) )
		{
			combo.deselectAll( );
			combo.setText( choice.getDisplayName( ) );
		}
	}

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

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.extensions.IPropertyDescriptor#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public Control createControl( Composite parent )
	{
		container = new Composite( parent, SWT.NONE );
		FormLayout layout = new FormLayout( );
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.spacing = 0;
		container.setLayout( layout );

		spinner = new Spinner( container, SWT.NONE );
		spinner.setMaximum( 100.00 );
		spinner.setMinimum( -100.00 );
		spinner.setStep( 0.25 );
		spinner.setSelection( 0.00 );
		spinner.setFormatPattern( "0.00" ); //$NON-NLS-1$
		spinner.addValueChangeListener( new IValueChangedListener( ) {

			public void valueChanged( double newValue )
			{
				processAction( );
			}
		} );

		combo = new Combo( container, SWT.BORDER | SWT.READ_ONLY );
		combo.addSelectionListener( new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				processAction( );
			}
		} );
		FormData data = new FormData( );
		data.top = new FormAttachment( spinner, 0, SWT.BOTTOM );
		data.left = new FormAttachment( spinner, 0, SWT.LEFT );
		data.right = new FormAttachment( spinner, 0, SWT.RIGHT );
		combo.setLayoutData( data );
		return spinner;
	}

	private void processAction( )
	{
		String value = spinner.getText( );
		if ( choiceSet != null )
		{
			IChoice choice = choiceSet.findChoiceByDisplayName( combo.getText( ) );
			if ( choice != null )
				value += choice.getName( );
		}
		try
		{
			getPropertyProcessor( ).setStringValue( value, getModelList( ) );
		}
		catch ( SemanticException e )
		{
			WidgetUtil.processError( combo.getShell( ), e );
		}
	}
}