package com.example.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.layout.VLayout;
 /**
  * GWT entry point class.
  * 
  * @author hturksoy
  */
 public class FileUploadPanel extends VLayout {
    private static final String UPLOAD_ACTION_URL = GWT.getModuleBaseURL() + "upload";
    
    
    /**
     * This creates file upload widget.
     * @param key, this is a directory key which identifies file upload directory. This should have mapping in
     * BDSetupPaths.properties file which exists under project root directory.
     */
    public FileUploadPanel() {
 		fillPanel();
	}
    
    
	protected void fillPanel() {

        // Create a FormPanel and point it at a service.
       
		final FormPanel form = new FormPanel();
        form.setAction(UPLOAD_ACTION_URL);

        // Because we're going to add a FileUpload widget, we'll need to set the
        // form to use the POST method, and multipart MIME encoding.
        form.setEncoding(FormPanel.ENCODING_MULTIPART);
        form.setMethod(FormPanel.METHOD_POST);
        
        

        // Create a panel to hold all of the form widgets.
        VerticalPanel panel = new VerticalPanel();
        //VLayout panel=new VLayout();
        
        
        form.setWidget(panel);

        // Create a FileUpload widget.
        final FileUpload upload = new FileUpload();
        upload.setName("uploadFormElement");
        
        Element ee = upload.getElement();
        DOM.setElementPropertyInt((com.google.gwt.user.client.Element) ee, "size", 85);

        panel.add(upload);
        
        form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
			
			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				SC.say("result: "+event.getResults());
				//Here you can do whatever you want after file upload.
				
			}
		});

        Button btnLoad=new Button("Load", new ClickHandler() {
            public void onClick(ClickEvent event) {
            	if(upload.getFilename()==null || upload.getFilename().trim().length()==0){
            		SC.say("Please select a profile to load.");
            	}else{
            		form.submit();
            	}
            }
        });
        
        
        panel.setStyleName("fileUpload");
        panel.add(btnLoad);
       
       // RootPanel.get().add(form);

        addMember(form);
		
	}

 }