Use GWT.log for client-side debugging statements.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / ListActivity.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
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.
10  *
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
14  * for more details.
15  *
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/>.
18  */
19
20 package org.glom.web.client.activity;
21
22 import org.glom.web.client.ClientFactory;
23 import org.glom.web.client.OnlineGlomServiceAsync;
24 import org.glom.web.client.event.TableChangeEvent;
25 import org.glom.web.client.event.TableChangeEventHandler;
26 import org.glom.web.client.place.ListPlace;
27 import org.glom.web.client.ui.AuthenticationPopup;
28 import org.glom.web.client.ui.ListView;
29 import org.glom.web.shared.layout.LayoutGroup;
30
31 import com.google.gwt.activity.shared.AbstractActivity;
32 import com.google.gwt.core.client.GWT;
33 import com.google.gwt.event.dom.client.ClickEvent;
34 import com.google.gwt.event.dom.client.ClickHandler;
35 import com.google.gwt.event.shared.EventBus;
36 import com.google.gwt.place.shared.Place;
37 import com.google.gwt.user.client.rpc.AsyncCallback;
38 import com.google.gwt.user.client.ui.AcceptsOneWidget;
39
40 public class ListActivity extends AbstractActivity implements ListView.Presenter {
41
42         private final String documentID;
43         private final String tableName;
44         private final ClientFactory clientFactory;
45         private final ListView listView;
46         private final AuthenticationPopup authenticationPopup;
47
48         public ListActivity(ListPlace place, ClientFactory clientFactory) {
49                 this.documentID = place.getDocumentID();
50                 this.tableName = place.getTableName();
51                 this.clientFactory = clientFactory;
52                 listView = clientFactory.getListView();
53                 authenticationPopup = clientFactory.getAuthenticationPopup();
54         }
55
56         @Override
57         public void start(AcceptsOneWidget panel, final EventBus eventBus) {
58                 // register this class as the presenter
59                 listView.setPresenter(this);
60
61                 // TODO this should really be it's own Place/Activity
62                 // check if the authentication info has been set for the document
63                 AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
64                         public void onFailure(Throwable caught) {
65                                 // TODO: create a way to notify users of asynchronous callback failures
66                                 GWT.log("AsyncCallback Failed: OnlineGlomService.isAuthenticated()");
67                         }
68
69                         public void onSuccess(Boolean result) {
70                                 if (!result) {
71                                         setUpAuthClickHandler(eventBus);
72                                         authenticationPopup.center();
73                                 }
74                         }
75                 };
76                 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
77
78                 // set the change handler for the table selection widget
79                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
80                         @Override
81                         public void onTableChange(final TableChangeEvent event) {
82                                 goTo(new ListPlace(documentID, event.getTableName()));
83                         }
84                 });
85
86                 // populate the cell table with data
87                 AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
88                         public void onFailure(Throwable caught) {
89                                 // TODO: create a way to notify users of asynchronous callback failures
90                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getListViewLayout()");
91                         }
92
93                         public void onSuccess(LayoutGroup result) {
94                                 // TODO check if result.getTableName() is the same as the tableName field. Update it if it's the same.
95                                 listView.setCellTable(documentID, result);
96                         }
97                 };
98                 OnlineGlomServiceAsync.Util.getInstance().getListViewLayout(documentID, tableName, callback);
99
100                 // indicate that the view is ready to be displayed
101                 panel.setWidget(listView.asWidget());
102         }
103
104         private void setUpAuthClickHandler(final EventBus eventBus) {
105                 authenticationPopup.setClickOkHandler(new ClickHandler() {
106                         @Override
107                         public void onClick(ClickEvent event) {
108                                 authenticationPopup.setTextFieldsEnabled(false);
109                                 AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
110                                         @Override
111                                         public void onFailure(Throwable caught) {
112                                                 // TODO: create a way to notify users of asynchronous callback failures
113                                                 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication()");
114                                         }
115
116                                         @Override
117                                         public void onSuccess(Boolean result) {
118                                                 if (result) {
119                                                         authenticationPopup.hide();
120                                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
121                                                                         .getSelectedTable()));
122                                                 } else {
123                                                         authenticationPopup.setTextFieldsEnabled(true);
124                                                         authenticationPopup.setError();
125                                                 }
126                                         }
127                                 };
128                                 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
129                                                 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
130                         }
131
132                 });
133         }
134
135         private void clearView() {
136                 authenticationPopup.hide();
137                 authenticationPopup.clear();
138                 listView.clear();
139         }
140
141         /*
142          * (non-Javadoc)
143          * 
144          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
145          */
146         @Override
147         public void onCancel() {
148                 clearView();
149         }
150
151         /*
152          * (non-Javadoc)
153          * 
154          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
155          */
156         @Override
157         public void onStop() {
158                 clearView();
159         }
160
161         /*
162          * (non-Javadoc)
163          * 
164          * @see org.glom.web.client.ui.ListView.Presenter#goTo(com.google.gwt.place.shared.Place)
165          */
166         public void goTo(Place place) {
167                 clientFactory.getPlaceController().goTo(place);
168         }
169
170 }