package com.ssdi.test.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.AutoFitWidthApproach;
import com.smartgwt.client.types.ListGridFieldType;
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.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
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.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class TestsmartGwtProject implements EntryPoint {
  
  public static class MyListGrid extends ListGrid {
  CloningListDS cloningListDS = CloningListDS.getInstance();
  public static String IMG_REMOVE = (GWT.getHostPageBaseURL() + "images/del.png");
  
    public void initialize() {
      setWidth100();
      setHeight100();
      setMargin(10);
      addListGridField();
      setShowRecordComponents(true);
      setCanAutoFitFields(false);
      setAutoSaveEdits(false);
      
      setMargin(0);
      setDataSource(cloningListDS);
      setShowRecordComponentsByCell(true);
      setAutoFetchData(true);
      setAutoFitWidthApproach(AutoFitWidthApproach.BOTH);
      setSelectionType(SelectionStyle.SIMPLE);  
      setSelectionAppearance(SelectionAppearance.CHECKBOX);
    }

   

    
    public void addListGridField() {
      ListGridField fileTypeField = new ListGridField("fileType", "Type");
      fileTypeField.setCanEdit(false);
      fileTypeField.setCanSort(true);
      fileTypeField.setWidth(80);
      fileTypeField.setCanDragResize(true);
      ListGridField fileNameField = new ListGridField("fileName", "File Name");
      fileNameField.setCanSort(true);
      fileNameField.setCanEdit(false);
      fileNameField.setCanDragResize(true);
      ListGridField commentField = new ListGridField("comment", "Comment");
      commentField.setCanEdit(false);
      commentField.setCanSort(true);
      commentField.setEscapeHTML(true);
      commentField.setWidth(80);

      ListGridField uploadDateTimeField = new ListGridField("uploadDate", "Upload Date");
      uploadDateTimeField.setType(ListGridFieldType.DATETIME);
      uploadDateTimeField.setCanEdit(false);
      uploadDateTimeField.setCanSort(true);
      uploadDateTimeField.setCanDragResize(true);
     

      ListGridField executeButtonField = new ListGridField("executebutton", "Execute Button");
      executeButtonField.setWidth("220px");
      executeButtonField.setCanEdit(false);
      executeButtonField.setCanFilter(false);
      executeButtonField.setCanSort(false);
      executeButtonField.setCanDragResize(false);
    

      ListGridField executeDateTimeField = new ListGridField("executionDate", "Execution Date");
      executeDateTimeField.setType(ListGridFieldType.DATETIME);
      executeDateTimeField.setCanEdit(false);
      executeDateTimeField.setCanFilter(true);
      executeDateTimeField.setCanSort(true);
      executeDateTimeField.setCanDragResize(true);
    

      ListGridField lastExecuteStatusField = new ListGridField("executionStatus", "Execution Status");
      lastExecuteStatusField.setCanEdit(false);
      lastExecuteStatusField.setCanFilter(true);
      lastExecuteStatusField.setCanSort(true);
      lastExecuteStatusField.setAlign(Alignment.CENTER);
      ListGridField executeEncriptionPasswordField = new ListGridField("encriptionPassword", "Encription Password");
      executeEncriptionPasswordField.setWidth(150);
      executeEncriptionPasswordField.setCanEdit(false);
      executeEncriptionPasswordField.setCanFilter(false);
      executeEncriptionPasswordField.setCanSort(false);
      executeEncriptionPasswordField.setCanDragResize(true);
     

      ListGridField executeResultField = new ListGridField("resultButton", "Result Button");
      executeResultField.setCanEdit(false);
      executeResultField.setCanSort(false);
      executeResultField.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);

      ListGridField downloadButtonField = new ListGridField("download", "download");
      downloadButtonField.setCanEdit(false);
      downloadButtonField.setCanFilter(false);
      downloadButtonField.setCanSort(false);
      downloadButtonField.setCanDragResize(true);

      ListGridField fileIdField = new ListGridField("fileId", "fileId");
      fileIdField.setCanEdit(false);
      fileIdField.setCanSort(false);
      fileIdField.setCanHide(false);

      ListGridField downloadFlagField = new ListGridField("downloadFlag", "downloadFlag");
      downloadFlagField.setCanEdit(false);
      downloadFlagField.setCanSort(false);
      downloadFlagField.setHidden(true);
      downloadFlagField.setCanHide(false);
      downloadFlagField.setCanDragResize(true);

      ListGridField fileTypeValueField = new ListGridField("fileTypeValue", "fileTypeValue");
      fileTypeValueField.setCanEdit(false);
      fileTypeValueField.setCanSort(false);
      fileTypeValueField.setHidden(true);
      fileTypeValueField.setCanHide(false);

      executeButtonField.setWidth(247);
      fileTypeField.setWidth(60);
      commentField.setWidth(60);
      executeEncriptionPasswordField.setWidth(170);

      setFields(fileTypeField, fileIdField, fileNameField, commentField, uploadDateTimeField, executeButtonField,
          executeDateTimeField, lastExecuteStatusField, executeEncriptionPasswordField, executeResultField,
          downloadButtonField, downloadFlagField, fileTypeValueField, delIconField);
      
    }

    
      @Override
      protected Canvas createRecordComponent(final ListGridRecord record, Integer columnNumber) {

        HLayout recordCanvas = null;
        recordCanvas = new HLayout(3);
        recordCanvas.setHeight(22);
        recordCanvas.setAutoWidth();
        recordCanvas.setAlign(Alignment.CENTER);
        final String fieldName = getFieldName (columnNumber);
        
        if (fieldName != null) {
          if (fieldName.equalsIgnoreCase("executebutton")) {
            final IButton executeBtn = new IButton("Execute now");
            executeBtn.setTitle("Execute now");
            executeBtn.setHeight(20);

            executeBtn.setWidth(100);

            executeBtn.setTitle("Execute now");

            final IButton scheduleBtn = new IButton("Schedule");
            scheduleBtn.setTitle("Schedule");
            scheduleBtn.setHeight(20);
            scheduleBtn.setWidth(100);
            
           
                     
            recordCanvas.addMember(executeBtn);
            recordCanvas.addMember(scheduleBtn);
             return recordCanvas;
          }
        }
        if (fieldName != null) {
        if (fieldName.equalsIgnoreCase("encriptionPassword")) {
          final IButton executeEncriptionPasswordBtn = new IButton("PassWord Change");
          executeEncriptionPasswordBtn.setTitle("Password");
          executeEncriptionPasswordBtn.setHeight(20);

          executeEncriptionPasswordBtn.setWidth(160);
          recordCanvas.setAlign(Alignment.CENTER);
          recordCanvas.addMember(executeEncriptionPasswordBtn);
           return recordCanvas;

        }
        }
        if (fieldName != null){
        if (fieldName.equalsIgnoreCase("resultButton")) {

          final IButton executeResultBtn = new IButton("Result");
          executeResultBtn.setTitle("Result");
          executeResultBtn.setHeight(20);
          executeResultBtn.setWidth(120);
          recordCanvas.setAlign(Alignment.CENTER);
          recordCanvas.addMember(executeResultBtn);
         return recordCanvas;
        }
        }
        if (fieldName != null){
        if (fieldName.equalsIgnoreCase("download")) {

          final IButton downloadBtn = new IButton("Download");
          downloadBtn.setTitle("Download");
          downloadBtn.setHeight(20);
          downloadBtn.setWidth(140);
          recordCanvas.setAlign(Alignment.CENTER);
          recordCanvas.addMember(downloadBtn);
         return recordCanvas;
        }
        }
        if (fieldName != null){
        if (fieldName.equalsIgnoreCase("deleteRecord")) {
          recordCanvas.setAlign(Alignment.CENTER);
          ImgButton delImg = new ImgButton();
          delImg.setShowDown(false);
          delImg.setShowRollOver(false);
          delImg.setLayoutAlign(Alignment.CENTER);
          delImg.setSrc(IMG_REMOVE);
          delImg.setPrompt("delete");
          delImg.setHeight(16);
          delImg.setWidth(16);

          recordCanvas.addMember(delImg);
           return recordCanvas;
        }
        }
        return recordCanvas;
      }

  
  }
  @Override
  public void onModuleLoad() {
    final VLayout layout = new VLayout();
   
    layout.setWidth100();
    layout.setHeight("50%");
    layout.setOverflow(Overflow.AUTO);
    final MyListGrid listGrid = new MyListGrid ();
    listGrid.initialize();
    Canvas canvas = new Canvas();

    canvas.setHeight("95%");
    canvas.setWidth("100%");
    canvas.addChild(listGrid);
    Canvas canvasButton = new Canvas();

    canvasButton.setHeight(20);
    canvasButton.setWidth(150);
   
    IButton refershBtn = new IButton("Refersh");
    refershBtn.setTitle("Refersh");
    refershBtn.setHeight(20);
    refershBtn.setWidth(140);
    canvasButton.addChild(refershBtn);
    layout.addMembers(canvasButton);
    layout.addMember(canvas);
    
    refershBtn.addClickHandler(new ClickHandler() {
      
      @Override
      public void onClick(ClickEvent event) {
        listGrid.invalidateCache();
        listGrid.scrollToRow(0);
       
        
      }
    });
    layout.draw();
    
  }
 
}