/*
 * 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.setDataSource(new HelloGoodbyeListGridDataSource());
        listGrid.setAutoSaveEdits(true);
        listGrid.setCanEdit(true);
        listGrid.setAutoFetchData(true);

        DataSource helloGoodbyeDs = new HelloGoodbyeDataSource();

        ListGridField msgField1 = new ListGridField(
                HelloGoodbyeListGridDataSource.MSG_1_ID_FIELD_NAME, "Message 1", 120);
        msgField1.setEditorType(new HelloGoodbyeComboBox(
                HelloGoodbyeListGridDataSource.MSG_1_ID_FIELD_NAME, "Message 1", helloGoodbyeDs));
        msgField1.setValueField(HelloGoodbyeListGridDataSource.MSG_1_ID_FIELD_NAME);
        msgField1.setDisplayField(HelloGoodbyeListGridDataSource.MSG_1_FIELD_NAME);

        ListGridField msgField2 = new ListGridField(
                HelloGoodbyeListGridDataSource.MSG_2_ID_FIELD_NAME, "Message 2", 120);
        msgField2.setEditorType(new HelloGoodbyeComboBox(
                HelloGoodbyeListGridDataSource.MSG_2_ID_FIELD_NAME, "Message 2", helloGoodbyeDs));
        msgField2.setValueField(HelloGoodbyeListGridDataSource.MSG_2_ID_FIELD_NAME);
        msgField2.setDisplayField(HelloGoodbyeListGridDataSource.MSG_2_FIELD_NAME);

        listGrid.setFields(msgField1, msgField2);

        listGrid.draw();
    }

    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);
            idField.setHidden(true);
            idField.setAttribute("displayField", MSG_FIELD_NAME);

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

            setFields(idField, msgField);
        }
    }

    private class HelloGoodbyeListGridDataSource extends DataSource
    {
        private final Record       helloHelloRecord    = new HelloGoodbyeListGridRecord(1, 1,
                                                               "Hello", 1, "Hello");

        public static final String MSG_1_ID_FIELD_NAME = "msg1Id";
        public static final String MSG_1_FIELD_NAME    = "msg1";
        public static final String MSG_2_ID_FIELD_NAME = "msg2id";
        public static final String MSG_2_FIELD_NAME    = "msg2";

        public HelloGoodbyeListGridDataSource()
        {
            setClientOnly(true);
            setTestData(new Record[] { helloHelloRecord });

            DataSourceField idField = new DataSourceField("id", FieldType.INTEGER);
            idField.setPrimaryKey(true);
            idField.setHidden(true);

            DataSourceField msg1IdField = new DataSourceField(MSG_1_ID_FIELD_NAME,
                    FieldType.INTEGER);
            msg1IdField.setAttribute("displayField", MSG_1_FIELD_NAME);
            msg1IdField.setTitle("Message 1");

            DataSourceField msg1Field = new DataSourceField(MSG_1_FIELD_NAME, FieldType.TEXT);
            msg1Field.setHidden(true);

            DataSourceField msg2IdField = new DataSourceField(MSG_2_ID_FIELD_NAME,
                    FieldType.INTEGER);
            msg2IdField.setAttribute("displayField", MSG_2_FIELD_NAME);
            msg2IdField.setTitle("Message 2");

            DataSourceField msg2Field = new DataSourceField(MSG_2_FIELD_NAME, FieldType.TEXT);
            msg2Field.setHidden(true);

            setFields(idField, msg1IdField, msg1Field, msg2IdField, msg2Field);
        }
    }

    private class HelloGoodbyeListGridRecord extends Record
    {
        public HelloGoodbyeListGridRecord(int id, int msg1Id, String msg1, int msg2Id, String msg2)
        {
            setId(id);
            setMsg1Id(msg1Id);
            setMsg1(msg1);
            setMsg2Id(msg2Id);
            setMsg2(msg2);
        }

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

        public void setMsg1Id(int msg1Id)
        {
            setAttribute(HelloGoodbyeListGridDataSource.MSG_1_ID_FIELD_NAME, msg1Id);
        }

        public void setMsg1(String msg1)
        {
            setAttribute(HelloGoodbyeListGridDataSource.MSG_1_FIELD_NAME, msg1);
        }

        public void setMsg2Id(int msg2Id)
        {
            setAttribute(HelloGoodbyeListGridDataSource.MSG_2_ID_FIELD_NAME, msg2Id);
        }

        public void setMsg2(String msg2)
        {
            setAttribute(HelloGoodbyeListGridDataSource.MSG_2_FIELD_NAME, msg2);
        }
    }

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

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

        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);
        }
    }
}
