]> source.dussan.org Git - vaadin-framework.git/commitdiff
More generic way of measuring captions
authorLeif Åstrand <leif@vaadin.com>
Mon, 6 Feb 2012 14:26:03 +0000 (16:26 +0200)
committerLeif Åstrand <leif@vaadin.com>
Mon, 6 Feb 2012 14:26:03 +0000 (16:26 +0200)
src/com/vaadin/terminal/gwt/client/MeasureManager.java
src/com/vaadin/terminal/gwt/client/ui/VMeasuringOrderedLayout.java

index c2e254b1d051c42f7723496c7aff48ff623dca9b..46d1aeb9fcb386b32d946ae235d5d7159cf0f07e 100644 (file)
@@ -3,13 +3,16 @@ package com.vaadin.terminal.gwt.client;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
 
 import com.google.gwt.core.client.JsArrayString;
+import com.google.gwt.dom.client.Element;
 import com.google.gwt.user.client.ui.HasWidgets;
 import com.google.gwt.user.client.ui.RequiresResize;
 import com.google.gwt.user.client.ui.Widget;
-import com.vaadin.terminal.gwt.client.ui.VMeasuringOrderedLayout;
 
 public class MeasureManager {
 
@@ -17,8 +20,8 @@ public class MeasureManager {
         private int width = -1;
         private int height = -1;
         private boolean isDirty = true;
-        private int captionWidth = 0;
-        private int captionHeight = 0;
+
+        private final Map<Element, int[]> dependencySizes = new HashMap<Element, int[]>();
 
         public int getHeight() {
             return height;
@@ -50,26 +53,32 @@ public class MeasureManager {
             this.isDirty = isDirty;
         }
 
-        public void setCaptionHeight(int captionHeight) {
-            if (captionHeight != this.captionHeight) {
+        public void registerDependency(Element element) {
+            if (!dependencySizes.containsKey(element)) {
+                dependencySizes.put(element, new int[] { -1, -1 });
                 isDirty = true;
             }
-            this.captionHeight = captionHeight;
         }
 
-        public void setCaptionWidth(int captionWidth) {
-            if (captionWidth != this.captionWidth) {
-                isDirty = true;
-            }
-            this.captionWidth = captionWidth;
+        public void deRegisterDependency(Element element) {
+            dependencySizes.remove(element);
+        }
+
+        public int getDependencyWidth(Element e) {
+            return getDependencySize(e, 0);
         }
 
-        public int getCaptionHeight() {
-            return captionHeight;
+        public int getDependencyHeight(Element e) {
+            return getDependencySize(e, 1);
         }
 
-        public int getCaptionWidth() {
-            return captionWidth;
+        private int getDependencySize(Element e, int index) {
+            int[] sizes = dependencySizes.get(e);
+            if (sizes == null) {
+                return -1;
+            } else {
+                return sizes[index];
+            }
         }
     }
 
@@ -228,32 +237,6 @@ public class MeasureManager {
         FastStringSet changed = FastStringSet.create();
         for (VPaintableWidget paintableWidget : paintableWidgets) {
             Widget widget = paintableWidget.getWidgetForPaintable();
-            if (paintableWidget instanceof VMeasuringOrderedLayout) {
-                VMeasuringOrderedLayout hasCaptions = (VMeasuringOrderedLayout) paintableWidget;
-                Collection<VCaption> childCaptions = hasCaptions
-                        .getChildCaptions();
-                for (VCaption vCaption : childCaptions) {
-                    VPaintableWidget captionOwner = vCaption.getOwner();
-                    MeasureManager.MeasuredSize measuredSize = paintableMap
-                            .getMeasuredSize(captionOwner);
-
-                    int captionHeight = vCaption.getOffsetHeight();
-                    int captionWidth = vCaption.getOffsetWidth();
-                    if (captionHeight == 0 || captionWidth == 0) {
-                        // Empty caption is probably detached
-                        if (!Util.isAttachedAndDisplayed(vCaption)) {
-                            // Ignore if it is detached
-                            continue;
-                        }
-                    }
-                    measuredSize.setCaptionHeight(captionHeight);
-                    measuredSize.setCaptionWidth(captionWidth);
-                    if (measuredSize.isDirty()) {
-                        changed.add(paintableMap.getPid(captionOwner));
-                        measuredSize.setDirty(false);
-                    }
-                }
-            }
 
             MeasureManager.MeasuredSize measuredSize = paintableMap
                     .getMeasuredSize(paintableWidget);
@@ -261,6 +244,30 @@ public class MeasureManager {
             measuredSize.setWidth(widget.getOffsetWidth());
             measuredSize.setHeight(widget.getOffsetHeight());
 
+            boolean dirtyDependency = false;
+            for (Entry<Element, int[]> entry : measuredSize.dependencySizes
+                    .entrySet()) {
+                Element element = entry.getKey();
+                int[] sizes = entry.getValue();
+
+                int offsetWidth = element.getOffsetWidth();
+                if (offsetWidth != sizes[0]) {
+                    sizes[0] = offsetWidth;
+                    dirtyDependency = true;
+                }
+
+                int offsetHeight = element.getOffsetHeight();
+                if (offsetHeight != sizes[1]) {
+                    sizes[1] = offsetHeight;
+                    dirtyDependency = true;
+                }
+
+            }
+
+            if (dirtyDependency) {
+                measuredSize.setDirty(true);
+            }
+
             if (measuredSize.isDirty()) {
                 changed.add(paintableMap.getPid(paintableWidget));
                 measuredSize.setDirty(false);
index 5c5f67bf180da19477358155111df15d4acf232a..fa82c5b59f0ee410e652e4e0806b7714b92c37db 100644 (file)
@@ -196,6 +196,10 @@ public class VMeasuringOrderedLayout extends ComplexPanel implements Container,
                 // Adopt.
                 adopt(caption);
                 captions.put(component, caption);
+                MeasureManager.MeasuredSize measuredSize = client
+                        .getMeasuredSize(component);
+
+                measuredSize.registerDependency(caption.getElement());
             }
             caption.updateCaption(uidl);
         } else {
@@ -204,8 +208,7 @@ public class VMeasuringOrderedLayout extends ComplexPanel implements Container,
                 remove(removedCaption);
                 MeasureManager.MeasuredSize measuredSize = client
                         .getMeasuredSize(component);
-                measuredSize.setCaptionHeight(0);
-                measuredSize.setCaptionWidth(0);
+                measuredSize.deRegisterDependency(removedCaption.getElement());
             }
         }
 
@@ -453,22 +456,31 @@ public class VMeasuringOrderedLayout extends ComplexPanel implements Container,
     }
 
     private int getCaptionWidth(VPaintableWidget child) {
-        MeasureManager.MeasuredSize measuredSize = client
-                .getMeasuredSize(child);
-        return measuredSize.getCaptionWidth();
+        VCaption caption = captions.get(child);
+        if (caption == null) {
+            return 0;
+        } else {
+            MeasureManager.MeasuredSize measuredSize = client
+                    .getMeasuredSize(child);
+            return measuredSize.getDependencyWidth(caption.getElement());
+        }
     }
 
     private int getCaptionHeight(VPaintableWidget child) {
-        MeasureManager.MeasuredSize measuredSize = client
-                .getMeasuredSize(child);
-        int captionHeight = measuredSize.getCaptionHeight();
 
         VCaption caption = captions.get(child);
         if (caption != null) {
+            MeasureManager.MeasuredSize measuredSize = client
+                    .getMeasuredSize(child);
+            int captionHeight = measuredSize.getDependencyHeight(caption
+                    .getElement());
+
             caption.getElement().getStyle()
                     .setMarginTop(-captionHeight, Unit.PX);
+            return captionHeight;
+        } else {
+            return 0;
         }
-        return captionHeight;
     }
 
     private AlignmentInfo getAlignment(VPaintableWidget child) {