View Full Version : TreeLoader + RPC
netzone_tech
18 Oct 2008, 6:50 PM
Hi,
I'm trying to write a simple app that uses TreeLoader +RPC. I'm reusing my domain model as shown in http://extjs.com/blog/2008/07/14/preview-java-bean-support-with-ext-gwt/ (http://extjs.com/forum/../blog/2008/07/14/preview-java-bean-support-with-ext-gwt/)
So I have the following:
public class ModuleServicesRemoteImpl extends HibernateRemoteService implements ModuleServicesRemote {
..
public List<Module> getAllStudentClasses() {
List<Module> result = new ArrayList<Module>();
result.add(new Module("Blabla"));
return result;
}
}
the Remote interfaces:
public interface ModuleServicesRemote extends RemoteService {
public List<Module> getAllStudentClasses();
}
public interface ModuleServicesRemoteAsync {
void getAllStudentClasses(AsyncCallback<List<Module>> async);
}
and in the view...
RpcProxy<Module, List<Module>> proxy = new RpcProxy<Module, List<Module>>() {
protected void load(Module loadConfig, AsyncCallback<List<Module>> asyncCallback) {
service.getAllStudentClasses(asyncCallback);
}
};
BeanModelReader reader = new BeanModelReader();
TreeLoader loader = new BaseTreeLoader(proxy, reader){
public boolean hasChildren(Module parent) {
return false;
}
};
loader.load(null);
TreeStore<BeanModel> store = new TreeStore<BeanModel>(loader);
Tree tree = new Tree();
TreeBinder<BeanModel> binder = new TreeBinder<BeanModel>(tree, store);
binder.setAutoLoad(true);
binder.setDisplayProperty("moduleName");But I keep getting the following exception:
java.lang.RuntimeException: java.lang.ClassCastException: com.extjs.gxt.ui.client.data.BaseListLoadResult cannot be cast to java.util.List
at com.extjs.gxt.ui.client.store.TreeStore.onLoadException(TreeStore.java:490)
at com.extjs.gxt.ui.client.store.TreeStore$1.loaderLoadException(TreeStore.java:140)
at com.extjs.gxt.ui.client.event.LoadListener.handleEvent(LoadListener.java:27)
at com.extjs.gxt.ui.client.event.LoadListener.handleEvent(LoadListener.java:1)
at com.extjs.gxt.ui.client.event.BaseObservable.fireEvent(BaseObservable.java:74)
at com.extjs.gxt.ui.client.data.BaseTreeLoader.onLoadFailure(BaseTreeLoader.java:118)
at com.extjs.gxt.ui.client.data.BaseTreeLoader.onLoadFailure(BaseTreeLoader.java:1)
at com.extjs.gxt.ui.client.data.BaseLoader$1.onFailure(BaseLoader.java:115)
at com.extjs.gxt.ui.client.data.RpcProxy$1.onSuccess(RpcProxy.java:35)
at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:215)
at com.google.gwt.http.client.Request.fireOnResponseReceivedImpl(Request.java:254)
at com.google.gwt.http.client.Request.fireOnResponseReceivedAndCatch(Request.java:226)
at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:217)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)Can anyone tell me what I've done wrong?? Who has returned BaseListLoadResult here? ExtGWT (because my code returns ArrayList)
Thanks in advance!
netzone_tech
22 Oct 2008, 12:36 AM
Hmm.... has anyone ever used TreeLoader & RPC before?....
+ 1 , I Have the same issue
It's possible to have an example for to Use My pojo directly in TreeLoader async ?
jonjanisch
31 Oct 2008, 12:00 PM
I believe the issue is that BaseTreeLoader expects as arguments:
DataProxy<M, List<M>> proxy
DataReader<M, List<M>> reader
BeanModelReader does not return a List<M> it returns a ListLoadResult<M>. Why? I have no idea. I created a simple adapter class so that I can use the BeanModelReader with Trees.
public class BeanModelReaderAdapter<C> implements DataReader<C, List<ModelData>> {
private BeanModelReader<C> reader;
public BeanModelReaderAdapter(BeanModelReader<C> reader) {
this.reader = reader;
}
public List<ModelData> read(C loadConfig, Object data) {
List<ModelData> result = reader.read(loadConfig, data).getData();
return new ArrayList<ModelData>(result);
}
}
Usage:
BeanModelReader<ModelData> reader = new BeanModelReader<ModelData>();
BeanModelReaderAdapter<ModelData> adapter = new BeanModelReaderAdapter<ModelData>(reader);
loader = new BaseTreeLoader<ModelData>(proxy,adapter) {
public boolean hasChildren(ModelData parent) {
return !(Boolean)parent.get("leaf");
}
};
Darrel, can you clarify this please? Is there anyway to use the BeanModelReader with a TreeLoader without writing an adapter class as I did?
-Jonathan
normanmaurer
4 Nov 2008, 9:09 AM
I had the exact same problem. Using your Adapter class works.
Whould be nice to get more infos if this is a bug or just the design.. If its not a bug I would like to see your Adapter class included in GXT.
Cheers,
Norman
chriswesdorp
19 Jan 2009, 7:15 AM
I was looking for a similar problem and I found this topic. I have one question though which for something I can't get to work.
From the server I send a complete tree structure (groups and items), both entities implementing the BeanModelTag interface. So the BeanModelReader can convert them.
public class MenuItem {
private String caption;
}
public class Group extends MenuItem implements BeanModelTag {
private List<MenuItem> childs;
}
public class Item extends MenuItem implements BeanModelTag {
private String function;
}
// in in the panel where the tree is displayed
MemoryProxy<List<BaseMenuItem>> proxy = new MemoryProxy<List<BaseMenuItem>>(getRootMenuItem());
BeanModelReader<ModelData> reader = new BeanModelReader<ModelData>();
BeanModelReaderAdapter<ModelData> adapter = new BeanModelReaderAdapter<ModelData>(reader);
TreeLoader loader = new BaseTreeLoader(proxy, adapter) {
public boolean hasChildren(ModelData parent) {
Object subItems = parent.get("children");
return (subItems != null && ((java.util.Collection) subItems).size() > 0);
}
};
How do I tell the reader or store what the children attribute is in the ModelData created by the BeanModelReader. Somehow i think the BeanModelReader adapter should be aware of the fact it is reading a tree. Now it does nothing more as recursivly adding the root item to the tree without descending to the childs. Does anyone have experience with this?
Regards,
Chris
(sorry for a reply on an old thread)
chriswesdorp
20 Jan 2009, 6:26 AM
I already fixed the problem. I needed to convert the objects to ModelData type for every level in the tree which gave me some trouble.
dkann
3 Apr 2009, 10:23 AM
Hi,
I read the posts in this thread and tried to implement an async tree using Rpcproxy and BeanModel. However I keep running into a classcast exception, after the first level of tree nodes are loaded and I try to load the children by clicking on one of the nodes.:((:((:((:((
Error message:
[ERROR] Uncaught exception escaped
java.lang.ClassCastException: com.extjs.gxt.ui.client.data.BeanModel_com_XYZ_tcms_ui_client_model_entity_DestinationTreeModel
at com.wiley.tcms.ui.client.widget.DestinationTreeWidget$1.load(DestinationTreeWidget.java:1)
at com.extjs.gxt.ui.client.data.RpcProxy.load(RpcProxy.java:19)
at com.extjs.gxt.ui.client.data.BaseLoader.loadData(BaseLoader.java:126)
at com.extjs.gxt.ui.client.data.BaseLoader.load(BaseLoader.java:90)
at com.extjs.gxt.ui.client.data.BaseTreeLoader.loadChildren(BaseTreeLoader.java:85)
at com.extjs.gxt.ui.client.binder.TreeBinder.loadChildren(TreeBinder.java:335)
at com.extjs.gxt.ui.client.binder.TreeBinder.onBeforeExpand(TreeBinder.java:380)
at com.extjs.gxt.ui.client.binder.TreeBinder$2.handleEvent(TreeBinder.java:298)
at com.extjs.gxt.ui.client.binder.TreeBinder$2.handleEvent(TreeBinder.java:1)
at com.extjs.gxt.ui.client.event.BaseObservable.fireEvent(BaseObservable.java:74)
at com.extjs.gxt.ui.client.widget.Component.fireEvent(Component.java:443)
at com.extjs.gxt.ui.client.widget.tree.TreeItem.fireEvent(TreeItem.java:697)
at com.extjs.gxt.ui.client.widget.tree.TreeItem.setExpanded(TreeItem.java:578)
at com.extjs.gxt.ui.client.widget.tree.TreeItem.setExpanded(TreeItem.java:553)
at com.extjs.gxt.ui.client.widget.tree.TreeItem.toggle(TreeItem.java:687)
at com.extjs.gxt.ui.client.widget.tree.TreeItemUI.handleClickEvent(TreeItemUI.java:374)
at com.extjs.gxt.ui.client.widget.tree.TreeItemUI.handleEvent(TreeItemUI.java:152)
at com.extjs.gxt.ui.client.widget.tree.TreeItem.onComponentEvent(TreeItem.java:446)
at com.extjs.gxt.ui.client.widget.tree.Tree.onComponentEvent(Tree.java:473)
at com.extjs.gxt.ui.client.widget.Component.onBrowserEvent(Component.java:722)
at com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1308)
at com.google.gwt.user.client.DOM.dispatchEventAndCatch(DOM.java:1287)
at com.google.gwt.user.client.DOM.dispatchEvent(DOM.java:1255)
at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.ie.IDispatchImpl.callMethod(IDispatchImpl.java:126)
at com.google.gwt.dev.shell.ie.IDispatchProxy.invoke(IDispatchProxy.java:155)
at com.google.gwt.dev.shell.ie.IDispatchImpl.Invoke(IDispatchImpl.java:294)
at com.google.gwt.dev.shell.ie.IDispatchImpl.method6(IDispatchImpl.java:194)
at org.eclipse.swt.internal.ole.win32.COMObject.callback6(COMObject.java:117)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:1925)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2966)
at com.google.gwt.dev.GWTShell.pumpEventLoop(GWTShell.java:720)
at com.google.gwt.dev.GWTShell.run(GWTShell.java:593)
at com.google.gwt.dev.GWTShell.main(GWTShell.java:357)
Here is my model class:
public class DestinationTreeModel implements BeanModelTag,Serializable{
private String id;
private String name;
private String type;
public DestinationTreeModel(){
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Remote Services interfaces:
public interface LocationService extends RemoteService{
public List<DestinationTreeModel> getChildren(DestinationTreeModel dest);
}
public interface LocationServiceAsync{
public void getChildren(DestinationTreeModel dest, AsyncCallback<List<DestinationTreeModel>> callback);
}
Service Implementation class:
public class LocationServiceImpl extends RemoteServiceServlet implements LocationService{
public List<DestinationTreeModel> getChildren(DestinationTreeModel loc) {
List<DestinationTreeModel> locs = new ArrayList<DestinationTreeModel>();
if (loc == null) {
//Process the XML and add the first level locations to 'locs'
}else {
//Get the children of the incoming 'loc' object from XML and add the locations to 'locs'
}
return locs;
}
}
Finally, here is my UI class:
public class DestinationTree implements EntryPoint {
public static final String LOC_SERVICE = "locservice";
public void onModuleLoad() {
final LocationServiceAsync locService = (LocationServiceAsync) GWT.create(LocationService.class);
ServiceDefTarget endpoint = (ServiceDefTarget) locService;
String moduleRelativeURL = GWT.getModuleBaseURL() + "locService";
endpoint.setServiceEntryPoint(moduleRelativeURL);
RpcProxy<DestinationTreeModel, List<DestinationTreeModel>> proxy = new RpcProxy<DestinationTreeModel, List<DestinationTreeModel>>() {
@Override
protected void load(DestinationTreeModel loadConfig, AsyncCallback<List<DestinationTreeModel>> callback) {
locService.getChildren(loadConfig, callback);
}
};
BeanModelReader<ModelData> reader = new BeanModelReader<ModelData>();
BeanModelReaderAdapter<ModelData> adapter = new BeanModelReaderAdapter<ModelData>(reader);
// Modified Tree loader using BeanModel
TreeLoader loader = new BaseTreeLoader(proxy, adapter){
@Override
public boolean hasChildren(ModelData parent) {
if(locType.trim().equalsIgnoreCase("state") || locType.trim().equalsIgnoreCase("region"))
return false;
else
return true;
}
};
//Set the tree store
TreeStore<BeanModel> store = new TreeStore<BeanModel>(loader);
//Create the Tree content panel
ContentPanel tp = new ContentPanel();
tp.setHeading("Tree Panel");
tp.setHeading("Locations");
final Tree tree = new Tree();
//Create TreeBinder
TreeBinder<BeanModel> binder = new TreeBinder<BeanModel>(tree, store);
binder.setDisplayProperty("name");
//Load the tree
loader.load(null);
tp.add(tree);
tp.layout();
RootPanel.get().add(tp);
}
}
Can anyone plz help me with the above issue?
Thanks
DK
I got it!! I fixed the issue by changing the RpcProxy to this:
RpcProxy<BeanModel, List<DestinationTreeModel>> proxy = new RpcProxy<BeanModel, List<DestinationTreeModel>>() {
@Override
protected void load(BeanModel loadConfig, AsyncCallback<List<DestinationTreeModel>> callback) {
if (loadConfig != null)
locService.getChildren((DestinationTreeModel
)loadConfig.getBean(), callback);
else
locService.getChildren(null, callback);
}
};
Also I set the TreeStore and TreeBinder to use BeanModel:
TreeStore<BeanModel> store = new TreeStore<BeanModel>(loader);
TreeBinder<BeanModel> binder = new TreeBinder<BeanModel>(tree, store);
DK
Powered by vBulletin® Version 4.1.5 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.