|
package edu.fresno.gwt.vehiclereservation.client.page; |
|
|
|
import java.util.ArrayList; |
|
import java.util.List; |
|
|
|
import net.binarymuse.gwt.client.ui.wizard.WizardPage; |
|
import net.binarymuse.gwt.client.ui.wizard.event.NavigationEvent; |
|
|
|
import com.allen_sauer.gwt.log.client.Log; |
|
import com.google.gwt.event.dom.client.KeyPressHandler; |
|
import com.google.gwt.user.client.Command; |
|
import com.google.gwt.user.client.DeferredCommand; |
|
import com.google.gwt.user.client.ui.HasHTML; |
|
import com.google.gwt.user.client.ui.HasValue; |
|
import com.google.gwt.user.client.ui.Widget; |
|
|
|
import edu.fresno.gwt.vehiclereservation.shared.VehicleReservationContext; |
|
|
|
public class RequesterInfoPage extends WizardPage<VehicleReservationContext> { |
|
|
|
// The Display interface here defines methods that the |
|
// view for this page must define. The presenter (this object) |
|
// uses these methods to manipulate the view, without knowing |
|
// the details of exactly how the view is implemented. |
|
public interface Display { |
|
// HasX methods for getting fields that present a value |
|
HasValue<String> getRequesterNameValue(); |
|
HasValue<String> getRequesterEmailValue(); |
|
HasValue<String> getRequesterPhoneValue(); |
|
HasValue<String> getDepartmentBilledValue(); |
|
HasValue<String> getAccountNumberValue(); |
|
|
|
HasHTML getErrorHTML(); |
|
|
|
// Methods to implement various view functionality |
|
void addKeyPressHandlers(KeyPressHandler handler); |
|
void focusFirstBlankField(); |
|
void showErrors(List<String> errors); |
|
|
|
// asWidget() allows the page to be added to the wizard |
|
Widget asWidget(); |
|
} |
|
|
|
public final static PageID PAGEID = new PageID(); |
|
public final Display view; |
|
|
|
// Not technically good MVP practice here; ideally, we |
|
// would use Dependency Injection to inject the view. |
|
public RequesterInfoPage() { |
|
view = new RequesterInfoPageView(); |
|
} |
|
|
|
@Override |
|
public PageID getPageID() { |
|
return PAGEID; |
|
} |
|
|
|
@Override |
|
public Widget asWidget() { |
|
return view.asWidget(); |
|
} |
|
|
|
@Override |
|
public String getTitle() { |
|
return "Requester Information"; |
|
} |
|
|
|
// Fires only the first time the page is shown (before it is actually |
|
// shown). Gets any values from the context that may have been set |
|
// from previous pages (e.g. the login page) and sets the data |
|
// in the view. |
|
// |
|
// Also does some other basic view setup, such as injecting a |
|
// KeyPressHandler into the fields (so the user can go next by pressing |
|
// the enter key) and setting the focus to the first blank field (using |
|
// a DeferredCommand so that the code executes once the other events |
|
// finish.) |
|
@Override |
|
public void beforeFirstShow() { |
|
if(getContext().authFullname != null) |
|
view.getRequesterNameValue().setValue(getContext().authFullname); |
|
if(getContext().authEmail != null) |
|
view.getRequesterEmailValue().setValue(getContext().authEmail); |
|
|
|
view.addKeyPressHandlers(getWizard().getView().getHandlerFactory().getEnterKeyNextHandler()); |
|
|
|
DeferredCommand.addCommand(new Command() { |
|
@Override |
|
public void execute() { |
|
view.focusFirstBlankField(); |
|
} |
|
}); |
|
} |
|
|
|
@Override |
|
public void beforeShow() { |
|
view.showErrors(null); |
|
} |
|
|
|
// Commits the data to the context when moving to the next page |
|
// (assuming the data validates). |
|
@Override |
|
public void beforeNext(NavigationEvent event) { |
|
if(!validate()) |
|
event.cancel(); |
|
|
|
getContext().requesterName = view.getRequesterNameValue().getValue(); |
|
getContext().requesterEmail = view.getRequesterEmailValue().getValue(); |
|
getContext().requesterPhone = view.getRequesterPhoneValue().getValue(); |
|
getContext().departmentName = view.getDepartmentBilledValue().getValue(); |
|
getContext().accountNumber = view.getAccountNumberValue().getValue(); |
|
} |
|
|
|
@Override |
|
public void afterNext() { |
|
Log.debug("RequesterInfoPage afterNext()"); |
|
Log.debug(context.toString()); |
|
} |
|
|
|
public boolean validate() { |
|
List<String> errors = new ArrayList<String>(); |
|
if(view.getRequesterNameValue().getValue().isEmpty()) |
|
errors.add("You must provide your name."); |
|
if(view.getRequesterEmailValue().getValue().isEmpty()) |
|
errors.add("You must provide your email address."); |
|
if(view.getRequesterPhoneValue().getValue().isEmpty()) |
|
errors.add("You must provide your phone number."); |
|
if(view.getDepartmentBilledValue().getValue().isEmpty()) |
|
errors.add("You must specify the department to be billed."); |
|
if(view.getAccountNumberValue().getValue().isEmpty()) |
|
errors.add("You must specify the account number of the department to be billed."); |
|
|
|
|
|
if(errors.size() == 0) { |
|
view.showErrors(null); |
|
return true; |
|
} else { |
|
view.showErrors(errors); |
|
return false; |
|
} |
|
} |
|
|
|
} |