package com.tempusnova.handlertest.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.data.DataSourceField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.util.SC;
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;
import com.smartgwt.client.widgets.form.fields.events.FormItemInitHandler;

public class HandlerTest implements EntryPoint {

	@Override
	public void onModuleLoad() 
	{
		DataSource ds = new DataSource();
		DataSourceField dsfOne = new DataSourceTextField("firstItem");
		DataSourceField dsfTwo = new DataSourceTextField("secondItem");
		
		TextItem textItem = new TextItem();
		
		// Both of these handlers will fire on the first item, neither will fire on the second item
		textItem.addChangedHandler(new ChangedHandler() {
      @Override
      public void onChanged(ChangedEvent event)
      {
        SC.logWarn(event.getItem().getName() + " - editorType ChangedEvent fired");
      }
    });
		
		textItem.addChangedHandler(new ChangedHandler() {
      @Override
      public void onChanged(ChangedEvent event)
      {
        SC.logWarn(event.getItem().getName() + " - another editorType ChangedEvent fired");
      }
    });
		
		dsfOne.setEditorType(textItem);
		dsfTwo.setEditorType(textItem);
		ds.setFields(dsfOne, dsfTwo);
		
		DynamicForm form = new DynamicForm();
		
		FormItem firstItem = new FormItem("firstItem");
		FormItem secondItem = new FormItem("secondItem");
		
		// Will not be fired
		firstItem.addChangedHandler(new ChangedHandler() {
      
      @Override
      public void onChanged(ChangedEvent event)
      {
        SC.logWarn(event.getItem().getName() + " - formItem ChangedEvent fired");
      }
    });
		
		secondItem.setInitHandler(new FormItemInitHandler() {
      @Override
      public void onInit(FormItem item)
      {
        // Will be fired
        item.addChangedHandler(new ChangedHandler() {
          @Override
          public void onChanged(ChangedEvent event)
          {
            SC.logWarn(event.getItem().getName() + " - formItem ChangedEvent fired");
          }
        });
      }
    });
		
		form.setDataSource(ds);
		form.setFields(firstItem, secondItem);
		form.setWidth100();
		
		RootPanel.get().add(form);
	}
}
