7

I'm using a GWT library (gwt-openlayers) which allows me to create a map popup containing arbitrary HTML, similar to Google Maps. I need this HTML to contain a GWT Button widget.

I'm creating some HTML elements on-the-fly like this:

Element outerDiv = DOM.createDiv();
outerDiv.getStyle().setOverflow(Overflow.HIDDEN);
outerDiv.getStyle().setWidth(100, Unit.PCT);
outerDiv.appendChild(new HTML(mapPOI.getHtmlDetails()).getElement());
Button popupButton = new Button("View Property");
popupButton.getElement().getStyle().setFloat(com.google.gwt.dom.client.Style.Float.RIGHT);
outerDiv.appendChild(popupButton.getElement());

Then I'm getting the source HTML for these elements by calling

String src = outerDiv.toString();

and inserting this html into my map marker. Now my map marker displays the content ok, including the button. However, the button won't respond to any events! From what I can gather, this is because the buttons onAttach() method is never being called.

Is there a better way to do this?

Thanks,

Jon

~~~~EDIT~~~~

I'm now trying a new way of doing this, which seems to be the accepted method looking at other similar posts.

First I'm creating my div:

String divId = "popup-" + ref;
String innerHTML = "<div id=\"" +divId + "\"></div>";

Then I'm adding this to my map popup and displaying it (which adds it to the DOM). After the popup has been displayed, I'm getting the Element as follows and trying to wrap a HTMLPanel around it:

Element element = Document.get().getElementById(divId);
HTMLPanel popupHTML = HTMLPanel.wrap(element);

My div element is successfully retrieved. However, HTMLPanel.wrap(element); doesn't complete. The reason for this is that wrap(..) calls RootPanel.detachOnWindowClose(Widget widget), which includes the following assertions:

assert !widgetsToDetach.contains(widget) : "detachOnUnload() called twice "
    + "for the same widget";
assert !isElementChildOfWidget(widget.getElement()) : "A widget that has "
    + "an existing parent widget may not be added to the detach list";

I put some breakpoints in and it seems that the 2nd assertion is failing!

Does anybody have any idea why this might be the case? Should failing this assertion really result in a complete failure of the method (no return)?

5 Answers 5

5

Your first approach is good, you just need to register onClick event for your button like this:

DOM.sinkEvents(popupButton.getElement(), Event.ONCLICK);
DOM.setEventListener(popupButton.getElement(), new EventListener() {
    @Override
    public void onBrowserEvent(Event event) {
        //implement the logic after click
    }
});

I have checked this, it works 100%!

Sign up to request clarification or add additional context in comments.

Comments

2

You might try something like

RootPanel.get("idOfYourMapMarker").add(popupButton);

See RootPanel.get()

Unfortunately, RootPanels are AbsolutePanels which aren't so nice for layout but could work if you just have a simple button to add. You could also try RootLayoutPanel which will give you a LayoutPanel (also not so nice when you just want things to flow). You might end up creating a container widget that does the layout for you, and adding that to the RootPanel.

1 Comment

I tried this approach, but it doesn't seem to work. It doesn't even get past RootPanel.get("divId"); but it also doesn't throw an exception! Very odd.
2

SimplePanel is a DIV. Perhaps that can be used instead?

Comments

0

You added the element, but you have to keep the hierarchy of the actual GWT Widgets too.

I don't see a clean way to do this, but you could use something like jQuery to grab the button by and ID and add a click handler back to it that would call the original click handler.

private static native void registerEvents(String buttonId, MyClass instance)/*-{
    var $ = $wnd.$;

    //check click
    $('#'+buttonId).live('click', function(e) {
        e.preventDefault();
        [email protected]::handleButtonClick(Lcom/google/gwt/event/dom/client/ClickEvent;)(null);
    });
}-*/;

Call this registerEvents() either in your onAttach or constructor.

2 Comments

this will create memory leaks in older browsers, there are standard ways of building this with GWT
His problem is that he is building elements, then grabbing the HTML for all those elements and sending to a 3rd party widget (some map marker). The initial problem already sounds dangerous, which is why I said 'I don't see a clean way', however it IS a solution he could use, he just has to call and equivalent cleanup method himself.
0

I once had a similar problem. You can use the gwt-openlayer's MapWidget as follows:

private MapWidget createMapWidget() {
    final MapOptions defaultMapOptions = new MapOptions();
    defaultMapOptions.setDisplayProjection(DEFAULT_PROJECTION);
    defaultMapOptions.setNumZoomLevels(TOTAL_ZOOM_LEVELS);

    MapWidget mapWidget = new MapWidget(MAP_WIDGET_WIDTH, MAP_WIDGET_HEIGHT, defaultMapOptions);

    map = mapWidget.getMap();

    return mapWidget;
}

And then add it to any panel be it vertical or horizontal.

MapWidget mapWgt = createMapWidget();

VerticalPanel mainPanel = new VerticalPanel();
mainPanel.add(mapWgt);
...
... add whatever you want
...

You can finally add the created Panel(containing the MapWidget and the gwt widget) to the PopupPanel. Also, you should now be able to add handlers to the gwt button.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.