package cat.diba.jee.mediatecagwt.client;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;

public class MultipleItemTest implements EntryPoint {

	public void onModuleLoad() {

		Log.setUncaughtExceptionHandler();
		Log.info("Logging with gwt-log");

		try {
			Scheduler.get().scheduleDeferred(new ScheduledCommand() {
				@Override
				public void execute() {
					multipleItemTest();
				}
			});
		} catch (Exception e) {
			Log.error("Error", e);
		}
	}
	
	private void multipleItemTest() {
		
		FormItem valueInputItem = new TextItem();
		
		FormItem multipleItem = new MultipleItem<String>("test", "Test", valueInputItem, new String[0]);
		
		List<String> list = new ArrayList<String>();
		list.addAll( Arrays.asList("one", "two", "three") );
		multipleItem.setValue( list.toArray(new String[0]) );
		
		multipleItem.addChangedHandler(new ChangedHandler() {
			@Override
			public void onChanged(ChangedEvent event) {
				Log.debug("Changed to: " + Arrays.toString((Object[])event.getValue() ));
			}
		});
		
		multipleItem.setRequired(true);
		multipleItem.setWidth(200);
		
		final DynamicForm form = new DynamicForm();
		form.setFields( multipleItem );
	
		form.draw();
	}
}