Hi Raju,
I'm sorry, I haven't read your code entirely.
But, here is the way I do it, with an exemple of a tree containing
"QueryDesc" item (local business class, dont look for this in GWT doc),
which can be replaced by any of your classes.
//new tree
final Tree Queries = new Tree();
//declare event listener
Queries.addTreeListener(new TreeListener() {
//when a tree item is selected
public void onTreeItemSelected(final TreeItem item) {
QueryDesc qd;
try {
//retrieve "UserObject" associated with this item (here,
a QUeryDesc object)
qd = (QueryDesc) item.getUserObject();
} catch (Exception e) {
qd = null;
}
if (qd != null) {
showQueryResults(qd, 0, 20);
} else {
item.setState(!item.getState());
}
}
//Event launched for all of items seeing their status change ;
in that case, the old selected item when a new one is selected is target of
this function
public void onTreeItemStateChanged(final TreeItem item) {
}
});
//creating two main banches for this tree
generalQueries = new TreeItem("General");
personalizedQueries = new TreeItem("Your queries");
Queries.addItem(generalQueries);
Queries.addItem(personalizedQueries);
Now, the asynchronous load of the branches content :
private void updateGeneralQueries() {
Server.Util.getInstance().getGeneralQueries(sid,
new AsyncCallback<ArrayList<QueryDesc>>() {
public void onSuccess(ArrayList<QueryDesc> result) {
ArrayList<QueryDesc> queries = result;
//get the general queries branch in the tree and
free it
getGeneralQueries().removeItems();
int i = 0;
QueryDesc query;//user data
TreeItem t;
for (i = 0; i < queries.size(); i++) {
query = (QueryDesc) queries.get(i);
t = new TreeItem(query.getLibelle());
//add user data to the item
t.setUserObject(query);
//add item to the branch
getGeneralQueries().addItem(t);
}
}
public void onFailure(Throwable caught) {
try {
throw caught;
} catch (SessionExpiredException e) {
Window.alert("Your session is expired");
disconnect();
} catch (Throwable e) {
// last resort -- a very unexpected exception
}
}
});
}
And it works fine for me...
Hope this could help you.
Regards,
Damien
2009/2/9 Raju <[email protected]>
>
> ha,
> I am very new to GWT.When doing my application I need add to listener
> to my tree .But I am unable to do this .
> Any one can help me
> Here my code:
>
>
> /*
> * TreeMainEntryPoint.java
> *
> * Created on February 5, 2009, 2:36 PM
> *
> * To change this template, choose Tools | Template Manager
> * and open the template in the editor.
> */
>
> package org.karishma.client;
>
> import com.google.gwt.core.client.EntryPoint;
> import com.google.gwt.core.client.GWT;
> import com.google.gwt.user.client.Window;
> import com.google.gwt.user.client.rpc.AsyncCallback;
> import com.google.gwt.user.client.rpc.ServiceDefTarget;
>
> import com.google.gwt.user.client.ui.RootPanel;
> import com.google.gwt.user.client.ui.Tree;
> import com.google.gwt.user.client.ui.TreeItem;
> import com.gwtext.client.widgets.Panel;
> import java.util.ArrayList;
> import java.util.Iterator;
> import java.util.ListIterator;
> /**
> *
> * @author veerraju
> */
> public class TreeMainEntryPoint implements EntryPoint {
> public Tree tree ;
> /**
> The entry point method, called automatically by loading a
> module
> that declares an implementing class as an entry-point
> */
> public void onModuleLoad() {
> tree = new Tree();
> tree.setWidth("10px");
> Panel p1=new Panel();
> p1.add(tree);
> p1.setTitle("Tree Structure");
> p1.setBorder(true);
> p1.setPaddings(5);
> p1.setSize(250,500);
> p1.setAutoScroll(true);
> RootPanel.get().add(p1);
> // Create an asynchronous callback to handle the result.
> final AsyncCallback callback = new AsyncCallback() {
> public void onSuccess(Object result) {
> ArrayList pal1=(ArrayList)result;
> ListIterator li1=pal1.listIterator();
> while(li1.hasNext()){
> String code=(String)li1.next();
> String name=(String)li1.next();
> String parent=(String)li1.next();
> if(parent.equals("")){
> String count=getChildCount(pal1,code);
> Window.alert(count);
> if(Integer.parseInt(count)>0)
> {
> TreeItem i1=new TreeItem(name);
> Window.alert(name);
> getSubNodes(i1,pal1,code);
> tree.addItem(i1);
> }
> else
> {
> tree.addItem(name);
> }
> }
> }
> }
> public void onFailure(Throwable caught) {
> Window.alert("Communication failed....");
> }
> public String getChildCount(ArrayList a,String parent)
> {
> int i=0;
> Iterator j=a.iterator();
> while(j.hasNext())
> {
> String code=(String)j.next();
> String name=(String)j.next();
> String parent1=(String)j.next();
> if(parent1.equals(parent)) i=i+1;
> }
>
> return new Integer(i).toString();
> }
> public void getSubNodes(TreeItem i1,ArrayList a,String
> parent)
> {
>
> ListIterator j=a.listIterator();
> while(j.hasNext())
> {
> String code=(String)j.next();
> String name=(String)j.next();
> String parent1=(String)j.next();
> if(parent1.equals(parent)){
> String count=getChildCount(a,code);
> if(Integer.parseInt(count)>0)
> {
> TreeItem i2=new TreeItem(name);
> Window.alert(i2.toString());
> getSubNodes(i2,a,code);
> i1.addItem(i2);
>
> }
> else
> {
> i1.addItem(name);
> }
> }
> }
>
> }
>
> };
>
> getService().getTree(callback);
>
> }
> public static TreeServiceAsync getService(){
> // Create the client proxy. Note that although you are
> creating the
> // service interface proper, you cast the result to the
> asynchronous
> // version of
> // the interface. The cast is always safe because the
> generated proxy
> // implements the asynchronous interface automatically.
> TreeServiceAsync service = (TreeServiceAsync) GWT.create
> (TreeService.class);
> // Specify the URL at which our service implementation is
> running.
> // Note that the target URL must reside on the same domain and
> port from
> // which the host page was served.
> //
> ServiceDefTarget endpoint = (ServiceDefTarget) service;
> String moduleRelativeURL = GWT.getModuleBaseURL() +
> "treeservice";
> endpoint.setServiceEntryPoint(moduleRelativeURL);
> return service;
> }
>
>
>
> }
>
>
>
> thanks in advance
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---