diff options
author | Ilia Motornyi <elmot@vaadin.com> | 2015-03-17 11:47:53 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2015-05-28 14:59:49 +0000 |
commit | 96c08cd52e13e6d2ba5fee0580e9c2c7a5091a0f (patch) | |
tree | c78fc9b1b6b0126271279380c0d9f2d1d814c2dd /server/src/com | |
parent | 550bd1e5e42b97de54ebcd02fda17325d52ef67d (diff) | |
download | vaadin-framework-96c08cd52e13e6d2ba5fee0580e9c2c7a5091a0f.tar.gz vaadin-framework-96c08cd52e13e6d2ba5fee0580e9c2c7a5091a0f.zip |
Adds a textual null representation to Grid renderers(#16560)7.5.0.beta2
Adds an optional textual value for null to Grid.AbstractRenderer
Change-Id: I4998476308e4259306f1774f309968520d8672e5
Diffstat (limited to 'server/src/com')
10 files changed, 250 insertions, 41 deletions
diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index 2c7e63ddf8..73d3a91176 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -3286,8 +3286,15 @@ public class Grid extends AbstractComponent implements SelectionNotifier, private final Class<T> presentationType; - protected AbstractRenderer(Class<T> presentationType) { + private final String nullRepresentation; + + protected AbstractRenderer(Class<T> presentationType, String nullRepresentation) { this.presentationType = presentationType; + this.nullRepresentation = nullRepresentation; + } + + protected AbstractRenderer(Class<T> presentationType) { + this(presentationType, null); } /** @@ -3317,7 +3324,19 @@ public class Grid extends AbstractComponent implements SelectionNotifier, @Override public JsonValue encode(T value) { - return encode(value, getPresentationType()); + if (value == null) { + return encode(getNullRepresentation(), String.class); + } else { + return encode(value, getPresentationType()); + } + } + + /** + * Null representation for the renderer + * @return a textual representation of {@code null} + */ + protected String getNullRepresentation() { + return nullRepresentation; } /** diff --git a/server/src/com/vaadin/ui/renderers/AbstractJavaScriptRenderer.java b/server/src/com/vaadin/ui/renderers/AbstractJavaScriptRenderer.java index bed34cf6e3..104e07f02f 100644 --- a/server/src/com/vaadin/ui/renderers/AbstractJavaScriptRenderer.java +++ b/server/src/com/vaadin/ui/renderers/AbstractJavaScriptRenderer.java @@ -105,8 +105,12 @@ public abstract class AbstractJavaScriptRenderer<T> extends AbstractRenderer<T> private JavaScriptCallbackHelper callbackHelper = new JavaScriptCallbackHelper( this); + protected AbstractJavaScriptRenderer(Class<T> presentationType, String nullRepresentation) { + super(presentationType, nullRepresentation); + } + protected AbstractJavaScriptRenderer(Class<T> presentationType) { - super(presentationType); + super(presentationType, null); } @Override diff --git a/server/src/com/vaadin/ui/renderers/ButtonRenderer.java b/server/src/com/vaadin/ui/renderers/ButtonRenderer.java index 0b070d3f69..a747e45713 100644 --- a/server/src/com/vaadin/ui/renderers/ButtonRenderer.java +++ b/server/src/com/vaadin/ui/renderers/ButtonRenderer.java @@ -15,6 +15,8 @@ */ package com.vaadin.ui.renderers; +import elemental.json.JsonValue; + /** * 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 @@ -27,9 +29,12 @@ public class ButtonRenderer extends ClickableRenderer<String> { /** * Creates a new button renderer. + * + * @param nullRepresentation + * the textual representation of {@code null} value */ - public ButtonRenderer() { - super(String.class); + public ButtonRenderer(String nullRepresentation) { + super(String.class, nullRepresentation); } /** @@ -37,9 +42,34 @@ public class ButtonRenderer extends ClickableRenderer<String> { * * @param listener * the click listener to register + * @param nullRepresentation + * the textual representation of {@code null} value */ - public ButtonRenderer(RendererClickListener listener) { - this(); + public ButtonRenderer(RendererClickListener listener, String nullRepresentation) { + this(nullRepresentation); addClickListener(listener); } + + /** + * Creates a new button renderer. + */ + public ButtonRenderer() { + this(""); + } + + /** + * 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(listener, ""); + } + + @Override + public String getNullRepresentation() { + return super.getNullRepresentation(); + } + } diff --git a/server/src/com/vaadin/ui/renderers/ClickableRenderer.java b/server/src/com/vaadin/ui/renderers/ClickableRenderer.java index 38e9acef9c..01e939bb67 100644 --- a/server/src/com/vaadin/ui/renderers/ClickableRenderer.java +++ b/server/src/com/vaadin/ui/renderers/ClickableRenderer.java @@ -103,7 +103,11 @@ public class ClickableRenderer<T> extends AbstractRenderer<T> { } protected ClickableRenderer(Class<T> presentationType) { - super(presentationType); + this(presentationType, null); + } + + protected ClickableRenderer(Class<T> presentationType, String nullRepresentation) { + super(presentationType, nullRepresentation); registerRpc(new RendererClickRpc() { @Override public void click(String rowKey, String columnId, diff --git a/server/src/com/vaadin/ui/renderers/DateRenderer.java b/server/src/com/vaadin/ui/renderers/DateRenderer.java index 9dd4d19e87..092b3f405e 100644 --- a/server/src/com/vaadin/ui/renderers/DateRenderer.java +++ b/server/src/com/vaadin/ui/renderers/DateRenderer.java @@ -41,7 +41,7 @@ public class DateRenderer extends AbstractRenderer<Date> { * representation for the default locale. */ public DateRenderer() { - this(Locale.getDefault()); + this(Locale.getDefault(), ""); } /** @@ -56,7 +56,24 @@ public class DateRenderer extends AbstractRenderer<Date> { * if {@code locale} is {@code null} */ public DateRenderer(Locale locale) throws IllegalArgumentException { - this("%s", locale); + this("%s", locale, ""); + } + + /** + * Creates a new date renderer. + * <p> + * 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 + * @param nullRepresentation + * the textual representation of {@code null} value + * @throws IllegalArgumentException + * if {@code locale} is {@code null} + */ + public DateRenderer(Locale locale, String nullRepresentation) throws IllegalArgumentException { + this("%s", locale, nullRepresentation); } /** @@ -74,7 +91,27 @@ public class DateRenderer extends AbstractRenderer<Date> { * String Syntax</a> */ public DateRenderer(String formatString) throws IllegalArgumentException { - this(formatString, Locale.getDefault()); + this(formatString, ""); + } + + /** + * Creates a new date renderer. + * <p> + * 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 + * @param nullRepresentation + * the textual representation of {@code null} value + * @throws IllegalArgumentException + * if {@code formatString} is {@code null} + * @see <a + * href="http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">Format + * String Syntax</a> + */ + public DateRenderer(String formatString, String nullRepresentation) throws IllegalArgumentException { + this(formatString, Locale.getDefault(), nullRepresentation); } /** @@ -95,7 +132,29 @@ public class DateRenderer extends AbstractRenderer<Date> { */ public DateRenderer(String formatString, Locale locale) throws IllegalArgumentException { - super(Date.class); + this(formatString,locale, ""); + } + /** + * Creates a new date renderer. + * <p> + * 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 + * @param nullRepresentation + * the textual representation of {@code null} value + * @throws IllegalArgumentException + * if either argument is {@code null} + * @see <a + * href="http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">Format + * String Syntax</a> + */ + public DateRenderer(String formatString, Locale locale, String nullRepresentation) + throws IllegalArgumentException { + super(Date.class, nullRepresentation); if (formatString == null) { throw new IllegalArgumentException("format string may not be null"); @@ -121,7 +180,20 @@ public class DateRenderer extends AbstractRenderer<Date> { * if {@code dateFormat} is {@code null} */ public DateRenderer(DateFormat dateFormat) throws IllegalArgumentException { - super(Date.class); + this(dateFormat, ""); + } + /** + * Creates a new date renderer. + * <p> + * 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, String nullRepresentation) throws IllegalArgumentException { + super(Date.class, nullRepresentation); if (dateFormat == null) { throw new IllegalArgumentException("date format may not be null"); } @@ -132,9 +204,16 @@ public class DateRenderer extends AbstractRenderer<Date> { } @Override + public String getNullRepresentation() { + return super.getNullRepresentation(); + } + + @Override public JsonValue encode(Date value) { String dateString; - if (dateFormat != null) { + if (value == null) { + dateString = getNullRepresentation(); + } else if (dateFormat != null) { dateString = dateFormat.format(value); } else { dateString = String.format(locale, formatString, value); diff --git a/server/src/com/vaadin/ui/renderers/HtmlRenderer.java b/server/src/com/vaadin/ui/renderers/HtmlRenderer.java index 34774b3825..df89c60ad2 100644 --- a/server/src/com/vaadin/ui/renderers/HtmlRenderer.java +++ b/server/src/com/vaadin/ui/renderers/HtmlRenderer.java @@ -16,18 +16,33 @@ package com.vaadin.ui.renderers; import com.vaadin.ui.Grid.AbstractRenderer; +import elemental.json.JsonValue; /** * A renderer for presenting HTML content. - * - * @since 7.4 + * * @author Vaadin Ltd + * @since 7.4 */ public class HtmlRenderer extends AbstractRenderer<String> { /** * Creates a new HTML renderer. + * + * @param nullRepresentation the html representation of {@code null} value + */ + public HtmlRenderer(String nullRepresentation) { + super(String.class, nullRepresentation); + } + + /** + * Creates a new HTML renderer. */ public HtmlRenderer() { - super(String.class); + this(""); + } + + @Override + public String getNullRepresentation() { + return super.getNullRepresentation(); } } diff --git a/server/src/com/vaadin/ui/renderers/ImageRenderer.java b/server/src/com/vaadin/ui/renderers/ImageRenderer.java index 4bb3671033..2fb872583e 100644 --- a/server/src/com/vaadin/ui/renderers/ImageRenderer.java +++ b/server/src/com/vaadin/ui/renderers/ImageRenderer.java @@ -39,7 +39,7 @@ public class ImageRenderer extends ClickableRenderer<Resource> { * Creates a new image renderer. */ public ImageRenderer() { - super(Resource.class); + super(Resource.class, null); } /** @@ -55,7 +55,7 @@ public class ImageRenderer extends ClickableRenderer<Resource> { @Override public JsonValue encode(Resource resource) { - if (!(resource instanceof ExternalResource || resource instanceof ThemeResource)) { + if (!(resource == null || resource instanceof ExternalResource || resource instanceof ThemeResource)) { throw new IllegalArgumentException( "ImageRenderer only supports ExternalResource and ThemeResource (" + resource.getClass().getSimpleName() + "given )"); diff --git a/server/src/com/vaadin/ui/renderers/NumberRenderer.java b/server/src/com/vaadin/ui/renderers/NumberRenderer.java index 5c30e55b17..1d4d7e0ec9 100644 --- a/server/src/com/vaadin/ui/renderers/NumberRenderer.java +++ b/server/src/com/vaadin/ui/renderers/NumberRenderer.java @@ -24,7 +24,7 @@ import elemental.json.JsonValue; /** * A renderer for presenting number values. - * + * * @since 7.4 * @author Vaadin Ltd */ @@ -35,7 +35,7 @@ public class NumberRenderer extends AbstractRenderer<Number> { /** * Creates a new number renderer. - * <p> + * <p/> * The renderer is configured to render with the number's natural string * representation in the default locale. */ @@ -45,18 +45,35 @@ public class NumberRenderer extends AbstractRenderer<Number> { /** * Creates a new number renderer. - * <p> + * <p/> * 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) + public NumberRenderer(NumberFormat numberFormat) { + this(numberFormat, ""); + } + + /** + * Creates a new number renderer. + * <p/> + * 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 + * @param nullRepresentation + * the textual representation of {@code null} value + * @throws IllegalArgumentException + * if {@code numberFormat} is {@code null} + */ + public NumberRenderer(NumberFormat numberFormat, String nullRepresentation) throws IllegalArgumentException { - super(Number.class); + super(Number.class, nullRepresentation); if (numberFormat == null) { throw new IllegalArgumentException("Number format may not be null"); @@ -69,10 +86,10 @@ public class NumberRenderer extends AbstractRenderer<Number> { /** * Creates a new number renderer. - * <p> + * <p/> * 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 @@ -84,12 +101,29 @@ public class NumberRenderer extends AbstractRenderer<Number> { /** * Creates a new number renderer. - * <p> - * The renderer is configured to render with the given format string in the - * default locale. - * + * <p/> + * The renderer is configured to render with the number's natural string + * representation in the given locale. + * * @param formatString * the format string with which to format the number + * @param locale + * the locale in which to display numbers + * @throws IllegalArgumentException + * if {@code locale} is {@code null} + */ + public NumberRenderer(String formatString, Locale locale) throws IllegalArgumentException { + this(formatString, locale, ""); //This will call #toString() during formatting + } + + /** + * Creates a new number renderer. + * <p/> + * 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 <a @@ -97,15 +131,15 @@ public class NumberRenderer extends AbstractRenderer<Number> { * String Syntax</a> */ public NumberRenderer(String formatString) throws IllegalArgumentException { - this(formatString, Locale.getDefault()); + this(formatString, Locale.getDefault(), ""); } /** * Creates a new number renderer. - * <p> + * <p/> * 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 @@ -116,8 +150,8 @@ public class NumberRenderer extends AbstractRenderer<Number> { * href="http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax">Format * String Syntax</a> */ - public NumberRenderer(String formatString, Locale locale) { - super(Number.class); + public NumberRenderer(String formatString, Locale locale, String nullRepresentation) { + super(Number.class, nullRepresentation); if (formatString == null) { throw new IllegalArgumentException("Format string may not be null"); @@ -135,7 +169,9 @@ public class NumberRenderer extends AbstractRenderer<Number> { @Override public JsonValue encode(Number value) { String stringValue; - if (formatString != null && locale != null) { + if (value == null) { + stringValue = getNullRepresentation(); + } else if (formatString != null && locale != null) { stringValue = String.format(locale, formatString, value); } else if (numberFormat != null) { stringValue = numberFormat.format(value); @@ -160,4 +196,9 @@ public class NumberRenderer extends AbstractRenderer<Number> { return String.format("%s [%s]", getClass().getSimpleName(), fieldInfo); } + + @Override + public String getNullRepresentation() { + return super.getNullRepresentation(); + } } diff --git a/server/src/com/vaadin/ui/renderers/ProgressBarRenderer.java b/server/src/com/vaadin/ui/renderers/ProgressBarRenderer.java index a077cd8012..1566c47222 100644 --- a/server/src/com/vaadin/ui/renderers/ProgressBarRenderer.java +++ b/server/src/com/vaadin/ui/renderers/ProgressBarRenderer.java @@ -21,9 +21,9 @@ import elemental.json.JsonValue; /** * A renderer that represents a double values as a graphical progress bar. - * - * @since 7.4 + * * @author Vaadin Ltd + * @since 7.4 */ public class ProgressBarRenderer extends AbstractRenderer<Double> { @@ -31,13 +31,15 @@ public class ProgressBarRenderer extends AbstractRenderer<Double> { * Creates a new text renderer */ public ProgressBarRenderer() { - super(Double.class); + super(Double.class, null); } @Override public JsonValue encode(Double value) { if (value != null) { value = Math.max(Math.min(value, 1), 0); + } else { + value = 0d; } return super.encode(value); } diff --git a/server/src/com/vaadin/ui/renderers/TextRenderer.java b/server/src/com/vaadin/ui/renderers/TextRenderer.java index 3723a45f70..0045024b2f 100644 --- a/server/src/com/vaadin/ui/renderers/TextRenderer.java +++ b/server/src/com/vaadin/ui/renderers/TextRenderer.java @@ -16,6 +16,7 @@ package com.vaadin.ui.renderers; import com.vaadin.ui.Grid.AbstractRenderer; +import elemental.json.JsonValue; /** * A renderer for presenting simple plain-text string values. @@ -29,6 +30,20 @@ public class TextRenderer extends AbstractRenderer<String> { * Creates a new text renderer */ public TextRenderer() { - super(String.class); + this(""); + } + + /** + * Creates a new text renderer + * @param nullRepresentation + * the textual representation of {@code null} value + */ + public TextRenderer(String nullRepresentation) { + super(String.class, nullRepresentation); + } + + @Override + public String getNullRepresentation() { + return super.getNullRepresentation(); } } |