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 {
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;
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];
+ }
}
}
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);
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);
// Adopt.
adopt(caption);
captions.put(component, caption);
+ MeasureManager.MeasuredSize measuredSize = client
+ .getMeasuredSize(component);
+
+ measuredSize.registerDependency(caption.getElement());
}
caption.updateCaption(uidl);
} else {
remove(removedCaption);
MeasureManager.MeasuredSize measuredSize = client
.getMeasuredSize(component);
- measuredSize.setCaptionHeight(0);
- measuredSize.setCaptionWidth(0);
+ measuredSize.deRegisterDependency(removedCaption.getElement());
}
}
}
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) {