/*
 * Smart GWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * Smart GWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.  Smart GWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

/*
 * Smart GWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * Smart GWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * is published by the Free Software Foundation.  Smart GWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;

public class GridCellEditorCustomizerSample implements EntryPoint
{
    @Override
    public void onModuleLoad()
    {
        final ListGrid listGrid = new ListGrid();
        listGrid.setWidth(300);
        listGrid.setHeight(340);
        listGrid.setShowAllRecords(true);
        listGrid.setAutoSaveEdits(false);
        listGrid.setCanEdit(true);

        Record helloHelloRecord = new HelloGoodbyeListGridRecord("Hello", "Hello");

        listGrid.setData(new Record[] { helloHelloRecord });

        ListGridField msgField1 = new ListGridField("msgfield1", "Message 1", 120);
        msgField1.setEditorType(new HelloGoodbyeComboBox("msgfield1", "Message 1",
                new HelloGoodbyeDataSource()));

        ListGridField msgField2 = new ListGridField("msgfield2", "Message 2", 120);
        msgField2.setEditorType(new HelloGoodbyeComboBox("msgfield2", "Message 2",
                new HelloGoodbyeDataSource()));

        listGrid.setFields(msgField1, msgField2);

        listGrid.draw();

        listGrid.startEditingNew();
    }

    private class HelloGoodbyeDataSource extends DataSource
    {
        private Record             helloRecord    = new HelloGoodbyeRecord(1, "Hello");
        private Record             goodbyeRecord  = new HelloGoodbyeRecord(2, "Goodbye");
        public static final String ID_FIELD_NAME  = "id";
        public static final String MSG_FIELD_NAME = "msg";

        public HelloGoodbyeDataSource()
        {
            setClientOnly(true);
            setTestData(new Record[] { helloRecord, goodbyeRecord });

            DataSourceField idField = new DataSourceField(ID_FIELD_NAME, FieldType.INTEGER);
            idField.setPrimaryKey(true);

            DataSourceField msgField = new DataSourceField(MSG_FIELD_NAME, FieldType.TEXT);

            setFields(idField, msgField);
        }
    }

    private class HelloGoodbyeListGridRecord extends Record
    {
        public HelloGoodbyeListGridRecord(String msg1, String msg2)
        {
            setMsg1(msg1);
            setMsg2(msg2);
        }

        public void setMsg1(String msg1)
        {
            setAttribute("msg1", msg1);
        }

        public void setMsg2(String msg2)
        {
            setAttribute("msg2", msg2);
        }
    }

    private class HelloGoodbyeRecord extends Record
    {
        public HelloGoodbyeRecord(int id, String msg)
        {
            setId(id);
            setMsg(msg);
        }

        public int getId()
        {
            return getAttributeAsInt("id");
        }

        public void setId(int id)
        {
            setAttribute("id", id);
        }

        public String getMsg()
        {
            return getAttributeAsString("msg");
        }

        public void setMsg(String msg)
        {
            setAttribute("msg", msg);
        }
    }

    private class HelloGoodbyeComboBox extends ComboBoxItem
    {
        public HelloGoodbyeComboBox(String name, String title, DataSource ds)
        {
            super(name, title);
            setOptionDataSource(ds);
            setAutoFetchData(true);
            setValueField(HelloGoodbyeDataSource.ID_FIELD_NAME);
            setDisplayField(HelloGoodbyeDataSource.MSG_FIELD_NAME);
        }
    }
}
