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

import java.util.Set;

import org.eclipse.birt.report.designer.internal.ui.views.attributes.widget.PropertyDescriptorFactory;
import org.eclipse.birt.report.designer.ui.views.attributes.IPropertyDescriptor;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.AttributeConstant;
import org.eclipse.birt.report.model.api.ReportItemHandle;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.TextItemHandle;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.elements.ReportDesignConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

/**
 * The general attribute page of Text element.
 */
public class TextPage extends GeneralPage
{

	/**
	 * The PropertyDescriptor for <code>TextItem.CONTENT_TYPE_PROP</code>
	 * property.
	 */
	private IPropertyDescriptor formatDescriptor;

	/**
	 * @param parent
	 *            A widget which will be the parent of the new instance (cannot
	 *            be null)
	 * @param style
	 *            The style of widget to construct
	 */
	public TextPage( Composite parent, int style )
	{
		super( parent, style );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.attributes.page.CategoryTabPage#buildUI()
	 */
//	protected void buildUI( )
//	{
//		setLayout( WidgetUtil.createGridLayout( 5 ) );
//
//		WidgetUtil.buildGridControl( this,
//				propertiesMap,
//				ReportDesignConstants.TEXT_ITEM,
//				ReportItemHandle.NAME_PROP,
//				1,
//				false );
//		
//		WidgetUtil.createGridPlaceholder( this,3, true );
//		WidgetUtil.createHorizontalLine(this,5);
//		
//		WidgetUtil.buildGridControl( this,
//				propertiesMap,
//				ReportDesignConstants.TEXT_ITEM,
//				TextItemHandle.CONTENT_TYPE_PROP,
//				1,
//				false );
//		formatDescriptor = (IPropertyDescriptor) propertiesMap.get( TextItemHandle.CONTENT_TYPE_PROP );
//
//		WidgetUtil.buildGridControl( this,
//				propertiesMap,
//				ReportDesignConstants.REPORT_ITEM,
//				ReportItemHandle.STYLE_PROP,
//				1,
//				false );
//		
//		WidgetUtil.createGridPlaceholder( this,1, true );
//		WidgetUtil.createHorizontalLine( this, 5);
//
//		WidgetUtil.buildGridControl( this,
//				propertiesMap,
//				ReportDesignConstants.STYLE_ELEMENT,
//				StyleHandle.FONT_FAMILY_PROP,
//				1,
//				false );
//
//		WidgetUtil.buildGridControl( this,
//				propertiesMap,
//				ReportDesignConstants.STYLE_ELEMENT,
//				StyleHandle.FONT_SIZE_PROP,
//				1,
//				false );
//		
//		WidgetUtil.createGridPlaceholder( this,1, true );
//		WidgetUtil.buildGridControl( this,
//				propertiesMap,
//				ReportDesignConstants.STYLE_ELEMENT,
//				StyleHandle.COLOR_PROP,
//				1,
//				false );
//
//		WidgetUtil.createGridPlaceholder( this, 3, true );
//
//		WidgetUtil.buildGridControl( this,
//				propertiesMap,
//				ReportDesignConstants.STYLE_ELEMENT,
//				StyleHandle.BACKGROUND_COLOR_PROP,
//				1,
//				false );
//		
//		WidgetUtil.createGridPlaceholder( this, 3, true );
//		
//		WidgetUtil.createGridPlaceholder( this, 1, false );
//		Composite fontStylePane = WidgetUtil.buildFontStyleUI( this,
//				propertiesMap );
//		GridData data = new GridData( );
//		data.horizontalSpan = 3;
//		fontStylePane.setLayoutData( data );
//
//		int height = fontStylePane.computeSize( SWT.DEFAULT, SWT.DEFAULT ).y;
//		Label separator = new Label( fontStylePane, SWT.SEPARATOR
//				| SWT.VERTICAL
//				| SWT.CENTER );
//		int width = separator.computeSize( SWT.DEFAULT, SWT.DEFAULT ).x
//				+ WidgetUtil.SPACING
//				* 2;
//		separator.setLayoutData( new RowData( width, height ) );
//
//		IPropertyDescriptor descriptor = PropertyDescriptorFactory.getPropertyDescriptor( ReportDesignConstants.STYLE_ELEMENT,
//				AttributeConstant.TEXT_ALIGN );
//		propertiesMap.put( StyleHandle.TEXT_ALIGN_PROP, descriptor );
//		descriptor.createControl( fontStylePane );
//
//
//
//	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.page.AttributePage#refreshValues(java.util.Set)
	 */
	protected void refreshValues( Set propertiesSet )
	{
		super.refreshValues( propertiesSet );
		String textFormat = formatDescriptor.getPropertyProcessor( )
				.getStringValue( input );
		if ( DesignChoiceConstants.TEXT_CONTENT_TYPE_PLAIN.equals( textFormat ) )
		{
			setFormatEnable( true );
		}
		else
		{
			setFormatEnable( false );
		}
	}

	/**
	 * Sets enable status for format-relative controls.
	 * 
	 * @param enable
	 *            The boolean value for enable status
	 */
	private void setFormatEnable( boolean enable )
	{
		//ctlDataSet.setEnabled( !enable );
	}
	
	protected void buildUI( )
	{
		setContentLayout( WidgetUtil.createGridLayout( 5 ) );	
		super.buildUI( );	
	}
	


	void buildContent( Composite content )
	{
		
		WidgetUtil.buildGridControl( content,
				propertiesMap,
				ReportDesignConstants.TEXT_ITEM,
				ReportItemHandle.NAME_PROP,
				1,
				false );
		
		WidgetUtil.createGridPlaceholder( content,3, true );
		WidgetUtil.createHorizontalLine(content,5);
		
		WidgetUtil.buildGridControl( content,
				propertiesMap,
				ReportDesignConstants.TEXT_ITEM,
				TextItemHandle.CONTENT_TYPE_PROP,
				1,
				false );
		formatDescriptor = (IPropertyDescriptor) propertiesMap.get( TextItemHandle.CONTENT_TYPE_PROP );

		WidgetUtil.buildGridControl( content,
				propertiesMap,
				ReportDesignConstants.REPORT_ITEM,
				ReportItemHandle.STYLE_PROP,
				1,
				false );
		
		WidgetUtil.createGridPlaceholder( content,1, true );
		WidgetUtil.createHorizontalLine( content, 5);

		WidgetUtil.buildGridControl( content,
				propertiesMap,
				ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.FONT_FAMILY_PROP,
				1,
				false );

		WidgetUtil.buildGridControl( content,
				propertiesMap,
				ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.FONT_SIZE_PROP,
				1,
				false );
		
		WidgetUtil.createGridPlaceholder( content,1, true );
		WidgetUtil.buildGridControl( content,
				propertiesMap,
				ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.COLOR_PROP,
				1,
				false );

		WidgetUtil.createGridPlaceholder( content, 3, true );

		WidgetUtil.buildGridControl( content,
				propertiesMap,
				ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BACKGROUND_COLOR_PROP,
				1,
				false );
		
		WidgetUtil.createGridPlaceholder( content, 3, true );
		
		WidgetUtil.createGridPlaceholder( content, 1, false );
		Composite fontStylePane = WidgetUtil.buildFontStyleUI( content,
				propertiesMap );
		GridData data = new GridData( );
		data.horizontalSpan = 3;
		fontStylePane.setLayoutData( data );

		int height = fontStylePane.computeSize( SWT.DEFAULT, SWT.DEFAULT ).y;
		Label separator = new Label( fontStylePane, SWT.SEPARATOR
				| SWT.VERTICAL
				| SWT.CENTER );
		int width = separator.computeSize( SWT.DEFAULT, SWT.DEFAULT ).x
				+ WidgetUtil.SPACING
				* 2;
		separator.setLayoutData( new RowData( width, height ) );

		IPropertyDescriptor descriptor = PropertyDescriptorFactory.getPropertyDescriptor( ReportDesignConstants.STYLE_ELEMENT,
				AttributeConstant.TEXT_ALIGN );
		propertiesMap.put( StyleHandle.TEXT_ALIGN_PROP, descriptor );
		descriptor.createControl( fontStylePane );

	}
}