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