diff options
author | Manuel Carrasco Moñino <manuel.carrasco.m@gmail.com> | 2012-12-17 12:43:30 +0100 |
---|---|---|
committer | Manuel Carrasco Moñino <manuel.carrasco.m@gmail.com> | 2012-12-17 12:43:30 +0100 |
commit | 6245b30fd12f8a57617126f9ae55e50ae43f791e (patch) | |
tree | 22c5446cf4ae9de93fac9c754532b1c75fdb021f /gwtquery-core | |
parent | 2c1011b40fc29d354596a512fbb94cc7fc62d300 (diff) | |
download | gwtquery-6245b30fd12f8a57617126f9ae55e50ae43f791e.tar.gz gwtquery-6245b30fd12f8a57617126f9ae55e50ae43f791e.zip |
Handle attachment of widgets created with elements which already belong to a widget. Fixes issue 163
Diffstat (limited to 'gwtquery-core')
-rw-r--r-- | gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetsUtils.java | 43 |
1 files 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() - .<com.google.gwt.user.client.Element>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()(); }-*/; |