From 235ee04e1a8b1fb274be05c7755c511377908404 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Tue, 7 Jan 2014 07:52:46 +0100 Subject: [PATCH] Implement as method to convert between types --- .../src/main/java/com/google/gwt/query/client/Binder.java | 7 ++++++- .../src/main/java/com/google/gwt/query/client/GQ.java | 7 ++++++- .../main/java/com/google/gwt/query/client/Properties.java | 8 +++++++- .../com/google/gwt/query/client/builders/JsonBuilder.java | 2 +- .../google/gwt/query/client/builders/JsonBuilderBase.java | 6 +++++- .../com/google/gwt/query/rebind/JsonBuilderGenerator.java | 1 - .../main/java/com/google/gwt/query/vm/JsonFactoryJre.java | 6 +++++- 7 files changed, 30 insertions(+), 7 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Binder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Binder.java index ba45ceb1..2f0e47e5 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Binder.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Binder.java @@ -15,6 +15,8 @@ */ package com.google.gwt.query.client; +import com.google.gwt.query.client.builders.JsonBuilder; + /** * Interface using for Data Binders valid for JVM and JS. @@ -73,5 +75,8 @@ public interface Binder { /** * return the name for this type */ - String getName(); + String getJsonName(); + + + T as (Class clz); } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java index 5120bf3d..46cee7e1 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java @@ -22,7 +22,6 @@ import com.google.gwt.query.client.plugins.ajax.Ajax.AjaxTransport; import com.google.gwt.query.client.plugins.ajax.AjaxTransportJs; import com.google.gwt.query.vm.AjaxTransportJre; import com.google.gwt.query.vm.JsonFactoryJre; -import com.google.gwt.user.client.Window; public abstract class GQ { @@ -39,6 +38,12 @@ public abstract class GQ { return ret; } + public static T create(Class clz, Binder obj) { + T ret = create(clz); + ret.load(obj.getBound()); + return ret; + } + public static T create(String s) { return getFactory().create(s); } 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 b367e19f..60ac6a6b 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 @@ -18,6 +18,7 @@ package com.google.gwt.query.client; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArrayMixed; +import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.js.JsCache; import com.google.gwt.query.client.js.JsUtils; @@ -230,8 +231,13 @@ public class Properties extends JavaScriptObject implements Binder { return (J)this; } - public final String getName() { + public final String getJsonName() { return "jso"; } + public final T as(Class clz) { + T ret = GQ.create(clz); + ret.load(this); + return ret; + } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java index 6508d300..6e6d6f3a 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java @@ -40,5 +40,5 @@ public interface JsonBuilder extends Binder { /** * return the short name of this class, to use in json structures. */ - String getName(); + String getJsonName(); } 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 dd45ec7d..c4b5af88 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 @@ -97,7 +97,7 @@ public abstract class JsonBuilderBase> implements J } public String toJsonWithName() { - return "{\"" + getName() + "\":" + p.tostring() + "}"; + return "{\"" + getJsonName() + "\":" + p.tostring() + "}"; } @SuppressWarnings("unchecked") @@ -126,4 +126,8 @@ public abstract class JsonBuilderBase> implements J p.set(key, val); return (T)this; } + + public T as(Class clz) { + return p.as(clz); + } } 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 e1c905af..38f32b0e 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 @@ -273,7 +273,6 @@ public class JsonBuilderGenerator extends Generator { public void generateToJsonMethod(SourceWriter sw, String name, TreeLogger logger) { sw.println("public final String getJsonName() {return \"" + name + "\";}"); - sw.println("public final String getName() {return \"" + name + "\";}"); } protected SourceWriter getSourceWriter(TreeLogger logger, diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java index d3820772..abef1b8f 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java @@ -16,6 +16,7 @@ import org.json.JSONObject; import com.google.gwt.query.client.Binder; import com.google.gwt.query.client.Function; +import com.google.gwt.query.client.GQ; import com.google.gwt.query.client.Properties; import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.builders.JsonFactory; @@ -165,7 +166,10 @@ public class JsonFactoryJre implements JsonFactory { if ("getFieldNames".equals(mname)) { return JSONObject.getNames(jsonObject); - } else if ("getName".equals(mname)) { + } else if ("as".equals(mname)) { + Class clz = (Class)args[0]; + return jsonFactory.create(clz, jsonObject); + } else if ("getJsonName".equals(mname)) { return JsonBuilderGenerator.classNameToJsonName(getDataBindingClassName(proxy.getClass())); } else if (mname.matches("getProperties|getBound")) { return jsonObject; -- 2.39.5