From b2745dc14f17b510df7321ab3f6285ed317da3b3 Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Fri, 7 Sep 2018 12:10:16 +0300 Subject: Fix handler creation to happen on init (#11172) Fix handler creation to happen on init --- .../grid/ComponentRendererConnector.java | 46 +++++++++++++--------- 1 file changed, 28 insertions(+), 18 deletions(-) (limited to 'client/src/main') 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 649cec0b36..df8ace1a4e 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 @@ -25,6 +25,7 @@ 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.ServerConnector; import com.vaadin.client.renderers.Renderer; import com.vaadin.client.renderers.WidgetRenderer; import com.vaadin.client.ui.AbstractComponentConnector; @@ -43,11 +44,17 @@ import com.vaadin.ui.renderers.ComponentRenderer; */ @Connect(ComponentRenderer.class) public class ComponentRendererConnector - extends AbstractGridRendererConnector { + extends AbstractGridRendererConnector { private HashSet knownConnectors = new HashSet<>(); private HandlerRegistration handlerRegistration; + @Override + public void setParent(ServerConnector parent) { + super.setParent(parent); + createConnectorHierarchyChangeHandler(); + } + @Override protected Renderer createRenderer() { return new WidgetRenderer() { @@ -61,12 +68,12 @@ public class ComponentRendererConnector @Override public void render(RendererCellReference cell, String connectorId, - SimplePanel widget) { - createConnectorHierarchyChangeHandler(); + SimplePanel widget) { + assert handlerRegistration != null : "HirarchyChangeHandler should not be null when rendering."; Widget connectorWidget = null; if (connectorId != null) { ComponentConnector connector = (ComponentConnector) ConnectorMap - .get(getConnection()).getConnector(connectorId); + .get(getConnection()).getConnector(connectorId); if (connector != null) { connectorWidget = connector.getWidget(); knownConnectors.add(connectorId); @@ -95,23 +102,26 @@ public class ComponentRendererConnector /** * 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. + * 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 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(); + assert handlerRegistration == null : "Trying to re-initialize HierarchyChangeHandler"; + handlerRegistration = getGridConnector() + .addConnectorHierarchyChangeHandler(event -> { + Iterator 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() { -- cgit v1.2.3