Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FileUploadDialog.java @ d10f64c1

History | View | Annotate | Download (11.5 kB)

1
/*
2
 * Copyright 2011-2013 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 com.google.gwt.core.client.Scheduler;
38
import com.google.gwt.dom.client.NativeEvent;
39
import com.google.gwt.event.dom.client.ClickEvent;
40
import com.google.gwt.event.dom.client.ClickHandler;
41
import com.google.gwt.event.dom.client.KeyCodes;
42
import com.google.gwt.user.client.Command;
43
import com.google.gwt.user.client.Event.NativePreviewEvent;
44
import com.google.gwt.user.client.ui.*;
45
import gr.grnet.pithos.web.client.foldertree.Folder;
46

    
47
/**
48
 * The 'File upload' dialog box implementation.
49
 */
50
public class FileUploadDialog extends DialogBox {
51

    
52
    public static final boolean DONE = true;
53

    
54
    Anchor close;
55
    
56
        /**
57
         * The Form element that performs the file upload.
58
         */
59
    protected final FormPanel form = new FormPanel();
60

    
61
        private final FileUpload upload = new FileUpload();
62

    
63
        private final Label filenameLabel = new Label();
64

    
65
    private final Label foldernameLabel = new Label();
66

    
67
    private Button submit;
68

    
69
        protected Folder folder;
70

    
71
    protected Pithos app;
72
    
73
    private boolean inProgress = false;
74

    
75
        /**
76
         * The widget's constructor.
77
         */
78
        public FileUploadDialog(Pithos _app) {
79
                app = _app;
80
                close = new Anchor("close");
81
                close.addStyleName("close");
82
                close.addClickHandler(new ClickHandler() {
83
                        
84
                        @Override
85
                        public void onClick(ClickEvent event) {
86
                                close();
87
                        }
88
                });
89
                // Set the dialog's caption.
90
                setText("File upload");
91
                setStyleName("pithos-DialogBox");
92
                setVisible(false);
93
                
94
                // Since we're going to add a FileUpload widget, we'll need to set the
95
                // form to use the POST method, and multipart MIME encoding.
96
                form.setEncoding(FormPanel.ENCODING_MULTIPART);
97
                form.setMethod(FormPanel.METHOD_POST);
98

    
99
                // Create a panel to hold all of the form widgets.
100
                VerticalPanel panel = new VerticalPanel();
101
                panel.setWidth("470px");
102
                panel.add(close);
103
                form.setWidget(panel);
104

    
105
                VerticalPanel inner = new VerticalPanel();
106
                inner.addStyleName("inner");
107

    
108
                upload.setName("X-Object-Data");
109
                upload.setVisible(false);
110
                filenameLabel.setText("");
111
                filenameLabel.setVisible(false);
112
                filenameLabel.setStyleName("props-labels");
113
                HorizontalPanel fileUploadPanel = new HorizontalPanel();
114
                fileUploadPanel.setVisible(false);
115
                fileUploadPanel.add(filenameLabel);
116
                fileUploadPanel.add(upload);
117
                Grid generalTable = new Grid(2, 2);
118
                generalTable.setText(0, 0, "Folder");
119
        generalTable.setWidget(0, 1, foldernameLabel);
120
                generalTable.setWidget(1, 1, fileUploadPanel);
121
                generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
122
        generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
123
        generalTable.getCellFormatter().setVisible(1, 0, false);
124
                generalTable.setCellSpacing(4);
125

    
126
                inner.add(generalTable);
127

    
128
                FlowPanel uploader = new FlowPanel();
129
                uploader.getElement().setId("uploader");
130
                inner.add(uploader);
131
                
132
                panel.add(inner);
133
                panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
134
                
135
                setWidget(form);
136
                
137
                Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
138
                        
139
                        @Override
140
                        public void execute() {
141
                                center();
142
                                close();
143
                        }
144
                });
145
        }
146

    
147
        private void refreshFolder() {
148
                if (app.getSelectedTree().equals(app.getFolderTreeView()))
149
                        app.updateFolder(folder, true, new Command() {
150
                                
151
                                @Override
152
                                public void execute() {
153
                                        app.updateStatistics();
154
                                }
155
                        }, true);
156
                else
157
                        app.updateOtherSharedFolder(folder, true, null);
158
        }
159

    
160
        native void setupUpload(FileUploadDialog dlg, Pithos app, String token) /*-{
161
                var uploader = $wnd.$('#uploader').pluploadQueue();
162
                var createUploader = function() {
163
                        $wnd.$("#uploader").pluploadQueue({
164
                                // General settings
165
                                runtimes : 'html5, flash, gears, silverlight, browserplus, html4',
166
                                unique_names : true,
167

168
                                // Flash settings
169
                                flash_swf_url : 'plupload/js/plupload.flash.swf',
170

171
                                // Silverlight settings
172
                                silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
173

174
                                multiple_queues: true,
175

176
                                preinit: {
177
                                        Init: function(up, info) {
178
                                                if ($wnd.console && $wnd.console.log) {
179
                          $wnd.console.log("Init fired");
180
                        }
181
                                                up.settings.file_data_name = "X-Object-Data";
182
                                        }
183

184
                                },
185

186
                                init: {
187
                                        FilesAdded: function(up, files) {
188
                                                var api = @gr.grnet.pithos.web.client.Pithos::getStorageAPIURL()();
189
                                                var folder = app.@gr.grnet.pithos.web.client.Pithos::getUploadFolder()();
190
                                                var owner = folder.@gr.grnet.pithos.web.client.foldertree.Folder::getOwnerID()();
191
                                                var uri = folder.@gr.grnet.pithos.web.client.foldertree.Folder::getUri()();
192
                                                var path = api + owner + uri;
193
                                                for (var j=0; j<files.length; j++)
194
                                                        files[j].url = path + "/" + files[j].name;
195
                                                dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(true);
196
                                                if (up.state == $wnd.plupload.STOPPED)
197
                                                        up.start();
198
                                                app.@gr.grnet.pithos.web.client.Pithos::showUploadIndicator()();
199
                                                if (!dlg.@gr.grnet.pithos.web.client.FileUploadDialog::isVisible()())
200
                                                        app.@gr.grnet.pithos.web.client.Pithos::showUploadAlert(I)(up.files.length);
201
                                        },
202

203
                                        FilesRemoved: function(up, files) {
204
                                                if (up.files.length == 0)
205
                                                        dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(false);
206
                                                else
207
                                                        dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(true);
208
                                        },
209

210
                                        BeforeUpload: function(up, file) {
211
                                                if ($wnd.console && $wnd.console.log)
212
                                                        $wnd.console.log('About to upload ' + file.url);
213
                                                up.settings.url = file.url;
214
                        up.settings.headers = {"X-Auth-Token": token };
215
                                        },
216

217
                                        UploadProgress: function(up, file) {
218
                                                $wnd.$('#upload_alert_progress_bar').css('width', up.total.percent + '%');
219
                                                $wnd.$('#upload_alert_percent').html(up.total.percent + '%');
220
                                        },
221

222
                                        FileUploaded: function(up, file, response) {
223
                                                if ($wnd.console && $wnd.console.log) {
224
                                                        $wnd.console.log('File ' + file.name + ' uploaded');
225
                                                        $wnd.console.log('Response: ' + response);
226
                                                }
227
                                                var folder = app.@gr.grnet.pithos.web.client.Pithos::getUploadFolder()();
228
                                                if (folder == file.folder)
229
                                                        app.@gr.grnet.pithos.web.client.Pithos::updateUploadFolder()();
230
                                        },
231

232
                                        UploadComplete: function(up, files) {
233
                                                if ($wnd.console && $wnd.console.log) {
234
                                                        $wnd.console.log('All files finished');
235
                                                }
236
                                                dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(false);
237
                                                dlg.@gr.grnet.pithos.web.client.FileUploadDialog::hideUploadIndicator()();
238
                                                app.@gr.grnet.pithos.web.client.Pithos::hideUploadAlert()();
239
                                                var uris = [];
240
                                                if (!dlg.@gr.grnet.pithos.web.client.FileUploadDialog::isVisible()())
241
                                                        while (files.length > 0) {
242
                                                                uris.push(files[0].url);
243
                                                                up.removeFile(files[0]);
244
                                                        }
245
                                                else
246
                                                        for (var i=0; i<files.length; i++)
247
                                                                uris.push(files[i].url);
248
                                                app.@gr.grnet.pithos.web.client.Pithos::updateUploadFolder(Lcom/google/gwt/core/client/JsArrayString;)(uris);
249
                                        },
250

251
                                        Error: function(up, error) {
252
                                                if ($wnd.console && $wnd.console.log)
253
                                                        $wnd.console.log("Error occured:" + error);
254
                                        }
255
                                }
256
                        });
257
                        return $wnd.$('#uploader').pluploadQueue();
258
                };
259

260
                if ($wnd.console && $wnd.console.log)
261
                        $wnd.console.log(uploader);
262
                if (!uploader) {
263
                        uploader = createUploader();
264
                }
265
                else {
266
                        var dropElm = $wnd.document.getElementById('rightPanel');
267
                        $wnd.plupload.removeAllEvents(dropElm, uploader.id);
268
                        var removeAll = true;
269
                        var files = uploader.files;
270
                        if ($wnd.console && $wnd.console.log)
271
                                $wnd.console.log('About to check ' + files.length + ' files');
272
                        for (var i=0; i<files.length; i++) {
273
                                var f = files[i];
274
                                if (f.status != $wnd.plupload.DONE && f.status != $wnd.plupload.FAILED) {
275
                                        removeAll = false;
276
                                        break;
277
                                }
278
                        }
279
                        if (removeAll) {
280
                                if ($wnd.console && $wnd.console.log)
281
                                        $wnd.console.log('About to remove ' + files.length + ' files');
282
                                uploader.destroy();
283
                                uploader = createUploader();
284
                                if ($wnd.console && $wnd.console.log)
285
                                        $wnd.console.log(uploader);
286
                                dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(false);
287
                        }
288
                        else {
289
                                dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(true);
290
                        }
291
                }
292
        }-*/;
293

    
294
        native boolean isUploading()/*-{
295
                var uploader = $wnd.$("#uploader").pluploadQueue();
296
                var files = uploader.files;
297
                for (var i=0; i<files.length; i++) {
298
                        var f = files[i];
299
                        if (f.status == $wnd.plupload.UPLOADING) {
300
                                return true;
301
                        }
302
                }
303
                return false;
304
        }-*/;
305
        
306
        @Override
307
        protected void onPreviewNativeEvent(NativePreviewEvent event) {
308
                super.onPreviewNativeEvent(event);
309

    
310
                NativeEvent evt = event.getNativeEvent();
311
                if (evt.getType().equals("keydown"))
312
                        // Use the popup's key preview hooks to close the dialog when
313
                        // escape is pressed.
314
                        switch (evt.getKeyCode()) {
315
                                case KeyCodes.KEY_ESCAPE:
316
                                        close();
317
                                        break;
318
                        }
319
        }
320

    
321
        public void setFolder(Folder folder) {
322
                this.folder = folder;
323
                foldernameLabel.setText(folder.getName());
324
        }
325

    
326
        @Override
327
        public void center() {
328
                app.hideUploadIndicator();
329
                setVisible(true);
330
                setModal(true);
331
                super.center();
332
                setupUpload(this, app, app.getUserToken());
333
                super.center();
334
        }
335
        
336
        private void hideUploadIndicator() {
337
                app.hideUploadIndicator();
338
        }
339
        
340
        void close() {
341
                setVisible(false);
342
                setModal(false);
343
                clearUploader();
344
                if (isUploading())
345
                        app.showUploadIndicator();
346
                setGlobalDropArea();
347
        }
348

    
349
        private native void clearUploader() /*-{
350
                var uploader = $wnd.$("#uploader").pluploadQueue();
351
                var files = uploader.files;
352
                var clear = true;
353
                for (var i=0; i<files.length; i++)
354
                        if (files[i].status == $wnd.plupload.UPLOADING)
355
                                clear = false;
356
                if (clear)
357
                        while (files.length > 0)
358
                                uploader.removeFile(files[0]);
359
        }-*/;
360
        
361
        native void setGlobalDropArea() /*-{
362
                var uploader = $wnd.$("#uploader").pluploadQueue();
363
                if (uploader.runtime == 'html5') {
364
                        uploader.settings.drop_element = 'rightPanel';
365
                        uploader.trigger('PostInit');
366
                }
367
        }-*/;
368

    
369
        private void setInProgress(boolean _inProgress) {
370
                inProgress = _inProgress;
371
                if (inProgress)
372
                        close.setText("hide");
373
                else
374
                        close.setText("close");
375
        }
376
}