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.StringUtils;
25 import org.glom.web.client.event.TableChangeEvent;
26 import org.glom.web.client.place.DocumentSelectionPlace;
27 import org.glom.web.client.place.HasDocumentPlace;
28 import org.glom.web.client.place.ListPlace;
29 import org.glom.web.client.ui.DocumentLoginView;
31 import com.google.gwt.core.client.GWT;
32 import com.google.gwt.event.dom.client.ClickEvent;
33 import com.google.gwt.event.dom.client.ClickHandler;
34 import com.google.gwt.event.shared.EventBus;
35 import com.google.gwt.user.client.rpc.AsyncCallback;
36 import com.google.gwt.user.client.ui.AcceptsOneWidget;
38 public class DocumentLoginActivity extends HasDocumentActivity {
40 private final DocumentLoginView view;
42 public DocumentLoginActivity(final HasDocumentPlace place, final ClientFactory clientFactory) {
43 super(place, clientFactory);
44 this.view = clientFactory.getDocumentLoginView();
48 public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
49 if (StringUtils.isEmpty(documentID)) {
50 goTo(new DocumentSelectionPlace());
53 // register this class as the presenter
54 view.setPresenter(this);
56 //Find out if there is any need for authentication,
57 //asking for the credentials if necessary:
58 checkAuthentication(eventBus);
60 // indicate that the view is ready to be displayed
61 panel.setWidget(view.asWidget());
65 protected void clearView() {
73 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
76 public void onCancel() {
83 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
86 public void onStop() {
90 //TODO: Remove or modify this in the base class.
94 protected void checkAuthentication(final EventBus eventBus) {
95 if(StringUtils.isEmpty(documentID)) {
96 //TODO: Show that no document was chosen?
100 // Check if the authentication info has been set for the document
101 final AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
103 public void onFailure(final Throwable caught) {
104 // TODO: create a way to notify users of asynchronous callback failures
105 GWT.log("AsyncCallback Failed: OnlineGlomService.isAuthenticated(): " + caught.getMessage());
109 public void onSuccess(final Boolean result) {
111 //If the user is not already authenticated,
113 setUpAuthClickHandlers(eventBus);
115 // The user was already authenticated, so go to the previous (or default) page:
116 goTo(new DocumentSelectionPlace());
117 eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
118 .getSelectedTableName()));
122 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
125 private void setUpAuthClickHandlers(final EventBus eventBus) {
128 view.setClickLoginHandler(new ClickHandler() {
130 public void onClick(final ClickEvent event) {
131 view.setTextFieldsEnabled(false);
133 final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
135 public void onFailure(final Throwable caught) {
136 // TODO: create a way to notify users of asynchronous callback failures
137 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication(): " + caught.getMessage());
141 public void onSuccess(final Boolean result) {
143 // If authentication succeeded, take us to the requested table:
144 // TODO: Take us to the previous page.
145 goTo(new ListPlace(documentID, null, null));
146 //eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
147 // .getSelectedTableName()));
149 // If authentication failed, tell the user:
150 view.setTextFieldsEnabled(true);
155 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
156 view.getUsername(), view.getPassword(), callback);
161 view.setClickCancelHandler(new ClickHandler() {
163 public void onClick(final ClickEvent event) {
164 //TODO: Return to the previous (or default page):
165 goTo(new DocumentSelectionPlace());