A singleton presenter, the basic building block of the
model-view-presenter architecture. Each logical page of your application will usually
correspond to a singleton
Presenter. If you need to separate logic from view
in a simple graphical component, you might consider using a
PresenterWidget.
For more details on the hierarchical organization of presenters, see
PresenterWidget.
Each presenter is associated to a
Proxy which is responsible for listening to the
various events of interest for this presenter. This makes it possible to lazily instantiate
the presenter and use GWT code splitting. Proxies are automatically generated for you
based on the information contained in the presenter. For this purpose, your presenter has to
define an embedded interface extending
Proxy or
com.gwtplatform.mvp.client.proxy.ProxyPlace. Then you should apply various
annotations to that interface in order to specify the characteristics of your presenter. For example,
the following code indicates that the presenter will use GWT code splitting and will be
reachable at url
http://mydomain.com#main:
public class MainPagePresenter extends
Presenter<MainPagePresenter.MyView, MainPagePresenter.MyProxy> {
public interface MyView extends View {}
@ProxyCodeSplit
@NameToken("main")
public interface MyProxy extends ProxyPlace<MainPagePresenter> {}
@Inject
public MainPagePresenter(EventBus eventBus, MyView view, MyProxy proxy) {
super(eventBus, view, proxy, Presenter.RevealType.Root);
}
}
One of
com.gwtplatform.mvp.client.annotations.ProxyStandard,
com.gwtplatform.mvp.client.annotations.ProxyCodeSplit or
com.gwtplatform.mvp.client.annotations.ProxyCodeSplitBundlemust always annotate the
Proxy interface.
To reveal a presenter associated to a
com.gwtplatform.mvp.client.proxy.ProxyPlaceyou can simply navigate to an hyperlink corresponding to this place's name token. The
com.gwtplatform.mvp.client.proxy.PlaceManager offers a number of method for building
such hyperlinks. If you want to reveal it programmatically, you should build a
com.gwtplatform.mvp.shared.proxy.PlaceRequest and call one of the
following method:
-
com.gwtplatform.mvp.client.proxy.PlaceManager#revealPlace(com.gwtplatform.mvp.shared.proxy.PlaceRequest)
-
com.gwtplatform.mvp.client.proxy.PlaceManager#revealRelativePlace(com.gwtplatform.mvp.shared.proxy.PlaceRequest)
-
com.gwtplatform.mvp.client.proxy.PlaceManager#revealRelativePlace(com.gwtplatform.mvp.shared.proxy.PlaceRequest,int)
If the presenter is associated to a regular
Proxy and does not have
a name token then you should call the
#forceReveal() method. For such
presenters, it is customary to define an event responsible of revealing them in
order to enforce loose coupling. This event is then handled by the presenter
using the
com.gwtplatform.mvp.client.annotations.ProxyEventmechanism.
If the presenter is revealed and is not currently visible, then its
#revealInParent() method
will be called.
To hide a presenter, you can reveal another one in the same slot or you can use
one of the methods described in
PresenterWidget.
A presenter has a number of lifecycle methods that you can hook on to:
-
#onBind()
-
#onReveal()
-
#onReset()
-
#onHide()
-
#onUnbind()
Revealing or hiding a
PresenterWidget triggers an internal chain of events that result
in these lifecycle methods being called. For an example, here is what happens when a presenter
is revealed (either via
#forceReveal(), or through a
com.gwtplatform.mvp.client.proxy.PlaceManager method):
- The presenter's
#revealInParent() is call and it asks to be set in one of its
parent slot by firing a
com.gwtplatform.mvp.client.proxy.RevealContentEvent
- If a presenter already occupies this slot it is removed.
- If the presenter owning the slot is currently visible then
#onHide() is called on the removed presenter and, recursively,
on its children (bottom-up: first the children, then the parent)
- If the parent is not visible, it asks to be set in one of its parent slot
by firing a
com.gwtplatform.mvp.client.proxy.RevealContentEventtoo, this continues recursively until a visible presenter is reached, or until a presenter fires
com.gwtplatform.mvp.client.proxy.RevealRootContentEventor
com.gwtplatform.mvp.client.proxy.RevealRootLayoutContentEvent
- When the above chain stops,
#onReveal is called on all the presenters
that were traversed. (top down: first the parent, then the children);
- Finally
#onReset() is called on all the
currently visible presenters (top-down: first the parent, then
the children).