package com.sample.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.types.TextMatchStyle;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.grid.events.EditorEnterEvent;
import com.smartgwt.client.widgets.grid.events.EditorEnterHandler;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Sample implements EntryPoint {

	public void onModuleLoad() {
		ItemSupplyXmlDS employeesDS = ItemSupplyXmlDS.getInstance();  
		  
        final TreeGrid treeGrid = new TreeGrid();  
        treeGrid.setCanEdit(true);  
        treeGrid.setLoadDataOnDemand(false);  
        treeGrid.setWidth100();
        treeGrid.setHeight100();  
        treeGrid.setDataSource(employeesDS);  
        treeGrid.setNodeIcon("icons/16/person.png");  
        treeGrid.setFolderIcon("icons/16/person.png");  
        treeGrid.setShowOpenIcons(false);  
        treeGrid.setShowFilterEditor(true);
        treeGrid.setShowDropIcons(false);  
        treeGrid.setClosedIconSuffix("");  
        treeGrid.setAutoFetchData(true);  
        treeGrid.setShowSelectedIcons(true);  
        
        treeGrid.addEditorEnterHandler(new EditorEnterHandler() {
			
			@Override
			public void onEditorEnter(EditorEnterEvent event) {
				FormItem editFormItem = treeGrid.getEditFormItem(treeGrid.getFieldName(event.getColNum()));
				SC.logWarn(editFormItem.getClassName());
			}
		});
        
        TreeGridField nameField = new TreeGridField("Name");
        TreeGridField jobField = new TreeGridField("Job");  
    	ComboBoxItem itemName = new ComboBoxItem();  
    	itemName.setPickListWidth(250);  
    	itemName.setDisplayField("Phone");// In my use case, I have key value pair based response. This is the name of value field in the response text
    	itemName.setValueField("Phone11");// In my use case, I have key value pair based response. This is the name of key field in the response text
    	itemName.setSortField("Phone");
    	itemName.setAlwaysFetchMissingValues(true);
    	itemName.setAddUnknownValues(false);
    	itemName.setRejectInvalidValueOnChange(true);
    	itemName.setTextMatchStyle(TextMatchStyle.SUBSTRING);
    	itemName.setOptionDataSource(ItemSupplyXmlDS2.getInstance());
    	jobField.setFilterEditorProperties(itemName);
    	jobField.setEditorProperties(itemName);
    	
    	// If i don't set below two properties, it shows value field data instead of display field data upon editing and selecting from dropdown.
    	// If i set below properties, it throws exception when i mouse over the "Title" column' header.
    	
    	jobField.setDisplayField("Phone"); 
    	jobField.setValueField("Phone11");
        treeGrid.setFields(nameField, jobField);  
        treeGrid.draw();  
	}
}
