PDA

View Full Version : Asynchronous Tree Panel does not display data.



madz
9 Jan 2011, 8:03 PM
Hi,

I have an asynchronous panel that needed to display a tree directory. The directory path will be read from an XML so I used GWT-RPC to parse the xml and get that directory path, I was able to get the FileModel using GXT RPC, the problem is after the widget has been displayed it does not display the FileModels coming from the GXT-RPC. Although my service implementation actually return a list of FileModel Bean which is correct. Please help. :)

Here is my code...

Parsing the XML and onSuccess process the Loader and Stores for Tree Panel.

private void loadXmlConfig() {
// Request XML file
fileService.getFileViewerConfiguration(xmlPath,
new AsyncCallback<FileViewerXmlConfigurationModel>() {

@Override
public void onFailure(Throwable caught) {
Window.alert(caught.getMessage());

}

@Override
public void onSuccess(FileViewerXmlConfigurationModel result) {
createFileDirectoryRPCProxy(result);
fileView.getTreeLoader().load();
}
});
}


private void createFileDirectoryRPCProxy(
FileViewerXmlConfigurationModel fileViewerXmlConfigurationModel) {

final String fileDirectory = fileViewerXmlConfigurationModel
.getFilePath();

RpcProxy<List<FileModel>> proxy = new RpcProxy<List<FileModel>>() {

@Override
protected void load(Object loadConfig,
AsyncCallback<List<FileModel>> callback) {
Log.info("Loading folders in page");

fileService.getFolderChildren((FileModel) loadConfig,
fileDirectory, callback);
}
};

fileView.setTreeLoader(proxy);
fileView.setTreeStore(fileView.getTreeLoader());
fileView.setTreePanel(fileView.getTreeStore());
}



This is my TreeLoader..

treeLoader = new BaseTreeLoader<FileModel>(proxy) {
@Override
public boolean hasChildren(FileModel parent) {
return parent instanceof FolderModel;
}
};

This is my TreeStore

treeStore = new TreeStore<FileModel>(treeLoader);
treeStore.setStoreSorter(new StoreSorter<FileModel>() {

@Override
public int compare(Store<FileModel> store, FileModel m1,
FileModel m2, String property) {
boolean m1Folder = m1 instanceof FolderModel;
boolean m2Folder = m2 instanceof FolderModel;

if (m1Folder && !m2Folder) {
return -1;
} else if (!m1Folder && m2Folder) {
return 1;
}

return m1.getName().compareTo(m2.getName());
}

});

treeStore.setKeyProvider(new ModelKeyProvider<FileModel>() {

@Override
public String getKey(FileModel model) {
return "node_" + model.<String> get("id");
}
});

This is how I create my TreePanel

treePanel = new TreePanel<FileModel>(treeStore);

treePanel.setIconProvider(new ModelIconProvider<FileModel>() {

@Override
public AbstractImagePrototype getIcon(FileModel model) {
if (!(model instanceof FolderModel)) {
return IconHelper.createPath("image/page_white.png");
}
return null;
}
});

// Create folder structure
treePanel.setStateful(true);
treePanel.setDisplayProperty("name");
treePanel.setId("statefullasynctreepanel");

leftNavPanel.add(treePanel);

my Service Implementation.

File folder = new File(filePath);
File[] listOfFiles = folder.listFiles();
HashMap<File, String> idMap = new HashMap<File, String>();

List<FileModel> models = new ArrayList<FileModel>();
// int counter = 0;
for (File f : listOfFiles) {
FileModel m = null;
if (f.isDirectory()) {
m = new FolderModel(f.getName(), f.getAbsolutePath());
} else {
m = new FileModel(f.getName(), f.getAbsolutePath());
m.set("size", f.length());
m.set("date", new Date(f.lastModified()));
}

if (idMap.containsKey(f)) {
m.set("id", idMap.get(f));
} else {
String id = String.valueOf(counter++);
idMap.put(f, id);
m.set("id", id);
}

models.add(m);
}

Collections.sort(models, new Comparator<FileModel>() {
public int compare(FileModel o1, FileModel o2) {
return o1.getName().compareTo(o2.getName());
}
});
return models;
}


and my Bean.

public FileModel(){
}

public FileModel(String name, String path) {
setName(name);
setPath(path);
}

public void setPath(String path) {
set("path",path);
}

public void setName(String name) {
set("name",name);
}

public String getPath() {
return get("path");
}

public String getName() {
return get("name");
}

@Override
public boolean equals(Object obj) {
if (obj != null && obj instanceof FileModel) {
FileModel mobj = (FileModel) obj;
return getName().equals(mobj.getName()) && getPath().equals(mobj.getPath());
}
return super.equals(obj);
}