PDA

View Full Version : Adding DoubleClick Listner to child nodes of Tree



saurabhsingh
2 Jul 2009, 12:33 PM
Hi,
I am a newbie to Gxt,
I have created a tree which loads its children onClick of the parent item
I have to add a double click listner to the child nodes now,
The type of the childNode is my own class, to which i am not able to do".addLister()"
if somebaody has any clue how to do it please help. i am stuck with it

markmacumber
2 Jul 2009, 5:48 PM
Hi, I have not done this using the Gxt trees, but we are using the Google FastTree from the incubator project and we have actually done this using the event type of the Dom event that gets fired.

You can 'get' the event by overriding the onBrowserEvent provided by the Widget class, then you can compare it to the Event.ONDBLCLICK constant, and if it matches, do you stuff...

here is the code snippet that we use:


@Override
@SuppressWarnings("fallthrough")
public void onBrowserEvent(Event event) {
event.cancelBubble(true);
event.preventDefault();

int eventType = DOM.eventGetType(event);
int buttons = DOM.eventGetButton(event);
if (eventType == Event.ONDBLCLICK && buttons == Event.BUTTON_LEFT){
//DO YOUR STUFF HERE!!!
}
super.onBrowserEvent(event);
}

Hope that helps...

saurabhsingh
2 Jul 2009, 9:33 PM
Hey ,
Thanks for the reply........
I will try that and see if i can achieve what i want out of it.

Thank you very much!!!!:)

saurabhsingh
3 Jul 2009, 11:38 AM
Hi,
I tried using the onBrowser evnet to handle the events ,
but as i am using the gxt, the onbrowserEvent is not beign recognized?
is it not supported by the Gxt?
the error it shows is"The method onBrowserEvent(Event) is undefined for the type Object"
i am not able to resolve it,
please help....

saurabhsingh
3 Jul 2009, 2:42 PM
to implement this onBrowser evnt , do we need to make our class implement any specific listner?

saurabhsingh
6 Jul 2009, 7:15 AM
Hi,
I am trying to catch the onBrowser event in my code,
I want to capture the doubleclick on any of my treeNodes to process something on it,
I am generating the treeNodes dynamically by onClick event,
so when one clicks on the parent, then only the child gets loaded.
Now i want to capture the onBrowserEvent to do some more processing

Please help , i am not able to get the onBrowserEvent

my code snippet is like this,

public class MyFrame extends Widget implements EntryPoint{

public void onModuleLoad() {

initial();

RootPanel.get().add(flxData);
flxData.setWidget(0,0,myLayout());

} // end of onModuleLoad()


@Override
public void onBrowserEvent(Event be) {
// System.out.println("Double clickicked on "+ be.item.getText());
DOM.eventCancelBubble(be, true);
DOM.eventPreventDefault(be);
int eventType = DOM.eventGetType(be);
if (eventType == Event.ONDBLCLICK){
System.out.println("double clickicked on ");
}
Window.alert("onBrowserEvent invoked");
super.onBrowserEvent(be);
}

public void initial(){
flxData = new FlexTable();

flxTree = new FlexTable();
}

public Widget myLayout(){

HorizontalPanel hpnl= new HorizontalPanel();

ContentPanel east = new ContentPanel();
ContentPanel west = new ContentPanel();

populateTree();
hpnl.add(west);
west.add(scrollPanel);

populateGrid();
hpnl.add(east);
east.add(grid);
}

}

markmacumber
6 Jul 2009, 6:05 PM
what exactly do you mean by "i am not able to get the onBrowserEvent"?

Is the event not firing?

The onBrowserEvent method is being overridden from the Widget com.google.gwt.user.client.ui.Widget GWT class, do the items you are adding dynamically extend Widget?

Can you show me how you are adding the new items?

saurabhsingh
7 Jul 2009, 9:43 AM
Hi,

I was able to catch the onBrowser event, but my problem remains the same even after that.
Actually when the tree initially loads , i have inserted dummy childrens for my initial parent nodes.
Then when i click on any of the parent nodes, i fire onclick event to loads its child using RPC,
and thn i keep adding the childrens to the parent node by onclick event.
I am able to sense the browser event on the dummy childrends i loaded intially, but once the dynamic childrens are loaded i cannot sense them using any listner or onBrowser event?
Please help i am stuck on same scenario again
here is the code snippet, i am using the getTreeModel method to initially load the tree, and thn onClick event to load the childrens...

public class MyFrame extends Widget implements EntryPoint{

public MyFrame(){

dataService = (DataServiceAsync) GWT.create(DataService.class);
ServiceDefTarget endpoint = ( ServiceDefTarget )dataService;
String moduleRelativeURL = GWT.getModuleBaseURL()+"data";
endpoint.setServiceEntryPoint( moduleRelativeURL );

}

public void onModuleLoad() {

initial();

RootPanel.get().add(flxData);
flxData.setWidget(0,0,myLayout());

} // end of onModuleLoad()


public void initial(){
flxData = new FlexTable();

flxTree = new FlexTable();
}

public Widget myLayout(){

HorizontalPanel hpnl= new HorizontalPanel();

ContentPanel east = new ContentPanel();
ContentPanel west = new ContentPanel();

populateTree();
hpnl.add(west);
west.add(scrollPanel);

populateGrid();
hpnl.add(east);
east.add(grid);
}

public void populateTree(){

tree = new Tree(){


@Override
public void onBrowserEvent(Event be) {

int eventType = DOM.eventGetType(be);

if (eventType == Event.ONCLICK){

//tree.fireEvent(eventType);
System.out.println("single clickicked eventtype is: " + eventType );
}
if (eventType == Event.ONDBLCLICK){

//tree.fireEvent(eventType);

System.out.println("double clickicked on eventtype is:" + eventType );
}

super.onBrowserEvent(be);

}
};

tree.getStyle().setLeafIconStyle("myIcon");

tStore.add(getTreeModel(oChildTree), true);
binder = new TreeBinder(tree,tStore);
tree.addListener(Events.OnClick, new Listener<TreeEvent>() {
loadChild();


}
public void loadChild(){
for (int row = 0,y=0 ; itt.hasNext(); row++,y++){


nodeStore.add(new TreeNodes(((UserTree)itt.next()).toString(),oLoadChild.get(row).getItemID(),oLoadChild.get(row).getItemType(),oLoadChild.get(row).getParentID(),oLoadChild.get(row).getIsStructured()));


}// end of iterator


tStore.remove((BaseTreeModel<TreeModel>) mParent,(BaseTreeModel<TreeModel>) treeRoot);
tStore.insert((BaseTreeModel<TreeModel>) mParent,nodeStore, index, true);
tStore.update((BaseTreeModel<TreeModel>) mParent);
}
getTreeModel(List ochildTree) {
root=new TreeNodes("root");

for(int i =0;i<oParentTree.size();i++){
temp=new TreeNodes(oParentTree.get(i).getCatName(),oParentTree.get(i).getCatID(),oParentTree.get(i).getSeqNo());


Iterator itChild = oChildTree.iterator();
for (int row = 0 ; itChild.hasNext(); row++){
try
{
temp.add(new TreeNodes(((UserTree)itChild.next()).toString()));
}
catch (Exception e) {
e.printStackTrace();

}
}

root.add(temp);

}

return root;

markmacumber
7 Jul 2009, 2:36 PM
Is there a reason why you are defining everything in one class which is a Widget AND an Entry Point?

Also, I noticed that you are using the Tree class, this has been deprecated, and you should now use the TreePanel class, http://extjs.com/deploy/gxtdocs/com/extjs/gxt/ui/client/widget/treepanel/TreePanel.html

You can see a good example of how to use the tree panel, (which includes adding children), on the demo page for Ext Gwt 2.0:

http://extjs.com/examples/explorer.html#fasttree

I just looks like what you are doing is overly complicated for what you say it should be doing...

saurabhsingh
20 Jul 2009, 4:00 PM
Hi mark,

I tried the way the fast tree handles the expansion on nodes.
basically i channged my onClick to before expand event: its works fine but the thing is the older node stays there in the tree, if i try to remove it and then on top of it add my node its throws null pointer....
this entire code is inside the BeforeExpand event method and earlier with the onClick event this peice of code used to work fine.
can you please help me with it ,
I am attaching the code snippet, i pass this ritem from the event , its of type TreeItem
and nodeStore is my list of modeldata to be added to the tree
if i delete the tSore.remove stmt , it works good but then the older node stays there
plz help


TreeModel treeRoot = (TreeModel) ritem.getModel();
TreeItem parent = ritem.getParentItem();
TreeModel mParent= (TreeModel)parent.getModel();

tStore.remove((BaseTreeModel<TreeModel>) mParent,(BaseTreeModel<TreeModel>) treeRoot);
tStore.insert((BaseTreeModel<TreeModel>) mParent,nodeStore, index, true);