root / src / gr / grnet / pithos / web / client / FileUploadDialog.java @ 78989564
History | View | Annotate | Download (9.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.dom.client.NativeEvent; |
40 |
import com.google.gwt.event.dom.client.ClickEvent; |
41 |
import com.google.gwt.event.dom.client.ClickHandler; |
42 |
import com.google.gwt.event.dom.client.KeyCodes; |
43 |
import com.google.gwt.user.client.Command; |
44 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
45 |
import com.google.gwt.user.client.ui.Anchor; |
46 |
import com.google.gwt.user.client.ui.Button; |
47 |
import com.google.gwt.user.client.ui.DialogBox; |
48 |
import com.google.gwt.user.client.ui.FileUpload; |
49 |
import com.google.gwt.user.client.ui.FlowPanel; |
50 |
import com.google.gwt.user.client.ui.FormPanel; |
51 |
import com.google.gwt.user.client.ui.Grid; |
52 |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
53 |
import com.google.gwt.user.client.ui.Hidden; |
54 |
import com.google.gwt.user.client.ui.HorizontalPanel; |
55 |
import com.google.gwt.user.client.ui.Label; |
56 |
import com.google.gwt.user.client.ui.VerticalPanel; |
57 |
|
58 |
/**
|
59 |
* The 'File upload' dialog box implementation.
|
60 |
*/
|
61 |
public class FileUploadDialog extends DialogBox { |
62 |
|
63 |
public static final boolean DONE = true; |
64 |
|
65 |
Anchor close; |
66 |
|
67 |
/**
|
68 |
* The Form element that performs the file upload.
|
69 |
*/
|
70 |
protected final FormPanel form = new FormPanel(); |
71 |
|
72 |
private final FileUpload upload = new FileUpload(); |
73 |
|
74 |
private final Label filenameLabel = new Label(); |
75 |
|
76 |
private final Label foldernameLabel = new Label(); |
77 |
|
78 |
private Button submit; |
79 |
|
80 |
protected Folder folder;
|
81 |
|
82 |
protected Pithos app;
|
83 |
|
84 |
private boolean inProgress = false; |
85 |
|
86 |
/**
|
87 |
* The widget's constructor.
|
88 |
*/
|
89 |
public FileUploadDialog(Pithos _app) {
|
90 |
app = _app; |
91 |
close = new Anchor("close"); |
92 |
close.addStyleName("close");
|
93 |
close.addClickHandler(new ClickHandler() {
|
94 |
|
95 |
@Override
|
96 |
public void onClick(ClickEvent event) { |
97 |
close(); |
98 |
} |
99 |
}); |
100 |
// Set the dialog's caption.
|
101 |
setText("File upload");
|
102 |
setAnimationEnabled(true);
|
103 |
setGlassEnabled(true);
|
104 |
setStyleName("pithos-DialogBox");
|
105 |
setVisible(false);
|
106 |
|
107 |
// Since we're going to add a FileUpload widget, we'll need to set the
|
108 |
// form to use the POST method, and multipart MIME encoding.
|
109 |
form.setEncoding(FormPanel.ENCODING_MULTIPART); |
110 |
form.setMethod(FormPanel.METHOD_POST); |
111 |
|
112 |
// Create a panel to hold all of the form widgets.
|
113 |
VerticalPanel panel = new VerticalPanel();
|
114 |
panel.setWidth("470px");
|
115 |
panel.add(close); |
116 |
form.setWidget(panel); |
117 |
|
118 |
VerticalPanel inner = new VerticalPanel();
|
119 |
inner.addStyleName("inner");
|
120 |
|
121 |
final Hidden auth = new Hidden("X-Auth-Token"); |
122 |
inner.add(auth); |
123 |
upload.setName("X-Object-Data");
|
124 |
upload.setVisible(false);
|
125 |
filenameLabel.setText("");
|
126 |
filenameLabel.setVisible(false);
|
127 |
filenameLabel.setStyleName("props-labels");
|
128 |
HorizontalPanel fileUploadPanel = new HorizontalPanel();
|
129 |
fileUploadPanel.setVisible(false);
|
130 |
fileUploadPanel.add(filenameLabel); |
131 |
fileUploadPanel.add(upload); |
132 |
Grid generalTable = new Grid(2, 2); |
133 |
generalTable.setText(0, 0, "Folder"); |
134 |
generalTable.setWidget(0, 1, foldernameLabel); |
135 |
generalTable.setWidget(1, 1, fileUploadPanel); |
136 |
generalTable.getCellFormatter().setStyleName(0, 0, "props-labels"); |
137 |
generalTable.getCellFormatter().setStyleName(0, 1, "props-values"); |
138 |
generalTable.getCellFormatter().setVisible(1, 0, false); |
139 |
generalTable.setCellSpacing(4);
|
140 |
|
141 |
inner.add(generalTable); |
142 |
|
143 |
FlowPanel uploader = new FlowPanel();
|
144 |
uploader.getElement().setId("uploader");
|
145 |
inner.add(uploader); |
146 |
|
147 |
panel.add(inner); |
148 |
panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER); |
149 |
|
150 |
setWidget(form); |
151 |
} |
152 |
|
153 |
private void refreshFolder() { |
154 |
if (app.getSelectedTree().equals(app.getFolderTreeView()))
|
155 |
app.updateFolder(folder, true, new Command() { |
156 |
|
157 |
@Override
|
158 |
public void execute() { |
159 |
app.updateStatistics(); |
160 |
} |
161 |
}, true);
|
162 |
else
|
163 |
app.updateOtherSharedFolder(folder, true);
|
164 |
} |
165 |
|
166 |
native void setupUpload(FileUploadDialog dlg, String path, String token) /*-{ |
167 |
var uploader = $wnd.$('#uploader').pluploadQueue();
|
168 |
var createUploader = function() {
|
169 |
$wnd.$("#uploader").pluploadQueue({
|
170 |
// General settings
|
171 |
runtimes : 'html5, flash, gears, silverlight, browserplus, html4',
|
172 |
unique_names : true,
|
173 |
|
174 |
// Flash settings
|
175 |
flash_swf_url : 'plupload/js/plupload.flash.swf',
|
176 |
|
177 |
// Silverlight settings
|
178 |
silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
|
179 |
|
180 |
multiple_queues: true,
|
181 |
|
182 |
preinit: {
|
183 |
Init: function(up, info) {
|
184 |
if ($wnd.console && $wnd.console.log)
|
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 |
dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(true);
|
196 |
},
|
197 |
|
198 |
FilesRemoved: function(up, files) {
|
199 |
if (up.files.length == 0)
|
200 |
dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(false);
|
201 |
else
|
202 |
dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(true);
|
203 |
},
|
204 |
|
205 |
BeforeUpload: function(up, file) {
|
206 |
if ($wnd.console && $wnd.console.log)
|
207 |
$wnd.console.log('About to upload ' + file.url);
|
208 |
up.settings.url = file.url;
|
209 |
},
|
210 |
|
211 |
FileUploaded: function(up, file, response) {
|
212 |
if ($wnd.console && $wnd.console.log) {
|
213 |
$wnd.console.log('File ' + file.name + ' uploaded');
|
214 |
$wnd.console.log('Response: ' + response);
|
215 |
}
|
216 |
},
|
217 |
|
218 |
UploadComplete: function(up, files) {
|
219 |
if ($wnd.console && $wnd.console.log)
|
220 |
$wnd.console.log('All files finished');
|
221 |
dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(false);
|
222 |
dlg.@gr.grnet.pithos.web.client.FileUploadDialog::hideUploadIndicator()();
|
223 |
dlg.@gr.grnet.pithos.web.client.FileUploadDialog::refreshFolder()();
|
224 |
},
|
225 |
|
226 |
Error: function(up, error) {
|
227 |
if ($wnd.console && $wnd.console.log)
|
228 |
$wnd.console.log("Error occured:" + error);
|
229 |
}
|
230 |
}
|
231 |
});
|
232 |
return $wnd.$('#uploader').pluploadQueue();
|
233 |
};
|
234 |
|
235 |
if ($wnd.console && $wnd.console.log)
|
236 |
$wnd.console.log(uploader);
|
237 |
if (!uploader) {
|
238 |
uploader = createUploader();
|
239 |
}
|
240 |
else {
|
241 |
var removeAll = true;
|
242 |
var files = uploader.files;
|
243 |
if ($wnd.console && $wnd.console.log)
|
244 |
$wnd.console.log('About to check ' + files.length + ' files');
|
245 |
for (var i=0; i<files.length; i++) {
|
246 |
var f = files[i];
|
247 |
if (f.status != $wnd.plupload.DONE && f.status != $wnd.plupload.FAILED) {
|
248 |
removeAll = false;
|
249 |
break;
|
250 |
}
|
251 |
}
|
252 |
if (removeAll) {
|
253 |
if ($wnd.console && $wnd.console.log)
|
254 |
$wnd.console.log('About to remove ' + files.length + ' files');
|
255 |
uploader.destroy();
|
256 |
uploader = createUploader();
|
257 |
if ($wnd.console && $wnd.console.log)
|
258 |
$wnd.console.log(uploader);
|
259 |
dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(false);
|
260 |
}
|
261 |
else {
|
262 |
dlg.@gr.grnet.pithos.web.client.FileUploadDialog::setInProgress(Z)(true);
|
263 |
}
|
264 |
}
|
265 |
uploader.path = path;
|
266 |
}-*/;
|
267 |
|
268 |
native boolean isUploading()/*-{ |
269 |
var uploader = $wnd.$("#uploader").pluploadQueue();
|
270 |
var files = uploader.files;
|
271 |
for (var i=0; i<files.length; i++) {
|
272 |
var f = files[i];
|
273 |
if (f.status == $wnd.plupload.UPLOADING) {
|
274 |
return true;
|
275 |
}
|
276 |
}
|
277 |
return false;
|
278 |
}-*/;
|
279 |
|
280 |
@Override
|
281 |
protected void onPreviewNativeEvent(NativePreviewEvent event) { |
282 |
super.onPreviewNativeEvent(event);
|
283 |
|
284 |
NativeEvent evt = event.getNativeEvent(); |
285 |
if (evt.getType().equals("keydown")) |
286 |
// Use the popup's key preview hooks to close the dialog when
|
287 |
// escape is pressed.
|
288 |
switch (evt.getKeyCode()) {
|
289 |
case KeyCodes.KEY_ESCAPE:
|
290 |
close(); |
291 |
break;
|
292 |
} |
293 |
} |
294 |
|
295 |
public void setFolder(Folder folder) { |
296 |
this.folder = folder;
|
297 |
foldernameLabel.setText(folder.getName()); |
298 |
} |
299 |
|
300 |
@Override
|
301 |
public void center() { |
302 |
app.hideUploadIndicator(); |
303 |
setVisible(true);
|
304 |
setModal(true);
|
305 |
super.center();
|
306 |
String path = app.getApiPath() + folder.getOwner() + folder.getUri();
|
307 |
setupUpload(this, path, app.getToken());
|
308 |
super.center();
|
309 |
} |
310 |
|
311 |
private void hideUploadIndicator() { |
312 |
app.hideUploadIndicator(); |
313 |
} |
314 |
|
315 |
void close() {
|
316 |
setVisible(false);
|
317 |
setModal(false);
|
318 |
if (isUploading())
|
319 |
app.showUploadIndicator(); |
320 |
} |
321 |
|
322 |
private void setInProgress(boolean _inProgress) { |
323 |
inProgress = _inProgress; |
324 |
if (inProgress)
|
325 |
close.setText("hide");
|
326 |
else
|
327 |
close.setText("close");
|
328 |
} |
329 |
} |