-
Notifications
You must be signed in to change notification settings - Fork 2
04. Defining Presenters and Views
Mvp4g2 mainly helps you define 2 types of elements:
- Presenter (or Event Handlers)
- View
This page will help you to understand how to build these different types of elements.
To create a presenter, you need to:
- extend AbstractPresenter<E,V>:
- E: type of your event bus interface.
- V: type of the view that will be injected in the presenter (normally a interface!)
By extending AbstractPresenter, you will have access to the following protected attributes:
- E eventBus: instance of the event bus injected to the presenter
- V view: instance of the view injected to the presenter
You can also override the bind method to bind your view to your presenter.
Mvp4g2 will automatically create an instance for each class annotated with @Presenter or @Handler.
- have a constructor with no parameter
- annotate the presenter with @Presenter and set the attributes 'viewClass' and 'viewInterface'
The 'viewInterface'-attribute will be used inside the presenter as reference for the view, where as the 'viewClass'-attribute will be used to instantiate the view. By default mvp4g2 uses 'new value of the ViewClass-attribute of the presenter annotation()' to create an instance of the view. (GWT-create() will no longer be available in J2CL / GWT 3! And, to be ready for J2CL / GWT 3 we have to avoid using it).
If you prefer to create your view by your own, just set the 'viewCreator'-attribute to
'Presenter.VIEW_CREATION_METHOD.PRESENTER' and implement the IsViewCreator<V>
interface. In this case mvp4g2
expects a 'createView()'-method inside your presenter which returns an instance of the view, which - of course - has to
implement the 'viewInterface'-attribute.
Here is an example of a presenter which does create the view.
@Presenter(viewClass = ListView.class,
viewInterface = IListView.class,
viewCreator = Presenter.VIEW_CREATION_METHOD.PRESENTER)
public class ListPresenter
extends AbstractPresenter<MyApplicationEventBus, IListView>
implements IListView.Presenter,
IsViewCreator<IListView> {
@Override
public IListView createView() {
return new ListView();
}
}
Mvp4g2 will no longer inject services into your presenter!
By default, a presenter will be created as a singleton, which means that you will have one instance of every presenter when the application starts. A multiple feature is available in order to allow developers to have several instances of the same presenter. When the multiple feature is activated for a presenter, all the instances of this presenter will have to be created manually by the developer (which means that no instance is created when the application starts).
To activate the multiple feature, just set the multiple attribute of the @Presenter to "true":
@Presenter(viewClass = OneView.class,
viewInterface = IOneView.class,
multiple=true)
public class OnePresenter
extends AbstractPresenter<IOneView, OneEventBus>
implements IOneView.Presenter {
...
}
When you have activated the multiple feature for a presenter, you can add a new instance to the event bus by create the instance and adding the instnace:
OnePresenter presenter = new Presenter();
PresenterRegistration presenterRegistration = eventBus.addHandler( presenter );
You can also delete an instance of the presenter set as multiple by calling the removeHandler method of the event bus with the instance to remove:
presenterRegistration.remove();
Only presenters set as multiple can be added/removed. If you try to add/remove a presenter not set as multiple, an error will occur at runtime.
Presenters provides a method that will be called right before each event that it needs to handle: onBeforeEvent. The onBeforeEvent-method has one parameter. The parameter contains the name of the event. You can override this method if you need to execute any action before an event is handled.
In some cases, you may want to prevent a presenter to handle events (for example, you don't want a presenter to handle events as long as the user is connected). This can easily be done by overriding the pass method of the presenter. This method is called right before a presenter has to handle an event. If the method returns false, the presenter is considered as deactivated and won't handle the event. This method receives as parameters the event name and the event parameters so that you can block a presenter only for some events.
Presenter(viewClass = OneView.class,
viewInterface = IOneView.class,
multiple=true)
public class OnePresenter
extends AbstractPresenter<IOneView, OneEventBus>
implements IOneView.Presenter {
public boolean pass(String eventName,
Object... parameters ) {
if (!isUserLoggedIn()) {
return false;
} else {
return true;
}
}
}
Inside your application you will have one sShell. That's the visiblearea of the application on your screen. Normally you devide this area into header, navigation, footer and content area. Mvp4g2 expects one presenter which is defined as ShellPresenter by adding this presenter to the shell-attribute of the EventBus-annotation.
@EventBus(shell = ShellPresenter.class)
public interface Mvp4g2MailEventBus
extends IsEventBus {
}
The ShellPresenter behaves like a normal presenter but implements also the IsShell
-interface.
@Presenter(viewClass = ShellView.class,
viewInterface = IShellView.class)
public class ShellPresenter
extends AbstractPresenter<Mvp4g2MailEventBus,
IShellView>
implements IShellView.Presenter,
IsShell<Mvp4g2MailEventBus,
IShellView> {
The``ÌsShell````interface expect two generics:
- event bus
- the shell interface.
and forces the presenter to implement the
etShell
method:
@Override
public void setShell() {
// add the shell to the browser viewport (here GWT 2.x)
RootPanel.get().add(view.asWidget());
}
An event handler is a presenter with no view. It can be used the same way as a presenter and has the same properties (except for the view).
To create an event handler, you need to:
- extend AbstractHandler:
- E: type of your event bus interface.
By extending AbstractHandler, you will have access to the following protected attributes:
- E eventBus: instance of the event bus injected to the presenter
Mvp4g2 forces you to use the Reverse View / View Delegate pattern.
To create a view, all you need to do is to create an interface which extends IsLazyReverseView<P>
and create a class,
which extends the LazyReverseView<P>
class and implements the view interface.
For example the interface for the shell view looks like this:
public interface IShellView
extends IsLazyReverseView<IShellView.Presenter>,
IsWidget {
...
interface Presenter {
...
}
}
and the view class like that:
public class ShellView
extends LazyReverseView<IShellView.Presenter>
implements IShellView {
...
}
The framework will create the view, the first time a event is handled by the corresponding presenter.
The following steps will be executed by the framework, once a view is created:
- create the view
- inject the instance of the presenter into the view
- call the method
createView()
- call hte method
bind()
This method is a good place to implement the layout of the view. A view has to implement the createView-method.
The bind-method is called after the createView-method. In case the widgets are created in the createView-method, is this a good place to bind the handlers. The bind()-method is optional.