2

我必须将扩展 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 页面中。

4

2 回答 2

1

看起来您从未将内容小部件添加到代码中的 contentElement 中。从逻辑上讲,除非您的 VideoPanelAppearance 以某种方式与 contentElement 相关(从代码中看不出来),否则渲染不会将它们关联起来。为什么不将内容元素附加为“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());

我还要确保外观.getContentElement(element) 返回具有“内容”类的预期元素。希望这可以帮助。

于 2013-06-20T11:52:08.097 回答
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());
}

}
于 2013-06-20T13:04:59.363 回答