package com.test.client;

import java.util.LinkedHashMap;

import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.types.ExportDisplay;
import com.smartgwt.client.types.ExportFormat;
import com.smartgwt.client.types.FetchMode;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;


public class FrmMainSearch extends VLayout {
	protected VLayout main = new VLayout();
	@SuppressWarnings("unchecked")
	public FrmMainSearch(){

		setWidth100();
    	setHeight100();
    	setOverflow(Overflow.AUTO);
    	main.setWidth100();
    	main.setHeight100();

    	DataSource customDS = DataSource.get("customDataSource_user");  
        
        final ListGrid userList = new ListGrid(); 
        ListGridField job = new ListGridField("job");  
        job.setEscapeHTML(true);
        
        userList.setWidth(600);  
        userList.setHeight(224);  
        userList.setDataSource(customDS);  
        userList.setCanEdit(true);  
        userList.setCanRemoveRecords(true);  
        userList.setLeaveScrollbarGap(false);  
        userList.setDataFetchMode(FetchMode.LOCAL);  
        userList.setAutoFetchData(true);  
        userList.setFields(  
                new ListGridField("userName"),  
                job,  
                new ListGridField("email"),  
                new ListGridField("employeeType"),  
                new ListGridField("salary")  
                );   
  
        final DynamicForm exportForm = new DynamicForm();  
        exportForm.setWidth(300);  
  
        SelectItem exportTypeItem = new SelectItem("exportType", "Export Type");  
        exportTypeItem.setWidth("*");  
        exportTypeItem.setDefaultToFirstOption(true);  
  
        @SuppressWarnings("rawtypes")
		LinkedHashMap valueMap = new LinkedHashMap();  
        valueMap.put("csv", "CSV (Excel)");  
        valueMap.put("xml", "XML");  
        valueMap.put("xls", "XLS (Excel97)");  
        valueMap.put("ooxml", "XLSX (Excel2007/OOXML)");  
  
        exportTypeItem.setValueMap(valueMap);  
        exportForm.setItems(exportTypeItem);  
  
        IButton exportButton = new IButton("Export");  
        exportButton.addClickHandler(new ClickHandler() {  
            public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {  
                String exportAs = (String) exportForm.getField("exportType").getValue();  
                   // exportAs is either XML or CSV, which we can do with requestProperties  
                    DSRequest dsRequestProperties = new DSRequest();  
                    dsRequestProperties.setExportAs((ExportFormat)com.smartgwt.client.util.EnumUtil.getEnum(ExportFormat.values(), exportAs));  
                    dsRequestProperties.setExportDisplay(ExportDisplay.DOWNLOAD);  
                    userList.exportData(dsRequestProperties);  
            }  
        });  
  
      
        HLayout formLayout = new HLayout();  
        formLayout.setHeight(20);
        formLayout.addMember(exportForm);  
        formLayout.addMember(exportButton);  
        main.addMember(formLayout);  
        main.addMember(userList);  
    	
    	
       	addMember(main);
	}
	

	
}

