Is anyone have any idea on how to do this?
My idea is like following:
//define tree
Tree tree = new Tree();
TreeItem root = new TreeItem("root");
tree.addItem(root);
TreeItem one= new TreeItem("1");
root.addITem(one);
TreeItem two= new TreeItem("2");
root.addItem(two);
TreeItem six= new TreeItem("6");
root.addItem(six);
TreeItem one_1= new TreeItem("11");
one.addItem(one_1);
TreeItem one_1_1= new TreeItem("111");
one_1.addItem(one_1_1);
TreeItem one_1_1_1= new TreeItem("1111");
one_1_1.addITem(one_1_1_1);
//add keyboardlistener
tree.addKeyboardListener(new KeyboardListener(){
public void onKeyDown(Widget sender, char keyCode, int modifiers) {
// TODO Auto-generated method stub
}
public void onKeyPress(Widget sender, char keyCode, int modifiers) {
if(Character.isDigit(keycode)){
int key = (int) keycode;
//I've been trying to use KEY_CTRL and
MODIFIER_CTRL but both didnt work.
if(key==KeyboardListener.KEY_CTRL ||
key==KeyboardListener.MODIFIER_CTRL || key==KeyboardListener.KEY_SHIFT
|| key==KeyboardListener.MODIFIER_SHIFT){
}
}
public void onKeyUp(Widget sender, char keyCode, int modifiers) {
// TODO Auto-generated method stub
}
});
/*---------------------------------- PROBLEM -
START---------------------------------*/
public void onKeyPress(Widget sender, char keyCode, int modifiers) {
if(Character.isDigit(keycode)){
int key = (int) keycode;
//I've been trying to use KEY_CTRL and
MODIFIER_CTRL but both didnt work.
if(key==KeyboardListener.KEY_CTRL
|| key==KeyboardListener.MODIFIER_CTRL
|| key==KeyboardListener.KEY_SHIFT
|| key==KeyboardListener.MODIFIER_SHIFT){
tree.addClickListener(new ClickListener(){
//get the Id of the selected tree...
});
}
}
}
/*---------------------------------- PROBLEM - END
---------------------------------*/
public void onKeyUp(Widget sender, char keyCode, int modifiers) {
// TODO Auto-generated method stub
}
});
Thank you in advance.
As for your problem: Looking at the documentation, I found
http://code.google.com/webtoolkit/documentation/com.google.gwt.user.client.DOM.html#eventGetCtrlKey(com.google.gwt.user.client.Event)
This will get you the depressed state of control given an event.
So maybe try extending Tree and overriding the onBrowserEvent method
and inspect the browser event yourself, looking for modifier keys and
firing new event handlers.
Listen for the onKeyUp and onKeyDown events - check the character like
you do in onKeyPress.
public void onKeyDown(Widget w, char key, int mods)
{
if(key == KeyboardListener.KEY_CTRL) enableMultSelect();
}
public void onKeyUp(Widget w, char key, int mods)
{
if(key == KeyboardListener.KEY_CTRL) disableMultSelect();
}
Have a ClickListener wait for click events, then when you get a click
event, check to see if the multiSelect property is enabled/disabled
and do your work from there. If you're still not getting keyboard
events on the tree, then maybe the tree doesn't have keyboard focus.
You can try to get focus on the element or you can look into the
EventPreview class and use DOM.addEventPreview() - you'll get all
keyboard events in the application that way (because you'll see them
at the topmost level before they're propagated). HTH!
-krispy