gpt4 book ai didi

java - GWT CellTree @UIHandler SelectionChanged

转载 作者:行者123 更新时间:2023-12-02 06:49:38 25 4
gpt4 key购买 nike

我正在使用 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

在上述所有情况下,我在表单加载时都会收到延迟绑定(bind)失败的消息。我觉得我错过了一些简单的东西,但似乎无法用我的 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);
}
}

关于java - GWT CellTree @UIHandler SelectionChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18211084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com