Using MVP with GWT2.7

119 views
Skip to first unread message

Richa Kulkarni

unread,
Jan 5, 2016, 4:47:12 PM1/5/16
to GWT Users
Is it mandatory to have a separate Handler and Event class for every event while trying t use the event bus? Can i not use a single class for all the events?
How can I do this? Any sample code will be greatly appreciated.

TIA.

N Troncoso

unread,
Jan 5, 2016, 4:55:35 PM1/5/16
to GWT Users
You could technically do that, but you'd need some way to differentiate between events; Maybe with an enum:

public class MyEvent extends GwtEvent<MyEvent.Handler>
{
    public static enum EventTypes
    {
        THIS_EVENT,
        THAT_EVENT;
    };
    
    public interface Handler extends EventHandler
    {
        void onDispatch(MyEvent event);
    }

    public static final GwtEvent.Type<Handler> TYPE = new GwtEvent.Type<>(); // NOSONAR

    private final EventTypes type;
    public MyEvent(EventTypes type)
    {
        this.type = type;
    }

    @Override
    public GwtEvent.Type<Handler> getAssociatedType()
    {
        return TYPE;
    }
    
    public EventTypes getType()
    {
        return type;
    }

    @Override
    protected void dispatch(Handler handler)
    {
        handler.onDispatch(this);
    }
}

I don't think this is a very good approach, though, as you lose the ability to pass specific parameters based on that particular kind of event.

Richa

unread,
Jan 5, 2016, 5:16:37 PM1/5/16
to google-we...@googlegroups.com
Thanks Troncoso.
I get what you say, and agree that its not a very good approach.
But my concern is, For a huge application having around 200 events . Will I have to write 200 Event handlers, 200 Event Classes and Event types ?

--
You received this message because you are subscribed to a topic in the Google Groups "GWT Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-web-toolkit/l0aEL5fsAok/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

Juan Pablo Gardella

unread,
Jan 5, 2016, 5:42:55 PM1/5/16
to google-we...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.

Collins, Pablo

unread,
Jan 5, 2016, 5:43:39 PM1/5/16
to google-we...@googlegroups.com
Hi Richa,

Have you looked at EventBinder?


Pablo

You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.

Jens

unread,
Jan 5, 2016, 6:05:46 PM1/5/16
to GWT Users
I get what you say, and agree that its not a very good approach.
But my concern is, For a huge application having around 200 events . Will I have to write 200 Event handlers, 200 Event Classes and Event types ?

There is https://github.com/google/gwteventbinder which avoids defining handlers by using annotations. Then there is https://github.com/ekuefler/gwt-supereventbus which pretty much works the same as Guava's EventBus in terms of event types and listening for events. It also has some other interesting features and provides an adapter so you can use SuperEventBus together with GWT classes that require GWT's EventBus.

-- J.

Frank Hossfeld

unread,
Jan 6, 2016, 2:21:37 AM1/6/16
to GWT Users
You can try mvp4g. (http://mvp4g.github.io/mvp4g/) Using mvp4g, the events are defined in a interface.  

Gourab Panda

unread,
Jan 10, 2016, 4:26:24 AM1/10/16
to google-we...@googlegroups.com

Improvising Noah code to send different Data as well.

public class MyEvent<T> extends GwtEvent<MyEvent.Handler>

{

    public static enum EventTypes

    {

        THIS_EVENT,

        THAT_EVENT;

    };

    

    public interface Handler<T> extends EventHandler

    {

        void onDispatch(MyEvent<T> event);

    }


    public static final GwtEvent.Type<Handler> TYPE = new GwtEvent.Type<>(); // NOSONAR


    private final EventTypes type;


    private T result;

    

    public MyEvent(EventTypes type, T result)

    {

        this.type = type;

        this.result = result;

    }


    @Override

    public GwtEvent.Type<Handler> getAssociatedType()

    {

        return TYPE;

    }

    

    public EventTypes getType()

    {

        return type;

    }


    @Override

    protected void dispatch(Handler handler)

    {

        handler.onDispatch(this);

    }

    /**

     *

     * @return result

     */

    public T getResult() {

        return result;

    }


}


Reply all
Reply to author
Forward
0 new messages