]> source.dussan.org Git - vaadin-framework.git/commitdiff
Paint Vaadin 6 changes in hierarchy order to retain backwards
authorArtur Signell <artur@vaadin.com>
Mon, 19 Mar 2012 09:42:14 +0000 (11:42 +0200)
committerArtur Signell <artur@vaadin.com>
Wed, 21 Mar 2012 13:27:40 +0000 (15:27 +0200)
compatibility, especially for component containers

src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java

index a3980d4c2a28a6ff25306860f51452a1ff328b4d..13bce26c1293b4b41abd19a3c819ea74d53f1d4f 100644 (file)
@@ -27,6 +27,7 @@ import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -802,20 +803,7 @@ public abstract class AbstractCommunicationManager implements
 
         JsonPaintTarget paintTarget = new JsonPaintTarget(this, outWriter,
                 !repaintAll);
-
-        for (Connector connector : dirtyVisibleConnectors) {
-            if (connector instanceof Paintable) {
-                System.out.println("  * Painting legacy Paintable "
-                        + connector.getClass().getName() + "@"
-                        + Integer.toHexString(connector.hashCode()));
-                Paintable p = (Paintable) connector;
-                paintTarget.startTag("change");
-                final String pid = connector.getConnectorId();
-                paintTarget.addAttribute("pid", pid);
-                p.paint(paintTarget);
-                paintTarget.endTag("change");
-            }
-        }
+        legacyPaint(paintTarget, dirtyVisibleConnectors);
 
         if (analyzeLayouts) {
             // TODO Check if this works
@@ -1113,6 +1101,59 @@ public abstract class AbstractCommunicationManager implements
         }
     }
 
+    private void legacyPaint(PaintTarget paintTarget,
+            ArrayList<ClientConnector> dirtyVisibleConnectors)
+            throws PaintException {
+        List<Paintable> paintables = new ArrayList<Paintable>();
+        for (Connector connector : dirtyVisibleConnectors) {
+            if (connector instanceof Paintable) {
+                paintables.add((Paintable) connector);
+            }
+        }
+        sortByHierarchy(paintables);
+        for (Paintable p : paintables) {
+            System.out.println("  * Painting legacy Paintable "
+                    + p.getClass().getName() + "@"
+                    + Integer.toHexString(p.hashCode()));
+            paintTarget.startTag("change");
+            final String pid = ((Connector) p).getConnectorId();
+            paintTarget.addAttribute("pid", pid);
+            p.paint(paintTarget);
+            paintTarget.endTag("change");
+        }
+
+    }
+
+    private void sortByHierarchy(List<Paintable> paintables) {
+        // Vaadin 6 requires parents to be painted before children as component
+        // containers rely on that their updateFromUIDL method has been called
+        // before children start calling e.g. updateCaption
+        Collections.sort(paintables, new Comparator<Paintable>() {
+            public int compare(Paintable o1, Paintable o2) {
+                Component c1 = (Component) o1;
+                Component c2 = (Component) o2;
+                int depth1 = 0;
+                while (c1.getParent() != null) {
+                    depth1++;
+                    c1 = c1.getParent();
+                }
+                int depth2 = 0;
+                while (c2.getParent() != null) {
+                    depth2++;
+                    c2 = c2.getParent();
+                }
+                if (depth1 < depth2) {
+                    return -1;
+                }
+                if (depth1 > depth2) {
+                    return 1;
+                }
+                return 0;
+            }
+        });
+
+    }
+
     private ClientCache getClientCache(Root root) {
         Integer rootId = Integer.valueOf(root.getRootId());
         ClientCache cache = rootToClientCache.get(rootId);