将带有Widget的TreeItem用作单个GWT UiBinder模板中的内容

时间:2013-09-10 11:16:37

标签: gwt uibinder

我正在尝试使用UI Binder模板中定义的TreeItem初始化Widget作为TreeItem的内容。最初我开始使用这样的模板:

<g:Tree>
    <g:TreeItem>
        <g:HTMLPanel>
            <p>This is the root item's content.</p>
        </g:HTMLPanel>
        <g:TreeItem>
            <g:HTMLPanel>
                <p>This is the child's content.</p>
            </g:HTMLPanel>
        </g:TreeItem>
    </g:TreeItem>
</g:Tree>

期望最终得到这样的树结构:

+ This is the root item's content.

扩展为:

- This is the root item's content.
 \ This is the child's content.

但这不起作用。 HTMLPanel小部件是作为TreeItem的子项创建的,而不是项目的小部件,所以我最终得到这样的结构:

- (root item has no content)
 \ This is the root item's content.
  - (first child has no content)
   \ This is the child's content.

所以我尝试像这样扩展TreeItem类:

public class WidgetTreeItem extends TreeItem {

    @UiChild(tagname="widget", limit=1)
    public void setWidget(IsWidget isWidget) {
        super.setWidget(isWidget.asWidget());
    }

}

然后,将包含WidgetTreeItem的包导入c命名空间,我有一个这样的模板:

<g:Tree>
    <c:WidgetTreeItem>
        <c:widget>
            <g:HTMLPanel>
                <p>This is the root item's content.</p>
            </g:HTMLPanel>
        </c:widget>
        <!-- Should create a child TreeItem with the HTMLPanel as its Widget. -->
        <g:HTMLPanel>
            <p>This is the child's content.</p>
        </g:HTMLPanel>
    </c:WidgetTreeItem>
</g:Tree>

但这也行不通。它失败并出现以下异常:

Only TreeItem or Widget subclasses are valid children: <c:WidgetTreeItem>

任何人都可以看到如何实现我的目标吗?

2 个答案:

答案 0 :(得分:0)

鉴于this answer的一些灵感,我找到了解决方案。鉴于我的原始示例,XML现在看起来像这样:

<g:Tree>
    <g:HTMLPanel ui:field="rootContent">
        <p>This is the root item's content.</p>
    </g:HTMLPanel>
    <g:HTMLPanel ui:field="childContent">
        <p>This is the child's content.</p>
    </g:HTMLPanel>
    <g:TreeItem widget="{rootContent}">
        <g:TreeItem widget="{childContent}">
        </g:TreeItem>
    </g:TreeItem>
</g:Tree>

这里需要一些游戏。最值得注意的是,只有在内容面板包含在实现HasWidgets接口的小部件中时,这才有效。 Tree类满足此要求。还值得注意的是,必须在使用之前定义内容字段。

答案 1 :(得分:0)

可能以下可以帮到你。

<g:Tree>
    <g:TreeItem text="This is the first parent item's content.">
        <g:TreeItem text="This is the child 1's content." />
        <g:TreeItem text="This is the child 2's content." />

    </g:TreeItem>

    <g:TreeItem text="This is the second parent item's content.">
        <g:TreeItem text="This is the child 3's content." />
        <g:TreeItem text="This is the child 4's content." />

    </g:TreeItem>
</g:Tree>

您可以根据需要追加TreeItems。