diff options
author | Marco Collovati <mcollovati@gmail.com> | 2018-09-07 10:06:03 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2018-09-07 11:06:03 +0300 |
commit | a3769ed12ee1456575ef491d59031c36243495b3 (patch) | |
tree | db3ce97cc2bc426734c4c3561cac20b30cdcd6d8 /client | |
parent | 78c6a7fbfb31101bb1d8e1fa42685d187e1a1e81 (diff) | |
download | vaadin-framework-a3769ed12ee1456575ef491d59031c36243495b3.tar.gz vaadin-framework-a3769ed12ee1456575ef491d59031c36243495b3.zip |
Fix handling of hidden Components in Grid(#10367)
Diffstat (limited to 'client')
-rw-r--r-- | client/src/main/java/com/vaadin/client/connectors/grid/ComponentRendererConnector.java | 63 |
1 files changed, 59 insertions, 4 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/ComponentRendererConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/ComponentRendererConnector.java index cc8647e58f..649cec0b36 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/ComponentRendererConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/ComponentRendererConnector.java @@ -15,12 +15,20 @@ */ package com.vaadin.client.connectors.grid; +import java.util.HashSet; +import java.util.Iterator; +import java.util.logging.Logger; + import com.google.gwt.core.client.GWT; +import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.ui.SimplePanel; +import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.ComponentConnector; import com.vaadin.client.ConnectorMap; import com.vaadin.client.renderers.Renderer; import com.vaadin.client.renderers.WidgetRenderer; +import com.vaadin.client.ui.AbstractComponentConnector; +import com.vaadin.client.ui.AbstractConnector; import com.vaadin.client.widget.grid.RendererCellReference; import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.grid.renderers.ComponentRendererState; @@ -35,7 +43,10 @@ import com.vaadin.ui.renderers.ComponentRenderer; */ @Connect(ComponentRenderer.class) public class ComponentRendererConnector - extends AbstractGridRendererConnector<String> { + extends AbstractGridRendererConnector<String> { + + private HashSet<String> knownConnectors = new HashSet<>(); + private HandlerRegistration handlerRegistration; @Override protected Renderer<String> createRenderer() { @@ -50,13 +61,22 @@ public class ComponentRendererConnector @Override public void render(RendererCellReference cell, String connectorId, - SimplePanel widget) { + SimplePanel widget) { + createConnectorHierarchyChangeHandler(); + Widget connectorWidget = null; if (connectorId != null) { ComponentConnector connector = (ComponentConnector) ConnectorMap - .get(getConnection()).getConnector(connectorId); - widget.setWidget(connector.getWidget()); + .get(getConnection()).getConnector(connectorId); + if (connector != null) { + connectorWidget = connector.getWidget(); + knownConnectors.add(connectorId); + } + } + if (connectorWidget != null) { + widget.setWidget(connectorWidget); } else if (widget.getWidget() != null) { widget.remove(widget.getWidget()); + knownConnectors.remove(connectorId); } } }; @@ -66,4 +86,39 @@ public class ComponentRendererConnector public ComponentRendererState getState() { return (ComponentRendererState) super.getState(); } + + @Override + public void onUnregister() { + unregisterHierarchyHandler(); + super.onUnregister(); + } + + /** + * Adds a listener for grid hierarchy changes to find detached connectors + * previously handled by this renderer in order to detach from DOM their widgets + * before {@link AbstractComponentConnector#onUnregister()} is invoked + * otherwise an error message is logged. + */ + private void createConnectorHierarchyChangeHandler() { + if (handlerRegistration == null) { + handlerRegistration = getGridConnector().addConnectorHierarchyChangeHandler(event -> { + Iterator<String> iterator = knownConnectors.iterator(); + while (iterator.hasNext()) { + ComponentConnector connector = (ComponentConnector) ConnectorMap.get(getConnection()).getConnector(iterator.next()); + if (connector != null && connector.getParent() == null) { + connector.getWidget().removeFromParent(); + iterator.remove(); + } + } + }); + } + } + + private void unregisterHierarchyHandler() { + if (this.handlerRegistration != null) { + this.handlerRegistration.removeHandler(); + this.handlerRegistration = null; + } + } + } |