Adds an optional textual value for null to Grid.AbstractRenderer Change-Id: I4998476308e4259306f1774f309968520d8672e5tags/7.5.0.beta2^0
@@ -42,9 +42,11 @@ public class ImageRendererConnector extends ClickableRendererConnector<String> { | |||
@Override | |||
public String decode(JsonValue value) { | |||
return ((URLReference) JsonDecoder.decodeValue( | |||
URLReference reference = (URLReference) JsonDecoder.decodeValue( | |||
TypeDataStore.getType(URLReference.class), value, null, | |||
getConnection())).getURL(); | |||
getConnection()); | |||
return reference != null ? reference.getURL() : null; | |||
} | |||
@Override |
@@ -29,6 +29,8 @@ import com.vaadin.client.widget.grid.RendererCellReference; | |||
*/ | |||
public class ImageRenderer extends ClickableRenderer<String, Image> { | |||
public static final String TRANSPARENT_GIF_1PX = "data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs="; | |||
@Override | |||
public Image createWidget() { | |||
Image image = GWT.create(Image.class); | |||
@@ -38,6 +40,11 @@ public class ImageRenderer extends ClickableRenderer<String, Image> { | |||
@Override | |||
public void render(RendererCellReference cell, String url, Image image) { | |||
image.setUrl(url); | |||
if (url == null) { | |||
image.setUrl(TRANSPARENT_GIF_1PX); | |||
} | |||
else { | |||
image.setUrl(url); | |||
} | |||
} | |||
} |
@@ -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; | |||
} | |||
/** |
@@ -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 |
@@ -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(); | |||
} | |||
} |
@@ -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, |
@@ -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"); | |||
} | |||
@@ -131,10 +203,17 @@ public class DateRenderer extends AbstractRenderer<Date> { | |||
this.dateFormat = dateFormat; | |||
} | |||
@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); |
@@ -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(); | |||
} | |||
} |
@@ -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 )"); |
@@ -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(); | |||
} | |||
} |
@@ -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); | |||
} |
@@ -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(); | |||
} | |||
} |
@@ -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() { |
@@ -19,6 +19,6 @@ import com.vaadin.ui.Grid.AbstractRenderer; | |||
public class IntArrayRenderer extends AbstractRenderer<int[]> { | |||
public IntArrayRenderer() { | |||
super(int[].class); | |||
super(int[].class, ""); | |||
} | |||
} |
@@ -28,7 +28,7 @@ import com.vaadin.ui.renderers.AbstractJavaScriptRenderer; | |||
public class MyBeanJSRenderer extends AbstractJavaScriptRenderer<MyBean> { | |||
public MyBeanJSRenderer() { | |||
super(MyBean.class); | |||
super(MyBean.class, ""); | |||
} | |||
} |
@@ -0,0 +1,131 @@ | |||
/* | |||
* Copyright 2000-2014 Vaadin Ltd. | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | |||
* use this file except in compliance with the License. You may obtain a copy of | |||
* the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |||
* License for the specific language governing permissions and limitations under | |||
* the License. | |||
*/ | |||
package com.vaadin.tests.components.grid; | |||
import java.util.Date; | |||
import java.util.Locale; | |||
import com.vaadin.data.util.IndexedContainer; | |||
import com.vaadin.server.Resource; | |||
import com.vaadin.server.VaadinRequest; | |||
import com.vaadin.tests.components.AbstractTestUI; | |||
import com.vaadin.ui.Grid; | |||
import com.vaadin.ui.Grid.SelectionMode; | |||
import com.vaadin.ui.renderers.ButtonRenderer; | |||
import com.vaadin.ui.renderers.ClickableRenderer.RendererClickEvent; | |||
import com.vaadin.ui.renderers.ClickableRenderer.RendererClickListener; | |||
import com.vaadin.ui.renderers.DateRenderer; | |||
import com.vaadin.ui.renderers.HtmlRenderer; | |||
import com.vaadin.ui.renderers.ImageRenderer; | |||
import com.vaadin.ui.renderers.NumberRenderer; | |||
import com.vaadin.ui.renderers.ProgressBarRenderer; | |||
import com.vaadin.ui.renderers.TextRenderer; | |||
@SuppressWarnings("all") | |||
public class NullRenderers extends AbstractTestUI { | |||
@Override | |||
protected void setup(VaadinRequest request) { | |||
IndexedContainer container = new IndexedContainer(); | |||
container.addContainerProperty(TextRenderer.class, String.class, null); | |||
container.addContainerProperty(HtmlRenderer.class, String.class, null); | |||
container.addContainerProperty(DateRenderer.class, Date.class, null); | |||
container | |||
.addContainerProperty(NumberRenderer.class, Number.class, null); | |||
container.addContainerProperty(ProgressBarRenderer.class, Double.class, | |||
null); | |||
container | |||
.addContainerProperty(ButtonRenderer.class, String.class, null); | |||
container.addContainerProperty(ImageRenderer.class, Resource.class, | |||
null); | |||
container.addItem(); | |||
final Grid gridDefaults = new Grid(container); | |||
gridDefaults.setId("test-grid-defaults"); | |||
gridDefaults.setSelectionMode(SelectionMode.NONE); | |||
gridDefaults.setWidth("100%"); | |||
gridDefaults.getColumn(TextRenderer.class).setRenderer(new TextRenderer("-- No Text --")); | |||
gridDefaults.getColumn(HtmlRenderer.class).setRenderer(new HtmlRenderer("-- No Jokes --")); | |||
gridDefaults.getColumn(DateRenderer.class).setRenderer(new DateRenderer("%s", Locale.getDefault(), "-- Never --")); | |||
gridDefaults.getColumn(NumberRenderer.class).setRenderer(new NumberRenderer("%s", Locale.getDefault(),"-- Nothing --")); | |||
gridDefaults.getColumn(ProgressBarRenderer.class).setRenderer( | |||
new ProgressBarRenderer()); | |||
gridDefaults.getColumn(ButtonRenderer.class).setRenderer( | |||
new ButtonRenderer(new RendererClickListener() { | |||
@Override | |||
public void click(RendererClickEvent event) { | |||
} | |||
}, "-- No Control --")); | |||
gridDefaults.getColumn(ImageRenderer.class).setRenderer( | |||
new ImageRenderer(new RendererClickListener() { | |||
@Override | |||
public void click(RendererClickEvent event) { | |||
} | |||
})); | |||
addComponent(gridDefaults); | |||
final Grid gridNoDefaults = new Grid(container); | |||
gridNoDefaults.setId("test-grid"); | |||
gridNoDefaults.setSelectionMode(SelectionMode.NONE); | |||
gridNoDefaults.setWidth("100%"); | |||
gridNoDefaults.getColumn(TextRenderer.class).setRenderer(new TextRenderer()); | |||
gridNoDefaults.getColumn(HtmlRenderer.class).setRenderer(new HtmlRenderer()); | |||
gridNoDefaults.getColumn(DateRenderer.class).setRenderer(new DateRenderer()); | |||
gridNoDefaults.getColumn(NumberRenderer.class).setRenderer(new NumberRenderer()); | |||
gridNoDefaults.getColumn(ProgressBarRenderer.class).setRenderer( | |||
new ProgressBarRenderer()); | |||
gridNoDefaults.getColumn(ButtonRenderer.class).setRenderer( | |||
new ButtonRenderer(new RendererClickListener() { | |||
@Override | |||
public void click(RendererClickEvent event) { | |||
} | |||
})); | |||
gridNoDefaults.getColumn(ImageRenderer.class).setRenderer( | |||
new ImageRenderer(new RendererClickListener() { | |||
@Override | |||
public void click(RendererClickEvent event) { | |||
} | |||
})); | |||
addComponent(gridNoDefaults); | |||
} | |||
@Override | |||
protected String getTestDescription() { | |||
return "Tests the functionality of widget-based renderers"; | |||
} | |||
@Override | |||
protected Integer getTicketNumber() { | |||
return Integer.valueOf(13334); | |||
} | |||
} |
@@ -0,0 +1,65 @@ | |||
/* | |||
* Copyright 2000-2014 Vaadin Ltd. | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | |||
* use this file except in compliance with the License. You may obtain a copy of | |||
* the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |||
* License for the specific language governing permissions and limitations under | |||
* the License. | |||
*/ | |||
package com.vaadin.tests.components.grid; | |||
import com.vaadin.testbench.elements.GridElement; | |||
import com.vaadin.testbench.elements.LabelElement; | |||
import com.vaadin.testbench.parallel.TestCategory; | |||
import com.vaadin.tests.tb3.MultiBrowserTest; | |||
import org.junit.Test; | |||
import java.util.List; | |||
import static org.junit.Assert.assertEquals; | |||
@TestCategory("grid") | |||
public class NullRenderersTest extends MultiBrowserTest { | |||
@Test | |||
public void testDefaults() throws Exception { | |||
openTestURL(); | |||
GridElement grid = findGridWithDefaults(); | |||
assertEquals("-- No Text --", grid.getCell(0, 0).getText()); | |||
assertEquals("-- No Jokes --", grid.getCell(0, 1).getText()); | |||
assertEquals("-- Never --", grid.getCell(0, 2).getText()); | |||
assertEquals("-- Nothing --", grid.getCell(0, 3).getText()); | |||
assertEquals("-- No Control --", grid.getCell(0, 5).getText()); | |||
} | |||
@Test | |||
public void testNoDefaults() throws Exception { | |||
openTestURL(); | |||
GridElement grid = findGridNoDefaults(); | |||
assertEquals("", grid.getCell(0, 0).getText()); | |||
assertEquals("", grid.getCell(0, 1).getText()); | |||
assertEquals("", grid.getCell(0, 2).getText()); | |||
assertEquals("", grid.getCell(0, 3).getText()); | |||
assertEquals("", grid.getCell(0, 5).getText()); | |||
} | |||
private GridElement findGridWithDefaults() { | |||
return $(GridElement.class).id("test-grid-defaults"); | |||
} | |||
private GridElement findGridNoDefaults() { | |||
return $(GridElement.class).id("test-grid"); | |||
} | |||
private LabelElement findDebugLabel() { | |||
return $(LabelElement.class).id(CustomRenderer.DEBUG_LABEL_ID); | |||
} | |||
} |
@@ -21,7 +21,7 @@ import com.vaadin.ui.Label; | |||
public class RowAwareRenderer extends AbstractRenderer<Void> { | |||
public RowAwareRenderer(final Label debugLabel) { | |||
super(Void.class); | |||
super(Void.class, ""); | |||
registerRpc(new RowAwareRendererRpc() { | |||
@Override | |||
public void clicky(String key) { |