1

I can't make a custom event be received in the Activity. Can anyone please tell me what am I missing? I am using GWT 2.1, MVP pattern and UiBinder.

Here's a sample of what I wrote:

Let's say I have MyCustomEvent class and its handler interface MyCustomEventHandler with its onMyCustomEvent(MyCustomEvent event) method.

I implement the handler interface in the Activity:

    class MyActivity extends AbstractActivity implements MyCustomEventHandler {
    ....

      public void onMyCustomEvent(MyCustomEvent event) {
        doWhatYouKnow();

      }

      //EventBus is injected with GIN
       public void start(AcceptsOneWidget container, EventBus eventBus) {


        ...         
        eventBus.addHandler(MyEvent.TYPE, this);


    }    



}

Now, the sending part in the view:

    public class MyWidget extends Composite {

    final PopUpPanel myPopUp;

        public MyWidget() {
             initWidget(uiBinder.createAndBindUi(this));

             myPopUp.addCloseHandler(new CloseHandler<PopupPanel>() {

            @Override
            public void onClose(CloseEvent<PopupPanel> event) {

                       MyEvent event = new MyEvent();
               fireEvent(event);

            }

        });

         }

}

No exception are thrown and unfortunately onMyCustomEvent is never called in the MyActivity class. Any idea? Thanks a million.

Christian Achilli
  • 5,547
  • 2
  • 25
  • 24

2 Answers2

0

@MyWidget

you can make the constructor take parameter ( eventBus ) which you can pass this from class MyActivity

so when you fire the event @MyActivity the action will be executed @MyWidget

try this , i think it will work .

ahmed Shoeib
  • 254
  • 6
  • 22
  • Thank, your approach is also explained [here](http://stackoverflow.com/questions/3946242/passing-an-object-to-the-constructor-of-a-widget-defined-in-uibinder). But As far as my understanding, I don't understand why my approach doesn't work. – Christian Achilli Mar 21 '11 at 13:36
  • I did some more debug. The outcome of which appears weird to me. The eventBus implementation passed in start() method is not the same that is injected. Infact i inject a SimpleEventBus but in start() method a ResettableEventBus is used instead. I moved the eventBus.addHandler(MyEvent.TYPE, this); to the Activity constructor, which takes the eventBus as parameter. This assure the eventBus object is the same that ships with onModuleLoad(). Nevertheless, the receiver still remain untriggered though! I really can't get this done. – Christian Achilli Mar 21 '11 at 13:51
0

I think one of your comments is pointing you in the right direction here. What I'm going to guess is going on is that you have more than one EventBus floating around (there should usually only be one event bus per application).

First of all, make sure the EventBus in your Gin module is bound in the Singleton scope. Also, make sure this is the event bus that you pass in to your PlaceController, and not one you're constructing on your own.

Also, I wouldn't be too worried about the fact that your object is a ResettableEventBus in one place. I believe that's just an object that's created by the Activities/Places framework that just wraps the EventBus object you give it.

Paul Blessing
  • 3,815
  • 2
  • 24
  • 25
  • Thanks for your comment. Actually everything is in place as you described. I Found a workaround to solve my problem, leaving out the custom event creation approach: I found out that I could exploit a built in event from the widget reaching out the same solution. I also admit that I am a GWT newbie, may be I am missing some other part around. – Christian Achilli Mar 22 '11 at 08:42