Include codemirror 3.13

Download and package codemirror 3.13 for inclusion in the UI.
This may eventually replace the current side-by-side viewer,
as well as offer rich online editing to make small fixes to
existing changes.

Change-Id: I483fb46d9e18164d189826eca5cb96f9a1471c7e
diff --git a/gerrit-gwtui/BUCK b/gerrit-gwtui/BUCK
index dc47d82..cf38248 100644
--- a/gerrit-gwtui/BUCK
+++ b/gerrit-gwtui/BUCK
@@ -74,6 +74,7 @@
     '//lib:gwtjsonrpc_src',
     '//lib:gwtorm',
     '//lib:jsr305',
+    '//lib/codemirror:codemirror',
     '//lib/gwt:user',
     '//lib/jgit:jgit',
   ],
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/GerritGwtUI.gwt.xml b/gerrit-gwtui/src/main/java/com/google/gerrit/GerritGwtUI.gwt.xml
index 5fdc5bb..d33a525 100644
--- a/gerrit-gwtui/src/main/java/com/google/gerrit/GerritGwtUI.gwt.xml
+++ b/gerrit-gwtui/src/main/java/com/google/gerrit/GerritGwtUI.gwt.xml
@@ -28,11 +28,15 @@
   <inherits name='com.google.gerrit.Common'/>
   <inherits name='com.google.gerrit.UserAgent'/>
   <inherits name='org.eclipse.jgit.JGit'/>
+  <inherits name='net.codemirror.CodeMirror'/>
 
   <extend-property name='locale' values='en'/>
   <set-property-fallback name='locale' value='en'/>
   <set-property name='locale' value='en'/>
   <set-configuration-property name='UiBinder.useSafeHtmlTemplates' value='true'/>
 
+  <set-property name='gwt.logging.logLevel' value='SEVERE'/>
+  <set-property name='gwt.logging.popupHandler' value='DISABLED'/>
+
   <entry-point class='com.google.gerrit.client.Gerrit'/>
 </module>
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/Gerrit.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/Gerrit.java
index c8f40b2..14008e6 100644
--- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/Gerrit.java
+++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/Gerrit.java
@@ -85,6 +85,8 @@
 import com.google.gwtjsonrpc.common.AsyncCallback;
 import com.google.gwtorm.client.KeyUtil;
 
+import net.codemirror.lib.CodeMirror;
+
 import java.util.ArrayList;
 
 public class Gerrit implements EntryPoint {
@@ -366,6 +368,7 @@
 
     initHostname();
     Window.setTitle(M.windowTitle1(myHost));
+    CodeMirror.install();
 
     final HostPageDataService hpd = GWT.create(HostPageDataService.class);
     hpd.load(new GerritCallback<HostPageData>() {
diff --git a/gerrit-gwtui/src/main/java/net/codemirror/CodeMirror.gwt.xml b/gerrit-gwtui/src/main/java/net/codemirror/CodeMirror.gwt.xml
new file mode 100644
index 0000000..20e413c
--- /dev/null
+++ b/gerrit-gwtui/src/main/java/net/codemirror/CodeMirror.gwt.xml
@@ -0,0 +1,20 @@
+<!--
+ Copyright (C) 2013 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.
+-->
+<module>
+  <inherits name='com.google.gwt.logging.Logging'/>
+  <inherits name='com.google.gwt.resources.Resources'/>
+  <source path='lib'/>
+</module>
diff --git a/gerrit-gwtui/src/main/java/net/codemirror/lib/CodeMirror.java b/gerrit-gwtui/src/main/java/net/codemirror/lib/CodeMirror.java
new file mode 100644
index 0000000..0f08943
--- /dev/null
+++ b/gerrit-gwtui/src/main/java/net/codemirror/lib/CodeMirror.java
@@ -0,0 +1,68 @@
+// Copyright (C) 2013 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 net.codemirror.lib;
+
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.dom.client.ScriptElement;
+import com.google.gwt.dom.client.StyleInjector;
+import com.google.gwt.resources.client.ExternalTextResource;
+import com.google.gwt.resources.client.ResourceCallback;
+import com.google.gwt.resources.client.ResourceException;
+import com.google.gwt.resources.client.TextResource;
+import com.google.gwt.safehtml.shared.SafeUri;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class CodeMirror {
+  public static void install() {
+    asyncInjectCss(Lib.I.css());
+    asyncInjectScript(Lib.I.js().getSafeUri());
+  }
+
+  private static void asyncInjectCss(ExternalTextResource css) {
+    try {
+      css.getText(new ResourceCallback<TextResource>() {
+        @Override
+        public void onSuccess(TextResource resource) {
+          StyleInjector.inject(resource.getText());
+        }
+
+        @Override
+        public void onError(ResourceException e) {
+          error(e);
+        }
+      });
+    } catch (ResourceException e) {
+      error(e);
+    }
+  }
+
+  private static void asyncInjectScript(SafeUri uri) {
+    ScriptElement script = Document.get().createScriptElement();
+    script.setSrc(uri.asString());
+    script.setLang("javascript");
+    script.setType("text/javascript");
+    Document.get().getBody().appendChild(script);
+  }
+
+  private static void error(ResourceException e) {
+    Logger log = Logger.getLogger("net.codemirror");
+    log.log(Level.SEVERE, "Cannot fetch CSS", e);
+  }
+
+  private CodeMirror() {
+  }
+}
diff --git a/gerrit-gwtui/src/main/java/net/codemirror/lib/Lib.java b/gerrit-gwtui/src/main/java/net/codemirror/lib/Lib.java
new file mode 100644
index 0000000..242fd54
--- /dev/null
+++ b/gerrit-gwtui/src/main/java/net/codemirror/lib/Lib.java
@@ -0,0 +1,32 @@
+// Copyright (C) 2013 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 net.codemirror.lib;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.resources.client.ClientBundle;
+import com.google.gwt.resources.client.DataResource;
+import com.google.gwt.resources.client.DataResource.DoNotEmbed;
+import com.google.gwt.resources.client.ExternalTextResource;
+
+interface Lib extends ClientBundle {
+  static final Lib I = GWT.create(Lib.class);
+
+  @Source("codemirror.css")
+  ExternalTextResource css();
+
+  @Source("codemirror.js")
+  @DoNotEmbed
+  DataResource js();
+}
diff --git a/lib/BUCK b/lib/BUCK
index 9f24e6f..a9d0bf6 100644
--- a/lib/BUCK
+++ b/lib/BUCK
@@ -9,6 +9,7 @@
 define_license(name = 'automaton')
 define_license(name = 'bouncycastle')
 define_license(name = 'clippy')
+define_license(name = 'codemirror')
 define_license(name = 'h2')
 define_license(name = 'jgit')
 define_license(name = 'jsch')
diff --git a/lib/LICENSE-codemirror b/lib/LICENSE-codemirror
new file mode 100644
index 0000000..cdb16cd
--- /dev/null
+++ b/lib/LICENSE-codemirror
@@ -0,0 +1,19 @@
+Copyright (C) 2013 by Marijn Haverbeke <marijnh@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/lib/codemirror/BUCK b/lib/codemirror/BUCK
new file mode 100644
index 0000000..5d5aaed
--- /dev/null
+++ b/lib/codemirror/BUCK
@@ -0,0 +1,35 @@
+VERSION = '3.13'
+SHA1 = '7a83ae686d75afd30bb152d7683f2dc27e59ea82'
+URL = 'http://codemirror.net/codemirror-%s.zip' % VERSION
+
+# TODO(sop) Repackage by license boundaries.
+# TODO(sop) Minify with Closure JS compiler.
+genrule(
+  name = 'codemirror',
+  cmd = ';'.join([
+    'cd $TMP',
+    'mkdir net META-INF',
+    'unzip -d net $SRCS',
+    'mv net/codemirror-%s net/codemirror' % VERSION,
+    'echo "Manifest-Version: 1.0" >META-INF/MANIFEST.MF',
+    'zip -r $OUT *'
+  ]),
+  srcs = [genfile('dist.zip')],
+  deps = [
+    ':download',
+    '//lib:LICENSE-codemirror',
+  ],
+  out = 'codemirror.jar',
+  visibility = ['PUBLIC'],
+)
+
+genrule(
+  name = 'download',
+  cmd = '${//tools:download_file}' +
+    ' -o $OUT' +
+    ' -u ' + URL +
+    ' -v ' + SHA1,
+  srcs = [],
+  deps = ['//tools:download_file'],
+  out = 'dist.zip',
+)