我正在使用GWT CellTree类,并且当选择更改时,我希望能够更新页面上的另一个UI元素。
我有这个:
@UiField (provided = true)
CellTree folderTree;
我在页面加载时填充了数据,然后需要@UIiHandler,例如:
@UiHandler("folderTree")
void onTreeCellChange(????? e){
//update UI
}
我已经尝试过类似的事情:
ValueChangeEvent<List<Folder>> //List<Folder> being the underlying class for the CellTree.
SelectionHandler<CellTree>
ClickEvent
在上述所有情况下,我在表单加载时遇到了Deferred绑定失败的问题。
我觉得我缺少一些简单的东西,但是似乎无法用我的google-fu找到它。
最佳答案
您可以将处理程序添加到CellTree的TreeViewModel的NodeInfo中。
这是可能的示例:
首先让我们定义什么是文件夹:
// the simplest possible folder even without subfolders just to make example working
public class Folder {
private String name;
public Folder(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
假设您的ui.xml部分如下:
<g:HorizontalPanel>
<cellview:CellTree ui:field="folderTree"/>
<g:Label ui:field="folderName"/>
</g:HorizontalPanel>
比起UiBinder:
public static interface Binder extends UiBinder<HorizontalPanel, BinderOwner> {}
这是定义CellTree所需的FolerTreeModel的方式:
public static class FolderTreeModel implements TreeViewModel {
private final ListDataProvider<Folder> dataProvider;
private final CellPreviewEvent.Handler<Folder> folderSelectionHandler;
public FolderTreeModel(List<Folder> folders, CellPreviewEvent.Handler<Folder> folderSelectionHandler) {
this.folderSelectionHandler = folderSelectionHandler;
dataProvider = new ListDataProvider<Folder>(folders);
}
@Override
public <T> TreeViewModel.NodeInfo<?> getNodeInfo(T value) {
return new DefaultNodeInfo<Folder>(dataProvider, new AbstractCell<Folder>() {
// simple renderer that renders folder name
@Override
public void render(Context context, Folder value, SafeHtmlBuilder sb) {
sb.appendEscaped(value.getName());
}
},
new SingleSelectionModel<Folder>(), // using single selection model
folderSelectionHandler, // add handler to the node info
null); // value updater can be null
}
@Override
public boolean isLeaf(Object value) {
return false; // I will say that every node is not leaf for simplicity
}
}
和您的BinderOwner类:
public static class BinderOwner {
@UiField(provided = true)
CellTree folderTree;
@UiField
Label folderName; //your other UI element that you will change: show the name of selected folder
public BinderOwner() {
//List of folders to show something
ArrayList<Folder> folders = new ArrayList<Folder>();
folders.add(new Folder("A"));
folders.add(new Folder("B"));
folders.add(new Folder("C"));
// define folder tree
folderTree = new CellTree(new FolderTreeModel(folders, new CellPreviewEvent.Handler<Folder>() {
// this is your handler
@Override
public void onCellPreview(CellPreviewEvent<Folder> event) {
// set folder name to label on folder selection
folderName.setText(event.getValue().getName());
}
}), null);
}
}