
package foo;

import java.util.Date;

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.DataSourceField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;

public class TableWithDateAndSelectFilters implements EntryPoint {

    private static final String[] VALUE_MAP = { "A", "B", "C" };

    public void onModuleLoad() {
        VLayout main = new VLayout();
        main.setWidth100();
        main.setHeight100();
        main.addMember(getTableWithFilters());
        main.draw();
    }

    private ListGrid getTableWithFilters() {
        ListGrid listGrid = new ListGrid();
        listGrid.setWidth100();
        listGrid.setHeight100();
        listGrid.setShowFilterEditor(true);

        ListGridField dateField = new ListGridField("date", "Date", 70);
        dateField.setFilterEditorType(new DateItem());
        dateField.setFilterOperator(OperatorId.EQUALS);

        ListGridField selectField = new ListGridField("select", "Select", 100);
        selectField.setValueMap(VALUE_MAP);

        listGrid.setFields(dateField, selectField);

        listGrid.setDataSource(new TestDataSource());
        listGrid.setAutoFetchData(true);

        return listGrid;
    }

    private class TestDataSource extends DataSource {

        private static final int NUM_ROWS = 100;

        TestDataSource() {
            setDataProtocol(DSProtocol.CLIENTCUSTOM);
            setDataFormat(DSDataFormat.CUSTOM);
            setClientOnly(false);
            addField(new DataSourceField("date", FieldType.DATE, "Date", 50, true));
            addField(new DataSourceField("select", FieldType.ENUM, "Select", 50, true));
            getField("select").setValueMap(VALUE_MAP);
        }

        @Override
        protected Object transformRequest(DSRequest request) {
            String requestId = request.getRequestId();
            DSResponse response = new DSResponse();
            response.setAttribute("clientContext", request.getAttributeAsObject("clientContext"));
            response.setStatus(0);
            if (request.getOperationType() == DSOperationType.FETCH) {
                response.setTotalRows(NUM_ROWS);
                ListGridRecord[] records = new ListGridRecord[NUM_ROWS];
                for (int i = 0; i < NUM_ROWS; i++) {
                    ListGridRecord record = new ListGridRecord();
                    record.setAttribute("date", new Date());
                    record.setAttribute("select", "A");
                    records[i] = record;
                }
                response.setData(records);
            }
            processResponse(requestId, response);
            return request.getData();
        }
    }
}
