diff options
author | Johannes Dahlström <johannesd@vaadin.com> | 2012-11-05 13:10:37 +0000 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2012-11-05 13:10:37 +0000 |
commit | 39d845b34f91d626f9d18d0ff7e601f97fc51b54 (patch) | |
tree | e42df3cbedc679f836eb33b54658b658227503b7 | |
parent | 207c76bbeebeaec71f43d1d19fba36e93e6b3351 (diff) | |
parent | fb38f5ac03b5818344cea984a74f6e5d579eabe4 (diff) | |
download | vaadin-framework-39d845b34f91d626f9d18d0ff7e601f97fc51b54.tar.gz vaadin-framework-39d845b34f91d626f9d18d0ff7e601f97fc51b54.zip |
Merge "Slots now handle attaching/detaching of element resize handlers #10068"
-rw-r--r-- | client/src/com/vaadin/client/ui/orderedlayout/AbstractOrderedLayoutConnector.java | 75 | ||||
-rw-r--r-- | client/src/com/vaadin/client/ui/orderedlayout/VOrderedLayout.java | 157 |
2 files changed, 162 insertions, 70 deletions
diff --git a/client/src/com/vaadin/client/ui/orderedlayout/AbstractOrderedLayoutConnector.java b/client/src/com/vaadin/client/ui/orderedlayout/AbstractOrderedLayoutConnector.java index 20d7beee51..8fb9f8c0e8 100644 --- a/client/src/com/vaadin/client/ui/orderedlayout/AbstractOrderedLayoutConnector.java +++ b/client/src/com/vaadin/client/ui/orderedlayout/AbstractOrderedLayoutConnector.java @@ -124,7 +124,10 @@ public abstract class AbstractOrderedLayoutConnector extends if (captionElement == widgetElement) { // Caption element already detached - removeResizeListener(captionElement, slotCaptionResizeListener); + Slot slot = getWidget().getSlot(widgetElement); + if (slot != null) { + slot.setCaptionResizeListener(null); + } childCaptionElementHeight.remove(widgetElement); return; } @@ -319,18 +322,12 @@ public abstract class AbstractOrderedLayoutConnector extends needsMeasure.remove(child.getWidget().getElement()); childCaptionElementHeight .remove(child.getWidget().getElement()); - getLayoutManager().removeElementResizeListener( - child.getWidget().getElement(), - childComponentResizeListener); + slot.setWidgetResizeListener(null); if (slot.hasCaption()) { - getLayoutManager() - .removeElementResizeListener( - slot.getCaptionElement(), - slotCaptionResizeListener); + slot.setCaptionResizeListener(null); } if (slot.getSpacingElement() != null) { - getLayoutManager().removeElementResizeListener( - slot.getSpacingElement(), spacingResizeListener); + slot.setSpacingResizeListener(null); } child.removeStateChangeHandler(childStateChangeHandler); layout.removeWidget(child.getWidget()); @@ -468,40 +465,32 @@ public abstract class AbstractOrderedLayoutConnector extends Slot slot = getWidget().getSlot(child.getWidget()); // Clear all possible listeners first - removeResizeListener(slot.getWidget().getElement(), - childComponentResizeListener); + slot.setWidgetResizeListener(null); if (slot.hasCaption()) { - removeResizeListener(slot.getCaptionElement(), - slotCaptionResizeListener); + slot.setCaptionResizeListener(null); } if (slot.hasSpacing()) { - removeResizeListener(slot.getSpacingElement(), - spacingResizeListener); + slot.setSpacingResizeListener(null); } // Add all necessary listeners if (needsFixedHeight()) { - addResizeListener(slot.getWidget().getElement(), - childComponentResizeListener); + slot.setWidgetResizeListener(childComponentResizeListener); if (slot.hasCaption()) { - addResizeListener(slot.getCaptionElement(), - slotCaptionResizeListener); + slot.setCaptionResizeListener(slotCaptionResizeListener); } } else if ((child.isRelativeHeight() || child.isRelativeWidth()) && slot.hasCaption()) { // If the slot has caption, we need to listen for it's size changes // in order to update the padding/margin offset for relative sized // components - addResizeListener(slot.getCaptionElement(), - slotCaptionResizeListener); + slot.setCaptionResizeListener(slotCaptionResizeListener); } if (needsExpand()) { - addResizeListener(slot.getWidget().getElement(), - childComponentResizeListener); + slot.setWidgetResizeListener(childComponentResizeListener); if (slot.hasSpacing()) { - addResizeListener(slot.getSpacingElement(), - spacingResizeListener); + slot.setSpacingResizeListener(spacingResizeListener); } } @@ -587,44 +576,16 @@ public abstract class AbstractOrderedLayoutConnector extends for (ComponentConnector child : getChildComponents()) { Slot slot = getWidget().getSlot(child.getWidget()); if (slot.hasCaption()) { - removeResizeListener(slot.getCaptionElement(), - slotCaptionResizeListener); + slot.setCaptionResizeListener(null); } if (slot.getSpacingElement() != null) { - removeResizeListener(slot.getSpacingElement(), - spacingResizeListener); + slot.setSpacingResizeListener(null); } - removeResizeListener(slot.getWidget().getElement(), - childComponentResizeListener); + slot.setWidgetResizeListener(null); } super.onUnregister(); } - - /** - * Helper method to add a resize listener to an element - * - * @param el - * The element to add the resize listener to - * @param listener - * The listener to add - */ - private void addResizeListener(Element el, ElementResizeListener listener) { - getLayoutManager().addElementResizeListener(el, listener); - } - - /** - * Helper method to remove a resize listener to an element - * - * @param el - * The element from where the resize listener should be removed - * @param listener - * The listener to remove - */ - private void removeResizeListener(Element el, ElementResizeListener listener) { - getLayoutManager().removeElementResizeListener(el, listener); - } - } diff --git a/client/src/com/vaadin/client/ui/orderedlayout/VOrderedLayout.java b/client/src/com/vaadin/client/ui/orderedlayout/VOrderedLayout.java index 0159e491b2..ad17eebe47 100644 --- a/client/src/com/vaadin/client/ui/orderedlayout/VOrderedLayout.java +++ b/client/src/com/vaadin/client/ui/orderedlayout/VOrderedLayout.java @@ -34,6 +34,7 @@ import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.LayoutManager; import com.vaadin.client.StyleConstants; import com.vaadin.client.Util; +import com.vaadin.client.ui.layout.ElementResizeListener; import com.vaadin.shared.ui.AlignmentInfo; import com.vaadin.shared.ui.MarginInfo; @@ -120,7 +121,8 @@ public class VOrderedLayout extends FlowPanel { } /** - * Get the containing slot for a widget + * Get the containing slot for a widget. If no slot is found a new slot is + * created and returned. * * @param widget * The widget whose slot you want to get @@ -130,13 +132,30 @@ public class VOrderedLayout extends FlowPanel { public Slot getSlot(Widget widget) { Slot slot = widgetToSlot.get(widget); if (slot == null) { - slot = new Slot(widget, this); + slot = new Slot(widget); widgetToSlot.put(widget, slot); } return slot; } /** + * Gets a slot based on the widget element. If no slot is found then null is + * returned. + * + * @param widgetElement + * The element of the widget ( Same as getWidget().getElement() ) + * @return + */ + public Slot getSlot(Element widgetElement) { + for (Map.Entry<Widget, Slot> entry : widgetToSlot.entrySet()) { + if (entry.getKey().getElement() == widgetElement) { + return entry.getValue(); + } + } + return null; + } + + /** * Defines where the caption should be placed */ public enum CaptionPosition { @@ -146,7 +165,7 @@ public class VOrderedLayout extends FlowPanel { /** * Represents a slot which contains the actual widget in the layout. */ - public static final class Slot extends SimplePanel { + public final class Slot extends SimplePanel { public static final String SLOT_CLASSNAME = "v-slot"; @@ -157,7 +176,12 @@ public class VOrderedLayout extends FlowPanel { private Icon icon; private Element errorIcon; private Element requiredIcon; - private final VOrderedLayout layout; + + private ElementResizeListener captionResizeListener; + + private ElementResizeListener widgetResizeListener; + + private ElementResizeListener spacingResizeListener; // Caption is placed after component unless there is some part which // moves it above. @@ -176,12 +200,118 @@ public class VOrderedLayout extends FlowPanel { * @param layoutManager * The layout manager used by the layout */ - private Slot(Widget widget, VOrderedLayout layout) { - this.layout = layout; + private Slot(Widget widget) { setStyleName(SLOT_CLASSNAME); setWidget(widget); } + /* + * (non-Javadoc) + * + * @see + * com.google.gwt.user.client.ui.SimplePanel#remove(com.google.gwt.user + * .client.ui.Widget) + */ + @Override + public boolean remove(Widget w) { + detachListeners(); + return super.remove(w); + } + + /* + * (non-Javadoc) + * + * @see + * com.google.gwt.user.client.ui.SimplePanel#setWidget(com.google.gwt + * .user.client.ui.Widget) + */ + @Override + public void setWidget(Widget w) { + detachListeners(); + super.setWidget(w); + attachListeners(); + } + + /** + * Attached resize listeners to the widget, caption and spacing elements + */ + private void attachListeners() { + if (getWidget() != null && getLayoutManager() != null) { + LayoutManager lm = getLayoutManager(); + if (getCaptionElement() != null + && captionResizeListener != null) { + lm.addElementResizeListener(getCaptionElement(), + captionResizeListener); + } + if (widgetResizeListener != null) { + lm.addElementResizeListener(getWidget().getElement(), + widgetResizeListener); + } + if (getSpacingElement() != null + && spacingResizeListener != null) { + lm.addElementResizeListener(getSpacingElement(), + spacingResizeListener); + } + } + } + + /** + * Detaches resize listeners from the widget, caption and spacing + * elements + */ + private void detachListeners() { + if (getWidget() == null && getLayoutManager() != null) { + LayoutManager lm = getLayoutManager(); + if (getCaptionElement() != null + && captionResizeListener != null) { + lm.removeElementResizeListener(getCaptionElement(), + captionResizeListener); + } + if (widgetResizeListener != null) { + lm.removeElementResizeListener(getWidget().getElement(), + widgetResizeListener); + } + if (getSpacingElement() != null + && spacingResizeListener != null) { + lm.removeElementResizeListener(getSpacingElement(), + spacingResizeListener); + } + } + } + + public ElementResizeListener getCaptionResizeListener() { + return captionResizeListener; + } + + public void setCaptionResizeListener( + ElementResizeListener captionResizeListener) { + detachListeners(); + this.captionResizeListener = captionResizeListener; + attachListeners(); + } + + public ElementResizeListener getWidgetResizeListener() { + return widgetResizeListener; + } + + public void setWidgetResizeListener( + ElementResizeListener widgetResizeListener) { + detachListeners(); + this.widgetResizeListener = widgetResizeListener; + attachListeners(); + } + + public ElementResizeListener getSpacingResizeListener() { + return spacingResizeListener; + } + + public void setSpacingResizeListener( + ElementResizeListener spacingResizeListener) { + detachListeners(); + this.spacingResizeListener = spacingResizeListener; + attachListeners(); + } + /** * Returns the alignment for the slot * @@ -302,8 +432,8 @@ public class VOrderedLayout extends FlowPanel { protected int getVerticalSpacing() { if (spacer == null) { return 0; - } else if (layout.getLayoutManager() != null) { - return layout.getLayoutManager().getOuterHeight(spacer); + } else if (getLayoutManager() != null) { + return getLayoutManager().getOuterHeight(spacer); } return spacer.getOffsetHeight(); } @@ -316,8 +446,8 @@ public class VOrderedLayout extends FlowPanel { protected int getHorizontalSpacing() { if (spacer == null) { return 0; - } else if (layout.getLayoutManager() != null) { - return layout.getLayoutManager().getOuterWidth(spacer); + } else if (getLayoutManager() != null) { + return getLayoutManager().getOuterWidth(spacer); } return spacer.getOffsetWidth(); } @@ -565,10 +695,10 @@ public class VOrderedLayout extends FlowPanel { super.onBrowserEvent(event); if (DOM.eventGetType(event) == Event.ONLOAD && icon.getElement() == DOM.eventGetTarget(event)) { - if (layout.getLayoutManager() != null) { - layout.getLayoutManager().layoutLater(); + if (getLayoutManager() != null) { + getLayoutManager().layoutLater(); } else { - layout.updateCaptionOffset(caption); + updateCaptionOffset(caption); } } } @@ -1006,4 +1136,5 @@ public class VOrderedLayout extends FlowPanel { } slot.setStyleNames(stylenames); } + } |