How to implement multiple views for the same presenter? (continued)

322 views
Skip to first unread message

Dennis

unread,
Jan 13, 2011, 4:17:29 AM1/13/11
to Mvp4g
Hi,

In http://groups.google.com/group/mvp4g/browse_thread/thread/af132bbab8459bc9/
an approach is described how to implement multiple views for a single
presenter. Unfortunately I cannot reply to this thread anymore, that's
why I am opening a new one.
I am wondering to what the 'view' attribute of the @Presenter
annotation should be set in this case?

thanks,
Dennis

Pierre

unread,
Jan 13, 2011, 8:59:27 AM1/13/11
to Mvp4g
Hi Dennis,

If you have the same scenario as the previous thread, there is
actually another way to do it by using GIN configuration instead of
Mvp4g to inject your views.

//don't use Mvp4g configuration
@EventHandler
public class OnePresenter extends
BasePresenter<OnePresenter.IOneView, ...>{

public interface IOneView {...}

//use GIN configuration
@Inject
public OnePresenter(IOneView view){
this.view = view;
}

...
}

public class GinModule1 extends AbstractGinModule {

@Override
protected void configure() {
bind( IOneView.class ).to( IOneViewImpl1.class );
}

...

}

public class GinModule2 extends AbstractGinModule {

@Override
protected void configure() {
bind( IOneView.class ).to( IOneViewImpl2.class );
}

...

}

public interface DefaultEventBus ... {
//here you define your events
}

@Events( ..., ginModules = OneGinModule.class, module =
Mvp4gModule1.class )
public interface EventBus1 extends DefaultEventBus ... {
//no code needed here
}

@Events( ..., ginModules = AnotherGinModule.class, module =
Mvp4gModule2.class )
public interface EventBus2 extends DefaultEventBus ... {
//no code needed here
}

and then in your entry point, you can decide which module you want to
start:

public class Mvp4gModules implements EntryPoint {

/**
* This is the entry point method.
*/
public void onModuleLoad() {
Mvp4gModule module;
if(...){
module = (Mvp4gModule)GWT.create( Mvp4gModule1.class );
else {
module = (Mvp4gModule)GWT.create( Mvp4gModule2.class );
}
module.createAndStartModule();
RootPanel.get().add( (Widget)module.getStartView() );
}

}

The 2 modules are exactly the same except the views injected are
different.

Hope this help,
Pierre


On Jan 13, 4:17 am, Dennis <googelyb...@gmail.com> wrote:
> Hi,
>
> Inhttp://groups.google.com/group/mvp4g/browse_thread/thread/af132bbab84...

Dennis

unread,
Jan 14, 2011, 7:38:25 AM1/14/11
to Mvp4g
Hi Pierre,

thank you very much for your detailled explanation. Unfortunately I
don't know GIN (yet - I wanted to check it out long time ago) - if I
have some spare time this weekend I'll finally have a look at it.

This is certainly a more proper solution than what I am currently
doing: There are 2 versions of a login screen, I created 2 pairs of
views and presenters for them, but initially always the first login
presenter is called via a 'showLoginScreen1' event. If this presenter
detectes that actually login screen 2 should be shown it just calls an
event on the 2nd presenter.

thanks again,
Dennis

Pierre Coirier

unread,
Mar 7, 2011, 6:48:07 PM3/7/11
to Mvp4g
As Roy noticed it, there is an error in the above code. In @Events,
OneGinModule should be GinModule1 and AnotherGinModule should be
GinModule2:

@Events( ..., ginModules = GinModule1.class, module =
Mvp4gModule1.class )
public interface EventBus1 extends DefaultEventBus ... {
//no code needed here
}

@Events( ..., ginModules = GinModule2.class, module =
Mvp4gModule2.class )
public interface EventBus2 extends DefaultEventBus ... {
//no code needed here
}

Pierre

yvesb

unread,
Mar 21, 2011, 6:17:15 AM3/21/11
to Mvp4g
Hello Pierre,
When I try the above, I get this during compilation:
[ERROR] com....client.EventBus2: There is no instance of
com....client.IOneView2. Have you forgotten to inject it to a
presenter?

How can I actually link the presenter to one of the viewer?

Thanks for your help
Yves

Pierre Coirier

unread,
Mar 21, 2011, 8:23:50 AM3/21/11
to Mvp4g
Hi Yves,

Is IOneView2 your start view? There is one limitation is that the
start view still needs to be managed directly by Mvp4g without GIN:

@Presenter(view = StartView1.class)
public StartPresenter1 extends BasePresenter<IStartView, OneEventBus>
{

public interface IStartView1 ... {

}

}

@Presenter(view = StartView2.class)
public StartPresenter2 extends StartPresenter1 {
...
}

@Events( startView = StartView1.class, ginModules = GinModule1.class,
module = Mvp4gModule1.class )
public interface EventBus1 extends DefaultEventBus ... {
//no code needed here
}


@Events( startView = StartView2.class, ginModules = GinModule2.class,
module = Mvp4gModule2.class )
public interface EventBus2 extends DefaultEventBus ... {
//no code needed here
}


Pierre

> > Pierre- Hide quoted text -
>
> - Show quoted text -

yvesb

unread,
Mar 21, 2011, 1:02:44 PM3/21/11
to Mvp4g
Thanks Pierre,

I was able to do all that.
But it seems to me that, when using the 2nd presenter, both views are
created (StartView1 and StartView2).

Yves

Pierre Coirier

unread,
Mar 21, 2011, 6:39:42 PM3/21/11
to Mvp4g
Hi Yves,

I think I missed something this morning. You probably have events
handled by StartView and have something like that:

public interface DefaultEventBus ... {

@Event(handler=StartPresenter1.class)
void oneEvent();

}

@Events( startView = StartView2.class, ginModules = GinModule2.class,
module = Mvp4gModule2.class )
public interface EventBus2 extends DefaultEventBus ... {
//no code needed here
}

In this case, Mvp4g will create StartPresenter1 because it handles an
event and StartPresenter2 because its views is the start view.

To solve this issue, you have to override any event handled by
StartPresenter1:

@Events( startView = StartView2.class, ginModules = GinModule2.class,
module = Mvp4gModule2.class )
public interface EventBus2 extends DefaultEventBus ... {

//override any event handled by StartPresenter1
@Event(handler=StartPresenter2.class)
void oneEvent();

}

I have to admit this is not very convenient.

I haven't tried it but to have multiple views for the same presenter,
there might be another way that is a little bit different but may make
more sense for Mvp4g.

The idea is to configure Mvp4g to inject view interfaces (instead of
view implementation) and then use GIN to set this implementation.

Compared to the previous solution, the main difference would be:

//use Mvp4g configuration but inject the view interface
@Presenter(View = IOneView.class)
public class OnePresenter extends
BasePresenter<OnePresenter.IOneView, ...>{


public interface IOneView {...}


//No need for GIN injection


}

//Create GIN module to set the implementation like before:
public class GinModule1 extends AbstractGinModule {

@Override
protected void configure() {
bind( IOneView.class ).to( IOneViewImpl1.class );
}
...

}


public class GinModule2 extends AbstractGinModule {

@Override
protected void configure() {
bind( IOneView.class ).to( IOneViewImpl2.class );
}
...

}


//You will still have to define 2 event bus but you can set the same
view interface for both event bus.
@Events( startView = IStartView.class, ginModules = GinModule1.class,
module = Mvp4gModule1.class )
public interface EventBus1 extends DefaultEventBus ... {
//no code needed here
}

@Events( startView = IStartView.class, ginModules = GinModule2.class,
module = Mvp4gModule2.class )
public interface EventBus2 extends DefaultEventBus ... {
//no code needed here
}


This way should solve the start view issue and you should have access
to all Mvp4g features (including ReverseView).

Pierre
> > > - Show quoted text -- Hide quoted text -

yvesb

unread,
Mar 22, 2011, 8:00:44 AM3/22/11
to Mvp4g
Great!
This is perfect, Pierre.

Thanks for your time
Yves

Bill Bruyn

unread,
Jun 7, 2011, 4:15:03 PM6/7/11
to mv...@googlegroups.com
This actually doesn't seem to work with ReverseView - it doesn't seem that setPresenter ever gets called.

Pierre Coirier

unread,
Jun 8, 2011, 8:54:56 AM6/8/11
to Mvp4g
Hi Bill,

Do you use the last solution I described? Also do you have the your
view interface extends the ReverseView inteface (ie in the example,
IOneView must extends ReverseViewInterface)?

Pierre

Bill Bruyn

unread,
Jun 8, 2011, 1:38:50 PM6/8/11
to Mvp4g
My mistake. I had the implementation (OneViewImpl) extending
ReverseViewInterface. It does in fact work as advertised.

Apologies for the noise.
Reply all
Reply to author
Forward
0 new messages