/*******************************************************************************
 * Copyright (c) 2005 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.engine.script.internal.element;

import org.eclipse.birt.report.engine.api.script.ScriptException;
import org.eclipse.birt.report.engine.api.script.element.IHighLightRule;
import org.eclipse.birt.report.model.api.HighlightRuleHandle;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportItemHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.structures.HighlightRule;
import org.eclipse.birt.report.model.elements.Style;

/**
 * Implements of HighLightRule.
 * 
 */

public class HighLightRuleImpl implements IHighLightRule
{

	private HighlightRuleHandle rule;

	private ReportItemHandle handle;

	/**
	 * Constructor
	 * 
	 * @param rule
	 * @param handle
	 */

	public HighLightRuleImpl( HighlightRuleHandle rule, ReportItemHandle handle )
	{
		this.handle = handle;
		this.rule = rule;
	}

	public String getColor( )
	{
		return rule.getColor( ).getStringValue( );
	}

	public String getDateTimeFormat( )
	{
		return rule.getDateTimeFormat( );
	}

	public String getFontStyle( )
	{

		return rule.getFontStyle( );
	}

	public String getFontWeight( )
	{
		return rule.getFontWeight( );
	}

	public String getStringFormat( )
	{

		return rule.getStringFormat( );
	}

	public String getTestExpression( )
	{
		return rule.getTestExpression( );
	}

	public void setColor( String color ) throws ScriptException
	{
		checkHandle( );
		try
		{
			rule.getColor( ).setStringValue( color );
		}
		catch ( SemanticException e )
		{
			throw new ScriptException( e.getLocalizedMessage( ) );
		}
	}

	public void setDateTimeFormat( String format ) throws ScriptException
	{
		checkHandle( );
		rule.setDateTimeFormat( format );
	}

	public void setFontStyle( String style ) throws ScriptException
	{
		checkHandle( );
		try
		{
			rule.setFontStyle( style );
		}
		catch ( SemanticException e )
		{
			throw new ScriptException( e.getLocalizedMessage( ) );
		}
	}

	public void setFontWeight( String weight ) throws ScriptException
	{
		checkHandle( );
		try
		{
			rule.setFontWeight( weight );
		}
		catch ( SemanticException e )
		{
			throw new ScriptException( e.getLocalizedMessage( ) );
		}
	}

	public void setStringFormat( String format ) throws ScriptException
	{
		checkHandle( );
		rule.setStringFormat( format );
	}

	public void setTestExpression( String expression ) throws ScriptException
	{
		checkHandle( );
		rule.setTestExpression( expression );
	}

	private void checkHandle( ) throws ScriptException
	{
		if ( rule != null )
			return;
		
		HighlightRule r = new HighlightRule( );

		PropertyHandle propHandle = handle.getPropertyHandle(
				Style.HIGHLIGHT_RULES_PROP );
		try
		{
			rule = (HighlightRuleHandle) propHandle.addItem( r );
		}
		catch ( SemanticException e )
		{
			throw new ScriptException( e.getLocalizedMessage( ) );
		}

	}

}
