From 5102cc98c295a49487fcf90cc6ac7980d166eb40 Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Mon, 17 Nov 2014 08:14:10 +0200 Subject: Implement JavaScript renderer support (#15485) Change-Id: Ifeac12d4124a4a7e5d0c143ff5c0590a2c98509d --- .../vaadin/server/AbstractJavaScriptExtension.java | 4 +- .../com/vaadin/ui/AbstractJavaScriptComponent.java | 4 +- .../ui/renderer/AbstractJavaScriptRenderer.java | 157 +++++++++++++++++++++ 3 files changed, 161 insertions(+), 4 deletions(-) create mode 100644 server/src/com/vaadin/ui/renderer/AbstractJavaScriptRenderer.java (limited to 'server/src/com/vaadin') diff --git a/server/src/com/vaadin/server/AbstractJavaScriptExtension.java b/server/src/com/vaadin/server/AbstractJavaScriptExtension.java index e182319c85..e9cf2c5e33 100644 --- a/server/src/com/vaadin/server/AbstractJavaScriptExtension.java +++ b/server/src/com/vaadin/server/AbstractJavaScriptExtension.java @@ -106,8 +106,8 @@ import com.vaadin.ui.JavaScriptFunction; *
  • Java Dates are represented by JavaScript numbers containing the timestamp *
  • *
  • List, Set and all arrays in Java are represented by JavaScript arrays.
  • - *
  • Map in Java is represented by JavaScript object with fields - * corresponding to the map keys.
  • + *
  • Map<String, ?> in Java is represented by JavaScript object with + * fields corresponding to the map keys.
  • *
  • Any other Java Map is represented by a JavaScript array containing two * arrays, the first contains the keys and the second contains the values in the * same order.
  • diff --git a/server/src/com/vaadin/ui/AbstractJavaScriptComponent.java b/server/src/com/vaadin/ui/AbstractJavaScriptComponent.java index f3cbf47b62..84023555bb 100644 --- a/server/src/com/vaadin/ui/AbstractJavaScriptComponent.java +++ b/server/src/com/vaadin/ui/AbstractJavaScriptComponent.java @@ -119,8 +119,8 @@ import com.vaadin.shared.ui.JavaScriptComponentState; *
  • Java Dates are represented by JavaScript numbers containing the timestamp *
  • *
  • List, Set and all arrays in Java are represented by JavaScript arrays.
  • - *
  • Map in Java is represented by JavaScript object with fields - * corresponding to the map keys.
  • + *
  • Map<String, ?> in Java is represented by JavaScript object with + * fields corresponding to the map keys.
  • *
  • Any other Java Map is represented by a JavaScript array containing two * arrays, the first contains the keys and the second contains the values in the * same order.
  • diff --git a/server/src/com/vaadin/ui/renderer/AbstractJavaScriptRenderer.java b/server/src/com/vaadin/ui/renderer/AbstractJavaScriptRenderer.java new file mode 100644 index 0000000000..92c5047279 --- /dev/null +++ b/server/src/com/vaadin/ui/renderer/AbstractJavaScriptRenderer.java @@ -0,0 +1,157 @@ +/* + * 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.ui.renderer; + +import com.vaadin.server.AbstractJavaScriptExtension; +import com.vaadin.server.JavaScriptCallbackHelper; +import com.vaadin.shared.JavaScriptExtensionState; +import com.vaadin.shared.communication.ServerRpc; +import com.vaadin.ui.Grid.AbstractRenderer; +import com.vaadin.ui.JavaScriptFunction; + +/** + * Base class for Renderers with all client-side logic implemented using + * JavaScript. + *

    + * When a new JavaScript renderer is initialized in the browser, the framework + * will look for a globally defined JavaScript function that will initialize the + * renderer. The name of the initialization function is formed by replacing . + * with _ in the name of the server-side class. If no such function is defined, + * each super class is used in turn until a match is found. The framework will + * thus first attempt with com_example_MyRenderer for the + * server-side + * com.example.MyRenderer extends AbstractJavaScriptRenderer class. + * If MyRenderer instead extends com.example.SuperRenderer , then + * com_example_SuperRenderer will also be attempted if + * com_example_MyRenderer has not been defined. + *

    + * + * In addition to the general JavaScript extension functionality explained in + * {@link AbstractJavaScriptExtension}, this class also provides some + * functionality specific for renderers. + *

    + * The initialization function will be called with this pointing to + * a connector wrapper object providing integration to Vaadin with the following + * functions: + *

    + * The connector wrapper also supports these special functions that can be + * implemented by the connector: + * + * + *

    + * The cell object passed to functions defined by the renderer has these + * properties: + *

    + * + * @author Vaadin Ltd + * @since + */ +public abstract class AbstractJavaScriptRenderer extends AbstractRenderer { + private JavaScriptCallbackHelper callbackHelper = new JavaScriptCallbackHelper( + this); + + protected AbstractJavaScriptRenderer(Class presentationType) { + super(presentationType); + } + + @Override + protected void registerRpc(R implementation, + Class rpcInterfaceType) { + super.registerRpc(implementation, rpcInterfaceType); + callbackHelper.registerRpc(rpcInterfaceType); + } + + /** + * Register a {@link JavaScriptFunction} that can be called from the + * JavaScript using the provided name. A JavaScript function with the + * provided name will be added to the connector wrapper object (initially + * available as this). Calling that JavaScript function will + * cause the call method in the registered {@link JavaScriptFunction} to be + * invoked with the same arguments. + * + * @param functionName + * the name that should be used for client-side callback + * @param function + * the {@link JavaScriptFunction} object that will be invoked + * when the JavaScript function is called + */ + protected void addFunction(String functionName, JavaScriptFunction function) { + callbackHelper.registerCallback(functionName, function); + } + + /** + * Invoke a named function that the connector JavaScript has added to the + * JavaScript connector wrapper object. The arguments should only contain + * data types that can be represented in JavaScript including primitives, + * their boxed types, arrays, String, List, Set, Map, Connector and + * JavaBeans. + * + * @param name + * the name of the function + * @param arguments + * function arguments + */ + protected void callFunction(String name, Object... arguments) { + callbackHelper.invokeCallback(name, arguments); + } + + @Override + protected JavaScriptExtensionState getState() { + return (JavaScriptExtensionState) super.getState(); + } +} -- cgit v1.2.3