V
- The View
type.public abstract class PresenterWidget<V extends View> extends HandlerContainerImpl implements HasHandlers, HasSlots, HasPopupSlot, IsWidget
Presenter
class.
Choosing between a Presenter
and PresenterWidget
is a design decision that
requires some thought. For example, a PresenterWidget
is useful when
you need a custom widget with extensive logic. For example, a chat box that can be instantiated
multiple times and needs to communicate with the server would be a good candidate for
a PresenterWidget
. The drawback of a PresenterWidget
is that it is managed by its
parent presenter, which increases coupling. Therefore, you should use a Presenter
when
the parent is not expected to know its child. Moreover, only Presenter
can be attached
to name tokens in order to support browser history.
PresenterWidget
s and Presenter
s are organized in a hierarchy.
Internally, parent presenters have links to their currently attached children presenters. A
parent Presenter
can contain either Presenter
s or PresenterWidget
s,
but a PresenterWidget
can only contain PresenterWidget
s.
To reveal a PresenterWidget
you should insert it within a slot
of its
containing presenter using one of the following methods:
setInSlot(Object, PresenterWidget)
setInSlot(Object, PresenterWidget, boolean)
addToSlot(Object, PresenterWidget)
addToPopupSlot(PresenterWidget)
addToPopupSlot(PresenterWidget, boolean)
Presenter
is done differently, refer to the class documentation for more details.
To hide a PresenterWidget
or a Presenter
you can use setInSlot(java.lang.Object, com.gwtplatform.mvp.client.PresenterWidget<?>)
to place
another presenter in the same slot, or you can call one of the following methods:
removeFromSlot(Object, PresenterWidget)
clearSlot(Object)
PopupView.hide()
if the presenter is a popup or a dialog box.
Presenter
using these methods, but
A presenter has a number of lifecycle methods that you can hook on to:
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 following
a call to setInSlot(Object, PresenterWidget)
:
onHide()
is called on the removed presenter and, recursively,
on its children (bottom-up: first the children, then the parent)Presenter
, it asks to be
set in one of its parent slot by firing a
RevealContentEvent
.
For more details, see the documentation for Presenter
.onReveal()
is called on the PresenterWidget
that
was just added.onReveal()
is called recursively on that presenter's children
(top-down: first the parent, then the children).setInSlot(Object, PresenterWidget, boolean)
was called with false
as the third parameter then the process stops. Otherwise, onReset()
is
called on all the currently visible presenters (top-down: first the parent, then
the children).Constructor and Description |
---|
PresenterWidget(boolean autoBind,
EventBus eventBus,
V view)
Creates a
PresenterWidget that is not necessarily using automatic
binding. |
PresenterWidget(EventBus eventBus,
V view)
Creates a
PresenterWidget that uses automatic binding. |
Modifier and Type | Method and Description |
---|---|
protected <H extends EventHandler> |
addHandler(GwtEvent.Type<H> type,
H handler)
Registers an event handler towards the
EventBus . |
protected <H extends EventHandler> |
addRegisteredHandler(GwtEvent.Type<H> type,
H handler)
Registers an event handler towards the
EventBus and
registers it to be automatically removed when HandlerContainerImpl.unbind()
is called. |
void |
addToPopupSlot(PresenterWidget<? extends PopupView> child)
This method sets some popup content within the
Presenter and
centers it. |
void |
addToPopupSlot(PresenterWidget<? extends PopupView> child,
boolean center)
This method sets some popup content within the
Presenter . |
void |
addToSlot(Object slot,
PresenterWidget<?> content)
This method adds some content in a specific slot of the
Presenter . |
protected <H extends EventHandler> |
addVisibleHandler(GwtEvent.Type<H> type,
H handler)
Registers an event handler towards the
EventBus and
registers it to be only active when the presenter is visible
is called. |
Widget |
asWidget() |
void |
clearSlot(Object slot)
This method clears the content in a specific slot.
|
void |
fireEvent(GwtEvent<?> event) |
protected EventBus |
getEventBus()
Access the
EventBus object associated with that presenter. |
V |
getView()
Returns the
View for the current presenter. |
Widget |
getWidget()
Deprecated.
|
boolean |
isVisible()
Verifies if the presenter is currently visible on the screen.
|
protected void |
onHide()
Lifecycle method called whenever this presenter is about to be
hidden.
|
protected void |
onReset()
Lifecycle method called on all visible presenters whenever a
presenter is revealed anywhere in the presenter hierarchy.
|
protected void |
onReveal()
Lifecycle method called whenever this presenter is about to be
revealed.
|
void |
removeFromPopupSlot(PresenterWidget<? extends PopupView> child)
This method removes popup content within the
Presenter . |
void |
removeFromSlot(Object slot,
PresenterWidget<?> content)
This method removes some content in a specific slot of the
Presenter . |
void |
setInSlot(Object slot,
PresenterWidget<?> content)
This method sets some content in a specific slot of the
Presenter . |
void |
setInSlot(Object slot,
PresenterWidget<?> content,
boolean performReset)
This method sets some content in a specific slot of the
Presenter . |
bind, isBound, onBind, onUnbind, registerHandler, unbind
public PresenterWidget(boolean autoBind, EventBus eventBus, V view)
PresenterWidget
that is not necessarily using automatic
binding. Automatic binding will only work when instantiating this object using
Guice/GIN dependency injection. See
HandlerContainerImpl.HandlerContainerImpl(boolean)
for
more details on automatic binding.public PresenterWidget(EventBus eventBus, V view)
PresenterWidget
that uses automatic binding. This will
only work when instantiating this object using Guice/GIN dependency injection.
See HandlerContainerImpl.HandlerContainerImpl()
for more details on
automatic binding.public void addToPopupSlot(PresenterWidget<? extends PopupView> child)
HasPopupSlot
Presenter
and
centers it. The view associated with the content
's presenter must
inherit from PopupView
. The popup will be visible and the
corresponding presenter will receive the lifecycle events as needed.
Contrary to the View.setInSlot(Object, com.google.gwt.user.client.ui.IsWidget)
method, no ResetPresentersEvent
is fired, so onReset()
is not invoked.addToPopupSlot
in interface HasPopupSlot
child
- The popup child, a PresenterWidget
.HasPopupSlot.addToPopupSlot(PresenterWidget)
public void addToPopupSlot(PresenterWidget<? extends PopupView> child, boolean center)
HasPopupSlot
Presenter
. The view
associated with the content
's presenter must inherit from
PopupView
. The popup will be visible and the corresponding
presenter will receive the lifecycle events as needed.
Contrary to the View.setInSlot(Object, com.google.gwt.user.client.ui.IsWidget)
method, no ResetPresentersEvent
is fired, so onReset()
is not invoked.addToPopupSlot
in interface HasPopupSlot
child
- The popup child, a PresenterWidget
.center
- Pass true
to center the popup, otherwise its position
will not be adjusted.HasPopupSlot.addToPopupSlot(PresenterWidget)
public void removeFromPopupSlot(PresenterWidget<? extends PopupView> child)
HasPopupSlot
Presenter
. The view
associated with the content
's presenter must inherit from PopupView
.removeFromPopupSlot
in interface HasPopupSlot
child
- The popup child, a PresenterWidget
, which has
previously been added using HasPopupSlot.addToPopupSlot(PresenterWidget)
or HasPopupSlot.addToPopupSlot(PresenterWidget, boolean)
public void addToSlot(Object slot, PresenterWidget<?> content)
HasSlots
Presenter
.
The attached View
should manage this slot when its
View#addToSlot(Object, com.google.gwt.user.client.ui.Widget)
is called.
Contrary to the HasSlots.setInSlot(java.lang.Object, com.gwtplatform.mvp.client.PresenterWidget<?>)
method, no
ResetPresentersEvent
is fired,
so onReset()
is not invoked.
For more details on slots, see HasSlots
.addToSlot
in interface HasSlots
slot
- An opaque object identifying which slot this content is being
added into.content
- The content, a PresenterWidget
. Passing null
will not add anything.public void clearSlot(Object slot)
HasSlots
ResetPresentersEvent
is fired.
The attached View
should manage this slot when its
View#setInSlot(Object, com.google.gwt.user.client.ui.Widget)
is called. It should also clear
the slot when the View#setInSlot(Object, com.google.gwt.user.client.ui.Widget)
method is
called with null
as a parameter.
For more details on slots, see HasSlots
.public void fireEvent(GwtEvent<?> event)
fireEvent
in interface HasHandlers
@Deprecated public Widget getWidget()
Widget
object associated with that
presenter.public boolean isVisible()
true
if the presenter is visible, false
otherwise.public void removeFromSlot(Object slot, PresenterWidget<?> content)
HasSlots
Presenter
. No
ResetPresentersEvent
is fired.
The attached View
should manage this slot when its
View#removeFromSlot(Object, com.google.gwt.user.client.ui.Widget)
is called.
For more details on slots, see HasSlots
.removeFromSlot
in interface HasSlots
slot
- An opaque object identifying which slot this content is being
removed from.content
- The content, a PresenterWidget
. Passing null
will not remove anything.public void setInSlot(Object slot, PresenterWidget<?> content)
HasSlots
Presenter
.
A ResetPresentersEvent
will be fired
after the top-most visible presenter is revealed, resulting in a call to
onReset()
.
For more details on slots, see HasSlots
.setInSlot
in interface HasSlots
slot
- An opaque object identifying which slot this content is being
set into. The attached view should know what to do with this slot.content
- The content, a PresenterWidget
. Passing null
will clear the slot.public void setInSlot(Object slot, PresenterWidget<?> content, boolean performReset)
HasSlots
Presenter
.
The attached View
should manage this slot when its
View#setInSlot(Object, com.google.gwt.user.client.ui.Widget)
is called. It should also clear the
slot when the setInSlot
method is called with null
as a
parameter.
For more details on slots, see HasSlots
.setInSlot
in interface HasSlots
slot
- An opaque object identifying which slot this content is being
set into.content
- The content, a PresenterWidget
. Passing null
will clear the slot.performReset
- Pass true
if you want a
ResetPresentersEvent
to be fired
after the content has been added and this presenter is visible, pass
false
otherwise.protected <H extends EventHandler> HandlerRegistration addHandler(GwtEvent.Type<H> type, H handler)
EventBus
.
Use this only in the rare situations where you want to manually
control when the handler is unregistered, otherwise call
addRegisteredHandler(com.google.gwt.event.shared.GwtEvent.Type, EventHandler)
.H
- The handler type.type
- See GwtEvent.Type
.handler
- The handler to register.HandlerRegistration
you should use to unregister the handler.protected <H extends EventHandler> void addRegisteredHandler(GwtEvent.Type<H> type, H handler)
EventBus
and
registers it to be automatically removed when HandlerContainerImpl.unbind()
is called. This is usually the desired behavior, but if you
want to unregister handlers manually use addHandler(com.google.gwt.event.shared.GwtEvent.Type<H>, H)
instead.H
- The handler type.type
- See GwtEvent.Type
.handler
- The handler to register.addHandler(com.google.gwt.event.shared.GwtEvent.Type, EventHandler)
protected <H extends EventHandler> void addVisibleHandler(GwtEvent.Type<H> type, H handler)
EventBus
and
registers it to be only active when the presenter is visible
is called.H
- The handler type.type
- See GwtEvent.Type
.handler
- The handler to register.addRegisteredHandler(com.google.gwt.event.shared.GwtEvent.Type, com.google.gwt.event.shared.EventHandler)
,
addHandler(com.google.gwt.event.shared.GwtEvent.Type, EventHandler)
protected final EventBus getEventBus()
EventBus
object associated with that presenter.
You should not usually use this method to interact with the event bus.
Instead call fireEvent(com.google.gwt.event.shared.GwtEvent<?>)
, addRegisteredHandler(com.google.gwt.event.shared.GwtEvent.Type<H>, H)
or
addHandler(com.google.gwt.event.shared.GwtEvent.Type<H>, H)
.protected void onHide()
onHide()
if
you override. Also, do not call directly, see PresenterWidget
for more details on lifecycle methods.
You should override this method to dispose of any object
created directly or indirectly during the call to onReveal()
.
This method will not be invoked a multiple times without onReveal()
being called.
In a presenter hierarchy, this method is called bottom-up: first on the
child presenters, then on the parent.protected void onReset()
onReset()
if
you override. Also, do not call directly, fire a ResetPresentersEvent
to perform a reset manually. See PresenterWidget
for more details on
lifecycle methods.
This is one of the most frequently used lifecycle method. This is usually a good
place to refresh any information displayed by your presenter.
Note that onReset()
is not called only when using
addToSlot(Object, PresenterWidget)
, addToPopupSlot(PresenterWidget)
or #setInSlot(Object, PresenterWidget, boolean)} with false
as the third
parameter.
In a presenter hierarchy, this method is called top-down: first on the
parent presenters, then on the children.protected void onReveal()
onReveal()
if
you override. Also, do not call directly, see PresenterWidget
for more details on lifecycle methods.
You should override this method to perform any action or initialisation
that needs to be done when the presenter is revealed. Any initialisation
you perform here should be taken down in onHide()
.
Information that needs to be updated whenever the user navigates should
be refreshed in onReset()
.
In a presenter hierarchy, this method is called top-down: first on the
parent presenters, then on the children.Copyright © 2010–2014 Arcbees. All rights reserved.