/*
- * Copyright 2011 GRNET S.A. All rights reserved.
+ * Copyright 2011-2012 GRNET S.A. All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
*/
package gr.grnet.pithos.web.client;
-import gr.grnet.pithos.web.client.foldertree.File;
import gr.grnet.pithos.web.client.foldertree.Folder;
-import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Command;
-import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Event.NativePreviewEvent;
-import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
+import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormPanel;
-import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
-import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
-import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
-import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
-import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Hidden;
/**
* The widget's constructor.
*/
- public FileUploadDialog() {
- Anchor close = new Anchor();
+ public FileUploadDialog(Pithos _app) {
+ app = _app;
+ Anchor close = new Anchor("close");
close.addStyleName("close");
close.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
- hide();
+ close();
}
});
// Set the dialog's caption.
setAnimationEnabled(true);
setGlassEnabled(true);
setStyleName("pithos-DialogBox");
+ setVisible(false);
+
// Since 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);
// Create a panel to hold all of the form widgets.
VerticalPanel panel = new VerticalPanel();
+ panel.setWidth("470px");
panel.add(close);
form.setWidget(panel);
VerticalPanel inner = new VerticalPanel();
inner.addStyleName("inner");
- final Hidden auth = new Hidden("X-Auth-Token", "");
+ final Hidden auth = new Hidden("X-Auth-Token");
inner.add(auth);
upload.setName("X-Object-Data");
+ upload.setVisible(false);
filenameLabel.setText("");
filenameLabel.setVisible(false);
filenameLabel.setStyleName("props-labels");
HorizontalPanel fileUploadPanel = new HorizontalPanel();
+ fileUploadPanel.setVisible(false);
fileUploadPanel.add(filenameLabel);
fileUploadPanel.add(upload);
Grid generalTable = new Grid(2, 2);
generalTable.setText(0, 0, "Folder");
generalTable.setWidget(0, 1, foldernameLabel);
- generalTable.setText(1, 0, "File");
generalTable.setWidget(1, 1, fileUploadPanel);
generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
- generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");
- generalTable.getCellFormatter().setStyleName(1, 1, "props-values");
+ generalTable.getCellFormatter().setVisible(1, 0, false);
generalTable.setCellSpacing(4);
inner.add(generalTable);
- // Create the 'upload' button, along with a listener that submits the
- // form.
- submit = new Button("Upload", new ClickHandler() {
- @Override
- public void onClick(@SuppressWarnings("unused") ClickEvent event) {
- prepareAndSubmit();
- }
- });
- submit.addStyleName("button");
- inner.add(submit);
-
- // Add an event handler to the form.
- form.addSubmitHandler(new SubmitHandler() {
-
- @Override
- public void onSubmit(@SuppressWarnings("unused") SubmitEvent event) {
- auth.setValue(app.getToken()); //This is done here because the app object is not available in the constructor
- Cookies.setCookie("X-Auth-Token", app.getToken(), null, Window.Location.getHostName(), app.getApiPath(), false);
- }
- });
- form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
-
- @Override
- public void onSubmitComplete(SubmitCompleteEvent event) {
- // When the form submission is successfully completed, this
- // event is fired. Assuming the service returned a response
- // of type text/html, we can get the result text here (see
- // the FormPanel documentation for further explanation).
- String results = event.getResults();
-
- // Unfortunately the results are never empty, even in
- // the absense of errors, so we have to check for '<pre></pre>'.
- if (results != null && results.length() > 0 && !results.equalsIgnoreCase("<pre></pre>")) {
- GWT.log(results, null);
- app.displayError(results);
- }
- if (app.getSelectedTree().equals(app.getFolderTreeView()))
- app.updateFolder(folder, true, new Command() {
-
- @Override
- public void execute() {
- app.updateStatistics();
- }
- });
- else
- app.updateOtherSharedFolder(folder, true);
- hide();
- }
- });
-
+ FlowPanel uploader = new FlowPanel();
+ uploader.getElement().setId("uploader");
+ inner.add(uploader);
+
panel.add(inner);
panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
setWidget(form);
}
+ private void refreshFolder() {
+ if (app.getSelectedTree().equals(app.getFolderTreeView()))
+ app.updateFolder(folder, true, new Command() {
+
+ @Override
+ public void execute() {
+ app.updateStatistics();
+ }
+ }, true);
+ else
+ app.updateOtherSharedFolder(folder, true);
+ }
+
+ native void setupUpload(FileUploadDialog dlg, String path, String token) /*-{
+ var uploader = $wnd.$('#uploader').pluploadQueue();
+ var createUploader = function() {
+ $wnd.$("#uploader").pluploadQueue({
+ // General settings
+ runtimes : 'html5, flash, gears, silverlight, browserplus, html4',
+ unique_names : true,
+
+ // Flash settings
+ flash_swf_url : 'plupload/js/plupload.flash.swf',
+
+ // Silverlight settings
+ silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
+
+ multiple_queues: true,
+
+ preinit: {
+ Init: function(up, info) {
+ if ($wnd.console && $wnd.console.log)
+ $wnd.console.log("Init fired");
+ up.settings.file_data_name = "X-Object-Data";
+ }
+
+ },
+
+ init: {
+ FilesAdded: function(up, files) {
+ for (var j=0; j<files.length; j++)
+ files[j].url = up.path + "/" + files[j].name + "?X-Auth-Token=" + encodeURIComponent(token);
+ },
+
+ BeforeUpload: function(up, file) {
+ if ($wnd.console && $wnd.console.log)
+ $wnd.console.log('About to upload ' + file.url);
+ up.settings.url = file.url;
+ },
+
+ FileUploaded: function(up, file, response) {
+ if ($wnd.console && $wnd.console.log) {
+ $wnd.console.log('File ' + file.name + ' uploaded');
+ $wnd.console.log('Response: ' + response);
+ }
+ },
+
+ UploadComplete: function(up, files) {
+ if ($wnd.console && $wnd.console.log)
+ $wnd.console.log('All files finished');
+ dlg.@gr.grnet.pithos.web.client.FileUploadDialog::hideUploadIndicator()();
+ dlg.@gr.grnet.pithos.web.client.FileUploadDialog::refreshFolder()();
+ },
+
+ Error: function(up, error) {
+ if ($wnd.console && $wnd.console.log)
+ $wnd.console.log("Error occured:" + error);
+ }
+ }
+ });
+ return $wnd.$('#uploader').pluploadQueue();
+ };
+
+ if ($wnd.console && $wnd.console.log)
+ $wnd.console.log(uploader);
+ if (!uploader) {
+ uploader = createUploader();
+ }
+ else {
+ var removeAll = true;
+ var files = uploader.files;
+ if ($wnd.console && $wnd.console.log)
+ $wnd.console.log('About to check ' + files.length + ' files');
+ for (var i=0; i<files.length; i++) {
+ var f = files[i];
+ if (f.status != $wnd.plupload.DONE && f.status != $wnd.plupload.FAILED) {
+ removeAll = false;
+ break;
+ }
+ }
+ if (removeAll) {
+ if ($wnd.console && $wnd.console.log)
+ $wnd.console.log('About to remove ' + files.length + ' files');
+ uploader.destroy();
+ uploader = createUploader();
+ if ($wnd.console && $wnd.console.log)
+ $wnd.console.log(uploader);
+ }
+ }
+ uploader.path = path;
+ }-*/;
+
+ native boolean isUploading()/*-{
+ var uploader = $wnd.$("#uploader").pluploadQueue();
+ var files = uploader.files;
+ for (var i=0; i<files.length; i++) {
+ var f = files[i];
+ if (f.status == $wnd.plupload.UPLOADING) {
+ return true;
+ }
+ }
+ return false;
+ }-*/;
+
@Override
- protected void onPreviewNativeEvent(NativePreviewEvent preview) {
- super.onPreviewNativeEvent(preview);
+ protected void onPreviewNativeEvent(NativePreviewEvent event) {
+ super.onPreviewNativeEvent(event);
- NativeEvent evt = preview.getNativeEvent();
+ NativeEvent evt = event.getNativeEvent();
if (evt.getType().equals("keydown"))
- // Use the popup's key preview hooks to close the dialog when either
- // enter or escape is pressed.
+ // Use the popup's key preview hooks to close the dialog when
+ // escape is pressed.
switch (evt.getKeyCode()) {
- case KeyCodes.KEY_ENTER:
- prepareAndSubmit();
- break;
case KeyCodes.KEY_ESCAPE:
- hide();
+ close();
break;
}
}
- /**
- * Make any last minute checks and start the upload.
- */
- protected void prepareAndSubmit() {
- if (upload.getFilename().length() == 0) {
- app.displayError("You must select a file!");
- return;
- }
- final String fname = getFilename(upload.getFilename());
- String apath = app.getApiPath() + folder.getOwner() + folder.getUri() + "/" + fname;
- form.setAction(apath);
- submit.setEnabled(false);
- upload.setVisible(false);
- filenameLabel.setText(fname);
- filenameLabel.setVisible(true);
-
- if (getFileForName(fname) == null) {
- form.submit();
- }
- else {
- // We are going to update an existing file, so show a confirmation dialog.
- ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
- "you want to update " + fname + "?", "Update") {
-
- @Override
- public void cancel() {
- FileUploadDialog.this.hide();
- }
-
- @Override
- public void confirm() {
- form.submit();
- }
-
- };
- confirm.center();
- }
+ public void setFolder(Folder folder) {
+ this.folder = folder;
+ foldernameLabel.setText(folder.getName());
}
- /**
- * Returns the file name from a potential full path argument. Apparently IE
- * insists on sending the full path name of a file when uploading, forcing
- * us to trim the extra path info. Since this is only observed on Windows we
- * get to check for a single path separator value.
- *
- * @param name the potentially full path name of a file
- * @return the file name without extra path information
- */
- protected String getFilename(String name) {
- int pathSepIndex = name.lastIndexOf("\\");
- if (pathSepIndex == -1) {
- pathSepIndex = name.lastIndexOf("/");
- if (pathSepIndex == -1)
- return name;
- }
- return name.substring(pathSepIndex + 1);
+ @Override
+ public void center() {
+ app.hideUploadIndicator();
+ setVisible(true);
+ setModal(true);
+ super.center();
+ String path = app.getApiPath() + folder.getOwner() + folder.getUri();
+ setupUpload(this, path, app.getToken());
+ super.center();
}
-
- protected File getFileForName(String name){
- for (File f : folder.getFiles())
- if (f.getName().equals(name))
- return f;
- return null;
+
+ private void hideUploadIndicator() {
+ app.hideUploadIndicator();
+ }
+
+ void close() {
+ setVisible(false);
+ setModal(false);
+ if (isUploading())
+ app.showUploadIndicator();
}
-
- public void setApp(Pithos app) {
- this.app = app;
- }
-
- public void setFolder(Folder folder) {
- this.folder = folder;
- foldernameLabel.setText(folder.getName());
- }
}