package com.smartgwt.sample.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.SortArrow;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
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.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VStack;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BuiltInDS implements EntryPoint {
    private IpListGrid boundList;

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {

        final ListGrid grid = new ListGrid();
        grid.setLeft(20);
        grid.setTop(75);
        grid.setWidth(130);
        grid.setLeaveScrollbarGap(false);
        grid.setShowSortArrow(SortArrow.NONE);
        grid.setCanSort(false);
        grid.setFields(new ListGridField("dsTitle", "Select a DataSource"));
        grid.setData(new ListGridRecord[]{
                new DSRecord("Animals", "animals"),
                new DSRecord("Office Supplies", "supplyItem"),
                new DSRecord("Employees", "employees")}
        );
        grid.setSelectionType(SelectionStyle.SINGLE);
        grid.addRecordClickHandler(new RecordClickHandler() {
            public void onRecordClick(RecordClickEvent event) {
                DSRecord record = (DSRecord) event.getRecord();
                bindComponents(record.getDsName());
            }
        });

        grid.draw();

        VStack vStack = new VStack();
        vStack.setLeft(175);
        vStack.setTop(75);
        vStack.setWidth("70%");
        vStack.setMembersMargin(20);

        Label label = new Label();
        label.setContents("<ul>" +
                "<li>select a datasource from the list at left to bind to these components</li>" +
                "<li>click a record in the grid to view and edit that record in the form</li>" +
                "<li>click <b>New</b> to start editing a new record in the form</li>" +
                "<li>click <b>Save</b> to save changes to a new or edited record in the form</li>" +
                "<li>click <b>Clear</b> to clear all fields in the form</li>" +
                "<li>click <b>Filter</b> to filter (substring match) the grid based on form values</li>" +
                "<li>click <b>Fetch</b> to fetch records (exact match) for the grid based on form values</li>" +
                "<li>double-click a record in the grid to edit inline (press Return, or arrow/tab to another record, to save)</li>" +
                "</ul>");
        vStack.addMember(label);

        boundList = new IpListGrid("canEditCellBug");
        boundList.setHeight(200);
        boundList.setCanEdit(true);
        boundList.setSelectionAppearance(SelectionAppearance.CHECKBOX);

        vStack.addMember(boundList);


        HLayout hLayout = new HLayout(10);
        hLayout.setMembersMargin(10);
        hLayout.setHeight(22);
        
        IButton editSelectedRecordsBtn = new IButton("Edit Selected Records");
        editSelectedRecordsBtn.setOverflow(Overflow.VISIBLE);
        editSelectedRecordsBtn.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
            	boundList.editSelectedRecords(false);
            }
        });
        
        hLayout.addMember(editSelectedRecordsBtn);

        vStack.addMember(hLayout);

        vStack.draw();
    }

    private void bindComponents(String dsName) {
        DataSource ds = DataSource.get(dsName);
        boundList.setDataSource(ds);
        boundList.fetchData();
    }
}
