Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT - ListBox, how to preview changeEvent

I am working on implementing a ListBox that I want to be able to alert the user when they make a selection in the ListBox. Is there a way to respond to the user clicking an item in the listbox, and respond to the selection before a "changeEvent" occurs so I can prevent the changeEvent from getting fired. I have tried using

Event.addNativePreviewHandler(new NativePreviewHandler() {
   @Override
   public void onPreviewNativeEvent(NativePreviewEvent event) {
      System.out.println("EVENT: " + event.getTypeInt());
   }
});

but this never responds to clicking on a specific element in the ListBox, it only responds to clicking on the ListBox initially, when you focus the ListBox. I want to be able to do something like:

Event.addNativePreviewHandler(new NativePreviewHandler() {
   @Override
   public void onPreviewNativeEvent(NativePreviewEvent event) {
      if(event is changeEvent && event source is listBox) {
          if(do some check here)
             event.stopPropagation();
      }
   }
});

Any suggestions would be greatly appreciated, thanks!

like image 981
Lakota Lefler Avatar asked Sep 25 '12 20:09

Lakota Lefler


2 Answers

You can simply kill the event if you don't want to treat it:

@Override
onChange(ChangeEvent event) {
    // ask user if he wants to continue
    // if yes
       doSomething();
    // if no
       event.kill();
}
like image 68
Adel Boutros Avatar answered Oct 02 '22 23:10

Adel Boutros


Thanks everyone for your input. What I ended up doing was saving the state of the selection when the list box gets focused. And saving this index and switching it when the changeEvent occurs only changing it to the next selection if permitted.

@UiHandler("listBox")
public void onListBoxFocus(FocusEvent event) {
   this.saveListBoxSelection = listBox.getSelectedIndex();
}

@UiHandler("listBox")
public void onChange(ChangeEvent event) {
   int newSelection = listBoxCompliant.getSelectedIndex();
   listBox.setSelectedIndex(this.saveListBoxSelection);
   if(proceed with change)
      listBox.setSelectedIndex(newSelection);
   else 
      //cancel event

This is done using a uibinder, but can also be done by simply adding this events directly to the listBox. Thanks again for the comments!

like image 26
Lakota Lefler Avatar answered Oct 02 '22 22:10

Lakota Lefler