package com.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class DocumentClick implements EntryPoint
{
	@Override
	public void onModuleLoad()
	{
		ComboBoxItem comboBox = createComboBox();
		comboBox.setOptionDataSource(new ComboDataSource());

		DynamicForm form = new DynamicForm();
		form.setFields(comboBox);
		form.draw();

		bind();
	}

	private static native void bind()/*-{
		$wnd.$($doc).on('click', $entry(function(e){
			// Calling this fixes the issue for some reason.
//			e.preventDefault();
			$wnd.isc.logWarn('click');
		}));
	}-*/;
	
	private static ComboBoxItem createComboBox()
	{
		ComboBoxItem comboBox = new ComboBoxItem();
		comboBox.setCompleteOnTab(true);
		comboBox.setWidth(250);
		comboBox.setTitle("ComboBox");

		ListGridField itemIdField = new ListGridField("ItemId", "Id");
		ListGridField itemNameField = new ListGridField("ItemName", "Name");

		itemIdField.setWidth(50);
		itemNameField.setWidth("*");
		
		comboBox.setPickListFields(itemIdField, itemNameField);
		comboBox.setDisplayField("ItemName");
		return comboBox;
	}

	private static class ComboDataSource extends DataSource
	{
		public ComboDataSource()
		{
			DataSourceTextField itemIdField = new DataSourceTextField("ItemId");
			itemIdField.setPrimaryKey(true);
			itemIdField.setDetail(true);
			addField(itemIdField);

			addField(new DataSourceTextField("ItemName", "Item Name"));

			setDataFormat(DSDataFormat.CUSTOM);

			setClientOnly(true);
			setTestData(getTheData());
		}

		private static ListGridRecord[] getTheData()
		{
			ListGridRecord record = new ListGridRecord();
			record.setAttribute("ItemId", 1);
			record.setAttribute("ItemName", "One");

			return new ListGridRecord[]{ record };
		}
	}
}
