CHEM_Eugene
CHEM_Eugene

Reputation: 438

How to attach GWT widget to Element

I have to attach widget that extends GWT Composite to my custom widget that use markup like this:

<div class="left">
  <div class="right">
    <div class="content">{content}</div>
  </div>
</div>

I need insert widget to nested div with content class.

Below I post constructor of my custom widget:

public class VideoPanel extends Component {

public VideoPanel(final Widget content,
        final VideoPanelAppearance appearance, final int width,
        final int height) {
    this.content = content;
    this.appearance = appearance;
    final SafeHtmlBuilder sb = new SafeHtmlBuilder();
    appearance.render(sb,
            SafeHtmlUtils.fromTrustedString(content.toString()));
    final XElement element = XDOM.create(sb.toSafeHtml());
    final XElement contentElement = appearance.getContentElement(element);
    contentElement
            .setSize(getContentWidth(width), getContentHeight(height));
    setElement(element);
    setPixelSize(width, height);
}

}

I'm not sure in string:

SafeHtmlUtils.fromTrustedString(content.toString())

content is video player widget. And above code results in EMBED-tag doesn't appear in HTML page.

Upvotes: 2

Views: 1714

Answers (2)

CHEM_Eugene
CHEM_Eugene

Reputation: 438

Solution:

public class VideoPanel extends Panel {

public VideoPanel(final Composite content, final VideoPanelAppearance appearance, final int width,
        final int height) {
    this.content = content;
    this.appearance = appearance;
    final SafeHtmlBuilder sb = new SafeHtmlBuilder();
    appearance.render(sb, title);
    setElement(XDOM.create(sb.toSafeHtml()));

    DOM.appendChild(getContainerElement(), content.getElement());
    adopt(content);
    getContainerElement().<XElement> cast().setSize(getContentWidth(width),
            getContentHeight(height));
    setPixelSize(width, height);
    sinkEvents(Event.ONCLICK);
}

public Element getContainerElement() {
    return appearance.getContentElement(getElement().<XElement> cast());
}

}

Upvotes: 0

nightsRey
nightsRey

Reputation: 124

It doesn't look like you ever add the content widget to the contentElement in your code. And logically, unless your VideoPanelAppearance were somehow related to the contentElement (which isn't evident from the code) the rendering would not relate them. Why don't you just append the content element as a child of the "contentElement":

/*this will relate them via the DOM (so the widgets wouldn't know about it but
but in your case it doesn't seem like it matters*/
contentElement.appendChild(content.getElement());

I would also make sure that appearance.getContentElement(element) is returning the expected element with class "content". Hope this helps.

Upvotes: 1

Related Questions