import java.util.Map;\r
\r
import com.google.gwt.core.client.GWT;\r
+import com.google.gwt.event.dom.client.ClickEvent;\r
+import com.google.gwt.event.dom.client.ClickHandler;\r
import com.google.gwt.gears.client.Factory;\r
import com.google.gwt.gears.client.desktop.Desktop;\r
import com.google.gwt.gears.client.desktop.File;\r
import com.google.gwt.gears.client.httprequest.ProgressEvent;\r
import com.google.gwt.gears.client.httprequest.ProgressHandler;\r
import com.google.gwt.gears.client.httprequest.RequestCallback;\r
+import com.google.gwt.http.client.URL;\r
import com.google.gwt.json.client.JSONObject;\r
import com.google.gwt.json.client.JSONString;\r
import com.google.gwt.user.client.Command;\r
import com.google.gwt.user.client.DeferredCommand;\r
import com.google.gwt.user.client.ui.Button;\r
-import com.google.gwt.user.client.ui.ClickListener;\r
import com.google.gwt.user.client.ui.FlexTable;\r
import com.google.gwt.user.client.ui.HTML;\r
import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
import com.google.gwt.user.client.ui.HorizontalPanel;\r
import com.google.gwt.user.client.ui.VerticalPanel;\r
-import com.google.gwt.user.client.ui.Widget;\r
\r
/**\r
* The 'File upload' dialog box implementation with Google Gears support.\r
*/\r
public class FileUploadGearsDialog extends FileUploadDialog implements Updateable {\r
\r
- private final Factory factory = Factory.getInstance();\r
+ protected final Factory factory = Factory.getInstance();\r
\r
/**\r
* The array of files to upload.\r
* A list of files to upload, created from files array. Used to signal\r
* finished state when empty.\r
*/\r
- private List<File> selectedFiles = new ArrayList<File>();\r
+ protected List<File> selectedFiles = new ArrayList<File>();\r
\r
/**\r
* The list of progress bars for individual files.\r
*/\r
- private List<ProgressBar> progressBars = new ArrayList<ProgressBar>();\r
+ protected List<ProgressBar> progressBars = new ArrayList<ProgressBar>();\r
\r
private Button browse;\r
\r
\r
private Map<String, FileResource> toRename;\r
\r
+ protected List<HttpRequest> requests = new ArrayList<HttpRequest>();\r
+\r
/**\r
* The widget's constructor.\r
*/\r
HorizontalPanel buttons = new HorizontalPanel();\r
\r
submit = new Button("Upload");\r
- submit.addClickListener(new ClickListener() {\r
-\r
- public void onClick(Widget sender) {\r
+ submit.addClickHandler(new ClickHandler() {\r
+ @Override\r
+ public void onClick(ClickEvent event) {\r
prepareAndSubmit();\r
}\r
});\r
buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);\r
// Create the 'Cancel' button, along with a listener that hides the\r
// dialog when the button is clicked.\r
- Button cancel = new Button("Cancel", new ClickListener() {\r
-\r
- public void onClick(Widget sender) {\r
- hide();\r
+ Button cancel = new Button("Cancel", new ClickHandler() {\r
+ @Override\r
+ public void onClick(ClickEvent event) {\r
+ cancelUpload();\r
}\r
});\r
buttons.add(cancel);\r
buttons.setSpacing(8);\r
buttons.addStyleName("gss-DialogBox");\r
\r
- browse.addClickListener(new ClickListener() {\r
- public void onClick(Widget sender) {\r
+ browse.addClickHandler(new ClickHandler() {\r
+ @Override\r
+ public void onClick(ClickEvent event) {\r
Desktop desktop = factory.createDesktop();\r
desktop.openFiles(new OpenFilesHandler() {\r
\r
}\r
\r
/**\r
+ * Cancels the file upload.\r
+ */\r
+ private void cancelUpload() {\r
+ for (HttpRequest request: requests)\r
+ request.abort();\r
+ hide();\r
+ }\r
+\r
+ /**\r
* Check whether the specified file name exists in the selected folder.\r
*/\r
private boolean canContinue(File file) {\r
final int index = i;\r
DeferredCommand.addCommand(new Command() {\r
public void execute() {\r
- doPut(fileObjects[index], index);\r
+ doSend(fileObjects[index], index);\r
}\r
});\r
}\r
}\r
\r
/**\r
- * Perform the HTTP PUT requests to upload the specified file.\r
+ * Perform the HTTP request to upload the specified file.\r
*/\r
- protected void doPut(final File file, final int index) {\r
+ protected void doSend(final File file, final int index) {\r
final GSS app = GSS.get();\r
HttpRequest request = factory.createHttpRequest();\r
+ requests.add(request);\r
String method = "PUT";\r
\r
String path;\r
final String filename = getFilename(file.getName());\r
- FileResource selectedResource = getFileForName(filename);\r
- if (selectedResource == null ) {\r
- // We are going to create a file.\r
- path = folder.getUri();\r
- if (!path.endsWith("/"))\r
- path = path + "/";\r
- path = path + encodeComponent(filename);\r
- } else\r
- path = selectedResource.getUri();\r
+ path = folder.getUri();\r
+ if (!path.endsWith("/"))\r
+ path = path + "/";\r
+ path = path + encode(filename);\r
\r
String token = app.getToken();\r
String resource = path.substring(app.getApiPath().length()-1, path.length());\r
request.setRequestHeader("Accept", "application/json; charset=utf-8");\r
request.setCallback(new RequestCallback() {\r
public void onResponseReceived(HttpRequest req) {\r
+ int state = req.getReadyState();\r
+ if (state != 4) return;\r
switch(req.getStatus()) {\r
case 201: // Created falls through to updated.\r
case 204:\r
default:\r
app.displayError("Error uploading file " + filename +\r
": " + req.getStatus());\r
- DisplayHelper.log(req.getStatus() + ":" + req.getStatusText());\r
}\r
}\r
});\r
/**\r
* Perform the final actions after the files are uploaded.\r
*/\r
- private void finish() {\r
+ protected void finish() {\r
if (!selectedFiles.isEmpty()) return;\r
hide();\r
GSS.get().showFileList(true);\r
GSS.get().getStatusPanel().updateStats();\r
}\r
+\r
+ /**\r
+ * Same as URL.encode, but also encode apostrophe since browsers aren't\r
+ * consistent about it (FF encodes, IE does not).\r
+ */\r
+ protected String encode(String decodedURL) {\r
+ String retv = decodedURL.replaceAll("@", "_"); // Replace bad character\r
+ retv = URL.encodeComponent(retv);\r
+ retv = retv.replaceAll("'", "%27");\r
+ return retv;\r
+ }\r
+\r
}\r