package com.utilinc.planner.gwt.web.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.HLayout;

public class TestModule implements EntryPoint
{

    public static int frozenColumns = 5;
    public static int columnCount   = 30;

    public void onModuleLoad()
    {
        Canvas p = getMainPanel();
        p.draw();
    }

    public Canvas getMainPanel()
    {
        final ListGrid grid = new ListGrid();

        grid.setCanEdit(true);
        grid.setVirtualScrolling(false);
        grid.setWrapCells(true);
        grid.setFixedRecordHeights(false);
        grid.setCanFreezeFields(true);
        grid.setAutoFetchData(true);
        grid.setFrozenFieldsMaxWidth("25%");
        grid.setAutoFitFieldWidths(false);

        buildFields(grid);

        DataSource ds = new DataSource("webstatic/data/ListGridData.xml");
        ds.setRecordXPath("/List/Row");
        ds.setClientOnly(true);
        buildFields(ds);
        grid.setDataSource(ds);

        grid.setHeight(1200);
        grid.setWidth(1500);

        HLayout panel = new HLayout();
        panel.setHeight100();
        panel.setWidth100();
        panel.addMember(grid);

        for (int i = 0; i < columnCount; i++)
        {
            ListGridField frozenField = grid.getField(i);
            boolean frozen = (i < frozenColumns);
            int width = frozen ? 200 : 100;
            frozenField.setWidth(width);
            frozenField.setFrozen(frozen);
        }

        return panel;
    }

    private void buildFields(ListGrid grid)
    {
        ArrayList<ListGridField> fields = new ArrayList<ListGridField>();
        ListGridField frozenField = new ListGridField("frozen1");

        for (int i = 0; i < frozenColumns; i++)
        {
            ListGridField f = new ListGridField("frozen" + i);
            frozenField.setCanEdit(false);
            fields.add(f);
        }

        for (int i = frozenColumns; i < columnCount; i++)
        {
            ListGridField f = new ListGridField("unfrozen" + i);
            f.setCanEdit(false);
            fields.add(f);
        }

        grid.setFields(fields.toArray(new ListGridField[0]));
    }

    private void buildFields(DataSource ds)
    {
        ArrayList<DataSourceTextField> fields = new ArrayList<DataSourceTextField>();
        ListGridField frozenField = new ListGridField("frozen1");

        for (int i = 0; i < frozenColumns; i++)
        {
            DataSourceTextField f = new DataSourceTextField("frozen" + i);
            frozenField.setCanEdit(false);
            fields.add(f);
        }

        for (int i = frozenColumns; i < columnCount; i++)
        {
            DataSourceTextField f = new DataSourceTextField("unfrozen" + i);
            f.setCanEdit(true);
            fields.add(f);
        }
        ds.setFields(fields.toArray(new DataSourceTextField[0]));
    }

}
