package com.acme.main.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.util.KeyCallback;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.VLayout;


public class Main implements EntryPoint {

    @Override
    public void onModuleLoad() {

        final DataSource dataSource = new DataSource();
        dataSource.setClientOnly(true);
        {// datasource fields initialization
            final DataSourceIntegerField keyField = new DataSourceIntegerField(
                    "id", "ID");
            keyField.setPrimaryKey(true);
            final DataSourceTextField nameField = new DataSourceTextField(
                    "name", "Name");

            dataSource.setFields(keyField, nameField);
        }
        List<Record> listRecord = new ArrayList<Record>();
        for (int i = 0; i < 100; i++) {
            Record rec = new Record();
            rec.setAttribute("id", i);
            rec.setAttribute("name", "row" + i);
            listRecord.add(rec);
        }
        dataSource
                .setTestData(listRecord.toArray(new Record[listRecord.size()]));

        // UI initialization
        final VLayout layout = new VLayout();
        layout.setMembersMargin(5);
        layout.setMargin(20);
        layout.setWidth(400);
        layout.setHeight(400);

        final ListGrid listGrid = new ListGrid();
        listGrid.setWidth100();
        listGrid.setHeight100();
        listGrid.setDataSource(dataSource);
        listGrid.setDrawAllMaxCells(20);
        listGrid.setAutoFetchData(true);

        listGrid.setFixedRecordHeights(false);

        layout.addMember(listGrid);

        final KeyIdentifier debugKey = new KeyIdentifier();
        debugKey.setCtrlKey(true);
        debugKey.setShiftKey(true);
        debugKey.setKeyName("D");

        Page.registerKey(debugKey, new KeyCallback() {
            @Override
            public void execute(final String keyName) {
                SC.showConsole();
            }
        });

        layout.show();
    }
}
