/*******************************************************************************
 * 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.editors.script;

import org.eclipse.birt.report.designer.internal.ui.editors.script.JSObjectMetaData.JSField;
import org.eclipse.birt.report.model.api.metadata.IClassInfo;
import org.eclipse.birt.report.model.api.metadata.IMemberInfo;

class EngineClassField implements JSField, Comparable
{

	private IMemberInfo member;
	private String displayText;
	private IClassInfo classInfo;

	public EngineClassField( IMemberInfo member, IClassInfo classInfo )
	{
		this.classInfo = classInfo;
		this.member = member;
	}

	public String getName( )
	{
		return member.getDisplayName( );
	}

	public JSObjectMetaData getType( )
	{
		JSObjectMetaData meta = JSSyntaxContext.getEnginJSObject( member
				.getDataType( ) );
		if ( meta == null )
			try
			{
				return JSSyntaxContext.getJavaClassMeta( member.getDataType( ) );
			}
			catch ( ClassNotFoundException e )
			{
				return null;
			}
		return meta;
	}

	public String getDisplayText( )
	{
		if ( displayText == null )
		{
			StringBuffer strbuf = new StringBuffer( getName( ) );
			strbuf.append( " " ); //$NON-NLS-1$
			strbuf.append( classInfo.getName( ) );
			displayText = strbuf.toString( );
			strbuf = null;
		}
		return displayText;
	}

	public String getDescription( )
	{
		return null;
	}

	public int getVisibility( )
	{
		return 0;
	}

	public int compareTo( Object obj )
	{
		if ( obj instanceof EngineClassField
				&& ( (EngineClassField) obj ).getName( ) != null )
		{
			return getName( ).compareToIgnoreCase(
					( (EngineClassField) obj ).getName( ) );
		}
		return 0;
	}
}
