From: Artur Signell Date: Mon, 19 Mar 2012 14:02:52 +0000 (+0200) Subject: #8438 Event handler registration is now a Set in the shared state X-Git-Tag: 7.0.0.alpha2~278 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8ba0e099ac481384e75cd7f9a198ec361cd83cca;p=vaadin-framework.git #8438 Event handler registration is now a Set in the shared state --- diff --git a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java index 95483eac92..b74200d195 100644 --- a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java +++ b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java @@ -2162,14 +2162,19 @@ public class ApplicationConnection { * before the component is updated so the value is correct if called from * updatedFromUIDL. * + * @param paintable + * The connector to register event listeners for * @param eventIdentifier * The identifier for the event * @return true if at least one listener has been registered on server side * for the event identified by eventIdentifier. + * @deprecated Use {@link ComponentState#hasEventListener(String)} + * instead */ + @Deprecated public boolean hasEventListeners(ComponentConnector paintable, String eventIdentifier) { - return connectorMap.hasEventListeners(paintable, eventIdentifier); + return paintable.hasEventListener(eventIdentifier); } /** diff --git a/src/com/vaadin/terminal/gwt/client/ComponentConnector.java b/src/com/vaadin/terminal/gwt/client/ComponentConnector.java index 206be10700..1e9b3a3a0f 100644 --- a/src/com/vaadin/terminal/gwt/client/ComponentConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ComponentConnector.java @@ -101,4 +101,7 @@ public interface ComponentConnector extends ServerConnector { */ @Deprecated public boolean isReadOnly(); + + public boolean hasEventListener(String eventIdentifier); + } diff --git a/src/com/vaadin/terminal/gwt/client/ComponentDetail.java b/src/com/vaadin/terminal/gwt/client/ComponentDetail.java index eff5a76583..c13775d355 100644 --- a/src/com/vaadin/terminal/gwt/client/ComponentDetail.java +++ b/src/com/vaadin/terminal/gwt/client/ComponentDetail.java @@ -5,7 +5,6 @@ package com.vaadin.terminal.gwt.client; import java.util.HashMap; -import com.google.gwt.core.client.JsArrayString; import com.vaadin.terminal.gwt.client.RenderInformation.Size; class ComponentDetail { @@ -73,38 +72,4 @@ class ComponentDetail { } } - private JsArrayString eventListeners; - - /** - * Stores the event listeners registered on server-side and passed along in - * the UIDL. - * - * @param componentUIDL - * The UIDL for the component - * @since 6.2 - */ - native void registerEventListenersFromUIDL(UIDL uidl) - /*-{ - this.@com.vaadin.terminal.gwt.client.ComponentDetail::eventListeners = uidl[1].eventListeners; - }-*/; - - /** - * Checks if there is a registered server side listener for the event. - * - * @param eventIdentifier - * The identifier for the event - * @return true if at least one listener has been registered on server side - * for the event identified by eventIdentifier. - */ - public boolean hasEventListeners(String eventIdentifier) { - if (eventListeners != null) { - int l = eventListeners.length(); - for (int i = 0; i < l; i++) { - if (eventListeners.get(i).equals(eventIdentifier)) { - return true; - } - } - } - return false; - } } diff --git a/src/com/vaadin/terminal/gwt/client/ComponentState.java b/src/com/vaadin/terminal/gwt/client/ComponentState.java index 407782756f..b631f90d22 100644 --- a/src/com/vaadin/terminal/gwt/client/ComponentState.java +++ b/src/com/vaadin/terminal/gwt/client/ComponentState.java @@ -4,7 +4,9 @@ package com.vaadin.terminal.gwt.client; +import java.util.HashSet; import java.util.List; +import java.util.Set; import com.vaadin.terminal.gwt.client.communication.SharedState; import com.vaadin.terminal.gwt.client.communication.URLReference; @@ -31,6 +33,10 @@ public class ComponentState extends SharedState { private URLReference icon = null; private List styles = null; private String debugId = null; + /** + * A set of event identifiers with registered listeners. + */ + private Set registeredEventListeners = null; /** * Returns the component height as set by the server. @@ -319,4 +325,58 @@ public class ComponentState extends SharedState { this.debugId = debugId; } + /** + * Gets the identifiers for the event listeners that have been registered + * for the component (using an event id) + * + * @return A set of event identifiers or null if no identifiers have been + * registered + */ + public Set getRegisteredEventListeners() { + return registeredEventListeners; + } + + /** + * Sets the identifiers for the event listeners that have been registered + * for the component (using an event id) + * + * @param registeredEventListeners + * The new set of identifiers or null if no identifiers have been + * registered + */ + public void setRegisteredEventListeners(Set registeredEventListeners) { + this.registeredEventListeners = registeredEventListeners; + } + + /** + * Adds an event listener id. + * + * @param eventListenerId + * The event identifier to add + */ + public void addRegisteredEventListener(String eventListenerId) { + if (registeredEventListeners == null) { + registeredEventListeners = new HashSet(); + } + registeredEventListeners.add(eventListenerId); + + } + + /** + * Removes an event listener id. + * + * @param eventListenerId + * The event identifier to remove + */ + public void removeRegisteredEventListener(String eventIdentifier) { + if (registeredEventListeners == null) { + return; + } + registeredEventListeners.remove(eventIdentifier); + if (registeredEventListeners.size() == 0) { + registeredEventListeners = null; + } + + } + } diff --git a/src/com/vaadin/terminal/gwt/client/ConnectorMap.java b/src/com/vaadin/terminal/gwt/client/ConnectorMap.java index 79b16be3a7..4f8b5701cf 100644 --- a/src/com/vaadin/terminal/gwt/client/ConnectorMap.java +++ b/src/com/vaadin/terminal/gwt/client/ConnectorMap.java @@ -216,23 +216,6 @@ public class ConnectorMap { return result.toArray(new ComponentConnector[result.size()]); } - /** - * FIXME: Should not be here - * - * @param pid - * @param uidl - */ - @Deprecated - public void registerEventListenersFromUIDL(String pid, UIDL uidl) { - ComponentDetail cd = idToComponentDetail.get(pid); - if (cd == null) { - throw new IllegalArgumentException("Pid must not be null"); - } - - cd.registerEventListenersFromUIDL(uidl); - - } - /** * FIXME: Should not be here * @@ -299,19 +282,6 @@ public class ConnectorMap { } - /** - * FIXME: Should not be here - * - * @param componentConnector - * @return - */ - @Deprecated - public boolean hasEventListeners(ComponentConnector componentConnector, - String eventIdentifier) { - return getComponentDetail(componentConnector).hasEventListeners( - eventIdentifier); - } - /** * Tests if the widget is the root widget of a {@link ComponentConnector}. * diff --git a/src/com/vaadin/terminal/gwt/client/ui/AbsoluteLayoutConnector.java b/src/com/vaadin/terminal/gwt/client/ui/AbsoluteLayoutConnector.java index 4b7b5f6421..ca0d6bbc73 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/AbsoluteLayoutConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/AbsoluteLayoutConnector.java @@ -48,7 +48,7 @@ public class AbsoluteLayoutConnector extends return; } - clickEventHandler.handleEventHandlerRegistration(client); + clickEventHandler.handleEventHandlerRegistration(); HashSet unrenderedPids = new HashSet( getWidget().pidToComponentWrappper.keySet()); diff --git a/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java b/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java index f1529c0087..02f34ac279 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java @@ -3,6 +3,8 @@ */ package com.vaadin.terminal.gwt.client.ui; +import java.util.Set; + import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.ui.FocusWidget; import com.google.gwt.user.client.ui.Focusable; @@ -124,10 +126,6 @@ public abstract class AbstractComponentConnector extends AbstractConnector } ConnectorMap paintableMap = ConnectorMap.get(getConnection()); - // register the listened events by the server-side to the event-handler - // of the component - paintableMap.registerEventListenersFromUIDL(getConnectorId(), uidl); - // Visibility setVisible(!uidl.getBooleanAttribute("invisible"), uidl); @@ -441,4 +439,18 @@ public abstract class AbstractComponentConnector extends AbstractConnector this.parent = parent; } + /** + * Checks if there is a registered server side listener for the given event + * identifier. + * + * @param eventIdentifier + * The identifier to check for + * @return true if an event listener has been registered with the given + * event identifier on the server side, false otherwise + */ + public boolean hasEventListener(String eventIdentifier) { + Set reg = getState().getRegisteredEventListeners(); + return (reg != null && reg.contains(eventIdentifier)); + } + } diff --git a/src/com/vaadin/terminal/gwt/client/ui/AbstractSplitPanelConnector.java b/src/com/vaadin/terminal/gwt/client/ui/AbstractSplitPanelConnector.java index 3363aa792b..5725f6d4f5 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/AbstractSplitPanelConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/AbstractSplitPanelConnector.java @@ -112,7 +112,7 @@ public abstract class AbstractSplitPanelConnector extends } getWidget().setEnabled(isEnabled()); - clickEventHandler.handleEventHandlerRegistration(client); + clickEventHandler.handleEventHandlerRegistration(); if (getState().hasStyles()) { getWidget().componentStyleNames = getState().getStyles(); } else { diff --git a/src/com/vaadin/terminal/gwt/client/ui/ClickEventHandler.java b/src/com/vaadin/terminal/gwt/client/ui/ClickEventHandler.java index 4029a56e99..30e5c8ce46 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/ClickEventHandler.java +++ b/src/com/vaadin/terminal/gwt/client/ui/ClickEventHandler.java @@ -19,7 +19,6 @@ import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.Element; import com.vaadin.terminal.gwt.client.ApplicationConnection; import com.vaadin.terminal.gwt.client.ComponentConnector; -import com.vaadin.terminal.gwt.client.ConnectorMap; import com.vaadin.terminal.gwt.client.MouseEventDetails; import com.vaadin.terminal.gwt.client.MouseEventDetailsBuilder; @@ -32,7 +31,6 @@ public abstract class ClickEventHandler implements DoubleClickHandler, protected String clickEventIdentifier; protected ComponentConnector paintable; - private ApplicationConnection client; public ClickEventHandler(ComponentConnector paintable, String clickEventIdentifier) { @@ -40,8 +38,7 @@ public abstract class ClickEventHandler implements DoubleClickHandler, this.clickEventIdentifier = clickEventIdentifier; } - public void handleEventHandlerRegistration(ApplicationConnection client) { - this.client = client; + public void handleEventHandlerRegistration() { // Handle registering/unregistering of click handler depending on if // server side listeners have been added or removed. if (hasEventListener()) { @@ -73,25 +70,23 @@ public abstract class ClickEventHandler implements DoubleClickHandler, final H handler, DomEvent.Type type); protected ApplicationConnection getApplicationConnection() { - return client; + return paintable.getConnection(); } public boolean hasEventListener() { - return getApplicationConnection().hasEventListeners(paintable, - clickEventIdentifier); + return paintable.hasEventListener(clickEventIdentifier); } protected void fireClick(NativeEvent event) { - ApplicationConnection client = getApplicationConnection(); - String pid = ConnectorMap.get(getApplicationConnection()) - .getConnectorId(paintable); + String pid = paintable.getConnectorId(); MouseEventDetails mouseDetails = MouseEventDetailsBuilder .buildMouseEventDetails(event, getRelativeToElement()); Map parameters = new HashMap(); parameters.put("mouseDetails", mouseDetails.serialize()); - client.updateVariable(pid, clickEventIdentifier, parameters, true); + paintable.getConnection().updateVariable(pid, clickEventIdentifier, + parameters, true); } diff --git a/src/com/vaadin/terminal/gwt/client/ui/CssLayoutConnector.java b/src/com/vaadin/terminal/gwt/client/ui/CssLayoutConnector.java index 4c244722af..d2a8497331 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/CssLayoutConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/CssLayoutConnector.java @@ -38,7 +38,7 @@ public class CssLayoutConnector extends AbstractComponentContainerConnector { if (!isRealUpdate(uidl)) { return; } - clickEventHandler.handleEventHandlerRegistration(client); + clickEventHandler.handleEventHandlerRegistration(); getWidget().setMarginAndSpacingStyles( new VMarginInfo(uidl.getIntAttribute("margins")), diff --git a/src/com/vaadin/terminal/gwt/client/ui/EmbeddedConnector.java b/src/com/vaadin/terminal/gwt/client/ui/EmbeddedConnector.java index 912f11f0aa..d57f56d9ef 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/EmbeddedConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/EmbeddedConnector.java @@ -40,7 +40,7 @@ public class EmbeddedConnector extends AbstractComponentConnector { boolean clearBrowserElement = true; - clickEventHandler.handleEventHandlerRegistration(client); + clickEventHandler.handleEventHandlerRegistration(); if (uidl.hasAttribute("type")) { getWidget().type = uidl.getStringAttribute("type"); diff --git a/src/com/vaadin/terminal/gwt/client/ui/GridLayoutConnector.java b/src/com/vaadin/terminal/gwt/client/ui/GridLayoutConnector.java index b26df5ddb6..dbe490f017 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/GridLayoutConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/GridLayoutConnector.java @@ -54,7 +54,7 @@ public class GridLayoutConnector extends AbstractComponentContainerConnector if (!isRealUpdate(uidl)) { return; } - clickEventHandler.handleEventHandlerRegistration(client); + clickEventHandler.handleEventHandlerRegistration(); int cols = uidl.getIntAttribute("w"); int rows = uidl.getIntAttribute("h"); diff --git a/src/com/vaadin/terminal/gwt/client/ui/PanelConnector.java b/src/com/vaadin/terminal/gwt/client/ui/PanelConnector.java index f5766d8ab4..87306896d8 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/PanelConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/PanelConnector.java @@ -132,7 +132,7 @@ public class PanelConnector extends AbstractComponentContainerConnector return; } - clickEventHandler.handleEventHandlerRegistration(client); + clickEventHandler.handleEventHandlerRegistration(); getWidget().client = client; getWidget().id = uidl.getId(); diff --git a/src/com/vaadin/terminal/gwt/client/ui/RootConnector.java b/src/com/vaadin/terminal/gwt/client/ui/RootConnector.java index 086ded1743..3aac8d7ac3 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/RootConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/RootConnector.java @@ -37,10 +37,6 @@ public class RootConnector extends AbstractComponentContainerConnector @Override public void updateFromUIDL(final UIDL uidl, ApplicationConnection client) { ConnectorMap paintableMap = ConnectorMap.get(getConnection()); - // register the listened events by the server-side to the event-handler - // of the component - paintableMap.registerEventListenersFromUIDL(getConnectorId(), uidl); - getWidget().rendering = true; getWidget().id = getConnectorId(); boolean firstPaint = getWidget().connection == null; @@ -69,7 +65,7 @@ public class RootConnector extends AbstractComponentContainerConnector } getWidget().setStyleName(styles.trim()); - clickEventHandler.handleEventHandlerRegistration(client); + clickEventHandler.handleEventHandlerRegistration(); if (!getWidget().isEmbedded() && getState().getCaption() != null) { // only change window title if we're in charge of the whole page diff --git a/src/com/vaadin/terminal/gwt/client/ui/TextFieldConnector.java b/src/com/vaadin/terminal/gwt/client/ui/TextFieldConnector.java index e1fb3a56a8..274810d6df 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/TextFieldConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/TextFieldConnector.java @@ -38,8 +38,7 @@ public class TextFieldConnector extends AbstractFieldConnector implements getWidget().immediate = getState().isImmediate(); - getWidget().listenTextChangeEvents = client.hasEventListeners(this, - "ie"); + getWidget().listenTextChangeEvents = hasEventListener("ie"); if (getWidget().listenTextChangeEvents) { getWidget().textChangeEventMode = uidl .getStringAttribute(VTextField.ATTR_TEXTCHANGE_EVENTMODE); diff --git a/src/com/vaadin/terminal/gwt/client/ui/WindowConnector.java b/src/com/vaadin/terminal/gwt/client/ui/WindowConnector.java index 943c2e1967..8d8931590d 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/WindowConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/WindowConnector.java @@ -106,7 +106,7 @@ public class WindowConnector extends AbstractComponentContainerConnector } getWidget().visibilityChangesDisabled = false; - clickEventHandler.handleEventHandlerRegistration(client); + clickEventHandler.handleEventHandlerRegistration(); getWidget().immediate = getState().isImmediate(); diff --git a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java index ac0cc7c46d..e4633a0081 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java @@ -1189,6 +1189,7 @@ public abstract class AbstractCommunicationManager implements } public static Iterable getChildComponents(HasComponents cc) { + // TODO This must be moved to Root/Panel if (cc instanceof Root) { Root root = (Root) cc; List children = new ArrayList(); diff --git a/src/com/vaadin/ui/AbstractComponent.java b/src/com/vaadin/ui/AbstractComponent.java index cc5ec68ecc..500b5afaaf 100644 --- a/src/com/vaadin/ui/AbstractComponent.java +++ b/src/com/vaadin/ui/AbstractComponent.java @@ -12,13 +12,11 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -75,11 +73,6 @@ public abstract class AbstractComponent implements Component, MethodEventSource */ private EventRouter eventRouter = null; - /** - * A set of event identifiers with registered listeners. - */ - private Set eventIdentifiers = null; - /** * The internal error message of the component. */ @@ -763,11 +756,6 @@ public abstract class AbstractComponent implements Component, MethodEventSource // Only paint content of visible components. if (isVisibleInContext()) { - if (eventIdentifiers != null) { - target.addAttribute("eventListeners", - eventIdentifiers.toArray()); - } - paintContent(target); final ErrorMessage error = getErrorMessage(); @@ -1023,14 +1011,11 @@ public abstract class AbstractComponent implements Component, MethodEventSource if (eventRouter == null) { eventRouter = new EventRouter(); } - if (eventIdentifiers == null) { - eventIdentifiers = new HashSet(); - } boolean needRepaint = !eventRouter.hasListeners(eventType); eventRouter.addListener(eventType, target, method); if (needRepaint) { - eventIdentifiers.add(eventIdentifier); + getState().addRegisteredEventListener(eventIdentifier); requestRepaint(); } } @@ -1079,7 +1064,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource if (eventRouter != null) { eventRouter.removeListener(eventType, target); if (!eventRouter.hasListeners(eventType)) { - eventIdentifiers.remove(eventIdentifier); + getState().removeRegisteredEventListener(eventIdentifier); requestRepaint(); } }