/*******************************************************************************
 * 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 java.util.Map;

import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

/**
 * Represents the scriptable object for <code>ElementDefinition</code>.
 */

public class NativeElementDefinition extends ScriptableObject
{

	/**
	 * Class name in JavaScript
	 */

	public static final String CLASS_NAME = "ElementDefn"; //$NON-NLS-1$

	/**
	 * The element definition instance.
	 */

	protected ElementDefinition elementDefn;

	/**
	 * The zero-argument constructor used by Rhino runtime to create instances
	 */

	public NativeElementDefinition( )
	{}

	/**
	 * JavaScript function that is the constructor .
	 * 
	 * @param elementDefn
	 *            an ElementDefinition instance
	 */

	public void jsConstructor( Object elementDefn )
	{
		assert elementDefn instanceof ElementDefinition;
		assert elementDefn != null;

		this.elementDefn = (ElementDefinition) elementDefn;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mozilla.javascript.Scriptable#get(java.lang.String,
	 *      org.mozilla.javascript.Scriptable)
	 */

    public Object get( String name, Scriptable start )
	{
		if ( "name".equals( name ) )          //$NON-NLS-1$
			return this.elementDefn.getName( );
		if ( "extendsElement".equals( name ) )        //$NON-NLS-1$
			return this.elementDefn.getExtendsElement( );
		if ( "propertyDefns".equals( name ) )     //$NON-NLS-1$
			return this.elementDefn.getPropertyDefns( );
		if ( "slotDefns".equals( name ) )         //$NON-NLS-1$
			return this.elementDefn.getSlotDefns( );
		if ( "properties".equals( name ) )         //$NON-NLS-1$
			return this.elementDefn.getProperties( );
		if ( "slots".equals( name ) )      //$NON-NLS-1$
			return "unsupported";            //$NON-NLS-1$
		if ( "allowsUserProperties".equals( name ) ) //$NON-NLS-1$
			return Boolean.valueOf( this.elementDefn.allowsUserProperties( ) );
		if ( "hasStyle".equals( name ) )      //$NON-NLS-1$
			return Boolean.valueOf( this.elementDefn.hasStyle( ) );
		if ( "container".equals( name ) )         //$NON-NLS-1$
			return this.elementDefn.getContainer( );
		if ( "containerSlot".equals( name ) ) //$NON-NLS-1$
			return this.elementDefn.getContainerSlot( );

		if ( elementDefn != null )
		{
			Map map = elementDefn.getPropertyDefns( );
			if ( map != null && map.containsKey( name ) )
			{
				return elementDefn.getProperty( name );
			}
		}

        return super.get( name, start );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mozilla.javascript.Scriptable#getClassName()
	 */
	public String getClassName( )
	{
		return CLASS_NAME;
	}
}