/*******************************************************************************
 * 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.chart.reportitem.ui.dialogs.widget;

import org.eclipse.birt.report.designer.ui.views.attributes.providers.PropertyProcessor;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

public class BooleanPropertyDescriptor extends PropertyDescriptorAdapter
{

	protected Button button;

	/**
	 * @param propertyProcessor
	 *            the property handle
	 */
	public BooleanPropertyDescriptor( 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( ) );

		if ( button.getText( ).length( ) == 0 )
		{
			button.setText( getPropertyProcessor( ).getDisplayName( getModelList( ) ) );
		}
		
		boolean stateFlag = ( ( value == null ) == button.getEnabled( ) );
		if ( stateFlag )
			button.setEnabled( value != null );

		boolean boolValue = "true".equalsIgnoreCase( value ); //$NON-NLS-1$
		if ( button.getSelection( ) != boolValue )
		{
			button.setSelection( boolValue );
		}
	}

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

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.extensions.IPropertyDescriptor#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public Control createControl( Composite parent )
	{
		button = new Button( parent, SWT.CHECK );
		button.setText( getDisplayName( ) );
		button.addSelectionListener( new SelectionAdapter( ) {

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

	private String getDisplayName( )
	{
		String name = null;
		PropertyProcessor processor = getPropertyProcessor( );
		if ( processor != null )
			name = processor.getDisplayName( );
		return name == null ? "" : name;//$NON-NLS-1$

	}

	/**
	 * Processes the save action.
	 */
	private void processAction( )
	{
		String value = button.getSelection( ) ? "true" : "false"; //$NON-NLS-1$ //$NON-NLS-2$
		try
		{
			getPropertyProcessor( ).setStringValue( value, getModelList( ) );
		}
		catch ( SemanticException e )
		{
			handleError( e );
		}
	}
}