Hi,
I solved this problem with the following approach:
1) Create an ActivityFactory superclass.
public abstract class ActivityFactory<P extends Place, V> {
protected abstract Activity create(EventBus eventBus, V view, P
place);
protected ActivityFactory(final EventBus eventBus, final V view) {
eventBus.addHandler(ActivityRequestEvent.TYPE, new
ActivityRequestEvent.Handler() {
@Override
protected Activity createActivity(Place place) {
try {
return create(eventBus, view, (P)
place);
} catch (ClassCastException e) {
return null;
}
}
});
}
}
2) ActivityFactories will listen to the EventBus for
ActivityRequestEvents.
public class ActivityRequestEvent extends
GwtEvent<ActivityRequestEvent.Handler> {
public abstract static class Handler implements EventHandler {
private void onEvent(ActivityRequestEvent e) {
if (!e.isLive())
GWT.log(ActivityRequestEvent.class.getName() +
" ignored");
else {
e.activity = createActivity(e.place);
if (e.activity!=null)
e.kill();
}
}
protected abstract Activity createActivity(Place place);
}
public final Place place;
private Activity activity;
public ActivityRequestEvent(Place place) {
this.place = place;
}
public Activity getActivity() {
return activity;
}
public static final Type TYPE = new Type();
@Override
public Type getAssociatedType() {
return TYPE;
}
@Override
protected void dispatch(Handler handler) {
handler.onEvent(this);
}
@Override
protected void revive() {
if (activity!=null)
throw new IllegalStateException("Activity already
resolved, cannot
revive this event!");
super.revive();
}
}
3) Every Activity must provide an inner Factory extending
ActivityFactory. Doing so, every Activity will have an ActivityFactory
listening to ActivityRequestEvents on EventBus.
public class SearchContactsActivity extends AbstractActivity {
private final EventBus eventBus;
private final SearchContactsView view;
private final SearchContactsPlace place;
public SearchContactsActivity(EventBus eventBus, SearchContactsView
view, SearchContactsPlace place) {
this.eventBus = eventBus;
this.view = view;
this.place = place;
}
@Override
public void start(final AcceptsOneWidget panel, EventBus eventBus) {
// do something
}
public static class Factory extends
ActivityFactory<SearchContactsPlace, SearchContactsView> {
@Inject
public Factory(EventBus eventBus, SearchContactsView view) {
super(eventBus, view);
}
@Override
protected Activity create(EventBus eventBus, SearchContactsView
view, SearchContactsPlace place) {
return new SearchContactsActivity(eventBus, view,
place);
}
}
}
4) At GIN Module, ActivityFactories will be created and passed as
arguments within an ActivityManager singleton call.
Additionally, ActivityMapper fires new ActivityRequestEvent(place) to
the eventBus. If any factory answers this event, ActivityMapper will
have the activity it shold return in its getActivity(place) call.
public class Experience21Module extends AbstractGinModule {
@Override
protected void configure() {
}
@Provides @Singleton
public EventBus eventBus() {
return new SimpleEventBus();
}
@Provides @Singleton
public PlaceController placeController(EventBus eventBus) {
return new PlaceController(eventBus);
}
@Provides @Singleton
public ActivityManager activityManager(final EventBus eventBus,
SearchContactsActivity.Factory searchActivityFactory,
EditContactActivity.Factory editActivityFactory) {
return new ActivityManager(new ActivityMapper() {
@Override
public final Activity getActivity(Place place) {
ActivityRequestEvent event = new
ActivityRequestEvent(place);
eventBus.fireEvent(event);
return event.getActivity();
}
}, eventBus);
}
@WithTokenizers({
SearchContactsPlace.Tokenizer.class,
EditContactPlace.Tokenizer.class
})
interface ContactsHistoryMapper extends PlaceHistoryMapper { }
@Provides @Singleton @Named("contactsHistoryHandler")
public PlaceHistoryHandler contactsHistoryHandler(EventBus eventBus,
PlaceController placeController) {
ContactsHistoryMapper contactsMapper =
GWT.create(ContactsHistoryMapper.class);
PlaceHistoryHandler handler = new
PlaceHistoryHandler(contactsMapper);
handler.register(placeController, eventBus, new
SearchContactsPlace(""));
return handler;
}
}
I would apreciate suggestions and evaluations for this approach.
Regards,
Fábio Miranda.
On 19 nov, 10:01, Brian Reilly <[email protected]> wrote:
> This is going to be a recurring question here and the source of much
> frustration for anyone who doesn't ask or read this group. It would be nice
> if someone would update that piece of the documentation.
>
> -Brian
>
> On Tue, Nov 16, 2010 at 10:35 PM, Ashton Thomas
> <[email protected]>wrote:
>
> >http://groups.google.com/group/google-web-toolkit/browse_thread/threa...
>
> > "Unfortunately, the mere mention of a need for something does not
> > imply
> > its current availability :-) I wrote the Activities and Places doc and
> > really should have left GIN out of it for the time being."
>
> > On Nov 16, 10:10 pm, zixzigma <[email protected]> wrote:
> > > from GWT MVP documentation for Activity Mapper:
>
> > > "[ActivityMapper] ....will likely have lots of code like "if (place
> > > instanceof SomePlace) return new SomeActivity(place)". A better way to
> > > implement the chain of nested ifs would be with a GIN module."
>
> > > Can someone please provide some tips on how GIN injection can replace
> > > the nested IFs ?
>
> > > I am familiar with GIN and have set it up, and use it, but have no
> > > idea how it can be used in this case ?
>
> > >http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAnd...
>
> > > Thank You
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<google-web-toolkit%2Bunsubs
> > [email protected]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/google-web-toolkit?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.