/*******************************************************************************
 * 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.util.ColorUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

/**
 * ColorPropertyDescriptor manages Color choice control.
 */
public class ColorPropertyDescriptor extends AbstractPropertyDescriptor
{

	private ColorBuilder builder;

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

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

		boolean stateFlag = ( ( strValue == null ) == builder.getEnabled( ) );
		if ( stateFlag )
			builder.setEnabled( strValue != null );
		builder.setColorValue(strValue);
	}

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

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.extensions.IPropertyDescriptor#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public Control createControl( Composite parent )
	{
		builder = new ColorBuilder( parent, SWT.NONE );
		String name = getPropertyProcessor( ).getElementName( );
		String propertyName = getPropertyProcessor( ).getProperty( );
		builder.setChoiceSet( ChoiceSetFactory.getElementChoiceSet( name,
				propertyName ) );
		builder.addListener( SWT.Modify, new Listener( ) {

			public void handleEvent( Event event )
			{
				processAction( );
			}
		} );
		return builder;
	}

	/**
	 * Processes the save action.
	 */
	private void processAction( )
	{
		int oldValue = ColorUtil.parseColor( getPropertyProcessor( )
				.getStringValue( getModelList( ) ) );
		ColorUtil.getRGBs( oldValue );

		RGB rgb = builder.getRGB( );

		int colorValue = -1;
		if ( rgb != null )
			colorValue = ColorUtil.formRGB( rgb.red, rgb.green, rgb.blue );

		if ( oldValue == colorValue )
			return;

		String value = builder.getPredefinedColor();
		if ( value == null && rgb != null )
			value = ColorUtil.format( colorValue, ColorUtil.INT_FORMAT );
		try
		{
			getPropertyProcessor( ).setStringValue( value, getModelList( ) );
		}
		catch ( SemanticException e )
		{
			WidgetUtil.processError( builder.getShell( ), e );
		}
		if ( rgb == null )
		{
			String colorString = getPropertyProcessor( ).getStringValue(
					getModelList( ) );
			builder.setColorValue(colorString);
		}
	}
}