From: Manuel Carrasco MoƱino Date: Mon, 17 Dec 2012 11:43:30 +0000 (+0100) Subject: Handle attachment of widgets created with elements which already belong to a widget... X-Git-Tag: release-1.3.2~25 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6245b30fd12f8a57617126f9ae55e50ae43f791e;p=gwtquery.git Handle attachment of widgets created with elements which already belong to a widget. Fixes issue 163 --- diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetsUtils.java index 7b42787b..3c82a618 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetsUtils.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetsUtils.java @@ -27,6 +27,8 @@ import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.ComplexPanel; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.HTMLPanel; +import com.google.gwt.user.client.ui.HasOneWidget; +import com.google.gwt.user.client.ui.HasWidgets; import com.google.gwt.user.client.ui.Panel; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.SimplePanel; @@ -171,24 +173,39 @@ public class WidgetsUtils { } /** - * Attach a widget to the GWT widget list. + * Attach a widget to the GWT widget list. Normally used when GQuery + * creates widgets wrapping existing dom elements. + * It does nothing if the widget is already attached to another widget. * * @param widget to attach * @param firstParentWidget the parent widget, - * If it is null we just add the widget to the gwt detach list + * If it is null and it is not inside any other widget, we just add + * the widget to the gwt detach list */ public static void attachWidget(Widget widget, Widget firstParentWidget) { if (widget != null && widget.getParent() == null) { if (firstParentWidget == null) { - RootPanel.detachOnWindowClose(widget); - widgetOnAttach(widget); + firstParentWidget = getFirstParentWidget(widget); + if (firstParentWidget == null) { + RootPanel.detachOnWindowClose(widget); + widgetOnAttach(widget); + } else { + attachWidget(widget, firstParentWidget); + } } else if (firstParentWidget instanceof HTMLPanel) { - ((HTMLPanel) firstParentWidget).add(widget, - widget.getElement().getParentElement() - .cast()); + HTMLPanel h = (HTMLPanel) firstParentWidget; + Element p = widget.getElement().getParentElement(); + if (p != null) { + h.add(widget, p); + } else { + h.add(widget); + } + } else if (firstParentWidget instanceof HasOneWidget) { + ((HasOneWidget)firstParentWidget).setWidget(widget); + } else if (firstParentWidget instanceof HasWidgets) { + ((HasWidgets)firstParentWidget).add(widget); } else { - throw new RuntimeException( - "No HTMLPanel available to attach the widget."); + widgetSetParent(widget, firstParentWidget); } } } @@ -233,6 +250,14 @@ public class WidgetsUtils { return null; } + /** + * Return the first widget parent of the element, or null if it is not + * attached to any widget yet. + */ + private static Widget getFirstParentWidgetElement(Element element) { + return $(element).parents().widget(); + } + private static native void widgetOnAttach(Widget w) /*-{ w.@com.google.gwt.user.client.ui.Widget::onAttach()(); }-*/;