/* Copyright (c) 2004 Actuate Corporation and others.
 * 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 implementation.
 ************************************************************************************/
package org.eclipse.birt.report.designer.internal.ui.editors.script;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.birt.report.model.api.metadata.IClassInfo;
import org.eclipse.birt.report.model.api.metadata.IMemberInfo;
import org.eclipse.birt.report.model.api.metadata.IMethodInfo;

class EngineClassJSObject implements JSObjectMetaData
{

	private JSMethod[] noMthods = new JSMethod[0];
	private JSField[] noFields = new JSField[0];

	private IClassInfo classInfo;
	private boolean showPublic;

	public EngineClassJSObject( IClassInfo classInfo )
	{
		this.classInfo = classInfo;
		// if type is DataSet or DataSource
		// show all methods and fields
		showPublic = "DataSet".equals( classInfo.getName( ) ) //$NON-NLS-1$
				|| "DataSource".equals( classInfo.getName( ) ); //$NON-NLS-1$
	}

	public String getName( )
	{
		if ( classInfo == null )
			return ""; //$NON-NLS-1$
		return classInfo.getName( );
	}

	// FIXME cache it.
	public JSMethod[] getMethods( )
	{
		if ( classInfo == null )
			return noMthods;
		List methods = classInfo.getMethods( );
		List all = new ArrayList( );
		for ( Iterator iter = methods.iterator( ); iter.hasNext( ); )
		{
			IMethodInfo method = (IMethodInfo) iter.next( );
			if ( showPublic || method.isStatic( ) )
				all.add( new EngineClassMethod( method, classInfo ) );
		}
		Collections.sort( all );
		return (JSMethod[]) all.toArray( new JSMethod[all.size( )] );
	}

	public JSField[] getFields( )
	{
		if ( classInfo == null )
			return noFields;
		List members = classInfo.getMembers( );
		List all = new ArrayList( );
		for ( Iterator iter = members.iterator( ); iter.hasNext( ); )
		{
			IMemberInfo member = (IMemberInfo) iter.next( );
			if ( showPublic || member.isStatic( ) )
				all.add( new EngineClassField( member, classInfo ) );
		}
		Collections.sort( all );
		return (JSField[]) all.toArray( new JSField[all.size( )] );
	}

	public String getDescription( )
	{
		return null;
	}

	public int getVisibility( )
	{
		return 0;
	}
}
