How to use applications EventBus in any class (not presenters, etc...)

134 views
Skip to first unread message

Tom

unread,
Apr 16, 2014, 10:55:10 AM4/16/14
to gwt-pl...@googlegroups.com
Hello,

currently I'm trying to figure out how to fire an event to the EventBus from any class (not only presenters etc.).
On my presenters firing events to the EventBus works.

I have a written a visitor-class which visits the nodes of a tree.
If a certain condition is matched an event should be fired that I can do an action based on the event (E.g. the ui-representation of the tree should be redrawn or other stuff in my UI).

How do I achieve that?
I've already Injected the EventBus. But when It comes to that point that the event should get fired, the EventBus is null.
So I think it isn't injected properly.

public class Visitor implements Visitor<TreeItem>, HasHandlers {
 
@Inject
 
private static EventBus eventBus;
 
...
 
CustomEvent customEvent = new CustomEvent();

       
 
private void doSomething() {
    fireEvent
(customEvent);
 
}
...


 
@Override
 
public void fireEvent(GwtEvent<?> event) {
      eventBus
.fireEvent(event);
 
}
}


when it comes to eventBus.fireEvent(event); this stacktrace appears:
com.google.gwt.event.shared.UmbrellaException: Exception caught: null
at com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:129)
at com.google.gwt.user.client.ui.Widget.fireEvent(Widget.java:129)
at com.google.gwt.event.dom.client.DomEvent.fireNativeEvent(DomEvent.java:125)
at com.google.gwt.user.client.ui.Widget.onBrowserEvent(Widget.java:177)
at com.google.gwt.user.client.ui.Composite.onBrowserEvent(Composite.java:80)
at com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1467)
at com.google.gwt.user.client.DOM.dispatchEvent(DOM.java:1406)
at com.google.gwt.user.client.impl.DOMImplStandard.dispatchEvent(DOMImplStandard.java:323)
at sun.reflect.GeneratedMethodAccessor70.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:576)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:284)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:347)
at sun.reflect.GeneratedMethodAccessor64.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at d***.Visitor.fireEvent(Visitor.java:216)
at d***.Visitor$4.onDrop(Visitor.java:203)
at com.google.gwt.event.dom.client.DropEvent.dispatch(DropEvent.java:58)
at com.google.gwt.event.dom.client.DropEvent.dispatch(DropEvent.java:1)
at com.google.gwt.event.shared.GwtEvent.dispatch(GwtEvent.java:1)
at com.google.web.bindery.event.shared.EventBus.dispatchEvent(EventBus.java:40)
at com.google.web.bindery.event.shared.SimpleEventBus.doFire(SimpleEventBus.java:193)
at com.google.web.bindery.event.shared.SimpleEventBus.fireEvent(SimpleEventBus.java:88)
at com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:127)
... 31 more


What do I have to do to get this to work? Did I miss something?

Regards
Tom

Clay Harris

unread,
Apr 16, 2014, 10:59:18 AM4/16/14
to gwt-pl...@googlegroups.com
It's not ideal, but I have used the ClientGinjector.getEventBus() in my widgets / other classes to access the eventbus.  This has worked fine, EXCEPT that it's NOT the same instance as is injected in your presenters (Despite the fact that it is bound as a singleton!).  So if you want them talking to each other, you have to use ClientGinjector everywhere.  

If you or someone else finds a solution to inject it properly into widgets though, I would love to fix my implementation!


--
You received this message because you are subscribed to the Google Groups "GWTP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gwt-platform...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian Goudreau

unread,
Apr 16, 2014, 2:31:44 PM4/16/14
to gwt-pl...@googlegroups.com
You need to use Gin functionality: requestStaticInjection. You can use that function in any Gin module
--
Christian Goudreau | CEO - Président
M: 1.877.635.1585 | S: christian.goudreau

Thomas Lang

unread,
Apr 23, 2014, 3:30:18 AM4/23/14
to gwt-pl...@googlegroups.com
Hello Christian,

thank you!
That worked. :-)

Regards
Tom
Reply all
Reply to author
Forward
0 new messages