a0d11b0c419b84eadb35667d80a92aaeb6bd94ec
[pithos-web-client] / src / gr / grnet / pithos / web / client / FileUploadDialog.java
1 /*
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35 package gr.grnet.pithos.web.client;
36
37 import gr.grnet.pithos.web.client.foldertree.Folder;
38
39 import com.google.gwt.core.client.Scheduler;
40 import com.google.gwt.dom.client.NativeEvent;
41 import com.google.gwt.event.dom.client.ClickEvent;
42 import com.google.gwt.event.dom.client.ClickHandler;
43 import com.google.gwt.event.dom.client.KeyCodes;
44 import com.google.gwt.event.logical.shared.CloseEvent;
45 import com.google.gwt.event.logical.shared.CloseHandler;
46 import com.google.gwt.http.client.URL;
47 import com.google.gwt.user.client.Command;
48 import com.google.gwt.user.client.Event.NativePreviewEvent;
49 import com.google.gwt.user.client.ui.Anchor;
50 import com.google.gwt.user.client.ui.Button;
51 import com.google.gwt.user.client.ui.DialogBox;
52 import com.google.gwt.user.client.ui.FileUpload;
53 import com.google.gwt.user.client.ui.FlowPanel;
54 import com.google.gwt.user.client.ui.FormPanel;
55 import com.google.gwt.user.client.ui.Grid;
56 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
57 import com.google.gwt.user.client.ui.Hidden;
58 import com.google.gwt.user.client.ui.HorizontalPanel;
59 import com.google.gwt.user.client.ui.Label;
60 import com.google.gwt.user.client.ui.PopupPanel;
61 import com.google.gwt.user.client.ui.VerticalPanel;
62
63 /**
64  * The 'File upload' dialog box implementation.
65  */
66 public class FileUploadDialog extends DialogBox {
67
68     public static final boolean DONE = true;
69
70         /**
71          * The Form element that performs the file upload.
72          */
73     protected final FormPanel form = new FormPanel();
74
75         private final FileUpload upload = new FileUpload();
76
77         private final Label filenameLabel = new Label();
78
79     private final Label foldernameLabel = new Label();
80
81     private Button submit;
82
83         protected Folder folder;
84
85     protected Pithos app;
86
87         /**
88          * The widget's constructor.
89          */
90         public FileUploadDialog(Pithos _app) {
91                 app = _app;
92                 Anchor close = new Anchor();
93                 close.addStyleName("close");
94                 close.addClickHandler(new ClickHandler() {
95                         
96                         @Override
97                         public void onClick(ClickEvent event) {
98                                 close();
99                         }
100                 });
101                 // Set the dialog's caption.
102                 setText("File upload");
103                 setAnimationEnabled(true);
104                 setGlassEnabled(true);
105                 setStyleName("pithos-DialogBox");
106                 setVisible(false);
107                 
108                 // Since we're going to add a FileUpload widget, we'll need to set the
109                 // form to use the POST method, and multipart MIME encoding.
110                 form.setEncoding(FormPanel.ENCODING_MULTIPART);
111                 form.setMethod(FormPanel.METHOD_POST);
112
113                 // Create a panel to hold all of the form widgets.
114                 VerticalPanel panel = new VerticalPanel();
115                 panel.setWidth("470px");
116                 panel.add(close);
117                 form.setWidget(panel);
118
119                 VerticalPanel inner = new VerticalPanel();
120                 inner.addStyleName("inner");
121
122         final Hidden auth = new Hidden("X-Auth-Token");
123         inner.add(auth);
124                 upload.setName("X-Object-Data");
125                 upload.setVisible(false);
126                 filenameLabel.setText("");
127                 filenameLabel.setVisible(false);
128                 filenameLabel.setStyleName("props-labels");
129                 HorizontalPanel fileUploadPanel = new HorizontalPanel();
130                 fileUploadPanel.setVisible(false);
131                 fileUploadPanel.add(filenameLabel);
132                 fileUploadPanel.add(upload);
133                 Grid generalTable = new Grid(2, 2);
134                 generalTable.setText(0, 0, "Folder");
135         generalTable.setWidget(0, 1, foldernameLabel);
136                 generalTable.setWidget(1, 1, fileUploadPanel);
137                 generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
138         generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
139         generalTable.getCellFormatter().setVisible(1, 0, false);
140                 generalTable.setCellSpacing(4);
141
142                 inner.add(generalTable);
143
144                 FlowPanel uploader = new FlowPanel();
145                 uploader.getElement().setId("uploader");
146                 inner.add(uploader);
147                 
148                 panel.add(inner);
149                 panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
150                 
151                 setWidget(form);
152         }
153
154         private void refreshFolder() {
155                 if (app.getSelectedTree().equals(app.getFolderTreeView()))
156                         app.updateFolder(folder, true, new Command() {
157                                 
158                                 @Override
159                                 public void execute() {
160                                         app.updateStatistics();
161                                 }
162                         });
163                 else
164                         app.updateOtherSharedFolder(folder, true);
165         }
166         
167         native void setupUpload(FileUploadDialog dlg, String path, String token) /*-{
168                 var uploader = $wnd.$('#uploader').pluploadQueue();
169                 var createUploader = function() {
170                         $wnd.$("#uploader").pluploadQueue({
171                                 // General settings
172                                 runtimes : 'html5, flash, gears, silverlight, browserplus, html4',
173                                 unique_names : true,
174                 
175                                 // Flash settings
176                                 flash_swf_url : 'plupload/js/plupload.flash.swf',
177                 
178                                 // Silverlight settings
179                                 silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
180                                 
181                                 multiple_queues: true,
182                                 
183                                 preinit: {
184                                         Init: function(up, info) {
185                                                 $wnd.console.log("Init fired");
186                                                 up.settings.file_data_name = "X-Object-Data";                           
187                                         }
188                                         
189                                 },
190                                 
191                                 init: {
192                                         FilesAdded: function(up, files) {
193                                                 for (var j=0; j<files.length; j++)
194                                                         files[j].url = up.path + "/" + files[j].name + "?X-Auth-Token=" + encodeURIComponent(token);
195                                         },
196                                         
197                                         BeforeUpload: function(up, file) {
198                                                 $wnd.console.log('About to upload ' + file.url);
199                                                 up.settings.url = file.url;
200                                         },
201                                         
202                                         FileUploaded: function(up, file, response) {
203                                                 $wnd.console.log('File ' + file.name + ' uploaded');
204                                                 $wnd.console.log('Response: ' + response);
205                                         },
206                                         
207                                         UploadComplete: function(up, files) {
208                                                 $wnd.console.log('All files finished');
209                                                 dlg.@gr.grnet.pithos.web.client.FileUploadDialog::hideUploadIndicator()();
210                                                 dlg.@gr.grnet.pithos.web.client.FileUploadDialog::refreshFolder()();
211                                         },
212                                         
213                                         Error: function(up, error) {
214                                                 $wnd.console.log("Error occured:" + error);
215                                         }
216                                 }
217                         });
218                         return $wnd.$('#uploader').pluploadQueue();
219                 };
220                 
221                 $wnd.console.log(uploader);
222                 if (!uploader) {
223                         uploader = createUploader();
224                 }
225                 else {
226                         var removeAll = true;
227                         var files = uploader.files;
228                         $wnd.console.log('About to check ' + files.length + ' files');
229                         for (var i=0; i<files.length; i++) {
230                                 var f = files[i];
231                                 if (f.status != $wnd.plupload.DONE && f.status != $wnd.plupload.FAILED) {
232                                         removeAll = false;
233                                         break;
234                                 }
235                         }
236                         if (removeAll) {
237                                 $wnd.console.log('About to remove ' + files.length + ' files');
238                                 uploader.destroy();
239                                 uploader = createUploader();
240                                 $wnd.console.log(uploader);
241                         }
242                 }
243                 uploader.path = path;
244         }-*/;
245         
246         native boolean isUploading()/*-{
247                 var uploader = $wnd.$("#uploader").pluploadQueue();
248                 var files = uploader.files;
249                 for (var i=0; i<files.length; i++) {
250                         var f = files[i];
251                         if (f.status == $wnd.plupload.UPLOADING) {
252                                 return true;
253                         }
254                 }
255                 return false;
256         }-*/;
257         
258         @Override
259         protected void onPreviewNativeEvent(NativePreviewEvent event) {
260                 super.onPreviewNativeEvent(event);
261
262                 NativeEvent evt = event.getNativeEvent();
263                 if (evt.getType().equals("keydown"))
264                         // Use the popup's key preview hooks to close the dialog when
265                         // escape is pressed.
266                         switch (evt.getKeyCode()) {
267                                 case KeyCodes.KEY_ESCAPE:
268                                         close();
269                                         break;
270                         }
271         }
272
273         public void setFolder(Folder folder) {
274                 this.folder = folder;
275                 foldernameLabel.setText(folder.getName());
276         }
277
278         @Override
279         public void center() {
280                 app.hideUploadIndicator();
281                 setVisible(true);
282                 setModal(true);
283                 super.center();
284                 String path = app.getApiPath() + folder.getOwner() + folder.getUri();
285                 setupUpload(this, path, app.getToken());
286                 super.center();
287         }
288         
289         private void hideUploadIndicator() {
290                 app.hideUploadIndicator();
291         }
292         
293         void close() {
294                 setVisible(false);
295                 setModal(false);
296                 if (isUploading())
297                         app.showUploadIndicator();
298         }
299 }