package com.smartgwt.sample.client;


import com.smartgwt.client.data.Criterion;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.form.FilterBuilder;
import com.smartgwt.client.widgets.layout.VLayout;


public class FilterBuilderCanvas extends VLayout
{

	//ensures a unique dynamically generated identifier
	private static int dynamicIdSuffix = 0;

	private FilterBuilder filterBuilder;

	private Label errorLabel;

	public FilterBuilderCanvas()
	{
		//dynamically create a unique id to support Selenium testing
	//	StringBuilder id = new StringBuilder( "FilterBuilderCanvasDyn" ).append( ++dynamicIdSuffix );
	//	setID( id.toString() );

		setAlign( Alignment.LEFT );
		setMargin( 10 );
		setWidth100();
		setHeight( "50%" );
		setOverflow( Overflow.AUTO );

		errorLabel = new Label();
		errorLabel.setWidth100();
		errorLabel.setHeight( 15 );
		addMember( errorLabel );

		filterBuilder = new FilterBuilder();
		filterBuilder
				.setDataSource( new MyDataSource( "fbDataSource" ) );
		filterBuilder.setWidth100();
		filterBuilder.setTopOperatorOptions( OperatorId.AND, OperatorId.OR );
		addMember( filterBuilder );
	}

	/**
	 * Forces validation of the FilterBuilder
	 * 
	 * @return true if passes validation, false if not
	
	public boolean validate()
	{

		boolean isValid = filterBuilder.validate();
		if ((isValid == true)
				&& (validateAdvancedCriteria(filterBuilder.getCriteria()) == false)) {
			setValidationMessage("Contains one or more clauses with an empty operand.");
			isValid = false;
		} else {
			clearValidationMessage();
		}

		return isValid;
	}
 */
	/**
	 * The FilterBuilder component doesn't return the criteria for clauses that have an empty operand, and returns
	 * list of criteria with length 0 when there aren't any complete criteria.  It would be preferable if the 
	 * FilterBuilder would show the clause as failing validation in these cases, but I have not found a way
	 * to do this.  Therefore, we are going to recursively walk the Criterion and verify that all the clauses are
	 * complete.
	 * 
	 * @param crit the criteria to check for completeness.
	 * @return true if the Criterion are complete.
	 */
	/*
	private boolean validateAdvancedCriteria(Criterion crit)
	{
		boolean result = true;
		//System.out.println( "crit is: " + JSON.encode( crit.getJsObj() ) );

		if ( crit.getAttributeAsJavaScriptObject( "criteria" ) != null )
		{
			Criterion criterion[] = crit.getCriteria();
			if ( criterion.length != 0 )
			{
				for ( Criterion criteria : criterion )
				{
					//System.out.println( "criteria: " + JSON.encode( criteria.getJsObj() ) );
					if ( validateAdvancedCriteria( criteria ) == false )
					{
						result = false;
						break;
					}
				}
			}
			else
			{
				//System.out.println( "Empty criteria!  Invalid!!!!" );
				result = false;
			}
		}
		else
		{
			//System.out.println( "no sub clause found" );
		}

		return result;
	}


	public FilterBuilder getFilterBuilder()
	{
		return filterBuilder;
	}

	
	public void setValidationMessage(String message)
	{
		errorLabel.setIcon( "[SKIN]actions/exclamation.png" );
		errorLabel.setContents( message );
	}
	
	public void clearValidationMessage()
	{
		errorLabel.setIcon( null );
		errorLabel.setContents( "" );
	}
*/
}

