248 lines
7.7 KiB
Java
248 lines
7.7 KiB
Java
|
package org.gcube.portlets.user.workspace.client.view.windows;
|
||
|
|
||
|
|
||
|
import org.gcube.portlets.user.workspace.client.AppControllerExplorer;
|
||
|
import org.gcube.portlets.user.workspace.client.ConstantsExplorer;
|
||
|
import org.gcube.portlets.user.workspace.client.event.CompletedFileUploadEvent;
|
||
|
import org.gcube.portlets.user.workspace.client.model.FileModel;
|
||
|
|
||
|
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
|
||
|
import com.extjs.gxt.ui.client.event.ButtonEvent;
|
||
|
import com.extjs.gxt.ui.client.event.Events;
|
||
|
import com.extjs.gxt.ui.client.event.FormEvent;
|
||
|
import com.extjs.gxt.ui.client.event.Listener;
|
||
|
import com.extjs.gxt.ui.client.event.SelectionListener;
|
||
|
import com.extjs.gxt.ui.client.widget.Window;
|
||
|
import com.extjs.gxt.ui.client.widget.button.Button;
|
||
|
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
|
||
|
import com.extjs.gxt.ui.client.widget.form.FormPanel;
|
||
|
import com.extjs.gxt.ui.client.widget.form.FormPanel.Encoding;
|
||
|
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
|
||
|
import com.google.gwt.user.client.Timer;
|
||
|
import com.google.gwt.user.client.ui.Hidden;
|
||
|
|
||
|
/**
|
||
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||
|
*
|
||
|
*/
|
||
|
public class DialogUpload extends Window {
|
||
|
|
||
|
|
||
|
private final FormPanel formPanel = new FormPanel();
|
||
|
private FileUploadField fileUploadField = new FileUploadField();
|
||
|
private MessageBoxWait messageBoxWait = null;
|
||
|
private boolean isStatusCompleted = false;
|
||
|
private Button btnSubmit = new Button("Submit");
|
||
|
private Button btnCancel = new Button("Cancel");
|
||
|
|
||
|
// private Text txtParentPath = new Text();
|
||
|
// private int widthSize = 300;
|
||
|
// private final Dialog simple = new Dialog();
|
||
|
|
||
|
|
||
|
public DialogUpload(String headerTitle, String parentName, final FileModel parent, String fieldLabel){
|
||
|
|
||
|
this.setHeaderVisible(true);
|
||
|
// this.setHeading("File Upload in: " + parentPath);
|
||
|
this.setHeading(headerTitle + parentName);
|
||
|
// this.setHeading(ConstantsExplorer.FILEUPLOADHEADER + parentPath);
|
||
|
|
||
|
// this.txtParentPath.setText("Parent Folder : " + parentPath);
|
||
|
|
||
|
this.setStyleAttribute("margin", "10px");
|
||
|
|
||
|
// Create a FormPanel and point it at a service.
|
||
|
|
||
|
|
||
|
// Create a FormPanel and point it at a service.
|
||
|
formPanel.setHeaderVisible(false);
|
||
|
formPanel.setFrame(true);
|
||
|
formPanel.setAction(ConstantsExplorer.UPLOAD_WORKSPACE_SERVICE);
|
||
|
formPanel.setEncoding(Encoding.MULTIPART);
|
||
|
formPanel.setMethod(Method.POST);
|
||
|
|
||
|
formPanel.setButtonAlign(HorizontalAlignment.CENTER);
|
||
|
formPanel.setWidth(400);
|
||
|
|
||
|
// TextField<String> name = new TextField<String>();
|
||
|
// name.setFieldLabel("Name");
|
||
|
// formPanel.add(name);
|
||
|
|
||
|
fileUploadField.setAllowBlank(false);
|
||
|
fileUploadField.setName("uploadFormElement");
|
||
|
// fileUploadField.setRegex("^[a-zA-Z0-9_-]+[ a-zA-Z0-9_().-]+$");
|
||
|
// fileUploadField.setAutoValidate(true);
|
||
|
// fileUploadField.getMessages().setRegexText(ConstantsExplorer.MESSAGE_NAME_FORCE_APHANUMERIC);
|
||
|
|
||
|
// Add a label
|
||
|
formPanel.add(new Hidden("idFolder",parent.getIdentifier()));
|
||
|
|
||
|
formPanel.add(new Hidden("uploadType",fieldLabel));
|
||
|
// fileUploadField.setFieldLabel(ConstantsExplorer.FILE);
|
||
|
fileUploadField.setFieldLabel(fieldLabel);
|
||
|
|
||
|
// fileUploadField.setWidth(widthSize);
|
||
|
|
||
|
// fileUploadField.setStyleAttribute("width", "350px");
|
||
|
|
||
|
// fileUploadField.setSize(400, 50);
|
||
|
|
||
|
formPanel.add(fileUploadField);
|
||
|
|
||
|
// formPanel.add(this.txtParentPath);
|
||
|
|
||
|
|
||
|
// formPanel.add(btn);
|
||
|
|
||
|
formPanel.addButton(btnSubmit);
|
||
|
|
||
|
formPanel.addButton(btnCancel);
|
||
|
|
||
|
|
||
|
// handle the post
|
||
|
formPanel.addListener(Events.Submit, new Listener<FormEvent>() {
|
||
|
|
||
|
public void handleEvent(FormEvent event) {
|
||
|
// When the form submission is successfully completed, this
|
||
|
// event is
|
||
|
// fired. Assuming the service returned a response of type
|
||
|
// text/html,
|
||
|
|
||
|
isStatusCompleted = true;
|
||
|
|
||
|
// Log.trace("onSubmitComplete");
|
||
|
String result = event.getResultHtml();
|
||
|
|
||
|
// Log.trace("Result "+result);
|
||
|
hide();
|
||
|
|
||
|
messageBoxWait.getMessageBoxWait().close();
|
||
|
|
||
|
if (result == null) {
|
||
|
// MessageUtil.showErrorMessage("Error during upload", "An error occurred during file upload.");
|
||
|
new MessageBoxAlert("Error during upload", "An error occurred during file upload.", null);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
//expected <pre>200:Upload complete</pre>
|
||
|
//we strip tags added by webserver
|
||
|
String strippedResult = result.replace("<pre>", "").replace("</pre>", "");
|
||
|
|
||
|
|
||
|
System.out.println(result);
|
||
|
|
||
|
UploadResultMessage resultMessage = UploadResultMessage.parseResult(strippedResult);
|
||
|
// Log.trace("status: "+resultMessage.getStatus());
|
||
|
|
||
|
switch (resultMessage.getStatus()) {
|
||
|
case ERROR:
|
||
|
new MessageBoxAlert("Error during upload", resultMessage.getMessage(), null);
|
||
|
break;
|
||
|
case UNKNOWN:
|
||
|
new MessageBoxAlert("Error during upload", resultMessage.getMessage(), null);
|
||
|
break;
|
||
|
case WARN: {
|
||
|
// workspace.refreshRoot();
|
||
|
// MessageUtil.showWarnMessage("Upload completed with warnings", resultMessage.getMessage());
|
||
|
new MessageBoxAlert("Upload completed with warnings", resultMessage.getMessage(), null);
|
||
|
break;
|
||
|
}
|
||
|
case OK: {
|
||
|
// workspace.refreshRoot();
|
||
|
new MessageBoxInfo("Upload completed successfully", resultMessage.getMessage(), null);
|
||
|
AppControllerExplorer.getEventBus().fireEvent(new CompletedFileUploadEvent(parent,null));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
add(formPanel);
|
||
|
|
||
|
this.addListeners();
|
||
|
|
||
|
this.setAutoWidth(true);
|
||
|
this.setAutoHeight(true);
|
||
|
|
||
|
this.show();
|
||
|
|
||
|
}
|
||
|
|
||
|
private void addListeners() {
|
||
|
|
||
|
// fileUploadField.addListener(Events.OnChange, new Listener<BaseEvent>() {
|
||
|
//
|
||
|
// @Override
|
||
|
// public void handleEvent(BaseEvent be) {
|
||
|
//// Window.alert("qui");
|
||
|
// }
|
||
|
// });
|
||
|
|
||
|
|
||
|
// fileUploadField.addKeyListener(new KeyListener() { // KEY ENTER
|
||
|
//
|
||
|
// public void componentKeyPress(ComponentEvent event) {
|
||
|
// if (event.getKeyCode() == KeyboardEvents.Enter.getEventCode())
|
||
|
// btnSubmit.fireEvent(Events.OnClick);
|
||
|
//
|
||
|
// }
|
||
|
// });
|
||
|
|
||
|
|
||
|
btnSubmit.addSelectionListener(new SelectionListener<ButtonEvent>() {
|
||
|
@Override
|
||
|
public void componentSelected(ButtonEvent ce) {
|
||
|
if (fileUploadField.getValue()==null || !(fileUploadField.getValue().length()>2)) {
|
||
|
new MessageBoxAlert(ConstantsExplorer.ERROR, ConstantsExplorer.NOFILESPECIFIED, null);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
messageBoxWait = new MessageBoxWait(ConstantsExplorer.PROGRESS, ConstantsExplorer.SAVINGYOURFILE, fileUploadField.getValue());
|
||
|
|
||
|
//Progress bar for upload
|
||
|
final Timer t = new Timer()
|
||
|
{
|
||
|
public void run()
|
||
|
{
|
||
|
if (isStatusCompleted)
|
||
|
{
|
||
|
cancel();
|
||
|
messageBoxWait.getMessageBoxWait().close();
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
t.scheduleRepeating(500);
|
||
|
|
||
|
|
||
|
formPanel.submit();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
|
||
|
btnCancel.addSelectionListener(new SelectionListener<ButtonEvent>() {
|
||
|
@Override
|
||
|
public void componentSelected(ButtonEvent ce) {
|
||
|
hide();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
//
|
||
|
// // Add an event handler to the form.
|
||
|
// formPanel.addHandler(Events., Type<new FormHandler() {
|
||
|
//
|
||
|
// @Override
|
||
|
// public void onSubmitComplete(FormSubmitCompleteEvent event) {
|
||
|
// // TODO Auto-generated method stub
|
||
|
//
|
||
|
// }
|
||
|
//
|
||
|
// @Override
|
||
|
// public void onSubmit(FormSubmitEvent event) {
|
||
|
// // TODO Auto-generated method stub
|
||
|
//
|
||
|
// }
|
||
|
// });
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|