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