/*******************************************************************************
 * 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.provider;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.birt.report.designer.internal.ui.dialogs.resource.AddJarResourceFileFolderSelectionDialog;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.model.api.ModuleHandle;
import org.eclipse.birt.report.model.api.ScriptLibHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.activity.NotificationEvent;
import org.eclipse.birt.report.model.api.command.PropertyEvent;
import org.eclipse.birt.report.model.api.elements.structures.ScriptLib;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Table;

/**
 * 
 */
public class JarFileFormProvider implements IFormHandleProvider
{

	/**
	 * 
	 */
	public JarFileFormProvider( )
	{
		// TODO Auto-generated constructor stub
	}

	private static final int[] COLUMN_WIDTHS = new int[]{
		400
	};
	private static final String[] COLUMNS = new String[]{
		Messages.getString( "JarFileFormProvider.Column.Name" ), //$NON-NLS-1$
	};
	private static final String TITLE = Messages.getString( "ReportPageGenerator.List.Resources.JarFile" ); //$NON-NLS-1$
	private static final String EMPTY_STRING = ""; //$NON-NLS-1$

	private ModuleHandle inputElement;

	public String[] getColumnNames( )
	{
		return COLUMNS;
	}

	public int[] getColumnWidths( )
	{
		return COLUMN_WIDTHS;
	}

	public String getTitle( )
	{
		return TITLE;
	}

	public CellEditor[] getEditors( Table table )
	{
		return null;
	}

	public boolean doMoveItem( int oldPos, int newPos ) throws Exception
	{
		inputElement.shiftScriptLibs( oldPos, newPos );
		return true;
	}

	public boolean doDeleteItem( int pos ) throws Exception
	{
		if ( getElements( inputElement ).length <= 0 )
		{
			return false;
		}
		ScriptLibHandle scriptLibHandle = ( (ScriptLibHandle) getElements( inputElement )[pos] );
		if ( ( scriptLibHandle != null )
				&& ( scriptLibHandle.getStructure( ) != null )
				&& ( scriptLibHandle.getStructure( ) instanceof ScriptLib ) )
		{
			ScriptLib scriptLib = (ScriptLib) ( scriptLibHandle.getStructure( ) );
			inputElement.dropScriptLib( scriptLib );
		}

		return true;
	}

	public boolean doAddItem( int pos ) throws Exception
	{

		AddJarResourceFileFolderSelectionDialog dialog = new AddJarResourceFileFolderSelectionDialog( );
		dialog.setExistFiles( getElmentNames( inputElement ) );

		if ( dialog.open( ) != Window.OK )
		{
			return false;
		}
		Object[] selection = dialog.getResult( );
		int length = selection.length;
		for ( int i = 0; i < length; i++ )
		{
			String fileName = dialog.getPath( i );
			ScriptLib lib = StructureFactory.createScriptLib( );
			lib.setName( fileName );
			inputElement.addScriptLib( lib );
		}
		return true;
	}

	public boolean doEditItem( int pos )
	{
		return false;
	}

	public String getColumnText( Object element, int columnIndex )
	{
		if(element instanceof ScriptLibHandle)
		{
			ScriptLibHandle srcriptLibHandle = (ScriptLibHandle) element;
			if ( columnIndex == 0 )
			{
				return srcriptLibHandle.getName( );
			}
		}
		return EMPTY_STRING;
	}

	public String getImagePath( Object element, int columnIndex )
	{
		return null;
	}

	public Object[] getElements( Object inputElement )
	{
		ArrayList list = new ArrayList( );
		if ( inputElement instanceof List )
		{
			inputElement = ( (List) inputElement ).get( 0 );
		}
		if ( inputElement instanceof ModuleHandle )
		{
			this.inputElement = (ModuleHandle) inputElement;
			list = (ArrayList) ( (ModuleHandle) inputElement ).getAllScriptLibs( );
			if ( list == null || list.size( ) == 0 )
			{
				return new ScriptLibHandle[0];
			}
		}

		return list.toArray( );
	}

	private String[] getElmentNames( Object inputElement )
	{
		Object[] obj = getElements( inputElement );
		String[] names = new String[obj.length];
		for ( int i = 0; i < names.length; i++ )
		{
			names[i] = ( (ScriptLibHandle) ( obj[i] ) ).getName( );
		}
		return names;
	}

	public boolean canModify( Object element, String property )
	{
		return false;
	}

	public Object getValue( Object element, String property )
	{
		return null;
	}

	public boolean modify( Object data, String property, Object value )
			throws Exception
	{
		return false;
	}

	public boolean needRefreshed( NotificationEvent event )
	{
		if ( !( event instanceof PropertyEvent ) )
		{
			return false;
		}
		PropertyEvent propertyEvent = (PropertyEvent) event;
		if ( propertyEvent.getPropertyName( )
				.equals( ModuleHandle.SCRIPTLIBS_PROP ) )
		{
			return true;
		}
		return false;
	}

}
