/** * */ package org.gcube.portlets.user.td.codelistmappingimportwidget.client; import org.gcube.portlets.user.td.wizardwidget.client.dataresource.ResourceBundle; import org.gcube.portlets.user.td.codelistmappingimportwidget.client.progress.FileUploadProgressBarUpdater; import org.gcube.portlets.user.td.codelistmappingimportwidget.client.progress.FileUploadProgressListener; import org.gcube.portlets.user.td.codelistmappingimportwidget.client.progress.FileUploadProgressUpdater; import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVImportSession; import com.allen_sauer.gwt.log.client.Log; import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ChangeEvent; import com.google.gwt.event.dom.client.ChangeHandler; import com.sencha.gxt.core.client.Style.HideMode; import com.sencha.gxt.core.client.util.Padding; import com.sencha.gxt.widget.core.client.ProgressBar; import com.sencha.gxt.widget.core.client.box.AlertMessageBox; import com.sencha.gxt.widget.core.client.button.TextButton; import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer; import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData; import com.sencha.gxt.widget.core.client.event.SelectEvent; import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler; import com.sencha.gxt.widget.core.client.event.ShowEvent; import com.sencha.gxt.widget.core.client.event.ShowEvent.ShowHandler; import com.sencha.gxt.widget.core.client.form.FieldLabel; import com.sencha.gxt.widget.core.client.form.FileUploadField; import com.sencha.gxt.widget.core.client.form.FormPanel; /** * * @author "Giancarlo Panichi" g.panichi@isti.cnr.it * */ public class CodelistMappingFileUploadPanel extends FormPanel { protected static final String UPLOAD_SERVLET = "LocalUploadServlet"; public static final int STATUS_POLLING_DELAY = 1000; protected ResourceBundle res; protected FileUploadField fileUploadField; protected TextButton uploadButton; protected FileUploadProgressUpdater progressUpdater; protected ProgressBar uploadProgressBar; protected TextButton cancelButton; public CodelistMappingFileUploadPanel(ResourceBundle res, final CodelistMappingUploadFileCard card, final CSVImportSession importSession) { this.res = res; setId("LocalUploadPanel"); setLabelAlign(LabelAlign.TOP); getElement().setPadding(new Padding(5)); addShowHandler(new ShowHandler() { public void onShow(ShowEvent event) { doLayout(); } }); setAction(GWT.getModuleBaseURL() + UPLOAD_SERVLET); setWidth("100%"); setEncoding(Encoding.MULTIPART); setMethod(Method.POST); VerticalLayoutContainer content = new VerticalLayoutContainer(); content.setWidth("100%"); add(content); fileUploadField = new FileUploadField(); fileUploadField.setName("uploadFormElement"); fileUploadField.setWidth("100%"); content.add(new FieldLabel(fileUploadField, "Select the csv file to import"), new VerticalLayoutData(-2, -1)); uploadButton = new TextButton("Upload"); content.add(uploadButton, new VerticalLayoutData(-1, -1)); fileUploadField.addChangeHandler(new ChangeHandler() { public void onChange(ChangeEvent event) { uploadButton.setEnabled(fileUploadField.isValid()); String path = fileUploadField.getValue(); int punto = path.lastIndexOf("."); if(punto<0){ punto=path.length(); } int slash = path.lastIndexOf("/"); int backslash = path.lastIndexOf("\\"); String filename=""; if (slash > backslash) { if (slash != -1) { filename = path.substring(slash + 1, punto); } } else { if (backslash != -1) { filename = path.substring(backslash + 1, punto); } } importSession.setLocalFileName(filename); } }); uploadProgressBar = new ProgressBar(); uploadProgressBar.setHideMode(HideMode.VISIBILITY); uploadProgressBar.getElement().setPadding(new Padding(3, 0, 5, 0)); content.add(uploadProgressBar, new VerticalLayoutData(-2, -1)); uploadProgressBar.hide(); cancelButton = new TextButton("Cancel"); cancelButton.hide(); content.add(cancelButton, new VerticalLayoutData(-1, -1)); uploadButton.addSelectHandler(new SelectHandler() { public void onSelect(SelectEvent event) { Log.info("request upload"); if (fileUploadField.getValue() == null || fileUploadField.getValue().equals("")) { Log.info("fileUploadField is null or empty"); AlertMessageBox alertMessageBox = new AlertMessageBox( "CSV file missing", "Please specify a CSV file."); alertMessageBox.show(); return; } else { Log.info("startUpload call"); startUpload(); } } }); progressUpdater = new FileUploadProgressUpdater(); progressUpdater.addListener(new FileUploadProgressBarUpdater( uploadProgressBar)); progressUpdater.addListener(new FileUploadProgressListener() { public void operationUpdate(float elaborated) { // } public void operationInitializing() { } public void operationFailed(Throwable caught, String reason, String failureDetails) { card.showErrorAndHide("Error uploading the csv file", reason, failureDetails, caught); } public void operationComplete() { card.setEnableNextButton(true); cancelButton.disable(); } }); } protected void startUpload() { disableUpload(); StringBuilder actionUrl = new StringBuilder(); actionUrl.append(GWT.getModuleBaseURL()); actionUrl.append(UPLOAD_SERVLET); setAction(actionUrl.toString()); Log.info("Start Upload action Url " + actionUrl.toString()); submit(); progressUpdater.scheduleRepeating(STATUS_POLLING_DELAY); } protected void disableUpload() { fileUploadField.disable(); uploadButton.disable(); uploadProgressBar.show(); cancelButton.show(); } }