Introduce Gerrit gwtui common project

The new project hosts code that is reused between Gerrit core and GWT
based plugins.

Change-Id: I1a6e4352f681266c2ce664881a64d35bd2639299
This commit is contained in:
David Ostrovsky
2014-02-06 22:36:13 +01:00
parent ac2086d183
commit 520f8a9e23
13 changed files with 94 additions and 134 deletions

View File

@@ -0,0 +1,18 @@
<!--
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.
-->
<module>
<source path='client' />
</module>

View File

@@ -0,0 +1,99 @@
// Copyright (C) 2012 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.google.gerrit.client.rpc;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.Set;
/** A map of native JSON objects, keyed by a string. */
public class NativeMap<T extends JavaScriptObject> extends JavaScriptObject {
public static <T extends JavaScriptObject> NativeMap<T> create() {
return createObject().cast();
}
/**
* Loop through the result map's entries and copy the key strings into the
* "name" property of the corresponding child object. This only runs on the
* top level map of the result, and requires the children to be JSON objects
* and not a JSON primitive (e.g. boolean or string).
*/
public static <T extends JavaScriptObject,
M extends NativeMap<T>> AsyncCallback<M> copyKeysIntoChildren(
AsyncCallback<M> callback) {
return copyKeysIntoChildren("name", callback);
}
/** Loop through the result map and set asProperty on the children. */
public static <T extends JavaScriptObject,
M extends NativeMap<T>> AsyncCallback<M> copyKeysIntoChildren(
final String asProperty, AsyncCallback<M> callback) {
return new TransformCallback<M, M>(callback) {
@Override
protected M transform(M result) {
result.copyKeysIntoChildren(asProperty);
return result;
}
};
}
protected NativeMap() {
}
public final Set<String> keySet() {
return Natives.keys(this);
}
public final native JsArray<T> values()
/*-{
var s = this;
var v = [];
var i = 0;
for (var k in s) {
if (s.hasOwnProperty(k)) {
v[i++] = s[k];
}
}
return v;
}-*/;
public final int size() {
return keySet().size();
}
public final boolean isEmpty() {
return size() == 0;
}
public final boolean containsKey(String n) {
return get(n) != null;
}
public final native T get(String n) /*-{ return this[n]; }-*/;
public final native void put(String n, T v) /*-{ this[n] = v; }-*/;
public final native void copyKeysIntoChildren(String p)
/*-{
var s = this;
for (var k in s) {
if (s.hasOwnProperty(k)) {
var c = s[k];
c[p] = k;
}
}
}-*/;
}

View File

@@ -0,0 +1,66 @@
// Copyright (C) 2012 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.google.gerrit.client.rpc;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.rpc.AsyncCallback;
/** Wraps a String that was returned from a JSON API. */
public final class NativeString extends JavaScriptObject {
public static final JavaScriptObject TYPE = init();
// Used from core and plugins
private static final native JavaScriptObject init() /*-{
if ($wnd.Gerrit === undefined || $wnd.Gerrit.JsonString === undefined) {
return function(s){this.s=s};
} else {
return $wnd.Gerrit.JsonString;
}
}-*/;
static final NativeString wrap(String s) {
return wrap0(TYPE, s);
}
private static final native NativeString wrap0(JavaScriptObject T, String s)
/*-{ return new T(s) }-*/;
public final native String asString() /*-{ return this.s; }-*/;
public static final AsyncCallback<NativeString>
unwrap(final AsyncCallback<String> cb) {
return new AsyncCallback<NativeString>() {
@Override
public void onSuccess(NativeString result) {
cb.onSuccess(result != null ? result.asString() : null);
}
@Override
public void onFailure(Throwable caught) {
cb.onFailure(caught);
}
};
}
public static final boolean is(JavaScriptObject o) {
return is(TYPE, o);
}
private static final native boolean is(JavaScriptObject T, JavaScriptObject o)
/*-{ return o instanceof T }-*/;
protected NativeString() {
}
}

View File

@@ -0,0 +1,124 @@
// Copyright (C) 2012 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.google.gerrit.client.rpc;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.json.client.JSONObject;
import java.util.AbstractList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class Natives {
/**
* Get the names of defined properties on the object. The returned set
* iterates in the native iteration order, which may match the source order.
*/
public static Set<String> keys(JavaScriptObject obj) {
if (obj != null) {
return new JSONObject(obj).keySet();
}
return Collections.emptySet();
}
public static List<String> asList(final JsArrayString arr) {
if (arr == null) {
return null;
}
return new AbstractList<String>() {
@Override
public String set(int index, String element) {
String old = arr.get(index);
arr.set(index, element);
return old;
}
@Override
public String get(int index) {
return arr.get(index);
}
@Override
public int size() {
return arr.length();
}
};
}
public static <T extends JavaScriptObject> List<T> asList(
final JsArray<T> arr) {
if (arr == null) {
return null;
}
return new AbstractList<T>() {
@Override
public T set(int index, T element) {
T old = arr.get(index);
arr.set(index, element);
return old;
}
@Override
public T get(int index) {
return arr.get(index);
}
@Override
public int size() {
return arr.length();
}
};
}
public static <T extends JavaScriptObject> JsArray<T> arrayOf(T element) {
JsArray<T> arr = JavaScriptObject.createArray().cast();
arr.push(element);
return arr;
}
@SuppressWarnings("unchecked")
public static <T extends JavaScriptObject> T parseJSON(String json) {
if (json.startsWith("\"")) {
return (T) NativeString.wrap(parseString(parser, json));
}
return Natives.<T> parseObject(parser, json); // javac generics bug
}
private static native <T extends JavaScriptObject>
T parseObject(JavaScriptObject p, String s)
/*-{ return p(s); }-*/;
private static native
String parseString(JavaScriptObject p, String s)
/*-{ return p(s); }-*/;
private static JavaScriptObject parser;
private static native JavaScriptObject bestJsonParser()
/*-{
if ($wnd.JSON && typeof $wnd.JSON.parse === 'function')
return $wnd.JSON.parse;
return function(s) { return eval('(' + s + ')'); };
}-*/;
static {
parser = bestJsonParser();
}
private Natives() {
}
}

View File

@@ -0,0 +1,38 @@
// Copyright (C) 2012 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.google.gerrit.client.rpc;
import com.google.gwt.user.client.rpc.AsyncCallback;
/** Transforms a value and passes it on to another callback. */
public abstract class TransformCallback<I, O> implements AsyncCallback<I>{
private final AsyncCallback<O> callback;
protected TransformCallback(AsyncCallback<O> callback) {
this.callback = callback;
}
@Override
public void onSuccess(I result) {
callback.onSuccess(transform(result));
}
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
protected abstract O transform(I result);
}