package com.gwt.components.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceFloatField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

/**
 * This application demonstrates gwt chooser component
 */
public class Example implements EntryPoint {

  /**
   * This method constructs the application user interface by instantiating
   * controls and hooking up event handler.
   */
  public void onModuleLoad() {
    forum();
  }
  
  class DynamicLoadDataSource extends DataSource {
	  
	  public boolean change;
	  
	  public DynamicLoadDataSource() {
		  setID("dynamicLoadDataSource");
		  setDataProtocol (DSProtocol.CLIENTCUSTOM);
		  setDataFormat (DSDataFormat.CUSTOM);
		  setClientOnly (false);
		  
		  DataSourceIntegerField pkField = new DataSourceIntegerField("itemID");  
	      pkField.setHidden(true);  
	      pkField.setPrimaryKey(true);  
		  addField(pkField);
	      
	      DataSourceTextField itemNameField = new DataSourceTextField("itemName", "Item", 128, true);  
		  DataSourceTextField skuField = new DataSourceTextField("SKU", "SKU", 10, true);  
		  addField(itemNameField);
		  addField(skuField);
		  
	      DataSourceTextField descriptionField = new DataSourceTextField("description", "Description", 2000);  
		  DataSourceTextField categoryField = new DataSourceTextField("category", "Category", 128, true);  
		  addField(descriptionField);
		  addField(categoryField);
		  
	      DataSourceTextField unitsField = new DataSourceTextField("units", "Units", 5);
	      addField(unitsField);
		  
	      DataSourceFloatField unitCostField = new DataSourceFloatField("unitCost", "Unit Cost", 5);
	      addField(unitCostField);
	  }
	  
	  protected Object transformRequest (DSRequest request) {
	        System.out.println("Example.DynamicLoadDataSource.transformRequest()");
		    String requestId = request.getRequestId ();
	        DSResponse response = new DSResponse ();
	        response.setAttribute ("clientContext", request.getAttributeAsObject ("clientContext"));
	        // Asume success
	        response.setStatus (0);
	        switch (request.getOperationType ()) {
	            case FETCH:
	                executeFetch (requestId, request, response);
	                break;
	            case ADD:
	                //executeAdd (requestId, request, response);
	                break;
	            case UPDATE:
	                //executeUpdate (requestId, request, response);
	                break;
	            case REMOVE:
	                //executeRemove (requestId, request, response);
	                break;
	            default:
	                // Operation not implemented.
	                break;
	        }
	        return request.getData ();
	    }
	  
	  protected void executeFetch (final String requestId, final DSRequest request, final DSResponse response) {
		  System.out.println("Example.DynamicLoadDataSource.executeFetch()");
		  if (change) {
			  response.setData(getRecords(15));
		  } else {
			  response.setData(getRecords(10));
		  }
		  processResponse (requestId, response);
	  }
  }
  
  ListGridRecord[] getRecords(int  j) { 
	  ListGridRecord[] result = new ListGridRecord[j];
	  ListGridRecord item = null;
	  for(int i = 0; i < j; i++) {
		  item = new ListGridRecord();
		  item.setAttribute("itemID", new Integer(i));
		  item.setAttribute("itemName", "item name " + i);
		  item.setAttribute("description", "description " + i);
		  item.setAttribute("category", "category " + i);
		  item.setAttribute("units", "unit " + i);
		  item.setAttribute("unitCost", new Float(i));
		  result[i] = item;
	  }
	  return result;
  }
  
  
  
  private void forum() {
	  final DynamicLoadDataSource supplyItemDS = new DynamicLoadDataSource();
	  //supplyItemDS.setUseTestDataFetch(true);
	  //supplyItemDS.setCacheAllData(true);
	  //supplyItemDS.setAutoCacheAllData(true);
	  //supplyItemDS.setTestData(getGridRecords(20));
	  
      
      ListGridField itemField = new ListGridField("itemName");  
      ListGridField unitsField = new ListGridField("description");  
      ListGridField unitCostField = new ListGridField("units");  

      final ComboBoxItem item = new ComboBoxItem("itemID");  
      item.setWidth(240);  
      item.setTitle("Item");  
      item.setValueField("itemName");  
      item.setDisplayField("description");  
      item.setPickListWidth(450);  
      item.setPickListFields(itemField, unitsField, unitCostField);
      item.setOptionDataSource(supplyItemDS);
      //item.setAutoFetchData(false);
      
      DynamicForm f  = new DynamicForm();
      ButtonItem b = new ButtonItem();
      b.addClickHandler(new com.smartgwt.client.widgets.form.fields.events.ClickHandler() {
		public void onClick(
				com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {
			supplyItemDS.fetchData();
			supplyItemDS.change = true;
		}
      });
      
      f.setFields(b, item);
      f.draw();  
 }
}
