ChrisSSocha
ChrisSSocha

Reputation: 253

How to implement SelectionEvent in Composite GWT

I am writing a Composite widget in GWT, and want for it to implement HasSelectionHander and fire a SelectionEvent when an element of the composite is selected

So far, I have the following:

public class SelectionClass extends Composite implements HasSelectionHandlers<Integer>{

    private final EventBus eventBus = new SimpleEventBus();

    //...   

    private void somethingHappens(int i){
            //How do i fire an event?
    }       

    @Override
    public HandlerRegistration addSelectionHandler(SelectionHandler<Integer> handler) {
            return eventBus.addHandler(SelectionEvent.getType(), handler);
    }       

}

public AnotherClass{

    // ...  

    SelectionClass.addSelectionHandler(new SelectionHandler<Integer>() {

            @Override
            public void onSelection(SelectionEvent<Integer> event) {
                    Window.alert(String.valueOf(event.getSelectedItem()));
            }       
    });     

    // ...  
}

I am a bit confused about how exactly to implement the firing of the event. Am I right to use an EventBus in the SelectionClass (above). Any help would be appreciated.

Upvotes: 1

Views: 779

Answers (1)

Alonso Dominguez
Alonso Dominguez

Reputation: 7858

The firing of the event is done through the EventBus API, in GWT 2.4 you don't need to instantiate your own instance of the 'EventBus' as you can delegate your addXXXHandler methods to the super Composite class.

It'd be something like the following:

public class SelectionClass extends Composite implements HasSelectionHandlers<Integer>{

    //...   

    private void somethingHappens(int i){
            SelectionEvent.<Integer>fire(this, i);
    }       

    @Override
    public HandlerRegistration addSelectionHandler(SelectionHandler<Integer> handler) {
            return super.addHandler(handler, SelectionEvent.getType());
    }       

}

Upvotes: 2

Related Questions