package calendarBug.client;

import java.util.LinkedHashMap;

import com.google.gwt.core.client.EntryPoint;

import com.smartgwt.client.types.FormItemType;
import com.smartgwt.client.widgets.calendar.Calendar;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceSequenceField;
import com.smartgwt.client.data.fields.DataSourceTextField;

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

	private Calendar calendar;
	private RestDataSource activityDS;
	private LinkedHashMap<String, String> personList;
	private SelectItem activityItem;
	
	
	/**
	 * This is the entry point method.
	 */	
	public void onModuleLoad() {
		
		/*
		 * Calendar's event DataSource
		 */
        DataSource eventDS = new DataSource();  
        DataSourceSequenceField eventIdField = new DataSourceSequenceField("eventId");  
        eventIdField.setPrimaryKey(true);  
  
        DataSourceTextField nameField = new DataSourceTextField("name");  
        DataSourceTextField descField = new DataSourceTextField("description");  
        DataSourceDateField startDateField = new DataSourceDateField("startDate");  
        DataSourceDateField endDateField = new DataSourceDateField("endDate");  
        DataSourceTextField personField = new DataSourceTextField("personID");  
        DataSourceTextField activityField = new DataSourceTextField("activityID");  
  
        eventDS.setFields(eventIdField, nameField, descField, startDateField, endDateField, personField, activityField);  
        eventDS.setClientOnly(true);  
        eventDS.setTestData(CalendarData.getRecords());
        eventDS.setID("eventDS");

		/*
		 * Activity DataSource for drop-down list's OptionDataSource
		 */        
        activityDS = new RestDataSource();
        DataSourceTextField a_activityField = new DataSourceTextField("activityID");
        a_activityField.setPrimaryKey(true);
        DataSourceTextField a_activityNameField = new DataSourceTextField("activityName");
        
        activityDS.setFields(a_activityField,a_activityNameField);
        activityDS.setFetchDataURL("/activities.xml");
        activityDS.setID("activityDS");
        
        
        /*
         * Calendar configuration
         */
        
        calendar = new Calendar();  
        calendar.setDataSource(eventDS);  
        calendar.setAutoFetchData(true);  
  
        //-----------------------------------------------------------------------
        // Event Editor
        
        //specify the last field from the default fields so that subsequent fields come after the  
        //default fields  
        TextItem descItem = new TextItem("description");  
    
        activityItem = new SelectItem("activityID", "Activity Name");
        activityItem.setColSpan(4);  
        activityItem.setOptionDataSource(activityDS);
        activityItem.setValueField("activityID");
        activityItem.setDisplayField("activityName");
        activityItem.setAttribute("optionDataSource", "activityDS"); // why is this needed?
        
        calendar.setEventEditorFields(descItem, activityItem);  

        /* For some reason, the names of datasource-related items are removed and have to
         * be reconfigured.
         */
        descItem.setName("description");  
        activityItem.setName("activityID");  
        activityItem.setTitle("Activity Name2");
      
        
        //-----------------------------------------------------------------------
        // EventDialog
        
        TextItem nameItem = new TextItem("name");  

        personList = new LinkedHashMap<String, String>();  
        personList.put("12", "Person1");  
        personList.put("16", "TestPerson");  
        personList.put("19", "Jeff");  
        
        SelectItem personItem = new SelectItem("personID");  
        personItem.setTitle("Person");  
        personItem.setValueMap(personList);  
        personItem.setWidth(250);  
          
        calendar.setEventDialogFields(nameItem, personItem);  
        /* For some reason, the names of datasource-related items are removed and have to
         * be reconfigured.
         */
        nameItem.setName("name");  
        personItem.setName("personID");

  
        calendar.draw();  
		
		//or
		//RootPanel.get().add(button);
	}
}
