root / src / org / gss_project / gss / web / client / FileUploadDialog.java @ 1205:fbeae20462e6
History | View | Annotate | Download (16.2 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
|
3 |
*
|
4 |
* This file is part of GSS.
|
5 |
*
|
6 |
* GSS is free software: you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation, either version 3 of the License, or
|
9 |
* (at your option) any later version.
|
10 |
*
|
11 |
* GSS is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with GSS. If not, see <http://www.gnu.org/licenses/>.
|
18 |
*/
|
19 |
package org.gss_project.gss.web.client; |
20 |
|
21 |
import org.gss_project.gss.web.client.rest.GetCommand; |
22 |
import org.gss_project.gss.web.client.rest.PostCommand; |
23 |
import org.gss_project.gss.web.client.rest.RestCommand; |
24 |
import org.gss_project.gss.web.client.rest.RestException; |
25 |
import org.gss_project.gss.web.client.rest.resource.FileResource; |
26 |
import org.gss_project.gss.web.client.rest.resource.FolderResource; |
27 |
import org.gss_project.gss.web.client.rest.resource.RestResourceWrapper; |
28 |
import org.gss_project.gss.web.client.rest.resource.UploadStatusResource; |
29 |
|
30 |
import java.util.ArrayList; |
31 |
import java.util.List; |
32 |
|
33 |
import com.google.gwt.core.client.GWT; |
34 |
import com.google.gwt.dom.client.NativeEvent; |
35 |
import com.google.gwt.event.dom.client.ClickEvent; |
36 |
import com.google.gwt.event.dom.client.ClickHandler; |
37 |
import com.google.gwt.event.dom.client.KeyCodes; |
38 |
import com.google.gwt.http.client.URL; |
39 |
import com.google.gwt.json.client.JSONObject; |
40 |
import com.google.gwt.json.client.JSONString; |
41 |
import com.google.gwt.user.client.DeferredCommand; |
42 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
43 |
import com.google.gwt.user.client.Timer; |
44 |
import com.google.gwt.user.client.ui.Button; |
45 |
import com.google.gwt.user.client.ui.DialogBox; |
46 |
import com.google.gwt.user.client.ui.FileUpload; |
47 |
import com.google.gwt.user.client.ui.FormPanel; |
48 |
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent; |
49 |
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler; |
50 |
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent; |
51 |
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler; |
52 |
import com.google.gwt.user.client.ui.Grid; |
53 |
import com.google.gwt.user.client.ui.HTML; |
54 |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
55 |
import com.google.gwt.user.client.ui.Hidden; |
56 |
import com.google.gwt.user.client.ui.HorizontalPanel; |
57 |
import com.google.gwt.user.client.ui.Label; |
58 |
import com.google.gwt.user.client.ui.VerticalPanel; |
59 |
|
60 |
/**
|
61 |
* The 'File upload' dialog box implementation.
|
62 |
*/
|
63 |
public class FileUploadDialog extends DialogBox implements Updateable { |
64 |
|
65 |
protected int prgBarInterval = 1500; |
66 |
|
67 |
private ProgressBar progressBar;
|
68 |
|
69 |
protected RepeatingTimer repeater = new RepeatingTimer(this, prgBarInterval); |
70 |
|
71 |
public static final boolean DONE = true; |
72 |
|
73 |
/**
|
74 |
* The Form element that performs the file upload.
|
75 |
*/
|
76 |
private final FormPanel form = new FormPanel(); |
77 |
|
78 |
private final FileUpload upload = new FileUpload(); |
79 |
|
80 |
protected final Label filenameLabel = new Label(""); |
81 |
|
82 |
protected List<FileResource> files; |
83 |
|
84 |
protected boolean cancelEvent = false; |
85 |
|
86 |
protected String fileNameToUse; |
87 |
|
88 |
protected FolderResource folder;
|
89 |
|
90 |
/**
|
91 |
* The widget's constructor.
|
92 |
*/
|
93 |
public FileUploadDialog() {
|
94 |
// Set the dialog's caption.
|
95 |
setText("File upload");
|
96 |
setAnimationEnabled(true);
|
97 |
// Since we're going to add a FileUpload widget, we'll need to set the
|
98 |
// form to use the POST method, and multipart MIME encoding.
|
99 |
form.setEncoding(FormPanel.ENCODING_MULTIPART); |
100 |
form.setMethod(FormPanel.METHOD_POST); |
101 |
|
102 |
// Create a panel to hold all of the form widgets.
|
103 |
VerticalPanel panel = new VerticalPanel();
|
104 |
form.setWidget(panel); |
105 |
final HTML info = new HTML("You may select a file to upload. Install" + |
106 |
" <a href='http://gears.google.com/' target='_blank'>Google " +
|
107 |
"Gears</a><br> for uploading multiple files simultaneously.");
|
108 |
info.addStyleName("gss-uploadNote");
|
109 |
panel.add(info); |
110 |
final Hidden date = new Hidden("Date", ""); |
111 |
panel.add(date); |
112 |
final Hidden auth = new Hidden("Authorization", ""); |
113 |
panel.add(auth); |
114 |
// Add an informative label with the folder name.
|
115 |
Object selection = GSS.get().getTreeView().getSelection();
|
116 |
folder = ((RestResourceWrapper) selection).getResource(); |
117 |
upload.setName("file");
|
118 |
filenameLabel.setText("");
|
119 |
filenameLabel.setVisible(false);
|
120 |
filenameLabel.setStyleName("props-labels");
|
121 |
HorizontalPanel fileUloadPanel = new HorizontalPanel();
|
122 |
fileUloadPanel.add(filenameLabel); |
123 |
fileUloadPanel.add(upload); |
124 |
upload.getElement().setId("fileUploadDiallog.uploadPanel");
|
125 |
Grid generalTable = new Grid(2, 2); |
126 |
generalTable.setText(0, 0, "Folder"); |
127 |
generalTable.setText(1, 0, "File"); |
128 |
generalTable.setText(0, 1, folder.getName()); |
129 |
generalTable.setWidget(1, 1, fileUloadPanel); |
130 |
generalTable.getCellFormatter().setStyleName(0, 0, "props-labels"); |
131 |
generalTable.getCellFormatter().setStyleName(1, 0, "props-labels"); |
132 |
generalTable.getCellFormatter().setStyleName(0, 1, "props-values"); |
133 |
generalTable.getCellFormatter().setStyleName(1, 1, "props-values"); |
134 |
generalTable.setCellSpacing(4);
|
135 |
|
136 |
panel.add(generalTable); |
137 |
|
138 |
// Create a panel to hold the buttons.
|
139 |
HorizontalPanel buttons = new HorizontalPanel();
|
140 |
|
141 |
// Create the 'upload' button, along with a listener that submits the
|
142 |
// form.
|
143 |
final Button submit = new Button("Upload", new ClickHandler() { |
144 |
@Override
|
145 |
public void onClick(ClickEvent event) { |
146 |
prepareAndSubmit(); |
147 |
} |
148 |
}); |
149 |
submit.getElement().setId("fileUploadDialog.button.upload");
|
150 |
buttons.add(submit); |
151 |
buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER); |
152 |
// Create the 'Cancel' button, along with a listener that hides the
|
153 |
// dialog when the button is clicked.
|
154 |
final Button cancel = new Button("Cancel", new ClickHandler() { |
155 |
@Override
|
156 |
public void onClick(ClickEvent event) { |
157 |
repeater.finish(); |
158 |
hide(); |
159 |
} |
160 |
}); |
161 |
cancel.getElement().setId("fileUploadDialog.button.cancel");
|
162 |
buttons.add(cancel); |
163 |
buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER); |
164 |
buttons.setSpacing(8);
|
165 |
buttons.addStyleName("gss-DialogBox");
|
166 |
|
167 |
// Add an event handler to the form.
|
168 |
form.addSubmitHandler(new SubmitHandler() {
|
169 |
|
170 |
@Override
|
171 |
public void onSubmit(SubmitEvent event) { |
172 |
GSS app = GSS.get(); |
173 |
// This event is fired just before the form is submitted. We can
|
174 |
// take this opportunity to perform validation.
|
175 |
if (upload.getFilename().length() == 0) { |
176 |
app.displayError("You must select a file!");
|
177 |
event.cancel(); |
178 |
hide(); |
179 |
} else {
|
180 |
|
181 |
canContinue(); |
182 |
GWT.log("Cancel:" + cancelEvent, null); |
183 |
if (cancelEvent) {
|
184 |
cancelEvent = false;
|
185 |
app.displayError("The specified file name already exists in this folder");
|
186 |
event.cancel(); |
187 |
hide(); |
188 |
} else {
|
189 |
|
190 |
fileNameToUse = getFilename(upload.getFilename()); |
191 |
String apath;
|
192 |
FileResource selectedFile = getFileForName(fileNameToUse); |
193 |
if (selectedFile == null ) { |
194 |
//we are going to create a file
|
195 |
apath = folder.getUri(); |
196 |
if (!apath.endsWith("/")) |
197 |
apath = apath + "/";
|
198 |
apath = apath + encodeComponent(fileNameToUse); |
199 |
} else
|
200 |
apath = selectedFile.getUri(); |
201 |
form.setAction(apath); |
202 |
String dateString = RestCommand.getDate();
|
203 |
String resource = apath.substring(app.getApiPath().length() - 1, apath.length()); |
204 |
String sig = RestCommand.calculateSig("POST", dateString, resource, RestCommand.base64decode(app.getToken())); |
205 |
date.setValue(dateString); |
206 |
auth.setValue(app.getCurrentUserResource().getUsername() + " " + sig);
|
207 |
GWT.log("FolderPATH:" + folder.getUri(), null); |
208 |
submit.setEnabled(false);
|
209 |
upload.setVisible(false);
|
210 |
filenameLabel.setText(fileNameToUse); |
211 |
filenameLabel.setVisible(true);
|
212 |
repeater.start(); |
213 |
progressBar.setVisible(true);
|
214 |
} |
215 |
} |
216 |
|
217 |
} |
218 |
}); |
219 |
form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
|
220 |
|
221 |
@Override
|
222 |
public void onSubmitComplete(SubmitCompleteEvent event) { |
223 |
// When the form submission is successfully completed, this
|
224 |
// event is fired. Assuming the service returned a response
|
225 |
// of type text/html, we can get the result text here (see
|
226 |
// the FormPanel documentation for further explanation).
|
227 |
String results = event.getResults();
|
228 |
|
229 |
// Unfortunately the results are never empty, even in
|
230 |
// the absense of errors, so we have to check for '<pre></pre>'.
|
231 |
if (!results.equalsIgnoreCase("<pre></pre>")) { |
232 |
GWT.log(results, null);
|
233 |
GSS.get().displayError(results); |
234 |
} |
235 |
progressBar.setProgress(100);
|
236 |
cancelUpload(); |
237 |
GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection()); |
238 |
GSS.get().getStatusPanel().updateStats(); |
239 |
|
240 |
} |
241 |
}); |
242 |
|
243 |
|
244 |
panel.add(buttons); |
245 |
progressBar = new ProgressBar(50, ProgressBar.SHOW_TIME_REMAINING); |
246 |
panel.add(progressBar); |
247 |
progressBar.setVisible(false);
|
248 |
panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER); |
249 |
panel.setCellHorizontalAlignment(progressBar, HasHorizontalAlignment.ALIGN_CENTER); |
250 |
panel.addStyleName("gss-DialogBox");
|
251 |
addStyleName("gss-DialogBox");
|
252 |
setWidget(form); |
253 |
} |
254 |
|
255 |
@Override
|
256 |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
257 |
super.onPreviewNativeEvent(preview);
|
258 |
|
259 |
NativeEvent evt = preview.getNativeEvent(); |
260 |
if (evt.getType().equals("keydown")) |
261 |
// Use the popup's key preview hooks to close the dialog when either
|
262 |
// enter or escape is pressed.
|
263 |
switch (evt.getKeyCode()) {
|
264 |
case KeyCodes.KEY_ENTER:
|
265 |
prepareAndSubmit(); |
266 |
break;
|
267 |
case KeyCodes.KEY_ESCAPE:
|
268 |
cancelUpload(); |
269 |
break;
|
270 |
} |
271 |
} |
272 |
|
273 |
|
274 |
|
275 |
/**
|
276 |
* Cancels the file upload.
|
277 |
*/
|
278 |
private void cancelUpload() { |
279 |
repeater.finish(); |
280 |
hide(); |
281 |
} |
282 |
|
283 |
/**
|
284 |
* Make any last minute checks and start the upload.
|
285 |
*/
|
286 |
public void prepareAndSubmit() { |
287 |
final String fname = getFilename(upload.getFilename()); |
288 |
if (getFileForName(fname) == null) { |
289 |
//we are going to create a file, so we check to see if there is a trashed file with the same name
|
290 |
FileResource same = null;
|
291 |
for (FileResource fres : folder.getFiles())
|
292 |
if (fres.isDeleted() && fres.getName().equals(fname))
|
293 |
same = fres; |
294 |
if (same == null) |
295 |
form.submit(); |
296 |
else {
|
297 |
final FileResource sameFile = same;
|
298 |
GWT.log("Same deleted file", null); |
299 |
ConfirmationDialog confirm = new ConfirmationDialog("A file with " + |
300 |
"the same name exists in the trash. If you continue,<br/>the trashed " +
|
301 |
"file '" + fname + "' will be renamed automatically for you.", "Continue") { |
302 |
|
303 |
@Override
|
304 |
public void cancel() { |
305 |
FileUploadDialog.this.hide(); |
306 |
} |
307 |
|
308 |
@Override
|
309 |
public void confirm() { |
310 |
updateTrashedFile(getBackupFilename(fname), sameFile); |
311 |
} |
312 |
|
313 |
}; |
314 |
confirm.center(); |
315 |
} |
316 |
} |
317 |
else {
|
318 |
// We are going to update an existing file, so show a confirmation dialog.
|
319 |
ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " + |
320 |
"you want to update " + fname + "?", "Update") { |
321 |
|
322 |
@Override
|
323 |
public void cancel() { |
324 |
FileUploadDialog.this.hide(); |
325 |
} |
326 |
|
327 |
@Override
|
328 |
public void confirm() { |
329 |
form.submit(); |
330 |
} |
331 |
|
332 |
}; |
333 |
confirm.center(); |
334 |
} |
335 |
} |
336 |
|
337 |
/**
|
338 |
* Returns the file name from a potential full path argument. Apparently IE
|
339 |
* insists on sending the full path name of a file when uploading, forcing
|
340 |
* us to trim the extra path info. Since this is only observed on Windows we
|
341 |
* get to check for a single path separator value.
|
342 |
*
|
343 |
* @param name the potentially full path name of a file
|
344 |
* @return the file name without extra path information
|
345 |
*/
|
346 |
protected String getFilename(String name) { |
347 |
int pathSepIndex = name.lastIndexOf("\\"); |
348 |
if (pathSepIndex == -1) { |
349 |
pathSepIndex = name.lastIndexOf("/");
|
350 |
if (pathSepIndex == -1) |
351 |
return name;
|
352 |
} |
353 |
return name.substring(pathSepIndex + 1); |
354 |
} |
355 |
|
356 |
/**
|
357 |
* Check whether the file name exists in selected folder.
|
358 |
*
|
359 |
* @return
|
360 |
*/
|
361 |
private boolean canContinue() { |
362 |
if (files == null) |
363 |
return false; |
364 |
String fileName = getFilename(upload.getFilename());
|
365 |
if (getFileForName(fileName) == null) { |
366 |
// For file creation, check to see if the file already exists.
|
367 |
GWT.log("filename to upload:" + fileName, null); |
368 |
for (FileResource dto : files) {
|
369 |
GWT.log("Check:" + dto.getName() + "/" + fileName, null); |
370 |
if (!dto.isDeleted() && dto.getName().equals(fileName)) {
|
371 |
cancelEvent = true;
|
372 |
return true; |
373 |
} |
374 |
} |
375 |
} |
376 |
return true; |
377 |
} |
378 |
|
379 |
class RepeatingTimer extends Timer { |
380 |
|
381 |
private Updateable updateable;
|
382 |
|
383 |
private int interval = 1500; |
384 |
|
385 |
private boolean running = true; |
386 |
|
387 |
RepeatingTimer(Updateable _updateable, int _interval) {
|
388 |
updateable = _updateable; |
389 |
interval = _interval; |
390 |
} |
391 |
|
392 |
@Override
|
393 |
public void run() { |
394 |
updateable.update(); |
395 |
} |
396 |
|
397 |
public void start() { |
398 |
running = true;
|
399 |
|
400 |
scheduleRepeating(interval); |
401 |
} |
402 |
|
403 |
public void finish() { |
404 |
running = false;
|
405 |
cancel(); |
406 |
} |
407 |
|
408 |
public int getInterval() { |
409 |
return interval;
|
410 |
} |
411 |
|
412 |
public void setInterval(int anInterval) { |
413 |
if (interval != anInterval) {
|
414 |
interval = anInterval; |
415 |
if (running) {
|
416 |
finish(); |
417 |
start(); |
418 |
} |
419 |
} |
420 |
} |
421 |
} |
422 |
|
423 |
@Override
|
424 |
public void update() { |
425 |
String apath = folder.getUri();
|
426 |
if (!apath.endsWith("/")) |
427 |
apath = apath + "/";
|
428 |
apath = apath + encodeComponent(fileNameToUse) + "?progress=" + encodeComponent(fileNameToUse);
|
429 |
GetCommand eg = new GetCommand<UploadStatusResource>(UploadStatusResource.class, apath, false, null) { |
430 |
|
431 |
@Override
|
432 |
public void onComplete() { |
433 |
UploadStatusResource res = getResult(); |
434 |
progressBar.setProgress(res.percent()); |
435 |
} |
436 |
|
437 |
@Override
|
438 |
public void onError(Throwable t) { |
439 |
GWT.log("", t);
|
440 |
} |
441 |
|
442 |
}; |
443 |
DeferredCommand.addCommand(eg); |
444 |
} |
445 |
|
446 |
protected String getBackupFilename(String filename) { |
447 |
List<FileResource> filesInSameFolder = new ArrayList<FileResource>(); |
448 |
for (FileResource deleted : folder.getFiles())
|
449 |
if (deleted.isDeleted())
|
450 |
filesInSameFolder.add(deleted); |
451 |
int i = 1; |
452 |
for (FileResource same : filesInSameFolder)
|
453 |
if (same.getName().startsWith(filename)) {
|
454 |
String toCheck = same.getName().substring(filename.length(), same.getName().length());
|
455 |
if (toCheck.startsWith(" ")) { |
456 |
int test = -1; |
457 |
try {
|
458 |
test = Integer.valueOf(toCheck.replace(" ", "")); |
459 |
} catch (NumberFormatException e) { |
460 |
// Do nothing since string is not a number.
|
461 |
} |
462 |
if (test >= i)
|
463 |
i = test + 1;
|
464 |
} |
465 |
} |
466 |
|
467 |
return filename + " " + i; |
468 |
} |
469 |
|
470 |
/**
|
471 |
* Rename the conflicting trashed file with the supplied new name.
|
472 |
*/
|
473 |
private void updateTrashedFile(String newName, FileResource trashedFile) { |
474 |
JSONObject json = new JSONObject();
|
475 |
json.put("name", new JSONString(newName)); |
476 |
PostCommand cf = new PostCommand(trashedFile.getUri() + "?update=", json.toString(), 200) { |
477 |
|
478 |
@Override
|
479 |
public void onComplete() { |
480 |
form.submit(); |
481 |
} |
482 |
|
483 |
@Override
|
484 |
public void onError(Throwable t) { |
485 |
GSS app = GSS.get(); |
486 |
GWT.log("", t);
|
487 |
if (t instanceof RestException) { |
488 |
int statusCode = ((RestException) t).getHttpStatusCode();
|
489 |
if (statusCode == 405) |
490 |
app.displayError("You don't have the necessary permissions");
|
491 |
else if (statusCode == 404) |
492 |
app.displayError("User in permissions does not exist");
|
493 |
else if (statusCode == 409) |
494 |
app.displayError("A file with the same name already exists");
|
495 |
else if (statusCode == 413) |
496 |
app.displayError("Your quota has been exceeded");
|
497 |
else
|
498 |
app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
|
499 |
} else
|
500 |
app.displayError("System error modifying file:" + t.getMessage());
|
501 |
} |
502 |
|
503 |
}; |
504 |
DeferredCommand.addCommand(cf); |
505 |
} |
506 |
|
507 |
protected FileResource getFileForName(String name){ |
508 |
for (FileResource f : folder.getFiles())
|
509 |
if (!f.isDeleted() && f.getName().equals(name))
|
510 |
return f;
|
511 |
return null; |
512 |
} |
513 |
|
514 |
|
515 |
/**
|
516 |
* Same as URL.encodeComponent, but also
|
517 |
* encode apostrophe since browsers aren't consistent about it
|
518 |
* (FF encodes, IE does not).
|
519 |
*/
|
520 |
private String encodeComponent(String decodedURLComponent) { |
521 |
String retv = URL.encodeComponent(decodedURLComponent); |
522 |
retv = retv.replaceAll("'", "%27"); |
523 |
return retv;
|
524 |
} |
525 |
|
526 |
/**
|
527 |
* Modify the files.
|
528 |
*
|
529 |
* @param newFiles the files to set
|
530 |
*/
|
531 |
public void setFiles(List<FileResource> newFiles) { |
532 |
files = newFiles; |
533 |
} |
534 |
} |