/*******************************************************************************
 * 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.nls.Messages;
import org.eclipse.birt.report.designer.ui.IReportGraphicConstants;
import org.eclipse.birt.report.designer.ui.ReportPlatformUIImages;
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 TogglePropertyDescriptor extends AbstractPropertyDescriptor
{

	Button button;

	protected String defaultValue = "", toggleValue = ""; //$NON-NLS-1$ //$NON-NLS-2$

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

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

		boolean stateFlag = ( ( value == null ) == button.getEnabled( ) );
		if ( stateFlag )
			button.setEnabled( value != null );
		String imageName = getPropertyProcessor( ).getProperty( );
		if ( !button.isEnabled( ) )
			imageName += IReportGraphicConstants.DIS;
		button.setImage( ReportPlatformUIImages.getImage( imageName ) );

		boolean boolValue = toggleValue.equals( value );
		if ( button.getSelection( ) != boolValue )
		{
			button.setSelection( boolValue );
		}
	}

	/**
	 * Sets the supported values.
	 * 
	 * @param defaultValue
	 *            Default value.
	 * @param toggleValue
	 *            When Button is selected, uses toggleValue.
	 */
	public void setAllowedValues( String defaultValue, String toggleValue )
	{
		this.defaultValue = null;
		this.toggleValue = toggleValue;
	}

	/*
	 * (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.TOGGLE );
		button.addSelectionListener( new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				String value = button.getSelection( ) ? toggleValue
						: defaultValue;
				try
				{
					getPropertyProcessor( ).setStringValue( value,
							getModelList( ) );
				}
				catch ( SemanticException e1 )
				{
					WidgetUtil.processError( button.getShell( ), e1 );
				}
			}
		} );
		if (toggleValue.equals("bold"))
			button.setToolTipText(Messages
					.getString("TogglePropertyDescriptor.toolTipText.Bold"));
		if (toggleValue.equals("italic"))
			button.setToolTipText(Messages
					.getString("TogglePropertyDescriptor.toolTipText.Italic"));
		if (toggleValue.equals("underline"))
			button
					.setToolTipText(Messages
							.getString("TogglePropertyDescriptor.toolTipText.Underline"));
		if (toggleValue.equals("line-through"))
			button
					.setToolTipText(Messages
							.getString("TogglePropertyDescriptor.toolTipText.Text_Line_Through"));
		if (toggleValue.equals("bold"))
			button.setToolTipText(Messages
					.getString("TogglePropertyDescriptor.toolTipText.Bold"));
		if (toggleValue.equals("italic"))
			button.setToolTipText(Messages
					.getString("TogglePropertyDescriptor.toolTipText.Italic"));
		if (toggleValue.equals("underline"))
			button
					.setToolTipText(Messages
							.getString("TogglePropertyDescriptor.toolTipText.Underline"));
		if (toggleValue.equals("line-through"))
			button
					.setToolTipText(Messages
							.getString("TogglePropertyDescriptor.toolTipText.Text_Line_Through"));
			
		return button;
	}
}