From 1e3810ad25a55a3f17be29e2627f89ec51bab07c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Sat, 28 Dec 2013 23:26:57 +0100 Subject: [PATCH] Using Binder interface in certain ajax properties --- .../com/google/gwt/query/client/Binder.java | 68 +++++++++++++++++++ .../google/gwt/query/client/Properties.java | 4 +- .../client/builders/JsonBuilderBase.java | 6 ++ .../gwt/query/client/plugins/ajax/Ajax.java | 36 ++++++---- .../plugins/deferred/PromiseReqBuilder.java | 8 +-- .../query/rebind/JsonBuilderGenerator.java | 7 +- 6 files changed, 108 insertions(+), 21 deletions(-) create mode 100644 gwtquery-core/src/main/java/com/google/gwt/query/client/Binder.java 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 new file mode 100644 index 00000000..c43122ac --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Binder.java @@ -0,0 +1,68 @@ +/* + * Copyright 2013, The gwtquery team. + * + * 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.gwt.query.client; + + +/** + * Interface using for Data Binders valid for JVM and JS. + */ +public interface Binder { + /** + * load a properties object. + */ + J load(Object prp); + + /** + * parses a json string and loads the resulting properties object. + */ + J parse(String json); + + /** + * Returns the underlying object, normally a Properties jso in client + * and a Json implementation in JVM. + */ + J getBound(); + + /** + * Return the Object with the given key. + */ + T get(Object key); + + /** + * Set an Object with the given key. + */ + T set(Object key, Object val); + + /** + * return a list of field names. + */ + String[] getFieldNames(); + + /** + * return a string which represents the object with an alias for the className + */ + String toJson(); + + /** + * return a string which represents the object in a queryString format. + */ + String toQueryString(); + + /** + * return the name for this type + */ + String getName(); +} 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 af671474..b8e341c4 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 @@ -179,8 +179,10 @@ public class Properties extends JavaScriptObject implements Binder { this[name].__f = f; }-*/; - public final void set(T name, O val) { + @SuppressWarnings("unchecked") + public final Properties set(Object name, Object val) { c().put(String.valueOf(name), val); + return this; } public final String tostring() { 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 af3a5128..61cca6f3 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 @@ -116,4 +116,10 @@ public abstract class JsonBuilderBase> implements J public T get(Object key) { return p.get(key); } + + @SuppressWarnings("unchecked") + public T set(Object key, Object val) { + p.set(key, val); + return (T)this; + } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java index 3170c3cb..9e99f3b5 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java @@ -4,12 +4,15 @@ import com.google.gwt.core.client.Callback; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.ScriptInjector; import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.ScriptElement; import com.google.gwt.http.client.Request; import com.google.gwt.http.client.Response; import com.google.gwt.query.client.Function; +import com.google.gwt.query.client.GQ; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.Promise; import com.google.gwt.query.client.Properties; +import com.google.gwt.query.client.Binder; import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.query.client.plugins.Plugin; @@ -39,11 +42,11 @@ public class Ajax extends GQuery { public interface Settings extends JsonBuilder { String getContentType(); Element getContext(); - Properties getData(); + Binder getData(); String getDataString(); String getDataType(); Function getError(); - Properties getHeaders(); + Binder getHeaders(); String getPassword(); Function getSuccess(); int getTimeout(); @@ -52,11 +55,11 @@ public class Ajax extends GQuery { String getUsername(); Settings setContentType(String t); Settings setContext(Element e); - Settings setData(Properties p); + Settings setData(Object p); Settings setDataString(String d); Settings setDataType(String t); Settings setError(Function f); - Settings setHeaders(Properties p); + Settings setHeaders(Binder p); Settings setPassword(String p); Settings setSuccess(Function f); Settings setTimeout(int t); @@ -172,16 +175,21 @@ public class Ajax extends GQuery { private static Promise createPromiseScriptInjector(final String url) { return new PromiseFunction() { + private ScriptElement scriptElement; public void f(final Deferred dfd) { - ScriptInjector.fromUrl(url).setWindow(window) + scriptElement = ScriptInjector.fromUrl(url).setWindow(window) .setCallback(new Callback() { public void onSuccess(Void result) { - dfd.resolve(); + $(window).delay(0, new Function(){ + public void f() { + dfd.resolve(scriptElement); + } + }); } public void onFailure(Exception reason) { dfd.reject(reason); } - }).inject(); + }).inject().cast(); } }; } @@ -197,15 +205,15 @@ public class Ajax extends GQuery { private static Object resolveData(Settings settings, String httpMethod) { Object data = settings.getDataString(); - Properties sdata = settings.getData(); + Binder sdata = settings.getData(); if (data == null && sdata != null) { String type = settings.getDataType(); if (type != null && (httpMethod.matches("(POST|PUT)")) && type.equalsIgnoreCase("json")) { - data = sdata.toJsonString(); - } else if (JsUtils.isFormData(sdata)) { - data = sdata; + data = sdata.toString(); +// } else if (JsUtils.isFormData(sdata)) { +// data = sdata; } else { data = sdata.toQueryString(); } @@ -244,9 +252,9 @@ public class Ajax extends GQuery { return createSettings($$(prop)); } - public static Settings createSettings(Properties p) { - Settings s = GWT.create(Settings.class); - s.load(p); + public static Settings createSettings(Binder p) { + Settings s = GQ.create(Settings.class); + s.load(p.getBound()); return s; } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java index 61273296..3d1d0194 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java @@ -21,8 +21,8 @@ import com.google.gwt.http.client.RequestCallback; import com.google.gwt.http.client.RequestException; import com.google.gwt.http.client.RequestPermissionException; import com.google.gwt.http.client.Response; +import com.google.gwt.query.client.Binder; import com.google.gwt.query.client.Function; -import com.google.gwt.query.client.Properties; import com.google.gwt.query.client.js.JsCache; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.query.client.plugins.ajax.Ajax.Settings; @@ -102,10 +102,10 @@ public class PromiseReqBuilder extends DeferredPromiseImpl implements RequestCal } })); - Properties headers = settings.getHeaders(); + Binder headers = settings.getHeaders(); if (headers != null) { - for (String headerKey : headers.keys()) { - xmlHttpRequest.setRequestHeader(headerKey, headers.getStr(headerKey)); + for (String headerKey : headers.getFieldNames()) { + xmlHttpRequest.setRequestHeader(headerKey, String.valueOf(headers.get(headerKey))); } } 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 8c9c7408..2e66bc3f 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 @@ -184,6 +184,9 @@ public class JsonBuilderGenerator extends Generator { String q = method.getReturnType().getQualifiedSourceName(); sw.println("return " + "((" + q + ")GWT.create(" + q + ".class))" + ".load(p.getJavaScriptObject(\"" + name + "\"));"); + } else if (isTypeAssignableTo(method.getReturnType(), settingsType)) { + String q = method.getReturnType().getQualifiedSourceName(); + sw.println("return " + "((" + q + ")p.getJavaScriptObject(\"" + name + "\"));"); } else if (retType.equals(Properties.class.getName())) { sw.println("return getPropertiesBase(\"" + name + "\");"); } else if (isTypeAssignableTo(method.getReturnType(), jsType)) { @@ -218,8 +221,8 @@ public class JsonBuilderGenerator extends Generator { sw.println("return Arrays.asList(" + ret + ");"); } } else if (method.getReturnType().isEnum() != null){ - sw.println("return "+method.getReturnType().getQualifiedSourceName()+".valueOf(p.getStr(\"" + name + "\"));"); - }else { + sw.println("return "+ method.getReturnType().getQualifiedSourceName() + ".valueOf(p.getStr(\"" + name + "\"));"); + } else { sw.println("System.err.println(\"JsonBuilderGenerator WARN: unknown return type " + retType + " " + ifaceName + "." + name + "()\"); "); // We return the object because probably the user knows how to handle it -- 2.39.5