Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.9 kB)

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.user.client.Command;
45
import com.google.gwt.user.client.Event.NativePreviewEvent;
46
import com.google.gwt.user.client.ui.Anchor;
47
import com.google.gwt.user.client.ui.Button;
48
import com.google.gwt.user.client.ui.DialogBox;
49
import com.google.gwt.user.client.ui.FileUpload;
50
import com.google.gwt.user.client.ui.FlowPanel;
51
import com.google.gwt.user.client.ui.FormPanel;
52
import com.google.gwt.user.client.ui.Grid;
53
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
54
import com.google.gwt.user.client.ui.Hidden;
55
import com.google.gwt.user.client.ui.HorizontalPanel;
56
import com.google.gwt.user.client.ui.Label;
57
import com.google.gwt.user.client.ui.VerticalPanel;
58

    
59
/**
60
 * The 'File upload' dialog box implementation.
61
 */
62
public class FileUploadDialog extends DialogBox {
63

    
64
    public static final boolean DONE = true;
65

    
66
    Anchor close;
67
    
68
        /**
69
         * The Form element that performs the file upload.
70
         */
71
    protected final FormPanel form = new FormPanel();
72

    
73
        private final FileUpload upload = new FileUpload();
74

    
75
        private final Label filenameLabel = new Label();
76

    
77
    private final Label foldernameLabel = new Label();
78

    
79
    private Button submit;
80

    
81
        protected Folder folder;
82

    
83
    protected Pithos app;
84
    
85
    private boolean inProgress = false;
86

    
87
        /**
88
         * The widget's constructor.
89
         */
90
        public FileUploadDialog(Pithos _app) {
91
                app = _app;
92
                close = new Anchor("close");
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
                Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
154
                        
155
                        @Override
156
                        public void execute() {
157
                                center();
158
                                close();
159
                        }
160
                });
161
        }
162

    
163
        private void refreshFolder() {
164
                if (app.getSelectedTree().equals(app.getFolderTreeView()))
165
                        app.updateFolder(folder, true, new Command() {
166
                                
167
                                @Override
168
                                public void execute() {
169
                                        app.updateStatistics();
170
                                }
171
                        }, true);
172
                else
173
                        app.updateOtherSharedFolder(folder, true, null);
174
        }
175
        
176
        native void setupUpload(FileUploadDialog dlg, Pithos app, String token) /*-{
177
                var uploader = $wnd.$('#uploader').pluploadQueue();
178
                var createUploader = function() {
179
                        $wnd.$("#uploader").pluploadQueue({
180
                                // General settings
181
                                runtimes : 'html5, flash, gears, silverlight, browserplus, html4',
182
                                unique_names : true,
183
                
184
                                // Flash settings
185
                                flash_swf_url : 'plupload/js/plupload.flash.swf',
186
                
187
                                // Silverlight settings
188
                                silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
189
                                
190
                                multiple_queues: true,
191
                                
192
                                preinit: {
193
                                        Init: function(up, info) {
194
                                                if ($wnd.console && $wnd.console.log)
195
                                                        $wnd.console.log("Init fired");
196
                                                up.settings.file_data_name = "X-Object-Data";                                
197
                                        }
198
                                        
199
                                },
200
                                
201
                                init: {
202
                                        FilesAdded: function(up, files) {
203
                                                var api = app.@gr.grnet.pithos.web.client.Pithos::getApiPath()();
204
                                                var folder = app.@gr.grnet.pithos.web.client.Pithos::getUploadFolder()();
205
                                                var owner = folder.@gr.grnet.pithos.web.client.foldertree.Folder::getOwner()();
206
                                                var uri = folder.@gr.grnet.pithos.web.client.foldertree.Folder::getUri()();
207
                                                var path = api + owner + uri;
208
                                                for (var j=0; j<files.length; j++)
209
                                                        files[j].url = path + "/" + files[j].name;
210
                                                dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(true);
211
                                                up.start();
212
                                                app.@gr.grnet.pithos.web.client.Pithos::showUploadIndicator()();
213
                                                if (!dlg.@gr.grnet.pithos.web.client.FileUploadDialog::isVisible()())
214
                                                        app.@gr.grnet.pithos.web.client.Pithos::showUploadAlert(I)(files.length);
215
                                        },
216
                                        
217
                                        FilesRemoved: function(up, files) {
218
                                                if (up.files.length == 0)
219
                                                        dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(false);
220
                                                else
221
                                                        dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(true);
222
                                        },
223
                                        
224
                                        BeforeUpload: function(up, file) {
225
                                                if ($wnd.console && $wnd.console.log)
226
                                                        $wnd.console.log('About to upload ' + file.url);
227
                                                up.settings.url = file.url + + "?X-Auth-Token=" + encodeURIComponent(token);
228
                                        },
229
                                        
230
                                        UploadProgress: function(up, file) {
231
                                                $wnd.$('#upload_alert_progress_bar').css('width', up.total.percent + '%');
232
                                                $wnd.$('#upload_alert_percent').html(up.total.percent + '%');
233
                                        },
234
                                        
235
                                        FileUploaded: function(up, file, response) {
236
                                                if ($wnd.console && $wnd.console.log) {
237
                                                        $wnd.console.log('File ' + file.name + ' uploaded');
238
                                                        $wnd.console.log('Response: ' + response);
239
                                                }
240
                                                var folder = app.@gr.grnet.pithos.web.client.Pithos::getUploadFolder()();
241
                                                if (folder == file.folder)
242
                                                        app.@gr.grnet.pithos.web.client.Pithos::updateUploadFolder()();
243
                                        },
244
                                        
245
                                        UploadComplete: function(up, files) {
246
                                                if ($wnd.console && $wnd.console.log) {
247
                                                        $wnd.console.log('All files finished');
248
                                                }
249
                                                dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(false);
250
                                                dlg.@gr.grnet.pithos.web.client.FileUploadDialog::hideUploadIndicator()();
251
                                                app.@gr.grnet.pithos.web.client.Pithos::hideUploadAlert()();
252
                                                var uris = [];
253
                                                if (!dlg.@gr.grnet.pithos.web.client.FileUploadDialog::isVisible()())
254
                                                        while (files.length > 0) {
255
                                                                uris.push(files[0].url);
256
                                                                up.removeFile(files[0]);
257
                                                        }
258
                                                else
259
                                                        for (var i=0; i<files.length; i++)
260
                                                                uris.push(files[i].url);
261
                                                app.@gr.grnet.pithos.web.client.Pithos::updateUploadFolder(Lcom/google/gwt/core/client/JsArrayString;)(uris);
262
                                        },
263
                                        
264
                                        Error: function(up, error) {
265
                                                if ($wnd.console && $wnd.console.log)
266
                                                        $wnd.console.log("Error occured:" + error);
267
                                        }
268
                                }
269
                        });
270
                        return $wnd.$('#uploader').pluploadQueue();
271
                };
272
                
273
                if ($wnd.console && $wnd.console.log)
274
                        $wnd.console.log(uploader);
275
                if (!uploader) {
276
                        uploader = createUploader();
277
                }
278
                else {
279
                        var dropElm = $wnd.document.getElementById('rightPanel');
280
                        $wnd.plupload.removeAllEvents(dropElm, uploader.id);
281
                        var removeAll = true;
282
                        var files = uploader.files;
283
                        if ($wnd.console && $wnd.console.log)
284
                                $wnd.console.log('About to check ' + files.length + ' files');
285
                        for (var i=0; i<files.length; i++) {
286
                                var f = files[i];
287
                                if (f.status != $wnd.plupload.DONE && f.status != $wnd.plupload.FAILED) {
288
                                        removeAll = false;
289
                                        break;
290
                                }
291
                        }
292
                        if (removeAll) {
293
                                if ($wnd.console && $wnd.console.log)
294
                                        $wnd.console.log('About to remove ' + files.length + ' files');
295
                                uploader.destroy();
296
                                uploader = createUploader();
297
                                if ($wnd.console && $wnd.console.log)
298
                                        $wnd.console.log(uploader);
299
                                dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(false);
300
                        }
301
                        else {
302
                                dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(true);
303
                        }
304
                }
305
        }-*/;
306
        
307
        native boolean isUploading()/*-{
308
                var uploader = $wnd.$("#uploader").pluploadQueue();
309
                var files = uploader.files;
310
                for (var i=0; i<files.length; i++) {
311
                        var f = files[i];
312
                        if (f.status == $wnd.plupload.UPLOADING) {
313
                                return true;
314
                        }
315
                }
316
                return false;
317
        }-*/;
318
        
319
        @Override
320
        protected void onPreviewNativeEvent(NativePreviewEvent event) {
321
                super.onPreviewNativeEvent(event);
322

    
323
                NativeEvent evt = event.getNativeEvent();
324
                if (evt.getType().equals("keydown"))
325
                        // Use the popup's key preview hooks to close the dialog when
326
                        // escape is pressed.
327
                        switch (evt.getKeyCode()) {
328
                                case KeyCodes.KEY_ESCAPE:
329
                                        close();
330
                                        break;
331
                        }
332
        }
333

    
334
        public void setFolder(Folder folder) {
335
                this.folder = folder;
336
                foldernameLabel.setText(folder.getName());
337
        }
338

    
339
        @Override
340
        public void center() {
341
                app.hideUploadIndicator();
342
                setVisible(true);
343
                setModal(true);
344
                super.center();
345
                setupUpload(this, app, app.getToken());
346
                super.center();
347
        }
348
        
349
        private void hideUploadIndicator() {
350
                app.hideUploadIndicator();
351
        }
352
        
353
        void close() {
354
                setVisible(false);
355
                setModal(false);
356
                clearUploader();
357
                if (isUploading())
358
                        app.showUploadIndicator();
359
                setGlobalDropArea();
360
        }
361

    
362
        private native void clearUploader() /*-{
363
                var uploader = $wnd.$("#uploader").pluploadQueue();
364
                var files = uploader.files;
365
                while (files.length > 0)
366
                        uploader.removeFile(files[0]);
367
        }-*/;
368
        
369
        native void setGlobalDropArea() /*-{
370
                var uploader = $wnd.$("#uploader").pluploadQueue();
371
                if (uploader.runtime == 'html5') {
372
                        uploader.settings.drop_element = 'rightPanel';
373
                        uploader.trigger('PostInit');
374
                }
375
        }-*/;
376

    
377
        private void setInProgress(boolean _inProgress) {
378
                inProgress = _inProgress;
379
                if (inProgress)
380
                        close.setText("hide");
381
                else
382
                        close.setText("close");
383
        }
384
}