我有一個GWT單元格樹,我用它來顯示CMS的文件結構。我正在使用從我創建的自定義RPC類加載數據的AsyncDataProvider
。我也有一個網絡套接字系統,可以從系統中的其他客戶端廣播事件(文件創建,重命名,移動,刪除等)。如何通過事件總線告訴GWT單元格小部件數據已更改?
我試圖包裹我的頭是當我收到這些事件之一,我如何正確更新我的細胞樹?
我想這個問題可能類似於我的單元格樹的兩個實例在頁面上,它們呈現相同的服務器端數據,並希望確保當用戶更新一個,另一個更新時,通過使用EventBus
。
我覺得這應該很簡單,但我現在已經花了大約6個小時,沒有進展。我的代碼包括如下:
注意:即使它看起來像我是我的自定義RPC框架,我沒有使用RequestFactory。此外,FileEntity只是一個文件的簡單表示,其名稱可由getName()
訪問。
private void drawTree() {
// fileService is injected earlier on and is my own custom rpc service
TreeViewModel model = new CustomTreeModel(new FileDataProvider(fileService));
CellTree tree = new CellTree(model, "Root");
tree.setAnimationEnabled(true);
getView().getWorkspace().add(tree);
}
private static class CustomTreeModel implements TreeViewModel {
// I am trying to use a single AsyncDataProvider so I have a single point of loading data which I can manipulate (Not sure if this is the correct way to go)
public CustomTreeModel(FileDataProvider dataProvider) {
this.provider = provider;
}
public <T> NodeInfo<?> getNodeInfo(final T value) {
if (!(value instanceof FileEntity)) {
// I already have the root File loaded in my presenter, if we are at the root of the tree, I just add it via a list here
ListDataProvider<FileEntity> dataProvider = new ListDataProvider<FileEntity>();
dataProvider.getList().add(TreeWorkspacePresenter.rootFolder);
return new DefaultNodeInfo<FileEntity>(dataProvider,
new FileCell());
} else {
// Otherwise I know that we are loading some tree child data, and I invoke the AsyncProvider to load it from the server
provider.setFocusFile(value);
return new DefaultNodeInfo<FileEntity>(provider,
new FileCell());
}
}
public boolean isLeaf(Object value) {
if(value == null || value instanceof Folder)
return false;
return true;
}
}
public class FileDataProvider extends AsyncDataProvider<FileEntity> {
private FileEntity focusFile;
private FileService service;
@Inject
public FileDataProvider(FileService service){
this.service = service;
}
public void setFocusFile(FileEntity focusFile){
this.focusFile = focusFile;
}
@Override
protected void onRangeChanged(HasData<FileEntity> display) {
service.getChildren(((Folder) focusFile),
new Reciever<List<FileEntity>>() {
@Override
public void onSuccess(List<FileEntity> files) {
updateRowData(0, files);
}
@Override
public void onFailure(Throwable error) {
Window.alert(error.toString());
}
});
}
}
/**
* The cell used to render Files.
*/
public static class FileCell extends AbstractCell<FileEntity> {
private FileEntity file;
public FileEntity getFile() {
return file;
}
@Override
public void render(Context context, FileEntity file, SafeHtmlBuilder sb) {
if (file != null) {
this.file = file;
sb.appendEscaped(file.getName());
}
}
}
不是我正在尋找的東西,但這是有效的。對於維護索引FileEntity-> TreeNode以改善查找時間,您有什麼想法?謝謝! – 2013-02-27 19:05:19
是的,它總是更好地使用索引... – Adarsha 2013-02-28 03:13:40