summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorIlia Motornyi <elmot@vaadin.com>2015-03-17 11:47:53 +0200
committerTeemu Suo-Anttila <teemusa@vaadin.com>2015-05-28 14:59:49 +0000
commit96c08cd52e13e6d2ba5fee0580e9c2c7a5091a0f (patch)
treec78fc9b1b6b0126271279380c0d9f2d1d814c2dd /server
parent550bd1e5e42b97de54ebcd02fda17325d52ef67d (diff)
downloadvaadin-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')
-rw-r--r--server/src/com/vaadin/ui/Grid.java23
-rw-r--r--server/src/com/vaadin/ui/renderers/AbstractJavaScriptRenderer.java6
-rw-r--r--server/src/com/vaadin/ui/renderers/ButtonRenderer.java38
-rw-r--r--server/src/com/vaadin/ui/renderers/ClickableRenderer.java6
-rw-r--r--server/src/com/vaadin/ui/renderers/DateRenderer.java91
-rw-r--r--server/src/com/vaadin/ui/renderers/HtmlRenderer.java21
-rw-r--r--server/src/com/vaadin/ui/renderers/ImageRenderer.java4
-rw-r--r--server/src/com/vaadin/ui/renderers/NumberRenderer.java77
-rw-r--r--server/src/com/vaadin/ui/renderers/ProgressBarRenderer.java8
-rw-r--r--server/src/com/vaadin/ui/renderers/TextRenderer.java17
-rw-r--r--server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java161
11 files changed, 357 insertions, 95 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();
}
}
diff --git a/server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java b/server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java
index 8bcbc6a5a9..8a01464180 100644
--- a/server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java
+++ b/server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java
@@ -15,15 +15,6 @@
*/
package com.vaadin.tests.server.renderer;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-
-import java.util.Locale;
-
-import org.junit.Before;
-import org.junit.Test;
-
import com.vaadin.data.Item;
import com.vaadin.data.RpcDataProviderExtension;
import com.vaadin.data.util.IndexedContainer;
@@ -34,9 +25,21 @@ import com.vaadin.tests.server.component.grid.TestGrid;
import com.vaadin.tests.util.AlwaysLockedVaadinSession;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.Column;
+import com.vaadin.ui.renderers.ButtonRenderer;
+import com.vaadin.ui.renderers.DateRenderer;
+import com.vaadin.ui.renderers.HtmlRenderer;
+import com.vaadin.ui.renderers.NumberRenderer;
import com.vaadin.ui.renderers.TextRenderer;
-
import elemental.json.JsonValue;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Date;
+import java.util.Locale;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
public class RendererTest {
@@ -64,14 +67,14 @@ public class RendererTest {
@Override
public TestBean convertToModel(String value,
- Class<? extends TestBean> targetType, Locale locale)
+ Class<? extends TestBean> targetType, Locale locale)
throws ConversionException {
return null;
}
@Override
public String convertToPresentation(TestBean value,
- Class<? extends String> targetType, Locale locale)
+ Class<? extends String> targetType, Locale locale)
throws ConversionException {
if (value instanceof ExtendedBean) {
return "ExtendedBean(" + value.i + ", "
@@ -94,101 +97,151 @@ public class RendererTest {
private Grid grid;
- private Column foo;
- private Column bar;
- private Column baz;
- private Column bah;
+ private Column intColumn;
+ private Column textColumn;
+ private Column beanColumn;
+ private Column htmlColumn;
+ private Column numberColumn;
+ private Column dateColumn;
+ private Column extendedBeanColumn;
+ private Column buttonColumn;
@Before
+ @SuppressWarnings("unchecked")
public void setUp() {
VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
IndexedContainer c = new IndexedContainer();
- c.addContainerProperty("foo", Integer.class, 0);
- c.addContainerProperty("bar", String.class, "");
- c.addContainerProperty("baz", TestBean.class, null);
- c.addContainerProperty("bah", ExtendedBean.class, null);
+ c.addContainerProperty("int", Integer.class, 0);
+ c.addContainerProperty("text", String.class, "");
+ c.addContainerProperty("html", String.class, "");
+ c.addContainerProperty("number", Number.class, null);
+ c.addContainerProperty("date", Date.class, null);
+ c.addContainerProperty("bean", TestBean.class, null);
+ c.addContainerProperty("button", String.class, null);
+ c.addContainerProperty("extendedBean", ExtendedBean.class, null);
Object id = c.addItem();
Item item = c.getItem(id);
- item.getItemProperty("foo").setValue(123);
- item.getItemProperty("bar").setValue("321");
- item.getItemProperty("baz").setValue(new TestBean());
- item.getItemProperty("bah").setValue(new ExtendedBean());
+ item.getItemProperty("int").setValue(123);
+ item.getItemProperty("text").setValue("321");
+ item.getItemProperty("html").setValue("<b>html</b>");
+ item.getItemProperty("number").setValue(3.14);
+ item.getItemProperty("date").setValue(new Date(123456789));
+ item.getItemProperty("bean").setValue(new TestBean());
+ item.getItemProperty("extendedBean").setValue(new ExtendedBean());
grid = new TestGrid(c);
- foo = grid.getColumn("foo");
- bar = grid.getColumn("bar");
- baz = grid.getColumn("baz");
- bah = grid.getColumn("bah");
+ intColumn = grid.getColumn("int");
+ textColumn = grid.getColumn("text");
+ htmlColumn = grid.getColumn("html");
+ numberColumn = grid.getColumn("number");
+ dateColumn = grid.getColumn("date");
+ beanColumn = grid.getColumn("bean");
+ extendedBeanColumn = grid.getColumn("extendedBean");
+ buttonColumn = grid.getColumn("button");
+
}
@Test
public void testDefaultRendererAndConverter() throws Exception {
- assertSame(TextRenderer.class, foo.getRenderer().getClass());
- assertSame(StringToIntegerConverter.class, foo.getConverter()
+ assertSame(TextRenderer.class, intColumn.getRenderer().getClass());
+ assertSame(StringToIntegerConverter.class, intColumn.getConverter()
.getClass());
- assertSame(TextRenderer.class, bar.getRenderer().getClass());
+ assertSame(TextRenderer.class, textColumn.getRenderer().getClass());
// String->String; converter not needed
- assertNull(bar.getConverter());
+ assertNull(textColumn.getConverter());
- assertSame(TextRenderer.class, baz.getRenderer().getClass());
+ assertSame(TextRenderer.class, beanColumn.getRenderer().getClass());
// MyBean->String; converter not found
- assertNull(baz.getConverter());
+ assertNull(beanColumn.getConverter());
}
@Test
public void testFindCompatibleConverter() throws Exception {
- foo.setRenderer(renderer());
- assertSame(StringToIntegerConverter.class, foo.getConverter()
+ intColumn.setRenderer(renderer());
+ assertSame(StringToIntegerConverter.class, intColumn.getConverter()
.getClass());
- bar.setRenderer(renderer());
- assertNull(bar.getConverter());
+ textColumn.setRenderer(renderer());
+ assertNull(textColumn.getConverter());
}
@Test(expected = IllegalArgumentException.class)
public void testCannotFindConverter() {
- baz.setRenderer(renderer());
+ beanColumn.setRenderer(renderer());
}
@Test
public void testExplicitConverter() throws Exception {
- baz.setRenderer(renderer(), converter());
- bah.setRenderer(renderer(), converter());
+ beanColumn.setRenderer(renderer(), converter());
+ extendedBeanColumn.setRenderer(renderer(), converter());
}
@Test
public void testEncoding() throws Exception {
- assertEquals("42", render(foo, 42).asString());
- foo.setRenderer(renderer());
- assertEquals("renderer(42)", render(foo, 42).asString());
+ assertEquals("42", render(intColumn, 42).asString());
+ intColumn.setRenderer(renderer());
+ assertEquals("renderer(42)", render(intColumn, 42).asString());
- assertEquals("2.72", render(bar, "2.72").asString());
- bar.setRenderer(new TestRenderer());
- assertEquals("renderer(2.72)", render(bar, "2.72").asString());
+ assertEquals("2.72", render(textColumn, "2.72").asString());
+ textColumn.setRenderer(new TestRenderer());
+ assertEquals("renderer(2.72)", render(textColumn, "2.72").asString());
}
@Test
public void testEncodingWithoutConverter() throws Exception {
- assertEquals("TestBean [42]", render(baz, new TestBean()).asString());
+ assertEquals("TestBean [42]", render(beanColumn, new TestBean())
+ .asString());
}
@Test
public void testBeanEncoding() throws Exception {
- baz.setRenderer(renderer(), converter());
- bah.setRenderer(renderer(), converter());
+ beanColumn.setRenderer(renderer(), converter());
+ extendedBeanColumn.setRenderer(renderer(), converter());
- assertEquals("renderer(TestBean(42))", render(baz, new TestBean())
- .asString());
+ assertEquals("renderer(TestBean(42))",
+ render(beanColumn, new TestBean()).asString());
assertEquals("renderer(ExtendedBean(42, 3.14))",
- render(baz, new ExtendedBean()).asString());
+ render(beanColumn, new ExtendedBean()).asString());
assertEquals("renderer(ExtendedBean(42, 3.14))",
- render(bah, new ExtendedBean()).asString());
+ render(extendedBeanColumn, new ExtendedBean()).asString());
+ }
+
+ @Test
+ public void testNullEncoding() {
+
+ textColumn.setRenderer(new TextRenderer());
+ htmlColumn.setRenderer(new HtmlRenderer());
+ numberColumn.setRenderer(new NumberRenderer());
+ dateColumn.setRenderer(new DateRenderer());
+ buttonColumn.setRenderer(new ButtonRenderer());
+
+ assertEquals("", textColumn.getRenderer().encode(null).asString());
+ assertEquals("", htmlColumn.getRenderer().encode(null).asString());
+ assertEquals("", numberColumn.getRenderer().encode(null).asString());
+ assertEquals("", dateColumn.getRenderer().encode(null).asString());
+ assertEquals("", buttonColumn.getRenderer().encode(null).asString());
+ }
+ @Test
+
+ public void testNullEncodingWithDefault() {
+
+ textColumn.setRenderer(new TextRenderer("default value"));
+ htmlColumn.setRenderer(new HtmlRenderer("default value"));
+ numberColumn.setRenderer(new NumberRenderer("%s", Locale.getDefault(), "default value"));
+ dateColumn.setRenderer(new DateRenderer("%s", "default value"));
+ buttonColumn.setRenderer(new ButtonRenderer("default value"));
+
+ assertEquals("default value", textColumn.getRenderer().encode(null).asString());
+ assertEquals("default value", htmlColumn.getRenderer().encode(null).asString());
+ assertEquals("default value", numberColumn.getRenderer().encode(null).asString());
+ assertEquals("default value", dateColumn.getRenderer().encode(null).asString());
+ assertEquals("default value", buttonColumn.getRenderer().encode(null).asString());
}
private TestConverter converter() {