1 package autotest.moblab; 2 3 import autotest.common.JsonRpcCallback; 4 import autotest.common.JsonRpcProxy; 5 import autotest.common.SimpleCallback; 6 import autotest.common.ui.TabView; 7 import autotest.common.ui.NotifyManager; 8 9 import com.google.gwt.event.dom.client.ClickHandler; 10 import com.google.gwt.event.dom.client.ClickEvent; 11 import com.google.gwt.json.client.JSONObject; 12 import com.google.gwt.json.client.JSONString; 13 import com.google.gwt.json.client.JSONValue; 14 import com.google.gwt.user.client.ui.Button; 15 import com.google.gwt.user.client.ui.FileUpload; 16 import com.google.gwt.user.client.ui.FormPanel; 17 import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent; 18 import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler; 19 import com.google.gwt.user.client.ui.FormPanel.SubmitEvent; 20 import com.google.gwt.user.client.ui.FormPanel.SubmitHandler; 21 22 23 public class KeyUploadView extends TabView { 24 private FileUpload keyUpload; 25 private Button submitButton; 26 private FormPanel keyUploadForm; 27 28 protected String fileUploadName = "key"; 29 protected String uploadViewName = "view_key"; 30 protected String submitButtonName = "view_submit_key"; 31 protected String rpcName = ""; 32 protected String rpcArgName = "key"; 33 protected String successMessage = "Key uploaded."; 34 35 @Override getElementId()36 public String getElementId() { 37 return "key"; 38 } 39 40 @Override initialize()41 public void initialize() { 42 super.initialize(); 43 44 keyUpload = new FileUpload(); 45 keyUpload.setName(fileUploadName); 46 47 keyUploadForm = new FormPanel(); 48 keyUploadForm.setAction(JsonRpcProxy.AFE_BASE_URL + "upload/"); 49 keyUploadForm.setEncoding(FormPanel.ENCODING_MULTIPART); 50 keyUploadForm.setMethod(FormPanel.METHOD_POST); 51 keyUploadForm.setWidget(keyUpload); 52 53 submitButton = new Button("Submit", new ClickHandler() { 54 public void onClick(ClickEvent event) { 55 keyUploadForm.submit(); 56 } 57 }); 58 59 keyUploadForm.addSubmitCompleteHandler(new SubmitCompleteHandler() { 60 public void onSubmitComplete(SubmitCompleteEvent event) { 61 String fileName = event.getResults(); 62 JSONObject params = new JSONObject(); 63 params.put(rpcArgName, new JSONString(fileName)); 64 rpcCall(params); 65 } 66 }); 67 68 addWidget(keyUploadForm, uploadViewName); 69 addWidget(submitButton, submitButtonName); 70 } 71 rpcCall(JSONObject params)72 public void rpcCall(JSONObject params) { 73 JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy(); 74 rpcProxy.rpcCall(rpcName, params, new JsonRpcCallback() { 75 @Override 76 public void onSuccess(JSONValue result) { 77 NotifyManager.getInstance().showMessage(successMessage); 78 } 79 }); 80 } 81 } 82