package ems.gwt.testcase.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.*;
import com.smartgwt.client.data.*;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class TestCase implements EntryPoint {

    private VerticalPanel mainPanel = new VerticalPanel();

    public void onModuleLoad() {

        final Label label = new Label();

        final ListGrid listGrid = new ListGrid();
        listGrid.setWidth(500);
        listGrid.setHeight(300);

        ListGridField lastname = new ListGridField("lastname", "Last Name");
        ListGridField firstname = new ListGridField("firstname","First Name");
        ListGridField mi = new ListGridField("mi", "MI");

        listGrid.setFields(lastname, firstname, mi);
        listGrid.setSortField("lastname");
        listGrid.setGroupStartOpen("all");
        listGrid.setGroupByField("lastname");
        listGrid.setShowRowNumbers(true);

        TestDataSource ds = new TestDataSource();
        listGrid.setDataSource(ds);
        listGrid.fetchData(null, new DSCallback(){
            public void execute(DSResponse response, Object rawData, DSRequest request){
                label.setText("Record Count: " + listGrid.getDataAsRecordList().getLength());
            }
        });
        mainPanel.add(listGrid);
        mainPanel.add(label);

      // Associate the Main panel with the HTML host page.
        RootPanel.get("testDiv").add(mainPanel);

        RootPanel.get("loading").setVisible(false);
    }

    
    public class TestDataSource extends DataSource{
        public TestDataSource(){

            setClientOnly(true);
            DataSourceField field = new DataSourceTextField("lastname");
            addField(field);
            field = new DataSourceTextField("firstname");
            addField(field);
            field = new DataSourceTextField("mi");
            addField(field);

            createRecord("Smith", "John", "A");
            createRecord("Smith", "Dave", "B");
            createRecord("Smith", "Pat", "C");
            createRecord("Jones", "Jim", "D");
            createRecord("Jones", "Mary", "E");
            createRecord("Johnson", "Paul", "F");
            createRecord("Brown", "Ed", "G");
        }

        private void createRecord(String last, String first, String mi){
            ListGridRecord record = new ListGridRecord();
            record.setAttribute("lastname", last);
            record.setAttribute("firstname", first);
            record.setAttribute("mi", mi);
            addData(record);
        }
    }
}
