package com.ursu.client.presenter;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyEvent;
import com.ursu.client.event.LoginAuthenticatedEvent;
import com.ursu.client.event.LoginAuthenticatedEvent.LoginAuthenticatedHandler;
import com.ursu.client.place.NameTokens;
import com.gwtplatform.mvp.client.annotations.UseGatekeeper;
import com.ursu.client.LoggedInGatekeeper;
import com.ursu.shared.actions.Logout;
import com.ursu.shared.actions.LogoutResult;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.google.inject.Inject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;
public class FirstApplicationPresenter
extends
Presenter<FirstApplicationPresenter.MyView, FirstApplicationPresenter.MyProxy> implements LoginAuthenticatedHandler {
private String username;
private final EventBus eventBus;
public interface MyView extends View {
public Button getLogoutBtn();
}
@ProxyCodeSplit
@NameToken(NameTokens.firstpage)
@UseGatekeeper(LoggedInGatekeeper.class)
public interface MyProxy extends ProxyPlace<FirstApplicationPresenter> {
}
@Inject DispatchAsync dispatchAsync;
@Inject
PlaceManager placeManager;
@Inject
public FirstApplicationPresenter(final EventBus eventBus, final MyView view,
final MyProxy proxy) {
super(eventBus, view, proxy);
this.eventBus = eventBus;
}
@Override
protected void revealInParent() {
RevealRootContentEvent.fire(this, this);
}
@Override
protected void onBind() {
super.onBind();
registerHandler(getView().getLogoutBtn().addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Logout action = new Logout();
dispatchAsync.execute(action, logoutCallback);
}
}));
}
@Override
protected void onReset() {
super.onReset();
getView().getLogoutBtn().setText("Logout "+username);
}
private AsyncCallback<LogoutResult> logoutCallback = new AsyncCallback<LogoutResult>() {
@Override
public void onSuccess(LogoutResult result) {
eventBus.fireEvent(new com.ursu.client.event.LoginAuthenticatedEvent(null));
PlaceRequest request = new PlaceRequest(NameTokens.getLoginpage());
placeManager.revealPlace(request);
}
@Override
public void onFailure(Throwable caught) {
}
};
@ProxyEvent
@Override
public void onLoginAuthenticated(LoginAuthenticatedEvent event) {
if(event.getCurrentUser() != null)
username = event.getCurrentUser().getUsername();
}
}