/*******************************************************************************
 * 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.data.ui.datasource;

import org.eclipse.birt.report.designer.data.ui.dataset.PropertyPageWrapper;
import org.eclipse.birt.report.designer.data.ui.property.AbstractPropertyDialog;
import org.eclipse.birt.report.designer.data.ui.util.DTPUtil;
import org.eclipse.birt.report.designer.data.ui.util.DataSetProvider;
import org.eclipse.birt.report.designer.data.ui.util.IHelpConstants;
import org.eclipse.birt.report.designer.data.ui.util.Utility;
import org.eclipse.birt.report.designer.internal.ui.util.ExceptionHandler;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.designer.ui.dialogs.properties.IPropertyPage;
import org.eclipse.birt.report.model.api.DataSourceHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.datatools.connectivity.oda.OdaException;
import org.eclipse.datatools.connectivity.oda.design.DataSourceDesign;
import org.eclipse.datatools.connectivity.oda.design.DesignSessionRequest;
import org.eclipse.datatools.connectivity.oda.design.ui.designsession.DataSourceDesignSession;
import org.eclipse.datatools.connectivity.oda.design.ui.designsession.DesignSessionUtil;
import org.eclipse.jface.preference.IPreferencePageContainer;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.PropertyPage;

/**
 * Dialog to edit datasource properties.
 */
public class DataSourceEditor extends AbstractPropertyDialog implements IPreferencePageContainer
{
	private DataSourceDesignSession m_designSession = null;
	private OdaDataSourceHandle dataSourceHandle = null;
	private PropertyPageWrapper propertyPageWrapper = null;

	/**
	 * The constructor.
	 * 
	 * @param parentShell
	 */
	public DataSourceEditor( Shell parentShell, DataSourceHandle ds )
	{
		super( parentShell, ds );
		String dataSourceType = null;
		if ( ds instanceof OdaDataSourceHandle )
		{
			dataSourceHandle = (OdaDataSourceHandle) ds;
			assert dataSourceHandle != null;

			dataSourceType = dataSourceHandle.getExtensionID( );
		}

		if ( DesignSessionUtil.hasValidOdaDesignUIExtension( dataSourceType ) )
		{
			addCustomPageODAV3( dataSourceHandle );
		}
		else
		{
			IConfigurationElement element = DataSetProvider.findDataSourceElement( dataSourceType );

			if ( element != null )
				addCustomPageODAV2( element );
		}

		addPageTo( "/", "org.eclipse.birt.datasource.editor.property", Messages.getString( "datasource.editor.property" ), null, new PropertyBindingPage( ) );//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}
	
	/**
	 * 
	 * @param dataSourceDesign
	 */
	private void addCustomPageODAV3( OdaDataSourceHandle dataSourceHandle )
	{
		try
		{
			DesignSessionRequest designSessionRequest = DTPUtil.getInstance( )
					.createDesignSessionRequest( dataSourceHandle );
			m_designSession = DataSourceDesignSession.startEditDesign( designSessionRequest );
			propertyPageWrapper = new PropertyPageWrapper( m_designSession.getEditorPage( ) );

			DataSourceDesign dataSourceDesign = designSessionRequest.getDataSourceDesign( );
			addPageTo( dataSourceDesign.getLinkedProfileStoreFilePath( ),
					dataSourceDesign.getName( ),
					propertyPageWrapper.getPropertyPage( ).getTitle( ),
					null,
					propertyPageWrapper );//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		}
		catch ( OdaException e )
		{
			ExceptionHandler.handle( e );
		}
	}

	/**
	 * 
	 * @param element
	 */
	private void addCustomPageODAV2( IConfigurationElement element)
	{
		try
		{
			IConfigurationElement[] editorPages = element.getChildren( "dataSourceEditorPage" );//$NON-NLS-1$
			if ( editorPages != null && editorPages.length > 0 )
			{
				if ( editorPages != null )
				{
					for ( int n = 0; n < editorPages.length; n++ )
					{
						IPropertyPage page = (IPropertyPage) editorPages[n].createExecutableExtension( "class" );//$NON-NLS-1$
						addPageTo( editorPages[n].getAttribute( "path" ), editorPages[n].getAttribute( "name" ), editorPages[n].getAttribute( "displayName" ), null, page );//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					}

				}

			}

		}
		catch ( CoreException e )
		{
			ExceptionHandler.handle( e );
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.dialogs.properties.AbstractPropertyDialog#performOk()
	 */
	public boolean performOk( )
	{
		if ( m_designSession != null )
		{
			try
			{
				DTPUtil.getInstance( )
						.updateDataSourceHandle( m_designSession.finish( )
								.getResponse( ),
								dataSourceHandle );
			}
			catch ( OdaException e )
			{
				ExceptionHandler.handle( e );
			}
		}

		return true;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.dialogs.properties.AbstractPropertyDialog#performCancel()
	 */
	public boolean performCancel( )
	{
		if ( m_designSession != null )
		{
			m_designSession.cancel( );
		}

		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.Composite)
	 */
	protected Control createContents( Composite parent )
	{
		String title = Messages.getFormattedString( "datasource.edit", new String[]{( (DataSourceHandle) getModel( ) ).getName( )} );//$NON-NLS-1$
		getShell( ).setText( title );

		Control control = super.createContents( parent );
		Utility.setSystemHelp( control,
				IHelpConstants.CONEXT_ID_DATASOURCE_EDIT );
		
		return control;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.preference.IPreferencePageContainer#getPreferenceStore()
	 */
	public IPreferenceStore getPreferenceStore( )
	{
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.preference.IPreferencePageContainer#updateButtons()
	 */
	public void updateButtons( )
	{
		if ( getOkButton( ) != null )
			getOkButton( ).setEnabled( propertyPageWrapper.getPropertyPage( )
					.isValid( ) );
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.preference.IPreferencePageContainer#updateMessage()
	 */
	public void updateMessage( )
	{
		PropertyPage propertyPage = propertyPageWrapper.getPropertyPage( );
		setMessage( propertyPage.getMessage( ), propertyPage.getMessageType( ) );
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.preference.IPreferencePageContainer#updateTitle()
	 */
	public void updateTitle( )
	{
		// TODO Auto-generated method stub
		
	}
	
}