package org.mytest.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.TransferImgButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.RecordDropEvent;
import com.smartgwt.client.widgets.grid.events.RecordDropHandler;
import com.smartgwt.client.widgets.layout.HLayout;

public class Test implements EntryPoint {
	
	@Override
	public void onModuleLoad() {
		
		HLayout mainLayout = new HLayout();
		mainLayout.setHeight100();
		mainLayout.setWidth100();
		
		final ListGrid fromGrid = new ListGrid();
		fromGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
		fromGrid.setDataSource(CountryDS.getInstance());
		fromGrid.setAutoFetchData(true);
		

		final ListGrid toGrid = new ListGrid();
		toGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
		toGrid.setDataSource(CountryDS2.getInstance());
		toGrid.setAutoFetchData(true);

        TransferImgButton rightImg = new TransferImgButton(TransferImgButton.RIGHT);  
        rightImg.addClickHandler(new ClickHandler() {  
            public void onClick(ClickEvent event) {  
            	toGrid.transferSelectedData(fromGrid);  
            }  
        });
        
        TransferImgButton leftImg = new TransferImgButton(TransferImgButton.LEFT);  
        leftImg.addClickHandler(new ClickHandler() {  
            public void onClick(ClickEvent event) {  
            	fromGrid.transferSelectedData(toGrid);  
            }  
        });

        
        toGrid.addRecordDropHandler(new RecordDropHandler() {
			
			@Override
			public void onRecordDrop(RecordDropEvent event) {
				SC.warn("toGrid RecordDropHandler Event!!!");
				
			}
		});

        fromGrid.addRecordDropHandler(new RecordDropHandler() {
			
			@Override
			public void onRecordDrop(RecordDropEvent event) {
				SC.warn("fromGrid RecordDropHandler Event!!!");
				
			}
		});

        mainLayout.setMembers(fromGrid, leftImg, rightImg, toGrid);

		mainLayout.draw();
	}
	
	
	private static class CountryDS extends DataSource {  
        // The DataSource would normally be defined external to any classes that use it.  
  
        private static CountryDS instance = null;  
          
        public static CountryDS getInstance() {  
            if (instance == null) {  
              instance = new CountryDS("countryDS_DS");  
            }  
            return instance;  
        }  
  
        public CountryDS(String id) {  
            setID(id);  
            setRecordXPath("/List/country");  
            DataSourceField countryNameField = new DataSourceField("countryName", FieldType.TEXT, "Country");  
            DataSourceField countryCodeField = new DataSourceField("countryCode", FieldType.TEXT, "Code");  
            DataSourceField independenceField = new DataSourceField("independence", FieldType.DATE, "Independence");  
            DataSourceField populationField = new DataSourceField("population", FieldType.INTEGER, "Population");  
            DataSourceField gdpField = new DataSourceField("gdp", FieldType.FLOAT, "GDP ($B)");  
            
            countryCodeField.setPrimaryKey(true);
  
            setFields(countryNameField, countryCodeField, independenceField, populationField, gdpField);  
            setDataURL("ds/test_data/country.data.xml");  
            
            setClientOnly(true);
        }  
  
    }  

	private static class CountryDS2 extends DataSource {  
        // The DataSource would normally be defined external to any classes that use it.  
  
        private static CountryDS2 instance = null;  
          
        public static CountryDS2 getInstance() {  
            if (instance == null) {  
              instance = new CountryDS2("countryDS_DS2");  
            }  
            return instance;  
        }  
  
        public CountryDS2(String id) {  
            setID(id);  
            setRecordXPath("/List/country");  
            DataSourceField countryNameField = new DataSourceField("countryName", FieldType.TEXT, "Country");  
            DataSourceField countryCodeField = new DataSourceField("countryCode", FieldType.TEXT, "Code");  
            DataSourceField independenceField = new DataSourceField("independence", FieldType.DATE, "Independence");  
            DataSourceField populationField = new DataSourceField("population", FieldType.INTEGER, "Population");  
            DataSourceField gdpField = new DataSourceField("gdp", FieldType.FLOAT, "GDP ($B)");  
            
            countryCodeField.setPrimaryKey(true);
  
            setFields(countryNameField, countryCodeField, independenceField, populationField, gdpField);  
            setDataURL("ds/test_data/country.data2.xml");  
            
            setClientOnly(true);
        }  
  
    }  

	

}
