]> source.dussan.org Git - vaadin-framework.git/commitdiff
Avoid duplicate maps in ConnectorMap and return native collections
authorLeif Åstrand <leif@vaadin.com>
Tue, 5 Feb 2013 10:04:20 +0000 (12:04 +0200)
committerLeif Åstrand <leif@vaadin.com>
Wed, 6 Feb 2013 12:12:48 +0000 (14:12 +0200)
Fixes #10939 and enables #10937

Change-Id: Icb46db31b02fa13a0612b0c24477b639a0e5684b

client/src/com/vaadin/client/ComponentDetail.java
client/src/com/vaadin/client/ComponentDetailMap.java
client/src/com/vaadin/client/ConnectorMap.java

index 102a133a376c496f54e4b3cd37d8b2eead5c3361..58b72b3d958d6e192f5e480c5d2ecd4438e842b2 100644 (file)
@@ -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;
+    }
+
 }
index 0b6799c47f9b0456f14cdf8c6537024c48311f81..94eba721b14278dfb2247e160757fdd509fc261b 100644 (file)
@@ -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;
+    }-*/;
+
 }
index ada515f2db1519abd8dfbca7b670250d7f5f232a..50df65397f8548d0c6e34d6e33ba43a2feb8d960 100644 (file)
@@ -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;
     }
 
     /**