package standalone.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;

public class Standalone_smartgwt implements EntryPoint {
	private ListGridRecord[] testData = new ListGridRecord[] { new ListGridRecord() {
		{
			setAttribute("value", "value1");
			setAttribute("secondValue", "secondValue1");
		}
	}, new ListGridRecord() {
		{
			setAttribute("value", "value2");
			setAttribute("secondValue", "secondValue2");
		}
	}, new ListGridRecord() {
		{
			setAttribute("value", "value2");
			setAttribute("secondValue", "secondValue3");
		}
	} };

	public void onModuleLoad() {
		
		DynamicForm form = new DynamicForm();

		
		ComboBoxItem comboBox = new ComboBoxItem("combo");
		DataSource optionDS = new DataSource();
		optionDS.setClientOnly(true);
		optionDS.setCacheData(testData);
		comboBox.setOptionDataSource(optionDS);
		comboBox.setPickListFields(new ListGridField[]{
			new ListGridField("value"),
			new ListGridField("secondValue")
		});
		comboBox.setValueField("value");
		comboBox.setDisplayField("secondValue");
		comboBox.addChangedHandler(new ChangedHandler() {
			@Override
			public void onChanged(ChangedEvent event) {
				ListGridRecord selectedRecord = event.getItem().getSelectedRecord();
				if (selectedRecord != null) {
					event.getForm().setValue("secondValue", selectedRecord.getAttribute("secondValue"));
				}
			}
		});
		
		StaticTextItem secondItem = new StaticTextItem("secondValue");
		
		form.setFields(comboBox, secondItem);

		RootPanel rootPanel = RootPanel.get("smartgwtbody");
		VLayout layout = new VLayout();
		layout.setBorder("1px solid grey");
		layout.addMember(form);
		rootPanel.add(layout);
	}
}
