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

import org.eclipse.birt.report.model.api.metadata.IChoice;
import org.eclipse.birt.report.model.metadata.PropertyDefn;
import org.eclipse.birt.report.model.metadata.SystemPropertyDefn;

/**
 * Adapter converting the Model interface of property definition to script
 * interface.
 */

public class PropertyDefinition
{

	/**
	 * The cached property definition
	 */

	private PropertyDefn propDefn;

	/**
	 * Constructs the adapter with the given property definition.
	 * 
	 * @param propDefn
	 *            the property definition
	 */

	public PropertyDefinition( PropertyDefn propDefn )
	{
		this.propDefn = propDefn;
	}

	/**
	 * Returns whether this property is user-defined property.
	 * 
	 * @return <code>true</code> if this property is user-defined.
	 */

	boolean isUserProperty( )
	{
		return propDefn.isUserProperty( );
	}

	/**
	 * Returns the name of this property.
	 * 
	 * @return the name of this property
	 */

	String getName( )
	{
		return propDefn.getName( );
	}

	/**
	 * Returns the group name of this property. If this property doesn't belong
	 * to any group, return empty string.
	 * 
	 * @return the group name of this property
	 */

	String getGroup( )
	{
		if ( propDefn instanceof SystemPropertyDefn )
		{
			String groupName = ( (SystemPropertyDefn) propDefn ).getGroupName( );
			if ( groupName != null )
				return groupName;
		}

		return ""; //$NON-NLS-1$
	}

	/**
	 * Returns whether this property can be inherited. If this property is not
	 * an element property, return <code>false</code>.
	 * 
	 * @return <code>true</code>, if this property is an element property and
	 *         can be inherited.
	 */

	boolean canInherit( )
	{
		if ( propDefn instanceof SystemPropertyDefn )
			return ( (SystemPropertyDefn) propDefn ).canInherit( );

		return false;
	}

	/**
	 * Returns whether this property is style property. If this property is not
	 * an element property, return <code>false</code>.
	 * 
	 * @return <code>true</code>, if this property is an element property and
	 *         style property.
	 */

	public boolean isStyle( )
	{
		if ( propDefn instanceof SystemPropertyDefn )
			return ( (SystemPropertyDefn) propDefn ).isStyleProperty( );

		return false;
	}

	/**
	 * Returns the name of property type. The value is defined in
	 * {@link org.eclipse.birt.report.model.metadata.PropertyType PropertyType}.
	 * 
	 * @return the name of property type
	 */

	public String getType( )
	{
		return propDefn.getType( ).getName( );
	}

	/**
	 * Returns the list of internal choice name, if this property has choices.
	 * 
	 * @return the list of internal choice name.
	 */

	public List getChoices( )
	{
		List choiceNames = new ArrayList( );

		if ( propDefn.getChoices( ) != null )
		{
			IChoice[] choices = propDefn.getChoices( ).getChoices( );
			if ( choices != null )
			{
				for ( int i = 0; i < choices.length; i++ )
					choiceNames.add( choices[i].getName( ) );
			}
			return choiceNames;
		}

		return null;
	}

	/**
	 * Returns the default value of this property.
	 * 
	 * @return the default value of this property
	 */

	public Object getDefaultValue( )
	{
		return propDefn.getDefault( );
	}
}