summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>2017-03-15 14:00:50 +0200
committerHenri Sara <henri.sara@gmail.com>2017-03-15 14:00:50 +0200
commitc766694bec0ec16d7dc7b52f8e48f3c4056e6d1f (patch)
tree9adcbf965914c9ad9c5d84a79cecdbc6117a1234 /server
parent0fe0e727784b311e2b76f3b5a06d3a823d2006f4 (diff)
downloadvaadin-framework-c766694bec0ec16d7dc7b52f8e48f3c4056e6d1f.tar.gz
vaadin-framework-c766694bec0ec16d7dc7b52f8e48f3c4056e6d1f.zip
Allow changing renderers after column creation (#8841)
Closes #8250
Diffstat (limited to 'server')
-rw-r--r--server/src/main/java/com/vaadin/ui/Grid.java45
-rw-r--r--server/src/main/java/com/vaadin/ui/TreeGrid.java36
-rw-r--r--server/src/test/java/com/vaadin/tests/components/treegrid/TreeGridColumnTest.java19
3 files changed, 94 insertions, 6 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java
index cfd0ff2006..19222d8c85 100644
--- a/server/src/main/java/com/vaadin/ui/Grid.java
+++ b/server/src/main/java/com/vaadin/ui/Grid.java
@@ -73,6 +73,7 @@ import com.vaadin.server.SerializableFunction;
import com.vaadin.server.SerializableSupplier;
import com.vaadin.server.Setter;
import com.vaadin.server.VaadinServiceClassLoaderUtil;
+import com.vaadin.shared.Connector;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.Registration;
import com.vaadin.shared.data.DataCommunicatorConstants;
@@ -1814,6 +1815,34 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
}
/**
+ * Sets the Renderer for this Column. Setting the renderer will cause
+ * all currently available row data to be recreated and sent to the
+ * client.
+ *
+ * @param renderer
+ * the new renderer
+ * @return this column
+ */
+ public Column<T, V> setRenderer(Renderer<? super V> renderer) {
+ Objects.requireNonNull(renderer, "Renderer can't be null");
+
+ // Remove old renderer
+ Connector oldRenderer = getState().renderer;
+ if (oldRenderer != null && oldRenderer instanceof Extension) {
+ removeExtension((Extension) oldRenderer);
+ }
+
+ // Set new renderer
+ getState().renderer = renderer;
+ addExtension(renderer);
+
+ // Trigger redraw
+ getParent().getDataCommunicator().reset();
+
+ return this;
+ }
+
+ /**
* Gets the grid that this column belongs to.
*
* @return the grid that this column belongs to, or <code>null</code> if
@@ -2341,11 +2370,25 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
public <V> Column<T, V> addColumn(ValueProvider<T, V> valueProvider,
AbstractRenderer<? super T, ? super V> renderer) {
String generatedIdentifier = getGeneratedIdentifier();
- Column<T, V> column = new Column<>(valueProvider, renderer);
+ Column<T, V> column = createColumn(valueProvider, renderer);
addColumn(generatedIdentifier, column);
return column;
}
+ /**
+ * Creates a column instance from a value provider and a renderer.
+ *
+ * @param valueProvider
+ * the value provider
+ * @param renderer
+ * the renderer
+ * @return a new column instance
+ */
+ protected <V> Column<T, V> createColumn(ValueProvider<T, V> valueProvider,
+ AbstractRenderer<? super T, ? super V> renderer) {
+ return new Column<>(valueProvider, renderer);
+ }
+
private void addColumn(String identifier, Column<T, ?> column) {
if (getColumns().contains(column)) {
return;
diff --git a/server/src/main/java/com/vaadin/ui/TreeGrid.java b/server/src/main/java/com/vaadin/ui/TreeGrid.java
index f438df4e8f..46e6c99b59 100644
--- a/server/src/main/java/com/vaadin/ui/TreeGrid.java
+++ b/server/src/main/java/com/vaadin/ui/TreeGrid.java
@@ -19,21 +19,24 @@ import java.util.Collection;
import java.util.Objects;
import java.util.stream.Stream;
+import com.vaadin.data.ValueProvider;
import com.vaadin.data.provider.DataProvider;
import com.vaadin.data.provider.HierarchicalDataProvider;
import com.vaadin.shared.ui.treegrid.NodeCollapseRpc;
import com.vaadin.shared.ui.treegrid.TreeGridCommunicationConstants;
import com.vaadin.shared.ui.treegrid.TreeGridState;
+import com.vaadin.ui.renderers.AbstractRenderer;
+import com.vaadin.ui.renderers.Renderer;
import elemental.json.Json;
import elemental.json.JsonObject;
/**
* A grid component for displaying hierarchical tabular data.
- *
+ *
* @author Vaadin Ltd
* @since 8.1
- *
+ *
* @param <T>
* the grid bean type
*/
@@ -106,9 +109,12 @@ public class TreeGrid<T> extends Grid<T> {
* <p>
* Setting a hierarchy column by calling this method also sets the column to
* be visible and not hidable.
- *
+ * <p>
+ * <strong>Note:</strong> Changing the Renderer of the hierarchy column is
+ * not supported.
+ *
* @see Column#setId(String)
- *
+ *
* @param id
* id of the column to use for displaying hierarchy
*/
@@ -137,7 +143,7 @@ public class TreeGrid<T> extends Grid<T> {
* expanded, it will be collapsed.
* <p>
* Toggling expansion on a leaf item in the hierarchy will have no effect.
- *
+ *
* @param item
* the item to toggle expansion for
*/
@@ -161,4 +167,24 @@ public class TreeGrid<T> extends Grid<T> {
}
return (HierarchicalDataProvider<T, ?>) dataProvider;
}
+
+ @Override
+ protected <V> Column<T, V> createColumn(ValueProvider<T, V> valueProvider,
+ AbstractRenderer<? super T, ? super V> renderer) {
+ return new Column<T, V>(valueProvider, renderer) {
+
+ @Override
+ public com.vaadin.ui.Grid.Column<T, V> setRenderer(
+ Renderer<? super V> renderer) {
+ // Disallow changing renderer for the hierarchy column
+ if (getInternalIdForColumn(this).equals(
+ TreeGrid.this.getState(false).hierarchyColumnId)) {
+ throw new IllegalStateException(
+ "Changing the renderer of the hierarchy column is not allowed.");
+ }
+
+ return super.setRenderer(renderer);
+ }
+ };
+ }
}
diff --git a/server/src/test/java/com/vaadin/tests/components/treegrid/TreeGridColumnTest.java b/server/src/test/java/com/vaadin/tests/components/treegrid/TreeGridColumnTest.java
new file mode 100644
index 0000000000..a928a48a08
--- /dev/null
+++ b/server/src/test/java/com/vaadin/tests/components/treegrid/TreeGridColumnTest.java
@@ -0,0 +1,19 @@
+package com.vaadin.tests.components.treegrid;
+
+import org.junit.Test;
+
+import com.vaadin.ui.TreeGrid;
+import com.vaadin.ui.renderers.TextRenderer;
+
+public class TreeGridColumnTest {
+
+ private TreeGrid<String> treeGrid = new TreeGrid<>();
+
+ @Test(expected = IllegalStateException.class)
+ public void testChangeRendererOfHierarchyColumn() {
+ treeGrid.addColumn(Object::toString).setId("foo");
+ treeGrid.setHierarchyColumn("foo");
+ // This should not be allowed.
+ treeGrid.getColumn("foo").setRenderer(new TextRenderer());
+ }
+}