From 394799f87a82d5df38741be573871c66d5701397 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Mon, 1 Oct 2012 08:54:07 +0200 Subject: [PATCH] Adding support for wrapping gwt Functions as js-functions and viceversa in properties and json objects --- .../google/gwt/query/client/Properties.java | 24 +++++++++++++++++++ .../client/builders/JsonBuilderBase.java | 15 +----------- .../google/gwt/query/client/js/JsUtils.java | 10 ++++++-- .../query/rebind/JsonBuilderGenerator.java | 4 +++- .../gwt/query/client/GQueryAjaxTestGwt.java | 23 ++++++++++++++++-- 5 files changed, 57 insertions(+), 19 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java index d2ae415e..04f32985 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java @@ -101,6 +101,22 @@ public class Properties extends JavaScriptObject { public final float getFloat(Object name) { return c().getFloat(String.valueOf(name)); } + + public final Function getFunction(Object name) { + final Object o = c().get(String.valueOf(name)); + if (o != null) { + if (o instanceof Function) { + return (Function)o; + } else if (o instanceof JavaScriptObject) { + Object f = ((JavaScriptObject)o).cast().getObject("__f"); + if (f != null && f instanceof Function) { + return (Function) f; + } + return new JsUtils.JsFunction((JavaScriptObject)o); + } + } + return null; + } public final int getInt(Object name) { return c().getInt(String.valueOf(name)); @@ -140,6 +156,14 @@ public class Properties extends JavaScriptObject { c().putBoolean(name, val); } + public final native void setFunction(T name, Function f) /*-{ + this[name] = function() { + f.@com.google.gwt.query.client.Function::fe(Ljava/lang/Object;)(arguments); + } + // We store the original function reference + this[name].__f = f; + }-*/; + public final void set(T name, O val) { c().put(String.valueOf(name), val); } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java index e1064332..43b892f7 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java @@ -17,7 +17,6 @@ package com.google.gwt.query.client.builders; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArray; -import com.google.gwt.query.client.Function; import com.google.gwt.query.client.Properties; import com.google.gwt.query.client.js.JsObjectArray; import com.google.gwt.query.client.js.JsUtils; @@ -83,19 +82,6 @@ public abstract class JsonBuilderBase> implements J return r != null ? r : Properties.create(); } - protected Function getFunctionBase(String n) { - final Object o = p.get(n); - if (o != null) { - if (o instanceof Function) { - return (Function)o; - } else if (o instanceof JavaScriptObject - && JsUtils.isFunction((JavaScriptObject)o)) { - return new JsUtils.JsFunction((JavaScriptObject)o); - } - } - return null; - } - public String toString() { return p.tostring(); } @@ -103,4 +89,5 @@ public abstract class JsonBuilderBase> implements J public Properties getProperties() { return p; } + } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java index a41d15e5..e9649b19 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java @@ -25,6 +25,7 @@ import com.google.gwt.dom.client.NodeList; import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.Properties; +import com.google.gwt.user.client.Command; import com.google.gwt.user.client.DOM; /** @@ -35,7 +36,7 @@ public class JsUtils { /** * A Function which wraps a javascript function. */ - public static class JsFunction extends Function { + public static class JsFunction extends Function implements Command { private JavaScriptObject jso = null; public JsFunction(JavaScriptObject f) { if (JsUtils.isFunction(f)) { @@ -50,11 +51,16 @@ public class JsUtils { private native void exec(JavaScriptObject f, Object data) /*-{ f(data); }-*/; + public void f() { if (jso != null) { - exec(jso, getData()[0]); + exec(jso, getDataObject()); } } + + public void execute() { + f(); + } } public static class JsUtilsImpl { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java index f61fd4e3..067285bf 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java @@ -169,7 +169,7 @@ public class JsonBuilderGenerator extends Generator { } else if (isTypeAssignableTo(method.getReturnType(), jsType)) { sw.println("return p.getJavaScriptObject(\"" + name + "\");"); } else if (isTypeAssignableTo(method.getReturnType(), functionType)) { - sw.println("return getFunctionBase(\"" + name + "\");"); + sw.println("return p.getFunction(\"" + name + "\");"); } else if (arr != null || list != null) { JType type = arr != null ? arr.getComponentType() : list.getTypeArgs()[0]; @@ -227,6 +227,8 @@ public class JsonBuilderGenerator extends Generator { sw.println("p.setNumber(\"" + name + "\", a);"); } else if (type.getParameterizedQualifiedSourceName().matches("(java.lang.Boolean|boolean)")) { sw.println("p.setBoolean(\"" + name + "\", a);"); + } else if (type.getParameterizedQualifiedSourceName().matches("com.google.gwt.query.client.Function")) { + sw.println("p.setFunction(\"" + name + "\", a);"); } else { sw.println("p.set(\"" + name + "\", a);"); } diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java index 03eddce8..45c976f7 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java @@ -82,8 +82,14 @@ public class GQueryAjaxTestGwt extends GWTTestCase { JsonExample setD(long l); List getItems(); void setItems(List a); + String y(); + void y(String s); + Function getF(); + void setF(Function f); } + + boolean functionRun = false; public void testJsonBuilder() { String json = "{a:1, b:{a:2,b:{a:3}},u:url, d:'2','t':['hola','adios'], 'z': true}"; JsonExample c = GWT.create(JsonExample.class); @@ -103,6 +109,17 @@ public class GQueryAjaxTestGwt extends GWTTestCase { assertEquals("foo", c.getT()[0]); assertEquals("bar", c.getT()[1]); assertEquals(1234l, c.getD()); + c.y("y"); + assertEquals("y", c.y()); + + c.setF(new Function() { + public void f() { + functionRun = true; + } + }); + assertFalse(functionRun); + c.getF().f(); + assertTrue(functionRun); Item i1 = GWT.create(Item.class); Item i2 = GWT.create(Item.class); @@ -112,8 +129,10 @@ public class GQueryAjaxTestGwt extends GWTTestCase { c.setItems(Arrays.asList(items)); assertEquals(2000l, c.getItems().get(0).getDate().getTime()); assertEquals(3000l, c.getItems().get(1).getDate().getTime()); - String s = "{\"a\":1,\"b\":{\"a\":2,\"b\":{\"a\":3}},\"u\":\"url\",\"d\":1234,\"t\":[\"foo\",\"bar\"],\"z\":false,\"items\":[{\"date\":2000},{\"date\":3000}]}"; - assertEquals(s, c.toString()); + String s = "{'a':1,'b':{'a':2,'b':{'a':3}},'u':'url','d':1234,'t':['foo','bar'],'z':false,'y':'y','items':[{'date':2000},{'date':3000}]}"; + assertEquals(s, c.toString().replaceAll("\"", "'")); + + } interface XmlExample extends XmlBuilder { -- 2.39.5