Based on the answer by @dominik I did some improvements, see my Gist. It contains some abstract base classes that can be used for any Modal/PopupView implementation. It's a bit more complex but also cleaner because we don't pass the whole Presenter
to the View
. The interface for the View
to interact with the Presenter
when the modal is closed is HasModalUnbind
.
You would use these classes as follows. Example presenter:
public class ErrorModalPresenter extends ModalPopupPresenter<ErrorModalPresenter.MyView> {
public interface MyView extends ModalPopupView {
DivElement getErrorMessage();
}
private final ErrorEvent error;
@Inject
public ErrorModalPresenter(final EventBus eventBus,
final MyView view,
@Assisted final ErrorEvent error) {
super(eventBus, view);
this.error = error;
}
@Override
public void unbindModal() {
ErrorDismissEvent.fire(this, this);
}
@Override
protected void onBind() {
super.onBind();
//noinspection ThrowableResultOfMethodCallIgnored
getView().getErrorMessage().setInnerText(error.getCause().getMessage());
}
}
Example view:
public class ErrorModalView extends ModalPopupViewImpl implements ErrorModalPresenter.MyView {
@UiField(provided = true)
Modal errorModal;
@UiField
DivElement errorMessage;
interface Binder extends UiBinder<Widget, ErrorModalView> {}
@Inject
public ErrorModalView(final EventBus eventBus,
final Binder uiBinder) {
super(eventBus);
errorModal = initModal();
initWidget(uiBinder.createAndBindUi(this));
}
@Override
public DivElement getErrorMessage() {
return errorMessage;
}
}
And the UiBinder XML just for the record:
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui'
xmlns:b='urn:import:com.github.gwtbootstrap.client.ui'>
<b:Modal ui:field='errorModal' title='Error'>
<g:HTML>
<div ui:field='errorMessage'/>
</g:HTML>
<b:ModalFooter>
<b:Button text='Close' dismiss='MODAL'/>
</b:ModalFooter>
</b:Modal>
</ui:UiBinder>
In unbindModal()
of ErrorModalPresenter
I fire an event which is caught by the parent presenter of ErrorModalPresenter
. There the modal presenter is removed from a container and then unbind()
is called on the presenter. Of course any other solution is possible in unbindModal()
.
The base classes assume that modals are one-shot modals that will be removed once they're hidden. This behaviour can be changed in initModal() of ModalPopupViewImpl
.