package com.hp.isomorphic.client.issues;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.FilterBuilder;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.events.ClickEvent;  
import com.smartgwt.client.widgets.events.ClickHandler;  

public class FilterBuilderOmitsEmptyFieldCritria extends VLayout {

	private FilterBuilder filterBuilder;

	public FilterBuilderOmitsEmptyFieldCritria () {

		DataSourceField dsStatusField = new DataSourceField("job_status", FieldType.TEXT, "Text Type");
		DataSourceField dsJob_dueDate = new DataSourceField("job_dueDate", FieldType.DATETIME, "Date Type");
		DataSourceField dsInteger = new DataSourceField("job_Integer", FieldType.INTEGER, "Integer Type");

		DataSourceField[] dsFields = new DataSourceField[] { dsStatusField, dsJob_dueDate, dsInteger };
		DataSource ds = new PMTableDataSource("jobs", "job", dsFields);

		filterBuilder = new FilterBuilder();
		filterBuilder.setID("filterBuilderID");
		filterBuilder.setWidth("100%"); //$NON-NLS-1$
		filterBuilder.setHeight(20);
		filterBuilder.setDataSource(ds);
		
		final TextAreaItem criteriaDisplay = new TextAreaItem(); 
		criteriaDisplay.setTitle("Criteria JSON");  
		criteriaDisplay.setWidth(400);  
		criteriaDisplay.setCanEdit(false);  
		
		IButton showCriteriaButton = new IButton();
		showCriteriaButton.setTitle("Display Criteria");
		showCriteriaButton.addClickHandler(new ClickHandler() {  
            @Override  
            public void onClick(ClickEvent event) {  
            	criteriaDisplay.clearValue();
            	criteriaDisplay.setValue(filterBuilder.getCriteria(true).toJSON());
            }  
        });
		
		IButton clearCriteriaButton = new IButton();
		clearCriteriaButton.setTitle("Clear Criteria");
		clearCriteriaButton.addClickHandler(new ClickHandler() {  
            @Override  
            public void onClick(ClickEvent event) {  
            	criteriaDisplay.clearValue();
            }  
        });

		DynamicForm form = new DynamicForm();  
		form.setID("order");  
		form.setFields(criteriaDisplay);

		addMember(showCriteriaButton);
		addMember(clearCriteriaButton);
		addMember(form);
		addMember(filterBuilder);
		
	}
}
