package com.smartgwt.sample.client;

import java.util.HashMap;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.shared.GWT;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.sample.client.tinymce.TinyMCECanvasItem;

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


	@Override
	public void onModuleLoad() {
		
		GWT.create(IpMetaFactory.class);
		
		final DynamicForm form = new DynamicForm();
		TinyMCECanvasItem item = new TinyMCECanvasItem("mce","MCE Editor");
		ButtonItem showValue = new ButtonItem();
		showValue.setTitle("Show Value");
		showValue.addClickHandler(new com.smartgwt.client.widgets.form.fields.events.ClickHandler() {
			
			@Override
			public void onClick(
					com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {
				SC.say(form.getValues().toString());
			}
		});
		form.setItems(item, showValue);
		
		ListGrid grid = new ListGrid();
		
		grid.setDataSource(getGridDS());
		grid.setData(getGridData());
		grid.setAutoFitData(Autofit.BOTH);
		grid.setCanEdit(true);
		grid.setSaveLocally(true);
		
		VLayout layout = new VLayout();
		layout.setWidth100();
		layout.setHeight100();
		layout.setMembers(form, grid);
		layout.draw();
		
		HashMap values = new HashMap();
		values.put("mce", "Some text to edit in the form item.");
		form.editNewRecord(values);
	}
	private DataSource getGridDS() {
		DataSource gridDS = new DataSource();
		DataSourceField keyField = new DataSourceField("keyField", FieldType.TEXT, "Key");
		keyField.setPrimaryKey(true);
		DataSourceField textField = new DataSourceField("textField", FieldType.TEXT, "Text");
		textField.setEditorType(TinyMCECanvasItem.class);
		gridDS.setFields(keyField, textField);
		return gridDS;
	}
	private RecordList getGridData() {
		Record record1 = new Record();
		record1.setAttribute("keyField", "1");
		record1.setAttribute("textField", "record 1");
		Record record2 = new Record();
		record2.setAttribute("keyField", "2");
		record2.setAttribute("textField", "record 2");
		RecordList data = new RecordList();
		data.add(record1);
		data.add(record2);
		return data;
	}
}
