/*******************************************************************************
 * Copyright (c) 2004, 2005 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.dataset;

import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.data.engine.api.DataEngine;
import org.eclipse.birt.data.engine.api.DataEngineContext;
import org.eclipse.birt.data.engine.api.IBaseDataSetDesign;
import org.eclipse.birt.data.engine.api.IBaseDataSourceDesign;
import org.eclipse.birt.report.designer.data.ui.property.AbstractDescriptionPropertyPage;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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;

/**
 * Cache preference set page. If the checkbox is selected, there are two optinal
 * ways to define the size of cache. User can assign the size of the cache or
 * select the entire data set as cache size. The selection will work on the data
 * set selection.
 */
public class CachePreferencePage extends AbstractDescriptionPropertyPage
{
	private int rowCount = 0;

	private transient PropertyHandle rowCountHandle = null;
	private transient IBaseDataSetDesign dataSetDesign;
	private transient IBaseDataSourceDesign dataSourceDesign;

	private static String DEFAULT_MESSAGE = Messages.getString( "dataset.editor.settings" );
	
	/**
	 * create Contents
	 */
	public Control createContents( Composite parent )
	{
		Composite composite = new Composite( parent, SWT.NONE );

		GridLayout layout = new GridLayout( );
		layout.numColumns = 5;
		composite.setLayout( layout );
		composite.setLayoutData( new GridData( GridData.HORIZONTAL_ALIGN_FILL
				| GridData.VERTICAL_ALIGN_FILL ) );

		Label label = new Label( composite, SWT.NONE );
		label.setText( Messages.getString( "SettingsPage.label.text" ) );

		GridData data = new GridData( );
		data.horizontalSpan = 2;
		Label lab1 = new Label( composite, SWT.NONE );
		lab1.setLayoutData( data );
		data = new GridData( );
		data.horizontalSpan = 3;
		
		data = new GridData( );
		data.widthHint = 50;
		
		
		final Text numText = new Text( composite, SWT.BORDER );
		numText.setLayoutData( data );
		numText.setData( rowCountHandle );

		numText.addModifyListener( new ModifyListener( ) {

			public void modifyText( ModifyEvent e )
			{
				getContainer( ).setMessage( DEFAULT_MESSAGE,
						IMessageProvider.NONE );
				setRowCount( numText.getText( ) );
			}
		} );

		initCachePreference( );
		numText.setText( Integer.toString( rowCount ) );
		return composite;
	}
	
	/**
	 * set row count
	 * @param count
	 */
	private void setRowCount( String count )
	{
		try
		{
			rowCount = Integer.parseInt( count );
		}
		catch ( NumberFormatException ex )
		{
			getContainer( ).setMessage( Messages.getString( "dataset.editor.settings.errorNumberFormat" ),
					IMessageProvider.ERROR );
			rowCount = -1;
		}
		if ( rowCount < 0 )
		{
			getContainer( ).setMessage( Messages.getString( "dataset.editor.settings.errorNumberFormat" ),
					IMessageProvider.ERROR );
		}
	}

	/**
	 * get the rowCount
	 * 
	 */
	private void initCachePreference( )
	{
		rowCount = ( (DataSetHandle) getContainer( ).getModel( ) ).getRowFetchLimit( );
		rowCountHandle = ( (DataSetHandle) getContainer( ).getModel( ) ).getPropertyHandle( DataSetHandle.ROW_FETCH_LIMIT_PROP );
	}

	/**
	 * clear the cache
	 *
	 */
	public boolean clearCache( )
	{
		try
		{
			DataEngine dataEngine = DataEngine.newDataEngine( DataEngineContext.newInstance( DataEngineContext.DIRECT_PRESENTATION,
					null,
					null,
					null ) );
			dataEngine.clearCache( this.dataSourceDesign, this.dataSetDesign );
		}
		catch ( BirtException e )
		{
			// TODO:
			e.printStackTrace( );
			return true;
		}
		return true;
	}
	
	/**
	 * activate the preference page
	 */
	public void pageActivated( )
	{
		getContainer( ).setMessage( DEFAULT_MESSAGE, IMessageProvider.NONE );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.dialogs.properties.AbstractPropertyPage#performCancel()
	 */
	public boolean performCancel( )
	{
		// selectorImage.dispose( );
		return super.performCancel( );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.dialogs.properties.AbstractPropertyPage#canLeave()
	 */
	public boolean canLeave( )
	{
		if ( canFinish( ) )
			return super.performOk( );
		else
			return false;
	}

	/**
	 * whether the page can be finished or leave.
	 */
	public boolean canFinish( )
	{
		if ( rowCount < 0 )
		{
			getContainer( ).setMessage( Messages.getString( "dataset.editor.settings.errorNumberFormat" ),
					IMessageProvider.ERROR );
			return false;
		}
		if ( rowCountHandle != null )
		{
			try
			{
				this.rowCountHandle.clearValue( );
				this.rowCountHandle.setIntValue( rowCount );
			}
			catch ( SemanticException e )
			{
				e.printStackTrace( );
			}
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.dialogs.properties.AbstractPropertyPage#performOk()
	 */
	public boolean performOk( )
	{
		if ( canFinish( ) )
		{
			return super.performOk( );
		}
		else
			return false;
	}
	
	/**
	 * set dataSetDesign and dataSourceDesign
	 * 
	 * @param baseDataSetDesign
	 * @param baseDataSourceDesign
	 */
	public void setBaseDesign( IBaseDataSetDesign baseDataSetDesign,
			IBaseDataSourceDesign baseDataSourceDesign )
	{
		this.dataSetDesign = baseDataSetDesign;
		this.dataSourceDesign = baseDataSourceDesign;
	}
} // @jve:decl-index=0:visual-constraint="351,164"
