/**
* Copyright 2010 Philippe Beaudoin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.philbeaudoin.gwtpsample.client;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.inject.Inject;
import com.philbeaudoin.gwtp.mvp.client.EventBus;
import com.philbeaudoin.gwtp.mvp.client.PresenterImpl;
import com.philbeaudoin.gwtp.mvp.client.View;
import com.philbeaudoin.gwtp.mvp.client.proxy.Place;
import com.philbeaudoin.gwtp.mvp.client.proxy.PlaceManager;
import com.philbeaudoin.gwtp.mvp.client.proxy.PlaceRequest;
import com.philbeaudoin.gwtp.mvp.client.proxy.Proxy;
import com.philbeaudoin.gwtp.mvp.client.proxy.RevealRootContentEvent;
import com.philbeaudoin.gwtp.mvp.client.annotations.NameToken;
import com.philbeaudoin.gwtp.mvp.client.annotations.ProxyStandard;
import com.philbeaudoin.gwtpsample.shared.FieldVerifier;
public class MainPagePresenter
extends PresenterImpl<MainPagePresenter.MyView, MainPagePresenter.MyProxy>{
public static final String nameToken = "main";
public interface MyView extends View {
public Button getSendButton();
public void setError(String errorText);
public String getName();
public void resetAndFocus();
}
@ProxyStandard
@NameToken(nameToken)
public interface MyProxy extends Proxy<MainPagePresenter>, Place {}
private final PlaceManager placeManager;
@Inject
public MainPagePresenter(EventBus eventBus, MyView view, MyProxy proxy, PlaceManager placeManager) {
super(eventBus, view, proxy);
this.placeManager = placeManager;
}
@Override
protected void onBind() {
super.onBind();
registerHandler( getView().getSendButton().addClickHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event) {
sendNameToServer();
}
}) );
}
@Override
protected void onReset() {
super.onReset();
getView().resetAndFocus();
}
@Override
protected void revealInParent() {
RevealRootContentEvent.fire( eventBus, this );
}
/**
* Send the name from the nameField to the server and wait for a response.
*/
private void sendNameToServer() {
// First, we validate the input.
getView().setError("");
String textToServer = getView().getName();
if (!FieldVerifier.isValidName(textToServer)) {
getView().setError("Please enter at least four characters");
return;
}
// Then, we transmit it to the ResponsePresenter, which will do the server call
placeManager.revealPlace( new PlaceRequest(
ResponsePresenter.nameToken).with(
ResponsePresenter.textToServerParam, textToServer) );
}
}