在单个GWT UiBinder模板中使用带有小部件的TreeItem作为内容
我正试图使用在UI活页夹模板中定义为在单个GWT UiBinder模板中使用带有小部件的TreeItem作为内容,gwt,uibinder,Gwt,Uibinder,我正试图使用在UI活页夹模板中定义为TreeItem内容的Widget初始化TreeItem。最初,我使用的模板如下: <g:Tree> <g:TreeItem> <g:HTMLPanel> <p>This is the root item's content.</p> </g:HTMLPanel> <g:TreeItem>
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.
- (root item has no content)
\ This is the root item's content.
- (first child has no content)
\ This is the child's content.
<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>
<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>
扩展到:
- This is the root item's content.
\ This is the child's content.
但这不起作用。HTMLPanel
小部件是作为TreeItem
s的子项创建的,而不是作为项的小部件,因此我最终得到如下结构:
<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.
- (root item has no content)
\ This is the root item's content.
- (first child has no content)
\ This is the child's content.
<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>
<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>
因此我尝试扩展TreeItem
类,如下所示:
public class WidgetTreeItem extends TreeItem {
@UiChild(tagname="widget", limit=1)
public void setWidget(IsWidget isWidget) {
super.setWidget(isWidget.asWidget());
}
}
然后,将包含widgetreeitem
的包导入c
命名空间,我有一个如下模板:
<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.
- (root item has no content)
\ This is the root item's content.
- (first child has no content)
\ This is the child's content.
<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>
<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>
这是根项目的内容
这是孩子的内容
但这也不行。它失败,但出现以下异常:
只有TreeItem或Widget子类是有效的子类:
有人知道如何实现我的目标吗?从中得到一些启发,我找到了解决方案。在我最初的示例中,XML现在如下所示:
<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.
- (root item has no content)
\ This is the root item's content.
- (first child has no content)
\ This is the child's content.
<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>
<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>
这是根项目的内容
这是孩子的内容
这里需要一些游戏。最值得注意的是,只有当内容面板包含在实现
haswidget
接口的小部件中时,这才有效。树
类满足此要求。同样值得注意的是,在使用内容字段之前,必须对其进行定义。以下内容可能会对您有所帮助
<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>
您可以根据需要添加任意数量的树项。这是我的方法,问题是,实际上,内容包含复杂的小部件,而不仅仅是文本。本质上,我是在寻找一种使用
TreeItem(Widget w)
构造函数的方法。一旦我了解了如何构造模板,我提供的自我回答就完全达到了预期的效果。