Fixed styling of plupload to match the rest of the ui
[pithos-web-client] / src / gr / grnet / pithos / web / client / FileUploadDialog.java
index 3026d4c..919c4fd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -37,20 +37,16 @@ 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.core.client.Scheduler;
 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.Event.NativePreviewEvent;
+import com.google.gwt.user.client.Command;
+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.Grid;
 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
 import com.google.gwt.user.client.ui.Hidden;
@@ -85,10 +81,24 @@ public class FileUploadDialog extends DialogBox {
        /**
         * The widget's constructor.
         */
-       public FileUploadDialog() {
+       public FileUploadDialog(Pithos _app, Folder _folder) {
+               app = _app;
+               folder = _folder;
+               foldernameLabel.setText(folder.getName());
+               Anchor close = new Anchor();
+               close.addStyleName("close");
+               close.addClickHandler(new ClickHandler() {
+                       
+                       @Override
+                       public void onClick(ClickEvent event) {
+                               hide();
+                       }
+               });
                // Set the dialog's caption.
                setText("File upload");
                setAnimationEnabled(true);
+               setGlassEnabled(true);
+               setStyleName("pithos-DialogBox");
                // 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);
@@ -96,178 +106,102 @@ public class FileUploadDialog extends DialogBox {
 
                // Create a panel to hold all of the form widgets.
                VerticalPanel panel = new VerticalPanel();
+               panel.add(close);
                form.setWidget(panel);
 
-        final Hidden auth = new Hidden("X-Auth-Token", "");
-        panel.add(auth);
+               VerticalPanel inner = new VerticalPanel();
+               inner.addStyleName("inner");
+
+        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);
 
-               panel.add(generalTable);
-
-               // Create a panel to hold the buttons.
-               HorizontalPanel buttons = new HorizontalPanel();
-
-               // 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();
-                       }
-               });
-               buttons.add(submit);
-               buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);
-               // Create the 'Cancel' button, along with a listener that hides the
-               // dialog when the button is clicked.
-               final Button cancel = new Button("Cancel", new ClickHandler() {
+               inner.add(generalTable);
+
+               FlowPanel uploader = new FlowPanel();
+               uploader.getElement().setId("uploader");
+               inner.add(uploader);
+               
+               panel.add(inner);
+               panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
+               
+               
+               Scheduler.get().scheduleDeferred(new Command() {
+                       
                        @Override
-                       public void onClick(@SuppressWarnings("unused") ClickEvent event) {
-                               hide();
-                       }
-               });
-               buttons.add(cancel);
-               buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
-               buttons.setSpacing(8);
-        panel.add(buttons);
-        panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
-
-               // 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
-                       }
-               });
-               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);
-                               }
-                app.updateFolder(folder, true, null);
-                               hide();
+                       public void execute() {
+                               String path = app.getApiPath() + folder.getOwner() + folder.getUri();
+                               setupUpload(FileUploadDialog.this, path, app.getToken());
                        }
                });
-
                setWidget(form);
        }
 
-       @Override
-       protected void onPreviewNativeEvent(NativePreviewEvent preview) {
-               super.onPreviewNativeEvent(preview);
-
-               NativeEvent evt = preview.getNativeEvent();
-               if (evt.getType().equals("keydown"))
-                       // Use the popup's key preview hooks to close the dialog when either
-                       // enter or escape is pressed.
-                       switch (evt.getKeyCode()) {
-                               case KeyCodes.KEY_ENTER:
-                                       prepareAndSubmit();
-                                       break;
-                               case KeyCodes.KEY_ESCAPE:
-                                       hide();
-                                       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() + app.getUsername() + 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();
-                               }
-
+       private void refreshFolder() {
+               if (app.getSelectedTree().equals(app.getFolderTreeView()))
+                       app.updateFolder(folder, true, new Command() {
+                               
                                @Override
-                               public void confirm() {
-                                       form.submit();
+                               public void execute() {
+                                       app.updateStatistics();
                                }
-
-                       };
-                       confirm.center();
-               }
-       }
-
-    /**
-        * 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);
-       }
-
-       protected File getFileForName(String name){
-               for (File f : folder.getFiles())
-                       if (f.getName().equals(name))
-                               return f;
-               return null;
+                       });
+               else
+                       app.updateOtherSharedFolder(folder, true);
        }
+       
+       native void setupUpload(FileUploadDialog dlg, String path, String token) /*-{
+               $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',
+                       
+                       preinit: {
+                               Init: function(up, info) {
+                                       up.settings.file_data_name = "X-Object-Data";                           
+                               }
+                       },
+                       
+                       init: {
+                               BeforeUpload: function(up, file) {
+                                       $wnd.console.log('About to upload ' + file.name);
+                                       up.settings.url = path + "/" + file.name + "?X-Auth-Token=" + token;
+                               },
+                               
+                               FileUploaded: function(up, file, response) {
+                                       $wnd.console.log('File ' + file.name + ' uploaded');
+                                       $wnd.console.log('Response: ' + response);
+                               },
+                               
+                               UploadComplete: function(up, files) {
+                                       $wnd.console.log('All files finished');
+                                       dlg.@gr.grnet.pithos.web.client.FileUploadDialog::refreshFolder()();
+                               }
+                       }
+               });
 
-    public void setApp(Pithos app) {
-        this.app = app;
-    }
-
-    public void setFolder(Folder folder) {
-        this.folder = folder;
-        foldernameLabel.setText(folder.getName());
-    }
+           dlg.@gr.grnet.pithos.web.client.FileUploadDialog::center()();
+       }-*/;
 }