On 7 mai, 06:32, David Terei <[email protected]> wrote:
> I have a project using GWT which integrates with existing HTML. I want
> to be able to place widgets into the page by retrieving HTML elements
> with specific ID's and inserting the widget as a child.
>
> In 1.5 I just used:
>
> RootPanel.get(id).add(widget);
>
> However I found as many people did once upgrading to 1.6 that this
> isn't a good way as nested elements cause problems. So I'm trying to
> figure out how this should be done. Simple DOM manipulation gets the
> widget in the right place and displaying properly but doesn't setup
> any of its event handlers properly.
>
> I have two solutions at the moment, although neither are ideal:
>
> Solution 1:
> Add the widget to the root panel and then moves it via DOM
> manipulation to be a child of its hook element.
>
> RootPanel.get().add(widget);
> widget.getElement().getParentElement().removeChild(widget.getElement
> ());
> DOM.getElementById(id).appendChild(widget.getElement());
>
> Solution 2:
> Attach via DOM, fix events by calling onAttach via proxy class to get
> around onAttach being protected.
>
> DOM.getElementById(id).appendChild(w.getElement());
> WidgetUtils.WidgetOnAttach(w);

I'd rather make a WidgetUtils.attach() method:
public static void attach(Widget widget, String id) { ... }

> WidgetUtils Class:
>
> package com.google.gwt.user.client.ui;

You could also use JSNI, this allows calling the protected method
without the need for WidgetUtils to be in the c.g.g.u.client.ui
package.

> public class WidgetUtils {
>         public static void WidgetOnAttach(Widget w) {
>                 w.onAttach();

Don't forget to call RootPanel.detachOnWindowClose() or you'll
probably face memory leaks wrt event handlers. See for example how the
Anchor.wrap(...) method is implemented:
http://code.google.com/p/google-web-toolkit/source/browse/releases/1.6/user/src/com/google/gwt/user/client/ui/Anchor.java#44

> Both these methods seem to work fine, have tried both by converting
> all my old 1.5 RootPanel.get(id).add(widget) calls to both of these
> methods under 1.6 and all tests have passed. However, both are
> somewhat of a hack and I seeing if there is a better way.

You could copy (part of) the code from HTMLPanel and add a wrap()
method to it; you'd then wrap() the document's body (or a "root
element" for your app) and use add(Widget,String) the way I proposed
above with WidgetUtils.
(and/or you could file a bug to add a wrap() method to HTMLPanel)

...or you could "just" use JSNI to call the protected add
(Widget,Element) method on a RootPanel (that's what HTMLPanel.add
(Widget,String) does after having "resolved" the id into an Element)

http://code.google.com/p/google-web-toolkit/source/browse/releases/1.6/user/src/com/google/gwt/user/client/ui/HTMLPanel.java

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to