root / src / gr / grnet / pithos / web / client / FileUploadDialog.java @ 3ebb88f4
History | View | Annotate | Download (9.2 kB)
1 | a57faaf0 | Christos Stathis | /*
|
---|---|---|---|
2 | 63366925 | Christos Stathis | * Copyright 2011 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 | a57faaf0 | Christos Stathis | import com.google.gwt.core.client.GWT; |
41 | a57faaf0 | Christos Stathis | import com.google.gwt.dom.client.NativeEvent; |
42 | a57faaf0 | Christos Stathis | import com.google.gwt.event.dom.client.ClickEvent; |
43 | a57faaf0 | Christos Stathis | import com.google.gwt.event.dom.client.ClickHandler; |
44 | a57faaf0 | Christos Stathis | import com.google.gwt.event.dom.client.KeyCodes; |
45 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.Event.NativePreviewEvent; |
46 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.Button; |
47 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.DialogBox; |
48 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.FileUpload; |
49 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.FormPanel; |
50 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent; |
51 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler; |
52 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.FormPanel.SubmitEvent; |
53 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.FormPanel.SubmitHandler; |
54 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.Grid; |
55 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
56 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.Hidden; |
57 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.HorizontalPanel; |
58 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.Label; |
59 | a57faaf0 | Christos Stathis | import com.google.gwt.user.client.ui.VerticalPanel; |
60 | a57faaf0 | Christos Stathis | |
61 | a57faaf0 | Christos Stathis | /**
|
62 | a57faaf0 | Christos Stathis | * The 'File upload' dialog box implementation.
|
63 | a57faaf0 | Christos Stathis | */
|
64 | bdda6b2f | Christos Stathis | public class FileUploadDialog extends DialogBox { |
65 | a57faaf0 | Christos Stathis | |
66 | bdda6b2f | Christos Stathis | public static final boolean DONE = true; |
67 | a57faaf0 | Christos Stathis | |
68 | a57faaf0 | Christos Stathis | /**
|
69 | a57faaf0 | Christos Stathis | * The Form element that performs the file upload.
|
70 | a57faaf0 | Christos Stathis | */
|
71 | 7811b9d1 | Christos Stathis | protected final FormPanel form = new FormPanel(); |
72 | a57faaf0 | Christos Stathis | |
73 | a57faaf0 | Christos Stathis | private final FileUpload upload = new FileUpload(); |
74 | a57faaf0 | Christos Stathis | |
75 | bdda6b2f | Christos Stathis | private final Label filenameLabel = new Label(); |
76 | a57faaf0 | Christos Stathis | |
77 | bdda6b2f | Christos Stathis | private final Label foldernameLabel = new Label(); |
78 | a57faaf0 | Christos Stathis | |
79 | bdda6b2f | Christos Stathis | private Button submit; |
80 | a57faaf0 | Christos Stathis | |
81 | bdda6b2f | Christos Stathis | protected Folder folder;
|
82 | a57faaf0 | Christos Stathis | |
83 | 749068ba | Christos Stathis | protected Pithos app;
|
84 | a57faaf0 | Christos Stathis | |
85 | a57faaf0 | Christos Stathis | /**
|
86 | a57faaf0 | Christos Stathis | * The widget's constructor.
|
87 | a57faaf0 | Christos Stathis | */
|
88 | a57faaf0 | Christos Stathis | public FileUploadDialog() {
|
89 | a57faaf0 | Christos Stathis | // Set the dialog's caption.
|
90 | a57faaf0 | Christos Stathis | setText("File upload");
|
91 | a57faaf0 | Christos Stathis | setAnimationEnabled(true);
|
92 | a57faaf0 | Christos Stathis | // Since we're going to add a FileUpload widget, we'll need to set the
|
93 | a57faaf0 | Christos Stathis | // form to use the POST method, and multipart MIME encoding.
|
94 | a57faaf0 | Christos Stathis | form.setEncoding(FormPanel.ENCODING_MULTIPART); |
95 | a57faaf0 | Christos Stathis | form.setMethod(FormPanel.METHOD_POST); |
96 | a57faaf0 | Christos Stathis | |
97 | a57faaf0 | Christos Stathis | // Create a panel to hold all of the form widgets.
|
98 | a57faaf0 | Christos Stathis | VerticalPanel panel = new VerticalPanel();
|
99 | a57faaf0 | Christos Stathis | form.setWidget(panel); |
100 | bdda6b2f | Christos Stathis | |
101 | bdda6b2f | Christos Stathis | final Hidden auth = new Hidden("X-Auth-Token", ""); |
102 | bdda6b2f | Christos Stathis | panel.add(auth); |
103 | 4123152d | Christos Stathis | upload.setName("X-Object-Data");
|
104 | a57faaf0 | Christos Stathis | filenameLabel.setText("");
|
105 | a57faaf0 | Christos Stathis | filenameLabel.setVisible(false);
|
106 | a57faaf0 | Christos Stathis | filenameLabel.setStyleName("props-labels");
|
107 | bdda6b2f | Christos Stathis | HorizontalPanel fileUploadPanel = new HorizontalPanel();
|
108 | bdda6b2f | Christos Stathis | fileUploadPanel.add(filenameLabel); |
109 | bdda6b2f | Christos Stathis | fileUploadPanel.add(upload); |
110 | a57faaf0 | Christos Stathis | Grid generalTable = new Grid(2, 2); |
111 | a57faaf0 | Christos Stathis | generalTable.setText(0, 0, "Folder"); |
112 | bdda6b2f | Christos Stathis | generalTable.setWidget(0, 1, foldernameLabel); |
113 | a57faaf0 | Christos Stathis | generalTable.setText(1, 0, "File"); |
114 | bdda6b2f | Christos Stathis | generalTable.setWidget(1, 1, fileUploadPanel); |
115 | a57faaf0 | Christos Stathis | generalTable.getCellFormatter().setStyleName(0, 0, "props-labels"); |
116 | bdda6b2f | Christos Stathis | generalTable.getCellFormatter().setStyleName(0, 1, "props-values"); |
117 | a57faaf0 | Christos Stathis | generalTable.getCellFormatter().setStyleName(1, 0, "props-labels"); |
118 | a57faaf0 | Christos Stathis | generalTable.getCellFormatter().setStyleName(1, 1, "props-values"); |
119 | a57faaf0 | Christos Stathis | generalTable.setCellSpacing(4);
|
120 | a57faaf0 | Christos Stathis | |
121 | a57faaf0 | Christos Stathis | panel.add(generalTable); |
122 | a57faaf0 | Christos Stathis | |
123 | a57faaf0 | Christos Stathis | // Create a panel to hold the buttons.
|
124 | a57faaf0 | Christos Stathis | HorizontalPanel buttons = new HorizontalPanel();
|
125 | a57faaf0 | Christos Stathis | |
126 | a57faaf0 | Christos Stathis | // Create the 'upload' button, along with a listener that submits the
|
127 | a57faaf0 | Christos Stathis | // form.
|
128 | bdda6b2f | Christos Stathis | submit = new Button("Upload", new ClickHandler() { |
129 | a57faaf0 | Christos Stathis | @Override
|
130 | 7811b9d1 | Christos Stathis | public void onClick(@SuppressWarnings("unused") ClickEvent event) { |
131 | a57faaf0 | Christos Stathis | prepareAndSubmit(); |
132 | a57faaf0 | Christos Stathis | } |
133 | a57faaf0 | Christos Stathis | }); |
134 | a57faaf0 | Christos Stathis | buttons.add(submit); |
135 | a57faaf0 | Christos Stathis | buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER); |
136 | a57faaf0 | Christos Stathis | // Create the 'Cancel' button, along with a listener that hides the
|
137 | a57faaf0 | Christos Stathis | // dialog when the button is clicked.
|
138 | a57faaf0 | Christos Stathis | final Button cancel = new Button("Cancel", new ClickHandler() { |
139 | a57faaf0 | Christos Stathis | @Override
|
140 | 7811b9d1 | Christos Stathis | public void onClick(@SuppressWarnings("unused") ClickEvent event) { |
141 | a57faaf0 | Christos Stathis | hide(); |
142 | a57faaf0 | Christos Stathis | } |
143 | a57faaf0 | Christos Stathis | }); |
144 | a57faaf0 | Christos Stathis | buttons.add(cancel); |
145 | a57faaf0 | Christos Stathis | buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER); |
146 | a57faaf0 | Christos Stathis | buttons.setSpacing(8);
|
147 | bdda6b2f | Christos Stathis | panel.add(buttons); |
148 | bdda6b2f | Christos Stathis | panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER); |
149 | a57faaf0 | Christos Stathis | |
150 | a57faaf0 | Christos Stathis | // Add an event handler to the form.
|
151 | a57faaf0 | Christos Stathis | form.addSubmitHandler(new SubmitHandler() {
|
152 | a57faaf0 | Christos Stathis | |
153 | a57faaf0 | Christos Stathis | @Override
|
154 | 7811b9d1 | Christos Stathis | public void onSubmit(@SuppressWarnings("unused") SubmitEvent event) { |
155 | bdda6b2f | Christos Stathis | auth.setValue(app.getToken()); //This is done here because the app object is not available in the constructor
|
156 | a57faaf0 | Christos Stathis | } |
157 | a57faaf0 | Christos Stathis | }); |
158 | a57faaf0 | Christos Stathis | form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
|
159 | a57faaf0 | Christos Stathis | |
160 | a57faaf0 | Christos Stathis | @Override
|
161 | a57faaf0 | Christos Stathis | public void onSubmitComplete(SubmitCompleteEvent event) { |
162 | a57faaf0 | Christos Stathis | // When the form submission is successfully completed, this
|
163 | a57faaf0 | Christos Stathis | // event is fired. Assuming the service returned a response
|
164 | a57faaf0 | Christos Stathis | // of type text/html, we can get the result text here (see
|
165 | a57faaf0 | Christos Stathis | // the FormPanel documentation for further explanation).
|
166 | a57faaf0 | Christos Stathis | String results = event.getResults();
|
167 | a57faaf0 | Christos Stathis | |
168 | a57faaf0 | Christos Stathis | // Unfortunately the results are never empty, even in
|
169 | a57faaf0 | Christos Stathis | // the absense of errors, so we have to check for '<pre></pre>'.
|
170 | 4123152d | Christos Stathis | if (results != null && results.length() > 0 && !results.equalsIgnoreCase("<pre></pre>")) { |
171 | a57faaf0 | Christos Stathis | GWT.log(results, null);
|
172 | bdda6b2f | Christos Stathis | app.displayError(results); |
173 | a57faaf0 | Christos Stathis | } |
174 | 3ebb88f4 | Christos Stathis | if (app.getSelectedTree().equals(app.getFolderTreeView()))
|
175 | 3ebb88f4 | Christos Stathis | app.updateFolder(folder, true, null); |
176 | 3ebb88f4 | Christos Stathis | else
|
177 | 3ebb88f4 | Christos Stathis | app.updateOtherSharedFolder(folder, true);
|
178 | bdda6b2f | Christos Stathis | hide(); |
179 | a57faaf0 | Christos Stathis | } |
180 | a57faaf0 | Christos Stathis | }); |
181 | a57faaf0 | Christos Stathis | |
182 | a57faaf0 | Christos Stathis | setWidget(form); |
183 | a57faaf0 | Christos Stathis | } |
184 | a57faaf0 | Christos Stathis | |
185 | a57faaf0 | Christos Stathis | @Override
|
186 | a57faaf0 | Christos Stathis | protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
187 | a57faaf0 | Christos Stathis | super.onPreviewNativeEvent(preview);
|
188 | a57faaf0 | Christos Stathis | |
189 | a57faaf0 | Christos Stathis | NativeEvent evt = preview.getNativeEvent(); |
190 | a57faaf0 | Christos Stathis | if (evt.getType().equals("keydown")) |
191 | a57faaf0 | Christos Stathis | // Use the popup's key preview hooks to close the dialog when either
|
192 | a57faaf0 | Christos Stathis | // enter or escape is pressed.
|
193 | a57faaf0 | Christos Stathis | switch (evt.getKeyCode()) {
|
194 | a57faaf0 | Christos Stathis | case KeyCodes.KEY_ENTER:
|
195 | a57faaf0 | Christos Stathis | prepareAndSubmit(); |
196 | a57faaf0 | Christos Stathis | break;
|
197 | a57faaf0 | Christos Stathis | case KeyCodes.KEY_ESCAPE:
|
198 | bdda6b2f | Christos Stathis | hide(); |
199 | a57faaf0 | Christos Stathis | break;
|
200 | a57faaf0 | Christos Stathis | } |
201 | a57faaf0 | Christos Stathis | } |
202 | a57faaf0 | Christos Stathis | |
203 | a57faaf0 | Christos Stathis | /**
|
204 | a57faaf0 | Christos Stathis | * Make any last minute checks and start the upload.
|
205 | a57faaf0 | Christos Stathis | */
|
206 | bdda6b2f | Christos Stathis | protected void prepareAndSubmit() { |
207 | bdda6b2f | Christos Stathis | if (upload.getFilename().length() == 0) { |
208 | bdda6b2f | Christos Stathis | app.displayError("You must select a file!");
|
209 | bdda6b2f | Christos Stathis | return;
|
210 | bdda6b2f | Christos Stathis | } |
211 | bdda6b2f | Christos Stathis | final String fname = getFilename(upload.getFilename()); |
212 | 3ebb88f4 | Christos Stathis | String apath = app.getApiPath() + folder.getOwner() + folder.getUri() + "/" + fname; |
213 | bdda6b2f | Christos Stathis | form.setAction(apath); |
214 | bdda6b2f | Christos Stathis | submit.setEnabled(false);
|
215 | bdda6b2f | Christos Stathis | upload.setVisible(false);
|
216 | bdda6b2f | Christos Stathis | filenameLabel.setText(fname); |
217 | bdda6b2f | Christos Stathis | filenameLabel.setVisible(true);
|
218 | bdda6b2f | Christos Stathis | |
219 | a57faaf0 | Christos Stathis | if (getFileForName(fname) == null) { |
220 | 4123152d | Christos Stathis | form.submit(); |
221 | a57faaf0 | Christos Stathis | } |
222 | a57faaf0 | Christos Stathis | else {
|
223 | a57faaf0 | Christos Stathis | // We are going to update an existing file, so show a confirmation dialog.
|
224 | a57faaf0 | Christos Stathis | ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " + |
225 | a57faaf0 | Christos Stathis | "you want to update " + fname + "?", "Update") { |
226 | a57faaf0 | Christos Stathis | |
227 | a57faaf0 | Christos Stathis | @Override
|
228 | a57faaf0 | Christos Stathis | public void cancel() { |
229 | a57faaf0 | Christos Stathis | FileUploadDialog.this.hide(); |
230 | a57faaf0 | Christos Stathis | } |
231 | a57faaf0 | Christos Stathis | |
232 | a57faaf0 | Christos Stathis | @Override
|
233 | a57faaf0 | Christos Stathis | public void confirm() { |
234 | a57faaf0 | Christos Stathis | form.submit(); |
235 | a57faaf0 | Christos Stathis | } |
236 | a57faaf0 | Christos Stathis | |
237 | a57faaf0 | Christos Stathis | }; |
238 | a57faaf0 | Christos Stathis | confirm.center(); |
239 | a57faaf0 | Christos Stathis | } |
240 | a57faaf0 | Christos Stathis | } |
241 | a57faaf0 | Christos Stathis | |
242 | bdda6b2f | Christos Stathis | /**
|
243 | a57faaf0 | Christos Stathis | * Returns the file name from a potential full path argument. Apparently IE
|
244 | a57faaf0 | Christos Stathis | * insists on sending the full path name of a file when uploading, forcing
|
245 | a57faaf0 | Christos Stathis | * us to trim the extra path info. Since this is only observed on Windows we
|
246 | a57faaf0 | Christos Stathis | * get to check for a single path separator value.
|
247 | a57faaf0 | Christos Stathis | *
|
248 | a57faaf0 | Christos Stathis | * @param name the potentially full path name of a file
|
249 | a57faaf0 | Christos Stathis | * @return the file name without extra path information
|
250 | a57faaf0 | Christos Stathis | */
|
251 | a57faaf0 | Christos Stathis | protected String getFilename(String name) { |
252 | a57faaf0 | Christos Stathis | int pathSepIndex = name.lastIndexOf("\\"); |
253 | a57faaf0 | Christos Stathis | if (pathSepIndex == -1) { |
254 | a57faaf0 | Christos Stathis | pathSepIndex = name.lastIndexOf("/");
|
255 | a57faaf0 | Christos Stathis | if (pathSepIndex == -1) |
256 | a57faaf0 | Christos Stathis | return name;
|
257 | a57faaf0 | Christos Stathis | } |
258 | a57faaf0 | Christos Stathis | return name.substring(pathSepIndex + 1); |
259 | a57faaf0 | Christos Stathis | } |
260 | a57faaf0 | Christos Stathis | |
261 | bdda6b2f | Christos Stathis | protected File getFileForName(String name){ |
262 | bdda6b2f | Christos Stathis | for (File f : folder.getFiles()) |
263 | 7b28ae07 | Christos Stathis | if (f.getName().equals(name))
|
264 | a57faaf0 | Christos Stathis | return f;
|
265 | a57faaf0 | Christos Stathis | return null; |
266 | a57faaf0 | Christos Stathis | } |
267 | a57faaf0 | Christos Stathis | |
268 | 749068ba | Christos Stathis | public void setApp(Pithos app) { |
269 | bdda6b2f | Christos Stathis | this.app = app;
|
270 | bdda6b2f | Christos Stathis | } |
271 | a57faaf0 | Christos Stathis | |
272 | bdda6b2f | Christos Stathis | public void setFolder(Folder folder) { |
273 | bdda6b2f | Christos Stathis | this.folder = folder;
|
274 | bdda6b2f | Christos Stathis | foldernameLabel.setText(folder.getName()); |
275 | bdda6b2f | Christos Stathis | } |
276 | a57faaf0 | Christos Stathis | } |