aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin/ui/Grid.java
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/main/java/com/vaadin/ui/Grid.java')
-rw-r--r--server/src/main/java/com/vaadin/ui/Grid.java39
1 files changed, 39 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java
index 5d03825c6f..d0b908a956 100644
--- a/server/src/main/java/com/vaadin/ui/Grid.java
+++ b/server/src/main/java/com/vaadin/ui/Grid.java
@@ -115,6 +115,7 @@ import com.vaadin.ui.declarative.DesignContext;
import com.vaadin.ui.declarative.DesignException;
import com.vaadin.ui.declarative.DesignFormatter;
import com.vaadin.ui.renderers.AbstractRenderer;
+import com.vaadin.ui.renderers.ComponentRenderer;
import com.vaadin.ui.renderers.HtmlRenderer;
import com.vaadin.ui.renderers.Renderer;
import com.vaadin.ui.renderers.TextRenderer;
@@ -831,6 +832,7 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
private DescriptionGenerator<T> descriptionGenerator;
private Binding<T, ?> editorBinding;
+ private Map<T, Component> activeComponents = new HashMap<>();
private String userId;
@@ -961,6 +963,11 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
V providerValue = valueProvider.apply(data);
+ // Make Grid track components.
+ if (renderer instanceof ComponentRenderer
+ && providerValue instanceof Component) {
+ addComponent(data, (Component) providerValue);
+ }
JsonValue rendererValue = renderer.encode(providerValue);
obj.put(communicationId, rendererValue);
@@ -981,6 +988,38 @@ public class Grid<T> extends AbstractListing<T> implements HasComponents,
}
}
+ private void addComponent(T data, Component component) {
+ if (activeComponents.containsKey(data)) {
+ if (activeComponents.get(data).equals(component)) {
+ // Reusing old component
+ return;
+ }
+ removeComponent(data);
+ }
+ activeComponents.put(data, component);
+ addComponentToGrid(component);
+ }
+
+ @Override
+ public void destroyData(T item) {
+ removeComponent(item);
+ }
+
+ private void removeComponent(T item) {
+ Component component = activeComponents.remove(item);
+ if (component != null) {
+ removeComponentFromGrid(component);
+ }
+ }
+
+ @Override
+ public void destroyAllData() {
+ // Make a defensive copy of keys, as the map gets cleared when
+ // removing components.
+ new HashSet<>(activeComponents.keySet())
+ .forEach(this::removeComponent);
+ }
+
/**
* Gets a data object with the given key from the given JsonObject. If
* there is no object with the key, this method creates a new