如何将GWT小部件附加到Element

时间:2013-06-17 11:55:24

标签: gwt gxt appearance

我必须附加将GWT Composite扩展到我的自定义小部件的小部件,该小部件使用如下标记:

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

我需要将嵌入式窗口小部件插入到内容类的嵌套div中。

下面我发布我的自定义小部件的构造函数:

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);
}

}

我不确定字符串:

SafeHtmlUtils.fromTrustedString(content.toString())

内容是视频播放器小部件。以上代码中的EMBED-tag结果不会出现在HTML页面中。

2 个答案:

答案 0 :(得分:1)

您似乎没有将内容窗口小部件添加到代码中的contentElement。从逻辑上讲,除非你的VideoPanelAppearance以某种方式与contentElement相关(从代码中看不出来),渲染与它们无关。为什么不直接将content元素作为“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());

我还要确保appearance.getContentElement(element)返回带有“content”类的期望元素。希望这会有所帮助。

答案 1 :(得分:0)

解决方案:

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());
}

}