views:

235

answers:

2

Hi, I try in GWT to create a Tree with multiple selection for the nodes and ran into a problem similar to this question http://stackoverflow.com/questions/1411752/shift-key-in-gwt. When a selectionEvent is raised from the Tree, I would like to know if the Shift key is pressed or not.

SelectionHandler<TreeItem> getSelectionHandler() {
    return new SelectionHandler<TreeItem>(){
        @Override
        public void onSelection(SelectionEvent<TreeItem> event) {
            // is shift key pressed ?
        }
    };
}

The solution in the question above cannot apply in this case as the SelectionHandler class does not inherit from DOMEvent and then does not have a getNativeEvent() function.

I tried a dirty solution by adding keyDownEventHandler and keyUpEventHandler to the Tree with a boolean flag but the handlers are only called when the focus is on the tree so this doesn't work.

Is there a simple solution (or just a solution even if it's not simple) ? Thanks.

Edit on aem response : The solution can work by enclosing the components in a FocusPanel with a keyUp/DownHandler but then I can't add any component needing keyboard input such as TextArea as the "global" handler takes the priority... So it don't really solve my problem.

+1  A: 

I'm not sure whether this will work, but it's worth a try:

What about adding key handlers to the root panel containing the Tree, and have them set a boolean indicating whether the Shift key is down? Then the tree's SelectionHandler can check that boolean.

The trouble with this is that the page might contain other widgets that capture the key events, which would make this behavior seem flaky to the user.

aem
that's one solution I was considering so I will try it...
Vinze
+3  A: 

My suggestion is to create a custom Tree class that temporary store the event and store this event by overriding the onBrowseEvent method. Then you can, in your onSelection method, check if the shift key was pressed by checking this stored event. Since JavaScript is not concurrent it should be no problem using the private variable. The code would be something like this:

public class MyTree extends Tree {
   private Event currentEvent;

   ... constructors...

   // Call this method from within the onSelection method to determine if the shift key
   // was pressed.
   public boolean isShiftPressed() {
      return currentEvent != null ? currentEvent.getShiftKey() : false;
   }

   @Override
   public void onBrowserEvent(Event event) {
     currentEvent = event;
     super.onBrowserEvent(event);
     currentEvent = null;
   }
}
Hilbrand
works perfectly fine, thanks :)
Vinze