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

import org.eclipse.birt.core.script.IJavascriptWrapper;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.elements.structures.ConfigVariable;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

/**
 * The wrap factory for Java objects which are defined by Model. Call
 * <code>Context.setWrapFactory(WrapFactory)</code> to set this wrap factory
 * to context.
 */

public class ModelJavaScriptWrapper implements IJavascriptWrapper
{

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.core.script.IJavascriptWrapper#wrap(org.mozilla.javascript.Context,
	 *      org.mozilla.javascript.Scriptable, java.lang.Object,
	 *      java.lang.Class)
	 */

	public Object wrap( Context cx, Scriptable scope, Object javaObject,
			Class staticType )
	{
        if( null == javaObject )
            return null;
            
		String className = javaObject.getClass( ).getName( );
		if ( Color.class.getName( ).equals( className ) )
		{
			return cx.newObject( scope, NativeColor.CLASS_NAME,
					new Object[]{javaObject} );
		}
		else if ( ConfigVariable.class.getName( ).equals( className ) )
		{
			return cx.newObject( scope, NativeConfigVariable.CLASS_NAME,
					new Object[]{javaObject} );
		}
		else if ( Dimension.class.getName( ).equals( className ) )
		{
			return cx.newObject( scope, NativeDimension.CLASS_NAME,
					new Object[]{javaObject} );
		}
		if ( ElementDefinition.class.getName( ).equals( className ) )
		{
			return cx.newObject( scope, NativeElementDefinition.CLASS_NAME,
					new Object[]{javaObject} );
		}
		else if ( PropertyDefinition.class.getName( ).equals( className ) )
		{
			return cx.newObject( scope, NativePropertyDefinition.CLASS_NAME,
					new Object[]{javaObject} );
		}
		else if ( ReportDefinition.class.getName( ).equals( className ) )
		{
			return cx.newObject( scope, NativeReportDefinition.CLASS_NAME,
					new Object[]{javaObject} );
		}
		else if ( Report.class.getName( ).equals( className ) )
		{
			return cx.newObject( scope, NativeReport.CLASS_NAME,
					new Object[]{javaObject} );
		}
		else if ( ReportDesignHandle.class.getName( ).equals( className ) )
		{
			return new Report( (ReportDesignHandle) javaObject );
		}

		return javaObject;
	}
}