2 * Copyright (C) 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
6 * GWT-Glom is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
11 * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with GWT-Glom. If not, see <http://www.gnu.org/licenses/>.
20 package org.glom.web.client.activity;
22 import org.glom.web.client.ClientFactory;
23 import org.glom.web.client.OnlineGlomServiceAsync;
24 import org.glom.web.client.PlaceControllerExt;
25 import org.glom.web.client.StringUtils;
26 import org.glom.web.client.event.TableChangeEvent;
27 import org.glom.web.client.place.HasDocumentPlace;
28 import org.glom.web.client.ui.DocumentLoginView;
29 import com.google.gwt.core.client.GWT;
30 import com.google.gwt.event.dom.client.ClickEvent;
31 import com.google.gwt.event.dom.client.ClickHandler;
32 import com.google.gwt.event.shared.EventBus;
33 import com.google.gwt.place.shared.PlaceController;
34 import com.google.gwt.user.client.rpc.AsyncCallback;
35 import com.google.gwt.user.client.ui.AcceptsOneWidget;
37 public class DocumentLoginActivity extends HasDocumentActivity {
39 private final DocumentLoginView view;
41 public DocumentLoginActivity(final HasDocumentPlace place, final ClientFactory clientFactory) {
42 super(place, clientFactory);
43 this.view = clientFactory.getDocumentLoginView();
46 private void goToPrevious() {
47 final PlaceController placeController = clientFactory.getPlaceController();
48 if(placeController instanceof PlaceControllerExt) {
49 final PlaceControllerExt ext = (PlaceControllerExt)placeController;
52 GWT.log("The PlaceController was not a PlaceControllerExt.");
56 private void goToDefault() {
57 final PlaceController placeController = clientFactory.getPlaceController();
58 if(placeController instanceof PlaceControllerExt) {
59 final PlaceControllerExt ext = (PlaceControllerExt)placeController;
60 goTo(ext.getDefaultPlace());
62 GWT.log("The PlaceController was not a PlaceControllerExt.");
67 public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
68 if (StringUtils.isEmpty(documentID)) {
72 // register this class as the presenter
73 view.setPresenter(this);
75 //Find out if there is any need for authentication,
76 //asking for the credentials if necessary:
77 checkAuthentication(eventBus);
79 // indicate that the view is ready to be displayed
80 panel.setWidget(view.asWidget());
84 protected void clearView() {
92 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
95 public void onCancel() {
102 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
105 public void onStop() {
109 //TODO: Remove or modify this in the base class.
113 protected void checkAuthentication(final EventBus eventBus) {
114 if(StringUtils.isEmpty(documentID)) {
115 //TODO: Show that no document was chosen?
119 // Check if the authentication info has been set for the document
120 final AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
122 public void onFailure(final Throwable caught) {
123 // TODO: create a way to notify users of asynchronous callback failures
124 GWT.log("AsyncCallback Failed: OnlineGlomService.isAuthenticated(): " + caught.getMessage());
128 public void onSuccess(final Boolean result) {
130 //If the user is not already authenticated,
132 setUpAuthClickHandlers(eventBus);
134 // The user was already authenticated, so go to the previous (or default) page:
136 eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
137 .getSelectedTableName()));
141 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
144 private void setUpAuthClickHandlers(final EventBus eventBus) {
147 view.setClickLoginHandler(new ClickHandler() {
149 public void onClick(final ClickEvent event) {
150 view.setTextFieldsEnabled(false);
152 final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
154 public void onFailure(final Throwable caught) {
155 // TODO: create a way to notify users of asynchronous callback failures
156 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication(): " + caught.getMessage());
160 public void onSuccess(final Boolean result) {
162 // If authentication succeeded, take us to the requested table:
164 //eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
165 // .getSelectedTableName()));
167 // If authentication failed, tell the user:
168 view.setTextFieldsEnabled(true);
173 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
174 view.getUsername(), view.getPassword(), callback);
179 view.setClickCancelHandler(new ClickHandler() {
181 public void onClick(final ClickEvent event) {