From 16535959cf97dcd7c3e1fb4530a9c9b1833eb5e9 Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Fri, 6 Feb 2015 15:09:44 +0200 Subject: Rename com.vaadin.ui.renderer to com.vaadin.ui.renderers (#16521) Change-Id: I6fd0313aaba5619a55f03fb52d72c032ea18f409 --- .../com/vaadin/data/RpcDataProviderExtension.java | 2 +- server/src/com/vaadin/ui/Grid.java | 4 +- .../ui/renderer/AbstractJavaScriptRenderer.java | 157 -------------------- .../src/com/vaadin/ui/renderer/ButtonRenderer.java | 45 ------ .../com/vaadin/ui/renderer/ClickableRenderer.java | 138 ----------------- .../src/com/vaadin/ui/renderer/DateRenderer.java | 156 -------------------- .../src/com/vaadin/ui/renderer/HtmlRenderer.java | 33 ----- .../src/com/vaadin/ui/renderer/ImageRenderer.java | 67 --------- .../src/com/vaadin/ui/renderer/NumberRenderer.java | 163 --------------------- .../vaadin/ui/renderer/ProgressBarRenderer.java | 44 ------ server/src/com/vaadin/ui/renderer/Renderer.java | 69 --------- .../src/com/vaadin/ui/renderer/TextRenderer.java | 34 ----- .../ui/renderers/AbstractJavaScriptRenderer.java | 157 ++++++++++++++++++++ .../com/vaadin/ui/renderers/ButtonRenderer.java | 45 ++++++ .../com/vaadin/ui/renderers/ClickableRenderer.java | 138 +++++++++++++++++ .../src/com/vaadin/ui/renderers/DateRenderer.java | 156 ++++++++++++++++++++ .../src/com/vaadin/ui/renderers/HtmlRenderer.java | 33 +++++ .../src/com/vaadin/ui/renderers/ImageRenderer.java | 67 +++++++++ .../com/vaadin/ui/renderers/NumberRenderer.java | 163 +++++++++++++++++++++ .../vaadin/ui/renderers/ProgressBarRenderer.java | 44 ++++++ server/src/com/vaadin/ui/renderers/Renderer.java | 69 +++++++++ .../src/com/vaadin/ui/renderers/TextRenderer.java | 34 +++++ 22 files changed, 909 insertions(+), 909 deletions(-) delete mode 100644 server/src/com/vaadin/ui/renderer/AbstractJavaScriptRenderer.java delete mode 100644 server/src/com/vaadin/ui/renderer/ButtonRenderer.java delete mode 100644 server/src/com/vaadin/ui/renderer/ClickableRenderer.java delete mode 100644 server/src/com/vaadin/ui/renderer/DateRenderer.java delete mode 100644 server/src/com/vaadin/ui/renderer/HtmlRenderer.java delete mode 100644 server/src/com/vaadin/ui/renderer/ImageRenderer.java delete mode 100644 server/src/com/vaadin/ui/renderer/NumberRenderer.java delete mode 100644 server/src/com/vaadin/ui/renderer/ProgressBarRenderer.java delete mode 100644 server/src/com/vaadin/ui/renderer/Renderer.java delete mode 100644 server/src/com/vaadin/ui/renderer/TextRenderer.java create mode 100644 server/src/com/vaadin/ui/renderers/AbstractJavaScriptRenderer.java create mode 100644 server/src/com/vaadin/ui/renderers/ButtonRenderer.java create mode 100644 server/src/com/vaadin/ui/renderers/ClickableRenderer.java create mode 100644 server/src/com/vaadin/ui/renderers/DateRenderer.java create mode 100644 server/src/com/vaadin/ui/renderers/HtmlRenderer.java create mode 100644 server/src/com/vaadin/ui/renderers/ImageRenderer.java create mode 100644 server/src/com/vaadin/ui/renderers/NumberRenderer.java create mode 100644 server/src/com/vaadin/ui/renderers/ProgressBarRenderer.java create mode 100644 server/src/com/vaadin/ui/renderers/Renderer.java create mode 100644 server/src/com/vaadin/ui/renderers/TextRenderer.java (limited to 'server/src') diff --git a/server/src/com/vaadin/data/RpcDataProviderExtension.java b/server/src/com/vaadin/data/RpcDataProviderExtension.java index 630336816e..924bd3de84 100644 --- a/server/src/com/vaadin/data/RpcDataProviderExtension.java +++ b/server/src/com/vaadin/data/RpcDataProviderExtension.java @@ -52,7 +52,7 @@ import com.vaadin.ui.Grid.CellStyleGenerator; import com.vaadin.ui.Grid.Column; import com.vaadin.ui.Grid.RowReference; import com.vaadin.ui.Grid.RowStyleGenerator; -import com.vaadin.ui.renderer.Renderer; +import com.vaadin.ui.renderers.Renderer; import elemental.json.Json; import elemental.json.JsonArray; diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index bbae501782..a668c42be1 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -90,8 +90,8 @@ import com.vaadin.shared.ui.grid.GridStaticSectionState.RowState; import com.vaadin.shared.ui.grid.HeightMode; import com.vaadin.shared.ui.grid.ScrollDestination; import com.vaadin.shared.util.SharedUtil; -import com.vaadin.ui.renderer.Renderer; -import com.vaadin.ui.renderer.TextRenderer; +import com.vaadin.ui.renderers.Renderer; +import com.vaadin.ui.renderers.TextRenderer; import com.vaadin.util.ReflectTools; import elemental.json.Json; diff --git a/server/src/com/vaadin/ui/renderer/AbstractJavaScriptRenderer.java b/server/src/com/vaadin/ui/renderer/AbstractJavaScriptRenderer.java deleted file mode 100644 index 8fabded536..0000000000 --- a/server/src/com/vaadin/ui/renderer/AbstractJavaScriptRenderer.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * 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 7.4 - */ -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(); - } -} diff --git a/server/src/com/vaadin/ui/renderer/ButtonRenderer.java b/server/src/com/vaadin/ui/renderer/ButtonRenderer.java deleted file mode 100644 index b0819794c0..0000000000 --- a/server/src/com/vaadin/ui/renderer/ButtonRenderer.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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; - -/** - * A Renderer that displays a button with a textual caption. The value of the - * corresponding property is used as the caption. Click listeners can be added - * to the renderer, invoked when any of the rendered buttons is clicked. - * - * @since 7.4 - * @author Vaadin Ltd - */ -public class ButtonRenderer extends ClickableRenderer { - - /** - * Creates a new button renderer. - */ - public ButtonRenderer() { - super(String.class); - } - - /** - * Creates a new button renderer and adds the given click listener to it. - * - * @param listener - * the click listener to register - */ - public ButtonRenderer(RendererClickListener listener) { - this(); - addClickListener(listener); - } -} diff --git a/server/src/com/vaadin/ui/renderer/ClickableRenderer.java b/server/src/com/vaadin/ui/renderer/ClickableRenderer.java deleted file mode 100644 index d640ce8b71..0000000000 --- a/server/src/com/vaadin/ui/renderer/ClickableRenderer.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * 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 java.lang.reflect.Method; - -import com.vaadin.event.ConnectorEventListener; -import com.vaadin.event.MouseEvents.ClickEvent; -import com.vaadin.shared.MouseEventDetails; -import com.vaadin.shared.ui.grid.renderers.RendererClickRpc; -import com.vaadin.ui.Grid; -import com.vaadin.ui.Grid.AbstractRenderer; -import com.vaadin.ui.Grid.Column; -import com.vaadin.util.ReflectTools; - -/** - * An abstract superclass for Renderers that render clickable items. Click - * listeners can be added to a renderer to be notified when any of the rendered - * items is clicked. - * - * @param - * the type presented by the renderer - * - * @since 7.4 - * @author Vaadin Ltd - */ -public class ClickableRenderer extends AbstractRenderer { - - /** - * An interface for listening to {@link RendererClickEvent renderer click - * events}. - * - * @see {@link ButtonRenderer#addClickListener(RendererClickListener)} - */ - public interface RendererClickListener extends ConnectorEventListener { - - static final Method CLICK_METHOD = ReflectTools.findMethod( - RendererClickListener.class, "click", RendererClickEvent.class); - - /** - * Called when a rendered button is clicked. - * - * @param event - * the event representing the click - */ - void click(RendererClickEvent event); - } - - /** - * An event fired when a button rendered by a ButtonRenderer is clicked. - */ - public static class RendererClickEvent extends ClickEvent { - - private Object itemId; - private Column column; - - protected RendererClickEvent(Grid source, Object itemId, Column column, - MouseEventDetails mouseEventDetails) { - super(source, mouseEventDetails); - this.itemId = itemId; - this.column = column; - } - - /** - * Returns the item ID of the row where the click event originated. - * - * @return the item ID of the clicked row - */ - public Object getItemId() { - return itemId; - } - - /** - * Returns the {@link Column} where the click event originated. - * - * @return the column of the click event - */ - public Column getColumn() { - return column; - } - - /** - * Returns the property ID where the click event originated. - * - * @return the property ID of the clicked cell - */ - public Object getPropertyId() { - return column.getPropertyId(); - } - } - - protected ClickableRenderer(Class presentationType) { - super(presentationType); - registerRpc(new RendererClickRpc() { - @Override - public void click(String rowKey, String columnId, - MouseEventDetails mouseDetails) { - fireEvent(new RendererClickEvent(getParentGrid(), - getItemId(rowKey), getColumn(columnId), mouseDetails)); - } - }); - } - - /** - * Adds a click listener to this button renderer. The listener is invoked - * every time one of the buttons rendered by this renderer is clicked. - * - * @param listener - * the click listener to be added - */ - public void addClickListener(RendererClickListener listener) { - addListener(RendererClickEvent.class, listener, - RendererClickListener.CLICK_METHOD); - } - - /** - * Removes the given click listener from this renderer. - * - * @param listener - * the click listener to be removed - */ - public void removeClickListener(RendererClickListener listener) { - removeListener(RendererClickEvent.class, listener); - } -} diff --git a/server/src/com/vaadin/ui/renderer/DateRenderer.java b/server/src/com/vaadin/ui/renderer/DateRenderer.java deleted file mode 100644 index d3d2df573d..0000000000 --- a/server/src/com/vaadin/ui/renderer/DateRenderer.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * 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 java.text.DateFormat; -import java.util.Date; -import java.util.Locale; - -import com.vaadin.ui.Grid.AbstractRenderer; - -import elemental.json.JsonValue; - -/** - * A renderer for presenting date values. - * - * @since 7.4 - * @author Vaadin Ltd - */ -public class DateRenderer extends AbstractRenderer { - private final Locale locale; - private final String formatString; - private final DateFormat dateFormat; - - /** - * Creates a new date renderer. - *

- * The renderer is configured to render with the {@link Date#toString()} - * representation for the default locale. - */ - public DateRenderer() { - this(Locale.getDefault()); - } - - /** - * Creates a new date renderer. - *

- * The renderer is configured to render with the {@link Date#toString()} - * representation for the given locale. - * - * @param locale - * the locale in which to present dates - * @throws IllegalArgumentException - * if {@code locale} is {@code null} - */ - public DateRenderer(Locale locale) throws IllegalArgumentException { - this("%s", locale); - } - - /** - * Creates a new date renderer. - *

- * The renderer is configured to render with the given string format, as - * displayed in the default locale. - * - * @param formatString - * the format string with which to format the date - * @throws IllegalArgumentException - * if {@code formatString} is {@code null} - * @see Format - * String Syntax - */ - public DateRenderer(String formatString) throws IllegalArgumentException { - this(formatString, Locale.getDefault()); - } - - /** - * Creates a new date renderer. - *

- * The renderer is configured to render with the given string format, as - * displayed in the given locale. - * - * @param formatString - * the format string to format the date with - * @param locale - * the locale to use - * @throws IllegalArgumentException - * if either argument is {@code null} - * @see Format - * String Syntax - */ - public DateRenderer(String formatString, Locale locale) - throws IllegalArgumentException { - super(Date.class); - - if (formatString == null) { - throw new IllegalArgumentException("format string may not be null"); - } - - if (locale == null) { - throw new IllegalArgumentException("locale may not be null"); - } - - this.locale = locale; - this.formatString = formatString; - dateFormat = null; - } - - /** - * Creates a new date renderer. - *

- * The renderer is configured to render with he given date format. - * - * @param dateFormat - * the date format to use when rendering dates - * @throws IllegalArgumentException - * if {@code dateFormat} is {@code null} - */ - public DateRenderer(DateFormat dateFormat) throws IllegalArgumentException { - super(Date.class); - if (dateFormat == null) { - throw new IllegalArgumentException("date format may not be null"); - } - - locale = null; - formatString = null; - this.dateFormat = dateFormat; - } - - @Override - public JsonValue encode(Date value) { - String dateString; - if (dateFormat != null) { - dateString = dateFormat.format(value); - } else { - dateString = String.format(locale, formatString, value); - } - return encode(dateString, String.class); - } - - @Override - public String toString() { - final String fieldInfo; - if (dateFormat != null) { - fieldInfo = "dateFormat: " + dateFormat.toString(); - } else { - fieldInfo = "locale: " + locale + ", formatString: " + formatString; - } - - return String.format("%s [%s]", getClass().getSimpleName(), fieldInfo); - } -} diff --git a/server/src/com/vaadin/ui/renderer/HtmlRenderer.java b/server/src/com/vaadin/ui/renderer/HtmlRenderer.java deleted file mode 100644 index 02d153dedf..0000000000 --- a/server/src/com/vaadin/ui/renderer/HtmlRenderer.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.ui.Grid.AbstractRenderer; - -/** - * A renderer for presenting HTML content. - * - * @since 7.4 - * @author Vaadin Ltd - */ -public class HtmlRenderer extends AbstractRenderer { - /** - * Creates a new HTML renderer. - */ - public HtmlRenderer() { - super(String.class); - } -} diff --git a/server/src/com/vaadin/ui/renderer/ImageRenderer.java b/server/src/com/vaadin/ui/renderer/ImageRenderer.java deleted file mode 100644 index 3ef3eed3e5..0000000000 --- a/server/src/com/vaadin/ui/renderer/ImageRenderer.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.ExternalResource; -import com.vaadin.server.Resource; -import com.vaadin.server.ResourceReference; -import com.vaadin.server.ThemeResource; -import com.vaadin.shared.communication.URLReference; - -import elemental.json.JsonValue; - -/** - * A renderer for presenting images. - *

- * The image for each rendered cell is read from a Resource-typed property in - * the data source. Only {@link ExternalResource}s and {@link ThemeResource}s - * are currently supported. - * - * @since 7.4 - * @author Vaadin Ltd - */ -public class ImageRenderer extends ClickableRenderer { - - /** - * Creates a new image renderer. - */ - public ImageRenderer() { - super(Resource.class); - } - - /** - * Creates a new image renderer and adds the given click listener to it. - * - * @param listener - * the click listener to register - */ - public ImageRenderer(RendererClickListener listener) { - this(); - addClickListener(listener); - } - - @Override - public JsonValue encode(Resource resource) { - if (!(resource instanceof ExternalResource || resource instanceof ThemeResource)) { - throw new IllegalArgumentException( - "ImageRenderer only supports ExternalResource and ThemeResource (" - + resource.getClass().getSimpleName() + "given )"); - } - - return encode(ResourceReference.create(resource, this, null), - URLReference.class); - } -} diff --git a/server/src/com/vaadin/ui/renderer/NumberRenderer.java b/server/src/com/vaadin/ui/renderer/NumberRenderer.java deleted file mode 100644 index 3406e1837a..0000000000 --- a/server/src/com/vaadin/ui/renderer/NumberRenderer.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * 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 java.text.NumberFormat; -import java.util.Locale; - -import com.vaadin.ui.Grid.AbstractRenderer; - -import elemental.json.JsonValue; - -/** - * A renderer for presenting number values. - * - * @since 7.4 - * @author Vaadin Ltd - */ -public class NumberRenderer extends AbstractRenderer { - private final Locale locale; - private final NumberFormat numberFormat; - private final String formatString; - - /** - * Creates a new number renderer. - *

- * The renderer is configured to render with the number's natural string - * representation in the default locale. - */ - public NumberRenderer() { - this(Locale.getDefault()); - } - - /** - * Creates a new number renderer. - *

- * The renderer is configured to render the number as defined with the given - * number format. - * - * @param numberFormat - * the number format with which to display numbers - * @throws IllegalArgumentException - * if {@code numberFormat} is {@code null} - */ - public NumberRenderer(NumberFormat numberFormat) - throws IllegalArgumentException { - super(Number.class); - - if (numberFormat == null) { - throw new IllegalArgumentException("Number format may not be null"); - } - - locale = null; - this.numberFormat = numberFormat; - formatString = null; - } - - /** - * Creates a new number renderer. - *

- * The renderer is configured to render with the number's natural string - * representation in the given locale. - * - * @param locale - * the locale in which to display numbers - * @throws IllegalArgumentException - * if {@code locale} is {@code null} - */ - public NumberRenderer(Locale locale) throws IllegalArgumentException { - this("%s", locale); - } - - /** - * Creates a new number renderer. - *

- * The renderer is configured to render with the given format string in the - * default locale. - * - * @param formatString - * the format string with which to format the number - * @throws IllegalArgumentException - * if {@code formatString} is {@code null} - * @see Format - * String Syntax - */ - public NumberRenderer(String formatString) throws IllegalArgumentException { - this(formatString, Locale.getDefault()); - } - - /** - * Creates a new number renderer. - *

- * The renderer is configured to render with the given format string in the - * given locale. - * - * @param formatString - * the format string with which to format the number - * @param locale - * the locale in which to present numbers - * @throws IllegalArgumentException - * if either argument is {@code null} - * @see Format - * String Syntax - */ - public NumberRenderer(String formatString, Locale locale) { - super(Number.class); - - if (formatString == null) { - throw new IllegalArgumentException("Format string may not be null"); - } - - if (locale == null) { - throw new IllegalArgumentException("Locale may not be null"); - } - - this.locale = locale; - numberFormat = null; - this.formatString = formatString; - } - - @Override - public JsonValue encode(Number value) { - String stringValue; - if (formatString != null && locale != null) { - stringValue = String.format(locale, formatString, value); - } else if (numberFormat != null) { - stringValue = numberFormat.format(value); - } else { - throw new IllegalStateException(String.format("Internal bug: " - + "%s is in an illegal state: " - + "[locale: %s, numberFormat: %s, formatString: %s]", - getClass().getSimpleName(), locale, numberFormat, - formatString)); - } - return encode(stringValue, String.class); - } - - @Override - public String toString() { - final String fieldInfo; - if (numberFormat != null) { - fieldInfo = "numberFormat: " + numberFormat.toString(); - } else { - fieldInfo = "locale: " + locale + ", formatString: " + formatString; - } - - return String.format("%s [%s]", getClass().getSimpleName(), fieldInfo); - } -} diff --git a/server/src/com/vaadin/ui/renderer/ProgressBarRenderer.java b/server/src/com/vaadin/ui/renderer/ProgressBarRenderer.java deleted file mode 100644 index 9bdc0b299a..0000000000 --- a/server/src/com/vaadin/ui/renderer/ProgressBarRenderer.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.ui.Grid.AbstractRenderer; - -import elemental.json.JsonValue; - -/** - * A renderer that represents a double values as a graphical progress bar. - * - * @since 7.4 - * @author Vaadin Ltd - */ -public class ProgressBarRenderer extends AbstractRenderer { - - /** - * Creates a new text renderer - */ - public ProgressBarRenderer() { - super(Double.class); - } - - @Override - public JsonValue encode(Double value) { - if (value != null) { - value = Math.max(Math.min(value, 1), 0); - } - return super.encode(value); - } -} diff --git a/server/src/com/vaadin/ui/renderer/Renderer.java b/server/src/com/vaadin/ui/renderer/Renderer.java deleted file mode 100644 index cab1cdfe3c..0000000000 --- a/server/src/com/vaadin/ui/renderer/Renderer.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.ClientConnector; -import com.vaadin.server.Extension; - -import elemental.json.JsonValue; - -/** - * A ClientConnector for controlling client-side - * {@link com.vaadin.client.widget.grid.Renderer Grid renderers}. Renderers - * currently extend the Extension interface, but this fact should be regarded as - * an implementation detail and subject to change in a future major or minor - * Vaadin revision. - * - * @param - * the type this renderer knows how to present - * - * @since 7.4 - * @author Vaadin Ltd - */ -public interface Renderer extends Extension { - - /** - * Returns the class literal corresponding to the presentation type T. - * - * @return the class literal of T - */ - Class getPresentationType(); - - /** - * Encodes the given value into a {@link JsonValue}. - * - * @param value - * the value to encode - * @return a JSON representation of the given value - */ - JsonValue encode(T value); - - /** - * This method is inherited from Extension but should never be called - * directly with a Renderer. - */ - @Override - @Deprecated - void remove(); - - /** - * This method is inherited from Extension but should never be called - * directly with a Renderer. - */ - @Override - @Deprecated - void setParent(ClientConnector parent); -} diff --git a/server/src/com/vaadin/ui/renderer/TextRenderer.java b/server/src/com/vaadin/ui/renderer/TextRenderer.java deleted file mode 100644 index 154a09ccd8..0000000000 --- a/server/src/com/vaadin/ui/renderer/TextRenderer.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.ui.Grid.AbstractRenderer; - -/** - * A renderer for presenting simple plain-text string values. - * - * @since 7.4 - * @author Vaadin Ltd - */ -public class TextRenderer extends AbstractRenderer { - - /** - * Creates a new text renderer - */ - public TextRenderer() { - super(String.class); - } -} diff --git a/server/src/com/vaadin/ui/renderers/AbstractJavaScriptRenderer.java b/server/src/com/vaadin/ui/renderers/AbstractJavaScriptRenderer.java new file mode 100644 index 0000000000..bed34cf6e3 --- /dev/null +++ b/server/src/com/vaadin/ui/renderers/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.renderers; + +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: + *

    + *
  • getRowKey(rowIndex) - Gets a unique identifier for the row + * at the given index. This identifier can be used on the server to retrieve the + * corresponding ItemId using {@link #getItemId(String)}.
  • + *
+ * The connector wrapper also supports these special functions that can be + * implemented by the connector: + *
    + *
  • render(cell, data) - Callback for rendering the given data + * into the given cell. The structure of cell and data are described in separate + * sections below. The renderer is required to implement this function. + * Corresponds to + * {@link com.vaadin.client.renderers.Renderer#render(com.vaadin.client.widget.grid.RendererCellReference, Object)} + * .
  • + *
  • init(cell) - Prepares a cell for rendering. Corresponds to + * {@link com.vaadin.client.renderers.ComplexRenderer#init(com.vaadin.client.widget.grid.RendererCellReference)} + * .
  • + *
  • destory(cell) - Allows the renderer to release resources + * allocate for a cell that will no longer be used. Corresponds to + * {@link com.vaadin.client.renderers.ComplexRenderer#destroy(com.vaadin.client.widget.grid.RendererCellReference)} + * .
  • + *
  • onActivate(cell) - Called when the cell is activated by the + * user e.g. by double clicking on the cell or pressing enter with the cell + * focused. Corresponds to + * {@link com.vaadin.client.renderers.ComplexRenderer#onActivate(com.vaadin.client.widget.grid.CellReference)} + * .
  • + *
  • getConsumedEvents() - Returns a JavaScript array of event + * names that should cause onBrowserEvent to be invoked whenever an event is + * fired for a cell managed by this renderer. Corresponds to + * {@link com.vaadin.client.renderers.ComplexRenderer#getConsumedEvents()}.
  • + *
  • onBrowserEvent(cell, event) - Called by Grid when an event + * of a type returned by getConsumedEvents is fired for a cell managed by this + * renderer. Corresponds to + * {@link com.vaadin.client.renderers.ComplexRenderer#onBrowserEvent(com.vaadin.client.widget.grid.CellReference, com.google.gwt.dom.client.NativeEvent)} + * .
  • + *
+ * + *

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

    + *
  • element - The DOM element corresponding to this cell. + * Readonly.
  • + *
  • rowIndex - The current index of the row of this cell. + * Readonly.
  • + *
  • columnIndex - The current index of the column of this cell. + * Readonly.
  • + *
  • colSpan - The number of columns spanned by this cell. Only + * supported in the object passed to the render function - other + * functions should not use the property. Readable and writable. + *
+ * + * @author Vaadin Ltd + * @since 7.4 + */ +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(); + } +} diff --git a/server/src/com/vaadin/ui/renderers/ButtonRenderer.java b/server/src/com/vaadin/ui/renderers/ButtonRenderer.java new file mode 100644 index 0000000000..0b070d3f69 --- /dev/null +++ b/server/src/com/vaadin/ui/renderers/ButtonRenderer.java @@ -0,0 +1,45 @@ +/* + * 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.renderers; + +/** + * A Renderer that displays a button with a textual caption. The value of the + * corresponding property is used as the caption. Click listeners can be added + * to the renderer, invoked when any of the rendered buttons is clicked. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class ButtonRenderer extends ClickableRenderer { + + /** + * Creates a new button renderer. + */ + public ButtonRenderer() { + super(String.class); + } + + /** + * Creates a new button renderer and adds the given click listener to it. + * + * @param listener + * the click listener to register + */ + public ButtonRenderer(RendererClickListener listener) { + this(); + addClickListener(listener); + } +} diff --git a/server/src/com/vaadin/ui/renderers/ClickableRenderer.java b/server/src/com/vaadin/ui/renderers/ClickableRenderer.java new file mode 100644 index 0000000000..38e9acef9c --- /dev/null +++ b/server/src/com/vaadin/ui/renderers/ClickableRenderer.java @@ -0,0 +1,138 @@ +/* + * 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.renderers; + +import java.lang.reflect.Method; + +import com.vaadin.event.ConnectorEventListener; +import com.vaadin.event.MouseEvents.ClickEvent; +import com.vaadin.shared.MouseEventDetails; +import com.vaadin.shared.ui.grid.renderers.RendererClickRpc; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.AbstractRenderer; +import com.vaadin.ui.Grid.Column; +import com.vaadin.util.ReflectTools; + +/** + * An abstract superclass for Renderers that render clickable items. Click + * listeners can be added to a renderer to be notified when any of the rendered + * items is clicked. + * + * @param + * the type presented by the renderer + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class ClickableRenderer extends AbstractRenderer { + + /** + * An interface for listening to {@link RendererClickEvent renderer click + * events}. + * + * @see {@link ButtonRenderer#addClickListener(RendererClickListener)} + */ + public interface RendererClickListener extends ConnectorEventListener { + + static final Method CLICK_METHOD = ReflectTools.findMethod( + RendererClickListener.class, "click", RendererClickEvent.class); + + /** + * Called when a rendered button is clicked. + * + * @param event + * the event representing the click + */ + void click(RendererClickEvent event); + } + + /** + * An event fired when a button rendered by a ButtonRenderer is clicked. + */ + public static class RendererClickEvent extends ClickEvent { + + private Object itemId; + private Column column; + + protected RendererClickEvent(Grid source, Object itemId, Column column, + MouseEventDetails mouseEventDetails) { + super(source, mouseEventDetails); + this.itemId = itemId; + this.column = column; + } + + /** + * Returns the item ID of the row where the click event originated. + * + * @return the item ID of the clicked row + */ + public Object getItemId() { + return itemId; + } + + /** + * Returns the {@link Column} where the click event originated. + * + * @return the column of the click event + */ + public Column getColumn() { + return column; + } + + /** + * Returns the property ID where the click event originated. + * + * @return the property ID of the clicked cell + */ + public Object getPropertyId() { + return column.getPropertyId(); + } + } + + protected ClickableRenderer(Class presentationType) { + super(presentationType); + registerRpc(new RendererClickRpc() { + @Override + public void click(String rowKey, String columnId, + MouseEventDetails mouseDetails) { + fireEvent(new RendererClickEvent(getParentGrid(), + getItemId(rowKey), getColumn(columnId), mouseDetails)); + } + }); + } + + /** + * Adds a click listener to this button renderer. The listener is invoked + * every time one of the buttons rendered by this renderer is clicked. + * + * @param listener + * the click listener to be added + */ + public void addClickListener(RendererClickListener listener) { + addListener(RendererClickEvent.class, listener, + RendererClickListener.CLICK_METHOD); + } + + /** + * Removes the given click listener from this renderer. + * + * @param listener + * the click listener to be removed + */ + public void removeClickListener(RendererClickListener listener) { + removeListener(RendererClickEvent.class, listener); + } +} diff --git a/server/src/com/vaadin/ui/renderers/DateRenderer.java b/server/src/com/vaadin/ui/renderers/DateRenderer.java new file mode 100644 index 0000000000..9dd4d19e87 --- /dev/null +++ b/server/src/com/vaadin/ui/renderers/DateRenderer.java @@ -0,0 +1,156 @@ +/* + * 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.renderers; + +import java.text.DateFormat; +import java.util.Date; +import java.util.Locale; + +import com.vaadin.ui.Grid.AbstractRenderer; + +import elemental.json.JsonValue; + +/** + * A renderer for presenting date values. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class DateRenderer extends AbstractRenderer { + private final Locale locale; + private final String formatString; + private final DateFormat dateFormat; + + /** + * Creates a new date renderer. + *

+ * The renderer is configured to render with the {@link Date#toString()} + * representation for the default locale. + */ + public DateRenderer() { + this(Locale.getDefault()); + } + + /** + * Creates a new date renderer. + *

+ * The renderer is configured to render with the {@link Date#toString()} + * representation for the given locale. + * + * @param locale + * the locale in which to present dates + * @throws IllegalArgumentException + * if {@code locale} is {@code null} + */ + public DateRenderer(Locale locale) throws IllegalArgumentException { + this("%s", locale); + } + + /** + * Creates a new date renderer. + *

+ * The renderer is configured to render with the given string format, as + * displayed in the default locale. + * + * @param formatString + * the format string with which to format the date + * @throws IllegalArgumentException + * if {@code formatString} is {@code null} + * @see Format + * String Syntax + */ + public DateRenderer(String formatString) throws IllegalArgumentException { + this(formatString, Locale.getDefault()); + } + + /** + * Creates a new date renderer. + *

+ * The renderer is configured to render with the given string format, as + * displayed in the given locale. + * + * @param formatString + * the format string to format the date with + * @param locale + * the locale to use + * @throws IllegalArgumentException + * if either argument is {@code null} + * @see Format + * String Syntax + */ + public DateRenderer(String formatString, Locale locale) + throws IllegalArgumentException { + super(Date.class); + + if (formatString == null) { + throw new IllegalArgumentException("format string may not be null"); + } + + if (locale == null) { + throw new IllegalArgumentException("locale may not be null"); + } + + this.locale = locale; + this.formatString = formatString; + dateFormat = null; + } + + /** + * Creates a new date renderer. + *

+ * The renderer is configured to render with he given date format. + * + * @param dateFormat + * the date format to use when rendering dates + * @throws IllegalArgumentException + * if {@code dateFormat} is {@code null} + */ + public DateRenderer(DateFormat dateFormat) throws IllegalArgumentException { + super(Date.class); + if (dateFormat == null) { + throw new IllegalArgumentException("date format may not be null"); + } + + locale = null; + formatString = null; + this.dateFormat = dateFormat; + } + + @Override + public JsonValue encode(Date value) { + String dateString; + if (dateFormat != null) { + dateString = dateFormat.format(value); + } else { + dateString = String.format(locale, formatString, value); + } + return encode(dateString, String.class); + } + + @Override + public String toString() { + final String fieldInfo; + if (dateFormat != null) { + fieldInfo = "dateFormat: " + dateFormat.toString(); + } else { + fieldInfo = "locale: " + locale + ", formatString: " + formatString; + } + + return String.format("%s [%s]", getClass().getSimpleName(), fieldInfo); + } +} diff --git a/server/src/com/vaadin/ui/renderers/HtmlRenderer.java b/server/src/com/vaadin/ui/renderers/HtmlRenderer.java new file mode 100644 index 0000000000..34774b3825 --- /dev/null +++ b/server/src/com/vaadin/ui/renderers/HtmlRenderer.java @@ -0,0 +1,33 @@ +/* + * 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.renderers; + +import com.vaadin.ui.Grid.AbstractRenderer; + +/** + * A renderer for presenting HTML content. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class HtmlRenderer extends AbstractRenderer { + /** + * Creates a new HTML renderer. + */ + public HtmlRenderer() { + super(String.class); + } +} diff --git a/server/src/com/vaadin/ui/renderers/ImageRenderer.java b/server/src/com/vaadin/ui/renderers/ImageRenderer.java new file mode 100644 index 0000000000..4bb3671033 --- /dev/null +++ b/server/src/com/vaadin/ui/renderers/ImageRenderer.java @@ -0,0 +1,67 @@ +/* + * 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.renderers; + +import com.vaadin.server.ExternalResource; +import com.vaadin.server.Resource; +import com.vaadin.server.ResourceReference; +import com.vaadin.server.ThemeResource; +import com.vaadin.shared.communication.URLReference; + +import elemental.json.JsonValue; + +/** + * A renderer for presenting images. + *

+ * The image for each rendered cell is read from a Resource-typed property in + * the data source. Only {@link ExternalResource}s and {@link ThemeResource}s + * are currently supported. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class ImageRenderer extends ClickableRenderer { + + /** + * Creates a new image renderer. + */ + public ImageRenderer() { + super(Resource.class); + } + + /** + * Creates a new image renderer and adds the given click listener to it. + * + * @param listener + * the click listener to register + */ + public ImageRenderer(RendererClickListener listener) { + this(); + addClickListener(listener); + } + + @Override + public JsonValue encode(Resource resource) { + if (!(resource instanceof ExternalResource || resource instanceof ThemeResource)) { + throw new IllegalArgumentException( + "ImageRenderer only supports ExternalResource and ThemeResource (" + + resource.getClass().getSimpleName() + "given )"); + } + + return encode(ResourceReference.create(resource, this, null), + URLReference.class); + } +} diff --git a/server/src/com/vaadin/ui/renderers/NumberRenderer.java b/server/src/com/vaadin/ui/renderers/NumberRenderer.java new file mode 100644 index 0000000000..5c30e55b17 --- /dev/null +++ b/server/src/com/vaadin/ui/renderers/NumberRenderer.java @@ -0,0 +1,163 @@ +/* + * 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.renderers; + +import java.text.NumberFormat; +import java.util.Locale; + +import com.vaadin.ui.Grid.AbstractRenderer; + +import elemental.json.JsonValue; + +/** + * A renderer for presenting number values. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class NumberRenderer extends AbstractRenderer { + private final Locale locale; + private final NumberFormat numberFormat; + private final String formatString; + + /** + * Creates a new number renderer. + *

+ * The renderer is configured to render with the number's natural string + * representation in the default locale. + */ + public NumberRenderer() { + this(Locale.getDefault()); + } + + /** + * Creates a new number renderer. + *

+ * The renderer is configured to render the number as defined with the given + * number format. + * + * @param numberFormat + * the number format with which to display numbers + * @throws IllegalArgumentException + * if {@code numberFormat} is {@code null} + */ + public NumberRenderer(NumberFormat numberFormat) + throws IllegalArgumentException { + super(Number.class); + + if (numberFormat == null) { + throw new IllegalArgumentException("Number format may not be null"); + } + + locale = null; + this.numberFormat = numberFormat; + formatString = null; + } + + /** + * Creates a new number renderer. + *

+ * The renderer is configured to render with the number's natural string + * representation in the given locale. + * + * @param locale + * the locale in which to display numbers + * @throws IllegalArgumentException + * if {@code locale} is {@code null} + */ + public NumberRenderer(Locale locale) throws IllegalArgumentException { + this("%s", locale); + } + + /** + * Creates a new number renderer. + *

+ * The renderer is configured to render with the given format string in the + * default locale. + * + * @param formatString + * the format string with which to format the number + * @throws IllegalArgumentException + * if {@code formatString} is {@code null} + * @see Format + * String Syntax + */ + public NumberRenderer(String formatString) throws IllegalArgumentException { + this(formatString, Locale.getDefault()); + } + + /** + * Creates a new number renderer. + *

+ * The renderer is configured to render with the given format string in the + * given locale. + * + * @param formatString + * the format string with which to format the number + * @param locale + * the locale in which to present numbers + * @throws IllegalArgumentException + * if either argument is {@code null} + * @see Format + * String Syntax + */ + public NumberRenderer(String formatString, Locale locale) { + super(Number.class); + + if (formatString == null) { + throw new IllegalArgumentException("Format string may not be null"); + } + + if (locale == null) { + throw new IllegalArgumentException("Locale may not be null"); + } + + this.locale = locale; + numberFormat = null; + this.formatString = formatString; + } + + @Override + public JsonValue encode(Number value) { + String stringValue; + if (formatString != null && locale != null) { + stringValue = String.format(locale, formatString, value); + } else if (numberFormat != null) { + stringValue = numberFormat.format(value); + } else { + throw new IllegalStateException(String.format("Internal bug: " + + "%s is in an illegal state: " + + "[locale: %s, numberFormat: %s, formatString: %s]", + getClass().getSimpleName(), locale, numberFormat, + formatString)); + } + return encode(stringValue, String.class); + } + + @Override + public String toString() { + final String fieldInfo; + if (numberFormat != null) { + fieldInfo = "numberFormat: " + numberFormat.toString(); + } else { + fieldInfo = "locale: " + locale + ", formatString: " + formatString; + } + + return String.format("%s [%s]", getClass().getSimpleName(), fieldInfo); + } +} diff --git a/server/src/com/vaadin/ui/renderers/ProgressBarRenderer.java b/server/src/com/vaadin/ui/renderers/ProgressBarRenderer.java new file mode 100644 index 0000000000..a077cd8012 --- /dev/null +++ b/server/src/com/vaadin/ui/renderers/ProgressBarRenderer.java @@ -0,0 +1,44 @@ +/* + * 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.renderers; + +import com.vaadin.ui.Grid.AbstractRenderer; + +import elemental.json.JsonValue; + +/** + * A renderer that represents a double values as a graphical progress bar. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class ProgressBarRenderer extends AbstractRenderer { + + /** + * Creates a new text renderer + */ + public ProgressBarRenderer() { + super(Double.class); + } + + @Override + public JsonValue encode(Double value) { + if (value != null) { + value = Math.max(Math.min(value, 1), 0); + } + return super.encode(value); + } +} diff --git a/server/src/com/vaadin/ui/renderers/Renderer.java b/server/src/com/vaadin/ui/renderers/Renderer.java new file mode 100644 index 0000000000..ac85174618 --- /dev/null +++ b/server/src/com/vaadin/ui/renderers/Renderer.java @@ -0,0 +1,69 @@ +/* + * 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.renderers; + +import com.vaadin.server.ClientConnector; +import com.vaadin.server.Extension; + +import elemental.json.JsonValue; + +/** + * A ClientConnector for controlling client-side + * {@link com.vaadin.client.widget.grid.Renderer Grid renderers}. Renderers + * currently extend the Extension interface, but this fact should be regarded as + * an implementation detail and subject to change in a future major or minor + * Vaadin revision. + * + * @param + * the type this renderer knows how to present + * + * @since 7.4 + * @author Vaadin Ltd + */ +public interface Renderer extends Extension { + + /** + * Returns the class literal corresponding to the presentation type T. + * + * @return the class literal of T + */ + Class getPresentationType(); + + /** + * Encodes the given value into a {@link JsonValue}. + * + * @param value + * the value to encode + * @return a JSON representation of the given value + */ + JsonValue encode(T value); + + /** + * This method is inherited from Extension but should never be called + * directly with a Renderer. + */ + @Override + @Deprecated + void remove(); + + /** + * This method is inherited from Extension but should never be called + * directly with a Renderer. + */ + @Override + @Deprecated + void setParent(ClientConnector parent); +} diff --git a/server/src/com/vaadin/ui/renderers/TextRenderer.java b/server/src/com/vaadin/ui/renderers/TextRenderer.java new file mode 100644 index 0000000000..3723a45f70 --- /dev/null +++ b/server/src/com/vaadin/ui/renderers/TextRenderer.java @@ -0,0 +1,34 @@ +/* + * 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.renderers; + +import com.vaadin.ui.Grid.AbstractRenderer; + +/** + * A renderer for presenting simple plain-text string values. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class TextRenderer extends AbstractRenderer { + + /** + * Creates a new text renderer + */ + public TextRenderer() { + super(String.class); + } +} -- cgit v1.2.3