diff options
6 files changed, 79 insertions, 9 deletions
diff --git a/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/RendererVisitor.java b/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/RendererVisitor.java index 2e54d00aab..8b645aa492 100644 --- a/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/RendererVisitor.java +++ b/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/RendererVisitor.java @@ -22,8 +22,11 @@ import com.google.gwt.core.ext.typeinfo.JClassType; import com.google.gwt.core.ext.typeinfo.JMethod; import com.google.gwt.core.ext.typeinfo.JParameterizedType; import com.google.gwt.core.ext.typeinfo.JType; +import com.google.gwt.core.ext.typeinfo.NotFoundException; import com.vaadin.client.connectors.AbstractRendererConnector; +import elemental.json.JsonValue; + /** * Generates type data for renderer connectors. * <ul> @@ -86,12 +89,33 @@ public class RendererVisitor extends TypeVisitor { JType presentationType = getPresentationType(type, logger); bundle.setPresentationType(type, presentationType); - bundle.setNeedsSerialize(presentationType); + if (!hasCustomDecodeMethod(type, logger)) { + bundle.setNeedsSerialize(presentationType); + } logger.log(Type.DEBUG, "Presentation type of " + type + " is " + presentationType); } + private static boolean hasCustomDecodeMethod(JClassType type, + TreeLogger logger) throws UnableToCompleteException { + try { + JMethod decodeMethod = ConnectorBundle.findInheritedMethod(type, + "decode", + type.getOracle().getType(JsonValue.class.getName())); + if (decodeMethod == null) { + throw new NotFoundException(); + } + + return !decodeMethod.getEnclosingType().getQualifiedSourceName() + .equals(AbstractRendererConnector.class.getName()); + } catch (NotFoundException e) { + logger.log(Type.ERROR, "Can't find decode method for renderer " + + type, e); + throw new UnableToCompleteException(); + } + } + private static JType getPresentationType(JClassType type, TreeLogger logger) throws UnableToCompleteException { JClassType originalType = type; diff --git a/client/src/com/vaadin/client/connectors/JavaScriptRendererConnector.java b/client/src/com/vaadin/client/connectors/JavaScriptRendererConnector.java index 112623d1fe..1229922a9c 100644 --- a/client/src/com/vaadin/client/connectors/JavaScriptRendererConnector.java +++ b/client/src/com/vaadin/client/connectors/JavaScriptRendererConnector.java @@ -42,9 +42,11 @@ import elemental.json.JsonValue; * @since 7.4 * @author Vaadin Ltd */ +// This is really typed to <JsonValue>, but because of the way native strings +// are not always instanceof JsonValue, we need to accept Object @Connect(AbstractJavaScriptRenderer.class) public class JavaScriptRendererConnector extends - AbstractRendererConnector<JsonValue> implements + AbstractRendererConnector<Object> implements HasJavaScriptConnectorHelper { private final JavaScriptConnectorHelper helper = new JavaScriptConnectorHelper( this); @@ -131,7 +133,7 @@ public class JavaScriptRendererConnector extends }-*/; @Override - protected Renderer<JsonValue> createRenderer() { + protected Renderer<Object> createRenderer() { helper.ensureJavascriptInited(); if (!hasFunction("render")) { @@ -146,11 +148,13 @@ public class JavaScriptRendererConnector extends final boolean hasGetConsumedEvents = hasFunction("getConsumedEvents"); final boolean hasOnBrowserEvent = hasFunction("onBrowserEvent"); - return new ComplexRenderer<JsonValue>() { + return new ComplexRenderer<Object>() { @Override - public void render(RendererCellReference cell, JsonValue data) { - render(helper.getConnectorWrapper(), getJsCell(cell), - Util.json2jso(data)); + public void render(RendererCellReference cell, Object data) { + if (data instanceof JsonValue) { + data = Util.json2jso((JsonValue) data); + } + render(helper.getConnectorWrapper(), getJsCell(cell), data); } private JavaScriptObject getJsCell(CellReference<?> cell) { @@ -159,7 +163,7 @@ public class JavaScriptRendererConnector extends } public native void render(JavaScriptObject wrapper, - JavaScriptObject cell, JavaScriptObject data) + JavaScriptObject cell, Object data) /*-{ wrapper.render(cell, data); }-*/; @@ -262,7 +266,7 @@ public class JavaScriptRendererConnector extends } @Override - public JsonValue decode(JsonValue value) { + public Object decode(JsonValue value) { // Let the js logic decode the raw json that the server sent return value; } diff --git a/uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderers.java b/uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderers.java index 4bfa244c22..8f88883161 100644 --- a/uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderers.java +++ b/uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderers.java @@ -55,6 +55,7 @@ public class JavaScriptRenderers extends AbstractTestUI { IndexedContainer container = new IndexedContainer(); container.addContainerProperty("id", Integer.class, Integer.valueOf(0)); container.addContainerProperty("bean", MyBean.class, null); + container.addContainerProperty("string", String.class, ""); for (int i = 0; i < 1000; i++) { Integer itemId = Integer.valueOf(i); @@ -62,6 +63,7 @@ public class JavaScriptRenderers extends AbstractTestUI { item.getItemProperty("id").setValue(itemId); item.getItemProperty("bean").setValue( new MyBean(i + 1, Integer.toString(i - 1))); + item.getItemProperty("string").setValue("string" + i); } Grid grid = new Grid(container); @@ -69,6 +71,8 @@ public class JavaScriptRenderers extends AbstractTestUI { grid.getColumn("bean").setRenderer(new MyBeanJSRenderer()); grid.getColumn("bean").setWidth(250); + grid.getColumn("string").setRenderer(new JavaScriptStringRenderer()); + addComponent(grid); } diff --git a/uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderersTest.java b/uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderersTest.java index 2e86053ef3..917245188a 100644 --- a/uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderersTest.java +++ b/uitest/src/com/vaadin/tests/components/grid/JavaScriptRenderersTest.java @@ -34,9 +34,13 @@ public class JavaScriptRenderersTest extends MultiBrowserTest { GridElement grid = $(GridElement.class).first(); GridCellElement cell_1_1 = grid.getCell(1, 1); + GridCellElement cell_2_2 = grid.getCell(2, 2); + // Verify render functionality Assert.assertEquals("Bean(2, 0)", cell_1_1.getText()); + Assert.assertEquals("string2", cell_2_2.getText()); + // Verify init functionality Assert.assertEquals("1", cell_1_1.getAttribute("column")); diff --git a/uitest/src/com/vaadin/tests/components/grid/JavaScriptStringRenderer.java b/uitest/src/com/vaadin/tests/components/grid/JavaScriptStringRenderer.java new file mode 100644 index 0000000000..ab3be6f4f1 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/grid/JavaScriptStringRenderer.java @@ -0,0 +1,29 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * 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.vaadin.tests.components.grid; + +import com.vaadin.annotations.JavaScript; +import com.vaadin.ui.renderers.AbstractJavaScriptRenderer; + +@JavaScript("JavaScriptStringRenderer.js") +public class JavaScriptStringRenderer extends + AbstractJavaScriptRenderer<String> { + + protected JavaScriptStringRenderer() { + super(String.class); + } + +} diff --git a/uitest/src/com/vaadin/tests/components/grid/JavaScriptStringRenderer.js b/uitest/src/com/vaadin/tests/components/grid/JavaScriptStringRenderer.js new file mode 100644 index 0000000000..475b453dcc --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/grid/JavaScriptStringRenderer.js @@ -0,0 +1,5 @@ +com_vaadin_tests_components_grid_JavaScriptStringRenderer = function() { + this.render = function(cell, data) { + cell.element.textContent = data; + } +}
\ No newline at end of file |