

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.grid.ListGridRecord;


// CountryRecord class
public class MainEntryPointComboSample implements EntryPoint {

  public void onModuleLoad() {
    DynamicForm df = new DynamicForm();
    final ComboBoxItem comboBoxItem = new ComboBoxItem("countryName", "Country Name");
    comboBoxItem.setOptionDataSource(CountryLocalDS.getInstance());
    df.setFields(comboBoxItem);
    
    IButton testButton = new IButton("Test");
    testButton.addClickHandler(new ClickHandler() {

      public void onClick(ClickEvent event) {
        Window.alert("Selected record: " + comboBoxItem.getSelectedRecord());
      }
    });
    df.draw();
    testButton.setLeft(200);
    testButton.draw();
  }
}
class CountryRecord extends ListGridRecord {

  public CountryRecord() {
  }

  public CountryRecord(int countryId, String countryCode, String countryName, int categoryId) {
    setCountryCode(countryCode);
    setCountryName(countryName);
    setCountryId(countryId);
    setCategoryId(categoryId);
  }

  public void setCountryId(int countryId) {
    setAttribute("countryId", countryId);
  }

  public int getCountryId(int countryId) {
    return getAttributeAsInt("countryId");
  }

  public void setCountryName(String countryName) {
    setAttribute("countryName", countryName);
  }

  public String getCountryName() {
    return getAttributeAsString("countryName");
  }

  public void setCountryCode(String countryCode) {
    setAttribute("countryCode", countryCode);
  }

  public String getCountryCode() {
    return getAttributeAsString("countryCode");
  }

  public void setCategoryId(int categoryId) {
    setAttribute("categoryId", categoryId);
  }

  public int getCategoryId(int categoryId) {
    return getAttributeAsInt("categoryId");
  }

}

// CountryData class
class CountryData {

  private static CountryRecord[] records;

  public static CountryRecord[] getRecords() {
    if (records == null) {
      records = getNewRecords();
    }
    return records;
  }

  public static CountryRecord[] getNewRecords() {
    return new CountryRecord[]{
              new CountryRecord(1, "United States", "US", 1),
              new CountryRecord(2, "China", "CH", 1)
            };
  }
}

class CountryLocalDS extends DataSource {

    private static CountryLocalDS instance = null;

    public static CountryLocalDS getInstance() {
        if (instance == null) {
            instance = new CountryLocalDS("CountryLocalDS");
        }
        return instance;
    }

    public CountryLocalDS(String id) {

        setID(id);
        DataSourceIntegerField idField = new DataSourceIntegerField("countryId");
        idField.setHidden(true);
        idField.setPrimaryKey(true);

        DataSourceTextField countryNameField = new DataSourceTextField("countryName", "Country Name");
        DataSourceTextField countryCodeField = new DataSourceTextField("countryCode", "Country Code");
        DataSourceTextField categoryIdField = new DataSourceTextField("categoryId", "Category");
        categoryIdField.setForeignKey("CategoryLocalDS.categoryId");
        categoryIdField.setCanEdit(true);

        setFields(idField, countryNameField, countryCodeField, categoryIdField);

        setClientOnly(true);
        setTestData(CountryData.getRecords());
        
    }
}

