package com.ssdi.test.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.data.Record;
/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ListGridSampleApp implements EntryPoint {


    public void onModuleLoad() {

        HLayout layout =new HLayout(10);
        layout.setHeight100();
        layout.setWidth100();

        final ListGrid countryGrid = new ListGrid() {
            @Override
            protected Canvas createRecordComponent(final ListGridRecord record, Integer colNum) {

                String fieldName = this.getFieldName(colNum);
                HLayout recordCanvas = new HLayout(3);
                recordCanvas.setHeight(22);
                recordCanvas.setAutoWidth();
                recordCanvas.setAlign(Alignment.CENTER);
                HLayout butlayout=new HLayout(3);
                butlayout.setBorder("1px soilid red");
                butlayout.setWidth("100%");
                butlayout.setHeight(22);
                butlayout.setOverflow(Overflow.HIDDEN);
                 if (fieldName.equals("buttonField")) {
                    IButton button = new IButton();
                    button.setHeight(18);
                    button.setWidth(120);
                    button.setTitle("Change Password");
                    /*button.addClickHandler(new ClickHandler() {});*/
                    butlayout.addMember(button);
                    return butlayout;
                } else if(fieldName.equals("countryCode")) {

                    IButton tbutton = new IButton();
                    tbutton.setHeight(18);
                    tbutton.setWidth(80);
                    tbutton.setTitle("Information");
                    butlayout.addMember(tbutton);
                    return butlayout;
                }
                
                else if (fieldName.equals("deleteRecord")) {

 					recordCanvas.setHeight(22);
 					recordCanvas.setAlign(Alignment.CENTER);
 					ImgButton delImg = new ImgButton();
 					/*delImg.setShowDown(false);
 					delImg.setShowRollOver(false);*/
 					delImg.setLayoutAlign(Alignment.CENTER);
 					delImg.setSrc("del.png");
 					delImg.setPrompt("Delete");
 					delImg.setHeight(16);
 					delImg.setWidth(16);
 					recordCanvas.addMember(delImg);
 					return recordCanvas;
                 }
                
                else{
                    return null;
                }

            }
        };
        countryGrid.setShowRecordComponents(true);
        countryGrid.setShowRecordComponentsByCell(true);
//        countryGrid.setCanRemoveRecords(true);
        
        countryGrid.setWidth(800);
        countryGrid.setHeight(300);
        countryGrid.setShowAllRecords(true);
        countryGrid.setLeft(100);
        countryGrid.setSelectionType(SelectionStyle.SIMPLE);  
        countryGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);  
        
        ListGridField countryCodeField = new ListGridField("countryCode", "Flag", 120);
        countryCodeField.setAlign(Alignment.CENTER);
        countryCodeField.setCanDragResize(false);

        ListGridField nameField = new ListGridField("countryName", "Country");
        ListGridField capitalField = new ListGridField("capital", "Capital");
        ListGridField continentField = new ListGridField("continent", "Continent");

        ListGridField buttonField = new ListGridField("buttonField", "Info");
        buttonField.setAlign(Alignment.CENTER);
        buttonField.setCanDragResize(false);
        
        ListGridField delIconField = new ListGridField("deleteRecord","deleteRecord");
		delIconField.setWidth("30px");
		delIconField.setCanReorder(false);
		delIconField.setCanDragResize(false);
		delIconField.setCanEdit(false);
		delIconField.setCanFilter(false);
		delIconField.setShowTitle(false);
		delIconField.setCanSort(false);
		delIconField.setAlign(Alignment.CENTER);
        

        countryGrid.setFields(countryCodeField, nameField, capitalField, continentField, buttonField,delIconField);
        countryGrid.setCanResizeFields(true);

       
        countryGrid.setData(CountrySampleData.getNewRecords());
        for(int i=0;i<CountrySampleData.getNewRecords().length;i++){
        	countryGrid.selectRecord(i);
        }
        
       // countryGrid.selectAllRecords();

        countryGrid.draw();
        
        layout.addMember(countryGrid);
        
    }
}
