From 6245b30fd12f8a57617126f9ae55e50ae43f791e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Mon, 17 Dec 2012 12:43:30 +0100 Subject: [PATCH] Handle attachment of widgets created with elements which already belong to a widget. Fixes issue 163 --- .../client/plugins/widgets/WidgetsUtils.java | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) 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()(); }-*/; -- 2.39.5