GWT CellTree @UIHandler SelectionChanged

时间:2013-08-13 13:55:37

标签: java gwt

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

在上述所有情况下,我在表单加载时遇到延迟绑定失败。 我觉得我错过了一些简单的东西,但似乎无法用我的google-fu找到它。

1 个答案:

答案 0 :(得分:2)

您可以将处理程序添加到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);
    }
}