diff options
author | Artur <artur@vaadin.com> | 2017-02-22 13:36:45 +0200 |
---|---|---|
committer | Aleksi Hietanen <aleksi@vaadin.com> | 2017-02-22 13:36:45 +0200 |
commit | f3d768252903b8c184cc7d8a77b70ccddc5ef20f (patch) | |
tree | d6c04079289e66e548f780d9eae6cef52f2edbc7 | |
parent | 1718666cd6a0aac7273e12d5bad459dea9fe997c (diff) | |
download | vaadin-framework-f3d768252903b8c184cc7d8a77b70ccddc5ef20f.tar.gz vaadin-framework-f3d768252903b8c184cc7d8a77b70ccddc5ef20f.zip |
Add support for rendering HTML in ButtonRenderer (#8606)
6 files changed, 81 insertions, 13 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/ButtonRendererConnector.java b/client/src/main/java/com/vaadin/client/connectors/ButtonRendererConnector.java index 6fbe428edc..170c32a616 100644 --- a/client/src/main/java/com/vaadin/client/connectors/ButtonRendererConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/ButtonRendererConnector.java @@ -16,6 +16,7 @@ package com.vaadin.client.connectors; import com.google.gwt.event.shared.HandlerRegistration; +import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.renderers.ButtonRenderer; import com.vaadin.client.renderers.ClickableRenderer.RendererClickHandler; import com.vaadin.shared.ui.Connect; @@ -48,4 +49,10 @@ public class ButtonRendererConnector public ButtonRendererState getState() { return (ButtonRendererState) super.getState(); } + + @Override + public void onStateChanged(StateChangeEvent stateChangeEvent) { + super.onStateChanged(stateChangeEvent); + getRenderer().setHtmlContentAllowed(getState().htmlContentAllowed); + } } diff --git a/client/src/main/java/com/vaadin/client/renderers/ButtonRenderer.java b/client/src/main/java/com/vaadin/client/renderers/ButtonRenderer.java index 3c4560ff36..09d388dd93 100644 --- a/client/src/main/java/com/vaadin/client/renderers/ButtonRenderer.java +++ b/client/src/main/java/com/vaadin/client/renderers/ButtonRenderer.java @@ -29,6 +29,8 @@ import com.vaadin.client.widget.grid.RendererCellReference; */ public class ButtonRenderer extends ClickableRenderer<String, Button> { + private boolean htmlContentAllowed = false; + @Override public Button createWidget() { Button b = GWT.create(Button.class); @@ -37,8 +39,20 @@ public class ButtonRenderer extends ClickableRenderer<String, Button> { return b; } + public void setHtmlContentAllowed(boolean htmlContentAllowed) { + this.htmlContentAllowed = htmlContentAllowed; + } + + public boolean isHtmlContentAllowed() { + return htmlContentAllowed; + } + @Override public void render(RendererCellReference cell, String text, Button button) { - button.setText(text); + if (htmlContentAllowed) { + button.setHTML(text); + } else { + button.setText(text); + } } } diff --git a/server/src/main/java/com/vaadin/ui/renderers/ButtonRenderer.java b/server/src/main/java/com/vaadin/ui/renderers/ButtonRenderer.java index b9eec4714b..40269adb26 100644 --- a/server/src/main/java/com/vaadin/ui/renderers/ButtonRenderer.java +++ b/server/src/main/java/com/vaadin/ui/renderers/ButtonRenderer.java @@ -83,4 +83,28 @@ public class ButtonRenderer<T> extends ClickableRenderer<T, String> { return (ButtonRendererState) super.getState(markAsDirty); } + /** + * Sets whether the data should be rendered as HTML (instead of text). + * <p> + * By default everything is rendered as text. + * + * @param htmlContentAllowed + * <code>true</code> to render as HTML, <code>false</code> to + * render as text + */ + public void setHtmlContentAllowed(boolean htmlContentAllowed) { + getState().htmlContentAllowed = htmlContentAllowed; + } + + /** + * Gets whether the data should be rendered as HTML (instead of text). + * <p> + * By default everything is rendered as text. + * + * @return <code>true</code> if the renderer renders a HTML, + * <code>false</code> if the content is rendered as text + */ + public boolean isHtmlContentAllowed() { + return getState(false).htmlContentAllowed; + } } diff --git a/shared/src/main/java/com/vaadin/shared/ui/grid/renderers/ButtonRendererState.java b/shared/src/main/java/com/vaadin/shared/ui/grid/renderers/ButtonRendererState.java index 9774eec90f..3421d67ae4 100644 --- a/shared/src/main/java/com/vaadin/shared/ui/grid/renderers/ButtonRendererState.java +++ b/shared/src/main/java/com/vaadin/shared/ui/grid/renderers/ButtonRendererState.java @@ -21,5 +21,5 @@ package com.vaadin.shared.ui.grid.renderers; * @since 8.0 */ public class ButtonRendererState extends ClickableRendererState { - + public boolean htmlContentAllowed = false; } diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridClickableRenderers.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridClickableRenderers.java index 75ce1738dd..e2fb91b230 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/GridClickableRenderers.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridClickableRenderers.java @@ -14,13 +14,15 @@ public class GridClickableRenderers extends AbstractReindeerTestUI { String testText; String imageUrl; String buttonText; + String buttonHtml; boolean truthValue; TestPOJO(String intValue, String imageUrl, String buttonText, - boolean truthValue) { + String buttonHtml, boolean truthValue) { testText = intValue; this.imageUrl = imageUrl; this.buttonText = buttonText; + this.buttonHtml = buttonHtml; this.truthValue = truthValue; } } @@ -37,6 +39,12 @@ public class GridClickableRenderers extends AbstractReindeerTestUI { new ButtonRenderer<>(event -> valueDisplayLabel .setValue(event.getItem().testText + " clicked"))) .setId("buttons").setCaption("Buttons"); + ButtonRenderer<TestPOJO> htmlButtonRenderer = new ButtonRenderer<>(); + htmlButtonRenderer.setHtmlContentAllowed(true); + htmlButtonRenderer.addClickListener(event -> valueDisplayLabel + .setValue(event.getItem().buttonHtml + " clicked")); + grid.addColumn(pojo -> pojo.buttonHtml, htmlButtonRenderer) + .setId("buttonsHtml").setCaption("Buttons HTML"); ButtonRenderer<TestPOJO> yesNoRenderer = new ButtonRenderer<>(); yesNoRenderer.addClickListener(event -> { @@ -48,8 +56,11 @@ public class GridClickableRenderers extends AbstractReindeerTestUI { grid.addColumn(pojo -> pojo.truthValue ? "Yes" : "No", yesNoRenderer) .setCaption("Truth").setId("truth"); - grid.setItems(new TestPOJO("first row", "", "button 1 text", true), - new TestPOJO("second row", "", "button 2 text", false)); + grid.setItems( + new TestPOJO("first row", "", "button 1 text", + "button 1 <b style='color: red'>html</b>", true), + new TestPOJO("second row", "", "button 2 text", + "button 2 <b style='color: red'>html</b>", false)); addComponents(valueDisplayLabel, checkBoxValueLabel, grid); } } diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridClickableRenderersTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridClickableRenderersTest.java index 12fcaabfae..541851b8a1 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/grid/GridClickableRenderersTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridClickableRenderersTest.java @@ -15,6 +15,8 @@ */ package com.vaadin.tests.components.grid; +import java.util.List; + import org.junit.Assert; import org.junit.Test; import org.openqa.selenium.By; @@ -35,16 +37,26 @@ public class GridClickableRenderersTest extends MultiBrowserTest { @Test public void buttonRendererReturnsCorrectItem() { openTestURL(); - WebElement firstRowButton = findElements(By.className("v-nativebutton")) - .get(0); - WebElement secondRowButton = findElements( - By.className("v-nativebutton")).get(2); + List<WebElement> findElements = findElements( + By.className("v-nativebutton")); + WebElement firstRowTextButton = findElements.get(0); + WebElement firstRowHtmlButton = findElements.get(1); + Assert.assertEquals("button 1 text", firstRowTextButton.getText()); + // If it was rendered as text, getText() would return the markup also + Assert.assertEquals("button 1 html", firstRowHtmlButton.getText()); + + WebElement secondRowTextButton = findElements.get(3); + WebElement secondRowHtmlButton = findElements.get(4); + Assert.assertEquals("button 2 text", secondRowTextButton.getText()); + // If it was rendered as text, getText() would return the markup also + Assert.assertEquals("button 2 html", secondRowHtmlButton.getText()); + LabelElement label = $(LabelElement.class).get(1); - firstRowButton.click(); + firstRowTextButton.click(); Assert.assertEquals("first row clicked", label.getText()); - secondRowButton.click(); + secondRowTextButton.click(); Assert.assertEquals("second row clicked", label.getText()); } @@ -52,9 +64,9 @@ public class GridClickableRenderersTest extends MultiBrowserTest { public void checkBoxRendererClick() { openTestURL(); WebElement firstRowButton = findElements(By.className("v-nativebutton")) - .get(1); + .get(2); WebElement secondRowButton = findElements( - By.className("v-nativebutton")).get(3); + By.className("v-nativebutton")).get(5); LabelElement label = $(LabelElement.class).get(2); firstRowButton.click(); |