Support uploading images from file system

Change-Id: I2969c2dd33294fff0667b8034df452f084a88c03
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/imagare/client/ImageUploadScreen.java b/src/main/java/com/googlesource/gerrit/plugins/imagare/client/ImageUploadScreen.java
index 453eb0c..e086af1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/imagare/client/ImageUploadScreen.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/imagare/client/ImageUploadScreen.java
@@ -32,6 +32,7 @@
   ImageUploadScreen() {
     setStyleName("imagare-image-upload-screen");
 
+    add(new UploadByFileSelection());
     add(new UploadByPastePanel());
     uploadedPanel = new UploadedImagesPanel();
     add(uploadedPanel);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/imagare/client/ImageUploader.java b/src/main/java/com/googlesource/gerrit/plugins/imagare/client/ImageUploader.java
index fb912b9..511c77e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/imagare/client/ImageUploader.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/imagare/client/ImageUploader.java
@@ -22,12 +22,17 @@
 public class ImageUploader {
 
   public static final void uploadImage(String imageData) {
-    uploadImage("All-Projects", imageData);
+    uploadImage("All-Projects", imageData, null);
   }
 
-  public static final void uploadImage(String project, String imageData) {
+  public static final void uploadImage(String imageData, String fileName) {
+    uploadImage("All-Projects", imageData, fileName);
+  }
+
+  public static final void uploadImage(String project, String imageData, String fileName) {
     ImageInput in = ImageInput.create();
     in.image_data(imageData);
+    in.file_name(fileName);
 
     new RestApi("projects").id(project).view(Plugin.get().getPluginName(), "images")
         .post(in, new AsyncCallback<ImageInfo>() {
@@ -45,6 +50,7 @@
 
   private static class ImageInput extends JavaScriptObject {
     final native void image_data(String d) /*-{ this.image_data = d; }-*/;
+    final native void file_name(String n) /*-{ this.file_name = n; }-*/;
 
     static ImageInput create() {
       return (ImageInput) createObject();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/imagare/client/UploadByFileSelection.java b/src/main/java/com/googlesource/gerrit/plugins/imagare/client/UploadByFileSelection.java
new file mode 100644
index 0000000..d7ede85
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/imagare/client/UploadByFileSelection.java
@@ -0,0 +1,50 @@
+// Copyright (C) 2014 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.imagare.client;
+
+import com.google.gwt.user.client.ui.FileUpload;
+import com.google.gwt.user.client.ui.FormPanel;
+import com.google.gwt.user.client.ui.HorizontalPanel;
+
+public class UploadByFileSelection extends HorizontalPanel {
+  UploadByFileSelection() {
+    this("All-Projects");
+  }
+
+  UploadByFileSelection(String project) {
+    init0();
+
+    FormPanel form = new FormPanel();
+    FileUpload upload = new FileUpload();
+    upload.setName("Select Image");
+    upload.getElement().setAttribute("onChange", "imagareSubmit(event)");
+    form.add(upload);
+
+    add(form);
+  }
+
+  private static native void init0() /*-{
+    $wnd.imagareSubmit = function submit(event) {
+      var f = event.target.files[0];
+      if (f) {
+        var r = new FileReader();
+        r.onload = function(e) {
+          @com.googlesource.gerrit.plugins.imagare.client.ImageUploader::uploadImage(Ljava/lang/String;Ljava/lang/String;)(e.target.result, f.name);
+        }
+        r.readAsDataURL(f);
+      }
+    }
+  }-*/;
+}