summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksi Hietanen <aleksi@vaadin.com>2016-09-08 11:22:15 +0300
committerVaadin Code Review <review@vaadin.com>2016-09-15 09:55:22 +0000
commitc73e1833b624f931777215d0b8b14b0b1b833b9e (patch)
tree6b9ab3b567b5ace5d49b3aeaa5f1e0b4fe39ece7
parenta6fd8bd06368da27bb97528da4a2cd7f864ea6d2 (diff)
downloadvaadin-framework-c73e1833b624f931777215d0b8b14b0b1b833b9e.tar.gz
vaadin-framework-c73e1833b624f931777215d0b8b14b0b1b833b9e.zip
Add CheckBoxRenderer
Change-Id: I32d31d758ee3c313133c7b122bb3ea8dcfcc1cc3
-rw-r--r--client/src/main/java/com/vaadin/client/connectors/CheckBoxRendererConnector.java45
-rw-r--r--client/src/main/java/com/vaadin/client/renderers/CheckBoxRenderer.java48
-rw-r--r--server/src/main/java/com/vaadin/ui/renderers/CheckBoxRenderer.java46
-rw-r--r--uitest/src/main/java/com/vaadin/tests/components/grid/GridClickableRenderers.java29
-rw-r--r--uitest/src/test/java/com/vaadin/tests/components/grid/GridClickableRenderersTest.java26
5 files changed, 182 insertions, 12 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/CheckBoxRendererConnector.java b/client/src/main/java/com/vaadin/client/connectors/CheckBoxRendererConnector.java
new file mode 100644
index 0000000000..9e052e917c
--- /dev/null
+++ b/client/src/main/java/com/vaadin/client/connectors/CheckBoxRendererConnector.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2000-2016 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.client.connectors;
+
+import com.google.web.bindery.event.shared.HandlerRegistration;
+import com.vaadin.client.renderers.CheckBoxRenderer;
+import com.vaadin.client.renderers.ClickableRenderer.RendererClickHandler;
+import com.vaadin.shared.ui.Connect;
+
+import elemental.json.JsonObject;
+
+/**
+ * A connector for {@link com.vaadin.ui.renderers.CheckBoxRenderer}.
+ *
+ * @since 8.0
+ * @author Vaadin Ltd
+ */
+@Connect(com.vaadin.ui.renderers.CheckBoxRenderer.class)
+public class CheckBoxRendererConnector
+ extends ClickableRendererConnector<Boolean> {
+
+ @Override
+ public CheckBoxRenderer getRenderer() {
+ return (CheckBoxRenderer) super.getRenderer();
+ }
+
+ @Override
+ protected HandlerRegistration addClickHandler(
+ RendererClickHandler<JsonObject> handler) {
+ return getRenderer().addClickHandler(handler);
+ }
+}
diff --git a/client/src/main/java/com/vaadin/client/renderers/CheckBoxRenderer.java b/client/src/main/java/com/vaadin/client/renderers/CheckBoxRenderer.java
new file mode 100644
index 0000000000..12a92f4574
--- /dev/null
+++ b/client/src/main/java/com/vaadin/client/renderers/CheckBoxRenderer.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2000-2016 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.client.renderers;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.user.client.ui.CheckBox;
+import com.vaadin.client.ui.VCheckBox;
+import com.vaadin.client.widget.grid.RendererCellReference;
+
+/**
+ * A Renderer that displays a boolean value as a checkbox.
+ *
+ * <p>
+ *
+ * Click handlers can be added to the renderer, invoked when any of the rendered
+ * checkboxes are clicked.
+ *
+ * @since 8.0
+ * @author Vaadin Ltd
+ */
+public class CheckBoxRenderer extends ClickableRenderer<Boolean, CheckBox> {
+
+ @Override
+ public CheckBox createWidget() {
+ CheckBox cb = GWT.create(VCheckBox.class);
+ cb.addClickHandler(this);
+ return cb;
+ }
+
+ @Override
+ public void render(RendererCellReference cell, Boolean data,
+ CheckBox widget) {
+ widget.setValue(data);
+ }
+}
diff --git a/server/src/main/java/com/vaadin/ui/renderers/CheckBoxRenderer.java b/server/src/main/java/com/vaadin/ui/renderers/CheckBoxRenderer.java
new file mode 100644
index 0000000000..92c52c1c0d
--- /dev/null
+++ b/server/src/main/java/com/vaadin/ui/renderers/CheckBoxRenderer.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2000-2016 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.ui.renderers;
+
+import java.util.function.BiConsumer;
+import java.util.function.Function;
+
+/**
+ * A renderer that displays boolean valued grid columns as checkboxes.
+ *
+ * @since 8.0
+ * @author Vaadin Ltd
+ */
+public class CheckBoxRenderer<T> extends ClickableRenderer<T, Boolean> {
+
+ /**
+ * Creates a new checkbox renderer, given getter and setter functions.
+ * Getters and setters are used to update the object corresponding to a
+ * given row in the grid on the server side.
+ *
+ * @param getter
+ * function for getting the desired boolean value
+ * @param setter
+ * function for setting the boolean value after a change from the
+ * client side
+ */
+ public CheckBoxRenderer(Function<T, Boolean> getter,
+ BiConsumer<T, Boolean> setter) {
+ super(Boolean.class, "");
+ addClickListener(clickEvent -> setter.accept(clickEvent.getItem(),
+ !getter.apply(clickEvent.getItem())));
+ }
+}
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 bb37d1d765..8a6bb5ea7c 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
@@ -6,6 +6,7 @@ import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Label;
import com.vaadin.ui.renderers.ButtonRenderer;
+import com.vaadin.ui.renderers.CheckBoxRenderer;
import com.vaadin.ui.renderers.ImageRenderer;
public class GridClickableRenderers extends AbstractTestUI {
@@ -14,24 +15,38 @@ public class GridClickableRenderers extends AbstractTestUI {
String testText;
String imageUrl;
String buttonText;
+ boolean truthValue;
- TestPOJO(String intValue, String imageUrl, String buttonText) {
+ TestPOJO(String intValue, String imageUrl, String buttonText,
+ boolean truthValue) {
testText = intValue;
this.imageUrl = imageUrl;
this.buttonText = buttonText;
+ this.truthValue = truthValue;
}
}
@Override
protected void setup(VaadinRequest request) {
- Label valueDisplayLabel = new Label();
+ Label valueDisplayLabel = new Label("button click label");
+ Label checkBoxValueLabel = new Label("checkbox click label");
Grid<TestPOJO> grid = new Grid<>();
+
grid.addColumn("images", pojo -> new ExternalResource(pojo.imageUrl),
new ImageRenderer<>());
- grid.addColumn("buttons", pojo -> pojo.buttonText, new ButtonRenderer<>(
- event -> valueDisplayLabel.setValue(event.getItem().testText)));
- grid.setItems(new TestPOJO("first button clicked", "", "button 1 text"),
- new TestPOJO("second button clicked", "", "button 2 text"));
- addComponents(valueDisplayLabel, grid);
+ grid.addColumn("buttons", pojo -> pojo.buttonText,
+ new ButtonRenderer<>(event -> valueDisplayLabel
+ .setValue(event.getItem().testText + " clicked")));
+
+ CheckBoxRenderer<TestPOJO> checkBoxRenderer = new CheckBoxRenderer<>(
+ pojo -> pojo.truthValue,
+ (pojo, newTruthValue) -> pojo.truthValue = newTruthValue);
+ checkBoxRenderer.addClickListener(click -> checkBoxValueLabel.setValue(
+ click.getItem().testText + " " + click.getItem().truthValue));
+ grid.addColumn("checkboxes", pojo -> pojo.truthValue, checkBoxRenderer);
+
+ grid.setItems(new TestPOJO("first row", "", "button 1 text", true),
+ new TestPOJO("second row", "", "button 2 text", 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 9fe2eb0047..a548bff679 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
@@ -26,12 +26,11 @@ import com.vaadin.tests.tb3.MultiBrowserTest;
public class GridClickableRenderersTest extends MultiBrowserTest {
@Test
- public void buttonAndImageRenderersPresent() {
+ public void clickableRenderersPresent() {
openTestURL();
-
Assert.assertTrue(isElementPresent(By.className("v-nativebutton")));
-
Assert.assertTrue(isElementPresent(By.className("gwt-Image")));
+ Assert.assertTrue(isElementPresent(By.className("v-checkbox")));
}
@Test
@@ -44,9 +43,26 @@ public class GridClickableRenderersTest extends MultiBrowserTest {
LabelElement label = $(LabelElement.class).get(1);
firstButton.click();
- Assert.assertEquals("first button clicked", label.getText());
+ Assert.assertEquals("first row clicked", label.getText());
secondButton.click();
- Assert.assertEquals("second button clicked", label.getText());
+ Assert.assertEquals("second row clicked", label.getText());
+ }
+
+ @Test
+ public void checkBoxRendererClick() {
+ openTestURL();
+ WebElement firstCheckBox = findElements(
+ By.cssSelector("input[type='checkbox']")).get(0);
+ WebElement secondCheckBox = findElements(
+ By.cssSelector("input[type='checkbox']")).get(1);
+
+ LabelElement label = $(LabelElement.class).get(2);
+
+ firstCheckBox.click();
+ Assert.assertEquals("first row false", label.getText());
+
+ secondCheckBox.click();
+ Assert.assertEquals("second row true", label.getText());
}
}