root / src / gr / grnet / pithos / web / client / FileUploadDialog.java @ dd081826
History | View | Annotate | Download (9.1 kB)
1 | a57faaf0 | Christos Stathis | /*
|
---|---|---|---|
2 | cae2a8db | Christos Stathis | * Copyright 2011-2012 GRNET S.A. All rights reserved.
|
3 | 63366925 | Christos Stathis | *
|
4 | 63366925 | Christos Stathis | * Redistribution and use in source and binary forms, with or
|
5 | 63366925 | Christos Stathis | * without modification, are permitted provided that the following
|
6 | 63366925 | Christos Stathis | * conditions are met:
|
7 | 63366925 | Christos Stathis | *
|
8 | 63366925 | Christos Stathis | * 1. Redistributions of source code must retain the above
|
9 | 63366925 | Christos Stathis | * copyright notice, this list of conditions and the following
|
10 | 63366925 | Christos Stathis | * disclaimer.
|
11 | 63366925 | Christos Stathis | *
|
12 | 63366925 | Christos Stathis | * 2. Redistributions in binary form must reproduce the above
|
13 | 63366925 | Christos Stathis | * copyright notice, this list of conditions and the following
|
14 | 63366925 | Christos Stathis | * disclaimer in the documentation and/or other materials
|
15 | 63366925 | Christos Stathis | * provided with the distribution.
|
16 | 63366925 | Christos Stathis | *
|
17 | 63366925 | Christos Stathis | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 | 63366925 | Christos Stathis | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 | 63366925 | Christos Stathis | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 | 63366925 | Christos Stathis | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 | 63366925 | Christos Stathis | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 | 63366925 | Christos Stathis | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 | 63366925 | Christos Stathis | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 | 63366925 | Christos Stathis | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 | 63366925 | Christos Stathis | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 | 63366925 | Christos Stathis | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 | 63366925 | Christos Stathis | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 | 63366925 | Christos Stathis | * POSSIBILITY OF SUCH DAMAGE.
|
29 | 63366925 | Christos Stathis | *
|
30 | 63366925 | Christos Stathis | * The views and conclusions contained in the software and
|
31 | 63366925 | Christos Stathis | * documentation are those of the authors and should not be
|
32 | 63366925 | Christos Stathis | * interpreted as representing official policies, either expressed
|
33 | 63366925 | Christos Stathis | * or implied, of GRNET S.A.
|
34 | a57faaf0 | Christos Stathis | */
|
35 | a57faaf0 | Christos Stathis | package gr.grnet.pithos.web.client; |
36 | a57faaf0 | Christos Stathis | |
37 | bdda6b2f | Christos Stathis | import gr.grnet.pithos.web.client.foldertree.File; |
38 | bdda6b2f | Christos Stathis | import gr.grnet.pithos.web.client.foldertree.Folder; |
39 | a57faaf0 | Christos Stathis | |
40 | 0c73a8ff | Christos Stathis | import com.google.gwt.core.client.Scheduler; |
41 | a57faaf0 | Christos Stathis | import com.google.gwt.event.dom.client.ClickEvent; |
42 | a57faaf0 | Christos Stathis | import com.google.gwt.event.dom.client.ClickHandler; |
43 | 64f42c0b | Christos Stathis | import com.google.gwt.user.client.Command; |
44 | ebead1b5 | Christos Stathis | import com.google.gwt.user.client.ui.Anchor; |
45 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.Button; |
46 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.DialogBox; |
47 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.FileUpload; |
48 | 0c73a8ff | Christos Stathis | import com.google.gwt.user.client.ui.FlowPanel; |
49 | fea6cde7 | Christos Stathis | import com.google.gwt.user.client.ui.FormPanel; |
50 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.Grid; |
51 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
52 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.Hidden; |
53 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.HorizontalPanel; |
54 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.Label; |
55 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.VerticalPanel; |
56 | a57faaf0 | Christos Stathis | |
57 | a57faaf0 | Christos Stathis | /**
|
58 | a57faaf0 | Christos Stathis | * The 'File upload' dialog box implementation.
|
59 | a57faaf0 | Christos Stathis | */
|
60 | bdda6b2f | Christos Stathis | public class FileUploadDialog extends DialogBox { |
61 | a57faaf0 | Christos Stathis | |
62 | bdda6b2f | Christos Stathis | public static final boolean DONE = true; |
63 | a57faaf0 | Christos Stathis | |
64 | a57faaf0 | Christos Stathis | /**
|
65 | a57faaf0 | Christos Stathis | * The Form element that performs the file upload.
|
66 | a57faaf0 | Christos Stathis | */
|
67 | 7811b9d1 | Christos Stathis | protected final FormPanel form = new FormPanel(); |
68 | a57faaf0 | Christos Stathis | |
69 | a57faaf0 | Christos Stathis | private final FileUpload upload = new FileUpload(); |
70 | a57faaf0 | Christos Stathis | |
71 | bdda6b2f | Christos Stathis | private final Label filenameLabel = new Label(); |
72 | a57faaf0 | Christos Stathis | |
73 | bdda6b2f | Christos Stathis | private final Label foldernameLabel = new Label(); |
74 | a57faaf0 | Christos Stathis | |
75 | bdda6b2f | Christos Stathis | private Button submit; |
76 | a57faaf0 | Christos Stathis | |
77 | bdda6b2f | Christos Stathis | protected Folder folder;
|
78 | a57faaf0 | Christos Stathis | |
79 | 749068ba | Christos Stathis | protected Pithos app;
|
80 | a57faaf0 | Christos Stathis | |
81 | a57faaf0 | Christos Stathis | /**
|
82 | a57faaf0 | Christos Stathis | * The widget's constructor.
|
83 | a57faaf0 | Christos Stathis | */
|
84 | eb2ae14b | Christos Stathis | public FileUploadDialog(Pithos _app, Folder _folder) {
|
85 | eb2ae14b | Christos Stathis | app = _app; |
86 | eb2ae14b | Christos Stathis | folder = _folder; |
87 | eb2ae14b | Christos Stathis | foldernameLabel.setText(folder.getName()); |
88 | 4420a247 | Christos Stathis | Anchor close = new Anchor();
|
89 | 4420a247 | Christos Stathis | close.addStyleName("close");
|
90 | 4420a247 | Christos Stathis | close.addClickHandler(new ClickHandler() {
|
91 | 4420a247 | Christos Stathis | |
92 | 4420a247 | Christos Stathis | @Override
|
93 | 4420a247 | Christos Stathis | public void onClick(ClickEvent event) { |
94 | 4420a247 | Christos Stathis | hide(); |
95 | 4420a247 | Christos Stathis | } |
96 | 4420a247 | Christos Stathis | }); |
97 | a57faaf0 | Christos Stathis | // Set the dialog's caption.
|
98 | a57faaf0 | Christos Stathis | setText("File upload");
|
99 | a57faaf0 | Christos Stathis | setAnimationEnabled(true);
|
100 | 4420a247 | Christos Stathis | setGlassEnabled(true);
|
101 | 4420a247 | Christos Stathis | setStyleName("pithos-DialogBox");
|
102 | a57faaf0 | Christos Stathis | // Since we're going to add a FileUpload widget, we'll need to set the
|
103 | a57faaf0 | Christos Stathis | // form to use the POST method, and multipart MIME encoding.
|
104 | a57faaf0 | Christos Stathis | form.setEncoding(FormPanel.ENCODING_MULTIPART); |
105 | a57faaf0 | Christos Stathis | form.setMethod(FormPanel.METHOD_POST); |
106 | a57faaf0 | Christos Stathis | |
107 | a57faaf0 | Christos Stathis | // Create a panel to hold all of the form widgets.
|
108 | a57faaf0 | Christos Stathis | VerticalPanel panel = new VerticalPanel();
|
109 | 4420a247 | Christos Stathis | panel.add(close); |
110 | a57faaf0 | Christos Stathis | form.setWidget(panel); |
111 | bdda6b2f | Christos Stathis | |
112 | 4420a247 | Christos Stathis | VerticalPanel inner = new VerticalPanel();
|
113 | 4420a247 | Christos Stathis | inner.addStyleName("inner");
|
114 | 4420a247 | Christos Stathis | |
115 | 0d01524d | Christos Stathis | final Hidden auth = new Hidden("X-Auth-Token"); |
116 | 4420a247 | Christos Stathis | inner.add(auth); |
117 | 4123152d | Christos Stathis | upload.setName("X-Object-Data");
|
118 | eb2ae14b | Christos Stathis | upload.setVisible(false);
|
119 | a57faaf0 | Christos Stathis | filenameLabel.setText("");
|
120 | a57faaf0 | Christos Stathis | filenameLabel.setVisible(false);
|
121 | a57faaf0 | Christos Stathis | filenameLabel.setStyleName("props-labels");
|
122 | bdda6b2f | Christos Stathis | HorizontalPanel fileUploadPanel = new HorizontalPanel();
|
123 | eb2ae14b | Christos Stathis | fileUploadPanel.setVisible(false);
|
124 | bdda6b2f | Christos Stathis | fileUploadPanel.add(filenameLabel); |
125 | bdda6b2f | Christos Stathis | fileUploadPanel.add(upload); |
126 | a57faaf0 | Christos Stathis | Grid generalTable = new Grid(2, 2); |
127 | a57faaf0 | Christos Stathis | generalTable.setText(0, 0, "Folder"); |
128 | bdda6b2f | Christos Stathis | generalTable.setWidget(0, 1, foldernameLabel); |
129 | bdda6b2f | Christos Stathis | generalTable.setWidget(1, 1, fileUploadPanel); |
130 | a57faaf0 | Christos Stathis | generalTable.getCellFormatter().setStyleName(0, 0, "props-labels"); |
131 | bdda6b2f | Christos Stathis | generalTable.getCellFormatter().setStyleName(0, 1, "props-values"); |
132 | eb2ae14b | Christos Stathis | generalTable.getCellFormatter().setVisible(1, 0, false); |
133 | a57faaf0 | Christos Stathis | generalTable.setCellSpacing(4);
|
134 | a57faaf0 | Christos Stathis | |
135 | 4420a247 | Christos Stathis | inner.add(generalTable); |
136 | a57faaf0 | Christos Stathis | |
137 | 0c73a8ff | Christos Stathis | FlowPanel uploader = new FlowPanel();
|
138 | 0c73a8ff | Christos Stathis | uploader.getElement().setId("uploader");
|
139 | 0c73a8ff | Christos Stathis | inner.add(uploader); |
140 | 0c73a8ff | Christos Stathis | |
141 | 4420a247 | Christos Stathis | panel.add(inner); |
142 | 4420a247 | Christos Stathis | panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER); |
143 | 4420a247 | Christos Stathis | |
144 | 0c73a8ff | Christos Stathis | |
145 | 0c73a8ff | Christos Stathis | Scheduler.get().scheduleDeferred(new Command() {
|
146 | 0c73a8ff | Christos Stathis | |
147 | 0c73a8ff | Christos Stathis | @Override
|
148 | 0c73a8ff | Christos Stathis | public void execute() { |
149 | 0c73a8ff | Christos Stathis | String path = app.getApiPath() + folder.getOwner() + folder.getUri();
|
150 | eb2ae14b | Christos Stathis | setupUpload(FileUploadDialog.this, path, app.getToken()); |
151 | 0c73a8ff | Christos Stathis | } |
152 | 0c73a8ff | Christos Stathis | }); |
153 | a57faaf0 | Christos Stathis | setWidget(form); |
154 | a57faaf0 | Christos Stathis | } |
155 | a57faaf0 | Christos Stathis | |
156 | eb2ae14b | Christos Stathis | private void refreshFolder() { |
157 | eb2ae14b | Christos Stathis | if (app.getSelectedTree().equals(app.getFolderTreeView()))
|
158 | eb2ae14b | Christos Stathis | app.updateFolder(folder, true, new Command() { |
159 | eb2ae14b | Christos Stathis | |
160 | eb2ae14b | Christos Stathis | @Override
|
161 | eb2ae14b | Christos Stathis | public void execute() { |
162 | eb2ae14b | Christos Stathis | app.updateStatistics(); |
163 | eb2ae14b | Christos Stathis | } |
164 | eb2ae14b | Christos Stathis | }); |
165 | eb2ae14b | Christos Stathis | else
|
166 | eb2ae14b | Christos Stathis | app.updateOtherSharedFolder(folder, true);
|
167 | eb2ae14b | Christos Stathis | } |
168 | eb2ae14b | Christos Stathis | |
169 | eb2ae14b | Christos Stathis | native void setupUpload(FileUploadDialog dlg, String path, String token) /*-{ |
170 | 0c73a8ff | Christos Stathis | $wnd.$("#uploader").pluploadQueue({
|
171 | 0c73a8ff | Christos Stathis | // General settings
|
172 | fe991fd0 | Christos Stathis | runtimes : 'html5, flash, gears, silverlight, browserplus, html4',
|
173 | 0c73a8ff | Christos Stathis | unique_names : true,
|
174 | 0c73a8ff | Christos Stathis |
|
175 | 0c73a8ff | Christos Stathis | // Flash settings
|
176 | 0c73a8ff | Christos Stathis | flash_swf_url : 'plupload/js/plupload.flash.swf',
|
177 | 0c73a8ff | Christos Stathis |
|
178 | 0c73a8ff | Christos Stathis | // Silverlight settings
|
179 | 0c73a8ff | Christos Stathis | silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
|
180 | 0c73a8ff | Christos Stathis |
|
181 | 0c73a8ff | Christos Stathis | preinit: {
|
182 | eb2ae14b | Christos Stathis | Init: function(up, info) {
|
183 | eb2ae14b | Christos Stathis | up.settings.file_data_name = "X-Object-Data";
|
184 | eb2ae14b | Christos Stathis | },
|
185 | eb2ae14b | Christos Stathis |
|
186 | 0c73a8ff | Christos Stathis | UploadFile: function(up, file) {
|
187 | eb2ae14b | Christos Stathis | $wnd.console.log('About to upload ' + file.name);
|
188 | 0c73a8ff | Christos Stathis | up.settings.url = path + "/" + file.name + "?X-Auth-Token=" + token;
|
189 | eb2ae14b | Christos Stathis | }
|
190 | eb2ae14b | Christos Stathis | },
|
191 | eb2ae14b | Christos Stathis |
|
192 | eb2ae14b | Christos Stathis | init: {
|
193 | eb2ae14b | Christos Stathis | FileUploaded: function(up, file, response) {
|
194 | eb2ae14b | Christos Stathis | $wnd.console.log('File ' + file.name + ' uploaded');
|
195 | eb2ae14b | Christos Stathis | $wnd.console.log('Response: ' + response);
|
196 | eb2ae14b | Christos Stathis | },
|
197 | eb2ae14b | Christos Stathis |
|
198 | eb2ae14b | Christos Stathis | UploadComplete: function(up, files) {
|
199 | eb2ae14b | Christos Stathis | $wnd.console.log('All files finished');
|
200 | eb2ae14b | Christos Stathis | dlg.@gr.grnet.pithos.web.client.FileUploadDialog::refreshFolder()();
|
201 | 0c73a8ff | Christos Stathis | }
|
202 | 0c73a8ff | Christos Stathis | }
|
203 | 0c73a8ff | Christos Stathis | });
|
204 | 0c73a8ff | Christos Stathis |
|
205 | 0c73a8ff | Christos Stathis | // Client side form validation
|
206 | 0c73a8ff | Christos Stathis | $wnd.$('form').submit(function(e) {
|
207 | 0c73a8ff | Christos Stathis | var uploader = $wnd.$('#uploader').pluploadQueue();
|
208 | 0c73a8ff | Christos Stathis |
|
209 | 0c73a8ff | Christos Stathis | // Files in queue upload them first
|
210 | 0c73a8ff | Christos Stathis | if (uploader.files.length > 0) {
|
211 | 0c73a8ff | Christos Stathis | // When all files are uploaded submit form
|
212 | 0c73a8ff | Christos Stathis | uploader.bind('StateChanged', function() {
|
213 | 0c73a8ff | Christos Stathis | if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
|
214 | 0c73a8ff | Christos Stathis | $wnd.$('form')[0].submit();
|
215 | 0c73a8ff | Christos Stathis | }
|
216 | 0c73a8ff | Christos Stathis | });
|
217 | 0c73a8ff | Christos Stathis |
|
218 | 0c73a8ff | Christos Stathis | uploader.start();
|
219 | 0c73a8ff | Christos Stathis | } else {
|
220 | 0c73a8ff | Christos Stathis | alert('You must queue at least one file.');
|
221 | 0c73a8ff | Christos Stathis | }
|
222 | 0c73a8ff | Christos Stathis |
|
223 | 0c73a8ff | Christos Stathis | return false;
|
224 | 0c73a8ff | Christos Stathis | });
|
225 | 0c73a8ff | Christos Stathis | }-*/;
|
226 | 0c73a8ff | Christos Stathis | |
227 | a57faaf0 | Christos Stathis | /**
|
228 | a57faaf0 | Christos Stathis | * Make any last minute checks and start the upload.
|
229 | a57faaf0 | Christos Stathis | */
|
230 | bdda6b2f | Christos Stathis | protected void prepareAndSubmit() { |
231 | bdda6b2f | Christos Stathis | if (upload.getFilename().length() == 0) { |
232 | bdda6b2f | Christos Stathis | app.displayError("You must select a file!");
|
233 | bdda6b2f | Christos Stathis | return;
|
234 | bdda6b2f | Christos Stathis | } |
235 | bdda6b2f | Christos Stathis | final String fname = getFilename(upload.getFilename()); |
236 | 0d01524d | Christos Stathis | String apath = app.getApiPath() + folder.getOwner() + folder.getUri() + "/" + fname + "?X-Auth-Token=" + app.getToken(); |
237 | bdda6b2f | Christos Stathis | form.setAction(apath); |
238 | bdda6b2f | Christos Stathis | submit.setEnabled(false);
|
239 | bdda6b2f | Christos Stathis | upload.setVisible(false);
|
240 | bdda6b2f | Christos Stathis | filenameLabel.setText(fname); |
241 | bdda6b2f | Christos Stathis | filenameLabel.setVisible(true);
|
242 | bdda6b2f | Christos Stathis | |
243 | a57faaf0 | Christos Stathis | if (getFileForName(fname) == null) { |
244 | 4123152d | Christos Stathis | form.submit(); |
245 | a57faaf0 | Christos Stathis | } |
246 | a57faaf0 | Christos Stathis | else {
|
247 | a57faaf0 | Christos Stathis | // We are going to update an existing file, so show a confirmation dialog.
|
248 | a57faaf0 | Christos Stathis | ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " + |
249 | a57faaf0 | Christos Stathis | "you want to update " + fname + "?", "Update") { |
250 | a57faaf0 | Christos Stathis | |
251 | a57faaf0 | Christos Stathis | @Override
|
252 | a57faaf0 | Christos Stathis | public void cancel() { |
253 | a57faaf0 | Christos Stathis | FileUploadDialog.this.hide(); |
254 | a57faaf0 | Christos Stathis | } |
255 | a57faaf0 | Christos Stathis | |
256 | a57faaf0 | Christos Stathis | @Override
|
257 | a57faaf0 | Christos Stathis | public void confirm() { |
258 | a57faaf0 | Christos Stathis | form.submit(); |
259 | a57faaf0 | Christos Stathis | } |
260 | a57faaf0 | Christos Stathis | |
261 | a57faaf0 | Christos Stathis | }; |
262 | a57faaf0 | Christos Stathis | confirm.center(); |
263 | a57faaf0 | Christos Stathis | } |
264 | a57faaf0 | Christos Stathis | } |
265 | a57faaf0 | Christos Stathis | |
266 | bdda6b2f | Christos Stathis | /**
|
267 | a57faaf0 | Christos Stathis | * Returns the file name from a potential full path argument. Apparently IE
|
268 | a57faaf0 | Christos Stathis | * insists on sending the full path name of a file when uploading, forcing
|
269 | a57faaf0 | Christos Stathis | * us to trim the extra path info. Since this is only observed on Windows we
|
270 | a57faaf0 | Christos Stathis | * get to check for a single path separator value.
|
271 | a57faaf0 | Christos Stathis | *
|
272 | a57faaf0 | Christos Stathis | * @param name the potentially full path name of a file
|
273 | a57faaf0 | Christos Stathis | * @return the file name without extra path information
|
274 | a57faaf0 | Christos Stathis | */
|
275 | a57faaf0 | Christos Stathis | protected String getFilename(String name) { |
276 | a57faaf0 | Christos Stathis | int pathSepIndex = name.lastIndexOf("\\"); |
277 | a57faaf0 | Christos Stathis | if (pathSepIndex == -1) { |
278 | a57faaf0 | Christos Stathis | pathSepIndex = name.lastIndexOf("/");
|
279 | a57faaf0 | Christos Stathis | if (pathSepIndex == -1) |
280 | a57faaf0 | Christos Stathis | return name;
|
281 | a57faaf0 | Christos Stathis | } |
282 | a57faaf0 | Christos Stathis | return name.substring(pathSepIndex + 1); |
283 | a57faaf0 | Christos Stathis | } |
284 | a57faaf0 | Christos Stathis | |
285 | bdda6b2f | Christos Stathis | protected File getFileForName(String name){ |
286 | bdda6b2f | Christos Stathis | for (File f : folder.getFiles()) |
287 | 7b28ae07 | Christos Stathis | if (f.getName().equals(name))
|
288 | a57faaf0 | Christos Stathis | return f;
|
289 | a57faaf0 | Christos Stathis | return null; |
290 | a57faaf0 | Christos Stathis | } |
291 | a57faaf0 | Christos Stathis | } |