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

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;

import org.eclipse.birt.chart.reportitem.ui.dialogs.widget.BooleanPropertyDescriptor;
import org.eclipse.birt.chart.reportitem.ui.dialogs.widget.ChoicePropertyDescriptor;
import org.eclipse.birt.chart.reportitem.ui.dialogs.widget.PropertyDescriptorAdapter;
import org.eclipse.birt.chart.reportitem.ui.dialogs.widget.TextPropertyDescriptor;
import org.eclipse.birt.chart.reportitem.ui.dialogs.widget.UnitPropertyDescriptor;
import org.eclipse.birt.chart.reportitem.ui.i18n.Messages;
import org.eclipse.birt.report.designer.internal.ui.views.attributes.page.WidgetUtil;
import org.eclipse.birt.report.designer.ui.extensions.PropertyTabUIAdapter;
import org.eclipse.birt.report.designer.ui.views.attributes.AttributesUtil;
import org.eclipse.birt.report.designer.ui.views.attributes.IPropertyDescriptor;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.PropertyProcessor;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.ReportItemHandle;
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.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * ChartGeneralTabUIImpl
 */
public class ChartGeneralTabUIImpl extends PropertyTabUIAdapter
{

	private Object page;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.extensions.IPropertyTabUI#buildUI(org.eclipse.swt.widgets.Composite)
	 */
	public void buildUI( Composite composite )
	{
		page = AttributesUtil.buildGeneralPage( composite, new String[]{
				null,
				// null,
				AttributesUtil.MARGIN,
				AttributesUtil.ALT,
				AttributesUtil.BORDER,
				AttributesUtil.SECTION,
				AttributesUtil.VISIBILITY,
				AttributesUtil.TOC,
				AttributesUtil.BOOKMARK,
				AttributesUtil.USERPROPERTIES,
				AttributesUtil.NAMEDEXPRESSIONS,
				AttributesUtil.EVENTHANDLER,

		}, new String[]{
			GeneralCategoryWrapper.CATEGORY_NAME,
		// TypesCategoryWrapper.CATEGORY_NAME
				},
				new AttributesUtil.PageWrapper[]{
					new GeneralCategoryWrapper( ),
				// new TypesCategoryWrapper( )
				} );

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.extensions.IPropertyTabUI#getTabDisplayName()
	 */
	public String getTabDisplayName( )
	{
		return AttributesUtil.getGeneralPageDisplayName( );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.extensions.IPropertyTabUI#setInput(java.util.List)
	 */
	public void setInput( List elements )
	{
		AttributesUtil.setPageInput( page, elements );
	}

	/**
	 * Base wrapper class to provide convenient methods for constructing UI
	 * components.
	 */
	abstract static class AbstractCategoryWrapper extends
			AttributesUtil.PageWrapper
	{

		final static int MIN_TEXT_WIDTH = 80;

		final static int SPACING = 7;

		final static int MARGIN_HEIGHT = 7;

		final static int MARGIN_WIDTH = 7;

		static Label createGridPlaceholder( Composite parent, int hSpan,
				boolean grabSpace )
		{
			Label label = new Label( parent, SWT.NONE );
			GridData data = new GridData( );
			data.horizontalSpan = hSpan;
			data.grabExcessHorizontalSpace = grabSpace;
			label.setLayoutData( data );
			return label;
		}

		static Label createHorizontalLine( Composite parent, int hSpan )
		{
			Label label = new Label( parent, SWT.SEPARATOR | SWT.HORIZONTAL );
			GridData data = new GridData( );
			data.horizontalSpan = hSpan;
			data.horizontalAlignment = GridData.FILL;
			label.setLayoutData( data );
			return label;
		}

		static GridLayout createGridLayout( int columns )
		{
			GridLayout layout = new GridLayout( columns, false );
			layout.marginHeight = MARGIN_HEIGHT;
			layout.marginWidth = MARGIN_WIDTH;
			layout.horizontalSpacing = SPACING;
			layout.verticalSpacing = SPACING;
			return layout;
		}
		
		static GridLayout createSpaceGridLayout( int columns, int space )
		{
			GridLayout layout = new GridLayout( columns, false );
			layout.marginHeight = space;
			layout.marginWidth = space;
			layout.horizontalSpacing = space;
			layout.verticalSpacing = space;
			return layout;
		}

		static Control buildGridControl( Composite parent, Map propertiesMap,
				String elementName, String property, int hSpan,
				boolean grabSpace, IPropertyDescriptor descriptor,
				boolean hasLabel )
		{
			propertiesMap.put( property, descriptor );

			if ( hasLabel )
			{
				Label label = new Label( parent, SWT.NONE );
				label.setText( getDisplayName( descriptor ) );
				if ( label.getText( ).length( ) == 0
						&& descriptor instanceof PropertyDescriptorAdapter )
				{
					( (PropertyDescriptorAdapter) descriptor ).setDiplayNameLabel( label );
				}
			}

			Control control = descriptor.createControl( parent );
			GridData data = new GridData( );
			data.horizontalSpan = hSpan;
			data.grabExcessHorizontalSpace = grabSpace;
			if ( control instanceof Text )
			{
				data.widthHint = MIN_TEXT_WIDTH;
			}
			data.horizontalAlignment = GridData.FILL;
			control.setLayoutData( data );
			return control;
		}

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

	}

	/**
	 * GeneralCategoryWrapper
	 */
	static class GeneralCategoryWrapper extends AbstractCategoryWrapper
	{

		static String CATEGORY_NAME = Messages.getString( "ChartGeneralTabUIImpl.category.general" ); //$NON-NLS-1$
		private Text pathText;
		private Composite contextComposite;

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.birt.report.designer.ui.views.attributes.AttributesUtil.PageWrapper#buildContent(org.eclipse.swt.widgets.Composite,
		 *      java.util.Map)
		 */

		public void setInput( List elements )
		{
			super.setInput( elements );
			if ( contextComposite != null )
			{
				if ( DEUtil.getGroupElementHandle( elements )
						.isExtendedElements( ) )
				{
					DesignElementHandle handle = (DesignElementHandle) DEUtil.getGroupElementHandle( elements )
							.getElements( )
							.get( 0 );
					try
					{
						File libraryFile = new File( new URL( handle.getExtends( )
								.getRoot( )
								.getFileName( ) ).getFile( ) );
						if ( libraryFile.exists( ) )
							pathText.setText( libraryFile.getAbsolutePath( ) );
					}
					catch ( MalformedURLException e )
					{
						e.printStackTrace( );
					}
					contextComposite.setVisible( true );
					GridData data = new GridData( GridData.FILL_HORIZONTAL );
					data.horizontalSpan = 5;
					contextComposite.setLayoutData( data );
				}
				else
				{
					contextComposite.setVisible( false );
					GridData data = (GridData) contextComposite.getLayoutData( );
					data.heightHint = 0;
					contextComposite.setLayoutData( data );
				}
			}
		}

		public void buildContent( Composite parent, Map propertyMap )
		{
			parent.setLayout( createSpaceGridLayout( 1,0 ) );

			contextComposite = new Composite( parent, 5 );
			GridLayout gridLayout = createGridLayout( 5 ) ;
			gridLayout.marginHeight = 0;
			gridLayout.marginTop = WidgetUtil.MARGIN_HEIGHT;
			contextComposite.setLayout( gridLayout );

			GridData gridData = new GridData( GridData.FILL_HORIZONTAL );
			gridData.horizontalSpan = 5;
			contextComposite.setLayoutData( gridData );

			Label pathLabel = new Label( contextComposite, SWT.NONE );
			pathLabel.setText( Messages.getString( "GeneralPage.Library.Included" ) ); //$NON-NLS-1$
			gridData = new GridData( );
			gridData.horizontalSpan = 1;
			pathLabel.setLayoutData( gridData );

			pathText = new Text( contextComposite, SWT.BORDER | SWT.READ_ONLY );
			gridData = new GridData( );
			gridData.horizontalSpan = 2;
			pathText.setLayoutData( gridData );

			createGridPlaceholder( contextComposite, 2, true );

			createHorizontalLine( contextComposite, 5 );
			
			Composite composite = new Composite(parent,SWT.NONE);
			GridData data = new GridData( GridData.FILL_HORIZONTAL );
			data.horizontalSpan = 1;
			data.grabExcessHorizontalSpace = true;
			composite.setLayoutData( data );
			composite.setLayout( createGridLayout( 5 ) );
			
			buildGridControl( composite,
					propertyMap,
					ReportDesignConstants.EXTENDED_ITEM,
					ReportItemHandle.NAME_PROP,
					1,
					true,
					new TextPropertyDescriptor( new PropertyProcessor( ReportDesignConstants.EXTENDED_ITEM,
							ReportItemHandle.NAME_PROP ) ),
					true );

			createHorizontalLine( composite, 5 );

			buildGridControl( composite,
					propertyMap,
					ReportDesignConstants.EXTENDED_ITEM,
					ReportItemHandle.WIDTH_PROP,
					1,
					true,
					new UnitPropertyDescriptor( new PropertyProcessor( ReportDesignConstants.EXTENDED_ITEM,
							ReportItemHandle.WIDTH_PROP ) ),
					true );

			buildGridControl( composite,
					propertyMap,
					ReportDesignConstants.EXTENDED_ITEM,
					ReportItemHandle.HEIGHT_PROP,
					1,
					true,
					new UnitPropertyDescriptor( new PropertyProcessor( ReportDesignConstants.EXTENDED_ITEM,
							ReportItemHandle.HEIGHT_PROP ) ),
					true );

			createGridPlaceholder( composite, 1, true );

			buildGridControl( composite,
					propertyMap,
					ReportDesignConstants.EXTENDED_ITEM,
					ReportItemHandle.STYLE_PROP,
					1,
					true,
					new ChoicePropertyDescriptor( new PropertyProcessor( ReportDesignConstants.EXTENDED_ITEM,
							ReportItemHandle.STYLE_PROP ) ),
					true );

			createGridPlaceholder( composite, 3, true );
		}
	}

	/**
	 * TypesCategoryWrapper
	 */
	static class TypesCategoryWrapper extends AbstractCategoryWrapper
	{

		static String CATEGORY_NAME = Messages.getString( "ChartGeneralTabUIImpl.category.types" ); //$NON-NLS-1$

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.birt.report.designer.ui.views.attributes.AttributesUtil.PageWrapper#buildContent(org.eclipse.swt.widgets.Composite,
		 *      java.util.Map)
		 */
		public void buildContent( Composite parent, Map propertyMap )
		{
			parent.setLayout( createGridLayout( 3 ) );

			buildGridControl( parent,
					propertyMap,
					ReportDesignConstants.EXTENDED_ITEM,
					"chart.dimension", //$NON-NLS-1$
					1,
					false,
					new ChoicePropertyDescriptor( new PropertyProcessor( ReportDesignConstants.EXTENDED_ITEM,
							"chart.dimension" ) ), true ); //$NON-NLS-1$

			createHorizontalLine( parent, 3 );

			buildGridControl( parent,
					propertyMap,
					ReportDesignConstants.EXTENDED_ITEM,
					"plot.transposed", //$NON-NLS-1$
					2,
					false,
					new BooleanPropertyDescriptor( new PropertyProcessor( ReportDesignConstants.EXTENDED_ITEM,
							"plot.transposed" ) ), false ); //$NON-NLS-1$

		}

	}

}
