package org.example.test.dynamicForm.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.util.SC;
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.form.DynamicForm;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class TestDynamicForm_getValuesAsRecord implements EntryPoint {
	
	private DynamicForm form;
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		VLayout layout = new VLayout(15);

        Label label = new Label();
        label.setHeight(10);
        label.setWidth(100);
        label.setContents("Showing items in Category 'Rollfix Glue");
        layout.addMember(label);

        final DataSource dataSource = ItemSupplyLocalDS.getInstance();

        form = new DynamicForm();
        form.setIsGroup(true);
        form.setGroupTitle("Update");
        form.setNumCols(4);
        form.setDataSource(dataSource);


        final ListGrid listGrid = new ListGrid();
        listGrid.setWidth(600);
        listGrid.setHeight(200);
        listGrid.setDataSource(dataSource);
        listGrid.setAutoFetchData(true);
        listGrid.addRecordClickHandler(new RecordClickHandler() {
            public void onRecordClick(RecordClickEvent event) {
                form.reset();
                form.editSelectedData(listGrid);
            }
        });

        layout.addMember(listGrid);
        layout.addMember(form);

        IButton button = new IButton("Save");
        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                form.saveData();                
            }
        });
        layout.addMember(button);
        
        //TEST
        IButton showFormRecord = new IButton ();
        showFormRecord.setTitle("get form values as record");
        showFormRecord.setAutoFit(Boolean.TRUE);
        showFormRecord.addClickHandler(new ClickHandler () {
			@Override
			public void onClick(ClickEvent event) {
				//FIXME
				form.getValuesAsRecord();
				SC.say("It's alright!");
			}
        });
        
        layout.addMember(showFormRecord);

        layout.draw();

	}
}
