diff options
author | Leif Åstrand <leif@vaadin.com> | 2013-02-05 12:04:20 +0200 |
---|---|---|
committer | Leif Åstrand <leif@vaadin.com> | 2013-02-06 14:12:48 +0200 |
commit | 43ebbf7069f5fdc5f69aaec68a580d4f90330493 (patch) | |
tree | e8c020b526eb8cf9495e903cd0d1457b66906a17 | |
parent | 2d9e28e19c27554ba84ba935271bbbfe101459d5 (diff) | |
download | vaadin-framework-43ebbf7069f5fdc5f69aaec68a580d4f90330493.tar.gz vaadin-framework-43ebbf7069f5fdc5f69aaec68a580d4f90330493.zip |
Avoid duplicate maps in ConnectorMap and return native collections
Fixes #10939 and enables #10937
Change-Id: Icb46db31b02fa13a0612b0c24477b639a0e5684b
-rw-r--r-- | client/src/com/vaadin/client/ComponentDetail.java | 10 | ||||
-rw-r--r-- | client/src/com/vaadin/client/ComponentDetailMap.java | 11 | ||||
-rw-r--r-- | client/src/com/vaadin/client/ConnectorMap.java | 80 |
3 files changed, 86 insertions, 15 deletions
diff --git a/client/src/com/vaadin/client/ComponentDetail.java b/client/src/com/vaadin/client/ComponentDetail.java index 102a133a37..58b72b3d95 100644 --- a/client/src/com/vaadin/client/ComponentDetail.java +++ b/client/src/com/vaadin/client/ComponentDetail.java @@ -21,6 +21,8 @@ class ComponentDetail { private TooltipInfo tooltipInfo = new TooltipInfo(); + private ServerConnector connector; + public ComponentDetail() { } @@ -65,4 +67,12 @@ class ComponentDetail { } } + public ServerConnector getConnector() { + return connector; + } + + public void setConnector(ServerConnector connector) { + this.connector = connector; + } + } diff --git a/client/src/com/vaadin/client/ComponentDetailMap.java b/client/src/com/vaadin/client/ComponentDetailMap.java index 0b6799c47f..94eba721b1 100644 --- a/client/src/com/vaadin/client/ComponentDetailMap.java +++ b/client/src/com/vaadin/client/ComponentDetailMap.java @@ -85,4 +85,15 @@ final class ComponentDetailMap extends JavaScriptObject { return list; } + public native JsArrayObject<ComponentDetail> valuesAsJsArray() + /*-{ + var result = []; + for(var key in this) { + if (this.hasOwnProperty(key)) { + result.push(this[key]); + } + } + return result; + }-*/; + } diff --git a/client/src/com/vaadin/client/ConnectorMap.java b/client/src/com/vaadin/client/ConnectorMap.java index ada515f2db..50df65397f 100644 --- a/client/src/com/vaadin/client/ConnectorMap.java +++ b/client/src/com/vaadin/client/ConnectorMap.java @@ -17,18 +17,14 @@ package com.vaadin.client; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; import com.google.gwt.core.client.GWT; +import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.dom.client.Element; import com.google.gwt.user.client.ui.Widget; public class ConnectorMap { - private Map<String, ServerConnector> idToConnector = new HashMap<String, ServerConnector>(); - public static ConnectorMap get(ApplicationConnection applicationConnection) { return applicationConnection.getConnectorMap(); } @@ -46,7 +42,12 @@ public class ConnectorMap { * registered */ public ServerConnector getConnector(String connectorId) { - return idToConnector.get(connectorId); + ComponentDetail componentDetail = idToComponentDetail.get(connectorId); + if (componentDetail == null) { + return null; + } else { + return componentDetail.getConnector(); + } } /** @@ -96,14 +97,13 @@ public class ConnectorMap { * otherwise */ public boolean hasConnector(String connectorId) { - return idToConnector.containsKey(connectorId); + return idToComponentDetail.containsKey(connectorId); } /** * Removes all registered connectors */ public void clear() { - idToConnector.clear(); idToComponentDetail.clear(); } @@ -122,14 +122,14 @@ public class ConnectorMap { public void registerConnector(String id, ServerConnector connector) { ComponentDetail componentDetail = GWT.create(ComponentDetail.class); idToComponentDetail.put(id, componentDetail); - idToConnector.put(id, connector); + componentDetail.setConnector(connector); if (connector instanceof ComponentConnector) { ComponentConnector pw = (ComponentConnector) connector; setConnectorId(pw.getWidget().getElement(), id); } } - private native void setConnectorId(Element el, String id) + private static native void setConnectorId(Element el, String id) /*-{ el.tkPid = id; }-*/; @@ -144,7 +144,7 @@ public class ConnectorMap { * element of the connector whose id is desired * @return the id of the element's connector, if it's a connector */ - native String getConnectorId(Element el) + native static final String getConnectorId(Element el) /*-{ return el.tkPid; }-*/; @@ -184,7 +184,6 @@ public class ConnectorMap { String connectorId = connector.getConnectorId(); idToComponentDetail.remove(connectorId); - idToConnector.remove(connectorId); connector.onUnregister(); for (ServerConnector child : connector.getChildren()) { @@ -205,11 +204,19 @@ public class ConnectorMap { * Gets all registered {@link ComponentConnector} instances * * @return An array of all registered {@link ComponentConnector} instances + * + * @deprecated As of 7.0.1, use {@link #getComponentConnectorsAsJsArray()} + * for better performance. */ + @Deprecated public ComponentConnector[] getComponentConnectors() { ArrayList<ComponentConnector> result = new ArrayList<ComponentConnector>(); - for (ServerConnector connector : getConnectors()) { + JsArrayObject<ServerConnector> connectors = getConnectorsAsJsArray(); + int size = connectors.size(); + + for (int i = 0; i < size; i++) { + ServerConnector connector = connectors.get(i); if (connector instanceof ComponentConnector) { result.add((ComponentConnector) connector); } @@ -218,6 +225,22 @@ public class ConnectorMap { return result.toArray(new ComponentConnector[result.size()]); } + public JsArrayObject<ComponentConnector> getComponentConnectorsAsJsArray() { + JsArrayObject<ComponentConnector> result = JavaScriptObject + .createArray().cast(); + + JsArrayObject<ServerConnector> connectors = getConnectorsAsJsArray(); + int size = connectors.size(); + for (int i = 0; i < size; i++) { + ServerConnector connector = connectors.get(i); + if (connector instanceof ComponentConnector) { + result.add((ComponentConnector) connector); + } + } + + return result; + } + @Deprecated private ComponentDetail getComponentDetail( ComponentConnector componentConnector) { @@ -225,11 +248,38 @@ public class ConnectorMap { } public int size() { - return idToConnector.size(); + return idToComponentDetail.size(); } + /** + * @return + * + * @deprecated As of 7.0.1, use {@link #getConnectorsAsJsArray()} for + * improved performance. + */ + @Deprecated public Collection<? extends ServerConnector> getConnectors() { - return Collections.unmodifiableCollection(idToConnector.values()); + Collection<ComponentDetail> values = idToComponentDetail.values(); + ArrayList<ServerConnector> arrayList = new ArrayList<ServerConnector>( + values.size()); + for (ComponentDetail componentDetail : values) { + arrayList.add(componentDetail.getConnector()); + } + return arrayList; + } + + public JsArrayObject<ServerConnector> getConnectorsAsJsArray() { + JsArrayObject<ComponentDetail> componentDetails = idToComponentDetail + .valuesAsJsArray(); + JsArrayObject<ServerConnector> connectors = JavaScriptObject + .createArray().cast(); + + int size = componentDetails.size(); + for (int i = 0; i < size; i++) { + connectors.add(componentDetails.get(i).getConnector()); + } + + return connectors; } /** |