Correct error in previous commit.
[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.getNewTableName()));
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 not the
95                                 // same.
96                                 listView.setCellTable(documentID, result);
97                         }
98                 };
99                 OnlineGlomServiceAsync.Util.getInstance().getListViewLayout(documentID, tableName, callback);
100
101                 // indicate that the view is ready to be displayed
102                 panel.setWidget(listView.asWidget());
103         }
104
105         private void setUpAuthClickHandler(final EventBus eventBus) {
106                 authenticationPopup.setClickOkHandler(new ClickHandler() {
107                         @Override
108                         public void onClick(ClickEvent event) {
109                                 authenticationPopup.setTextFieldsEnabled(false);
110                                 AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
111                                         @Override
112                                         public void onFailure(Throwable caught) {
113                                                 // TODO: create a way to notify users of asynchronous callback failures
114                                                 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication()");
115                                         }
116
117                                         @Override
118                                         public void onSuccess(Boolean result) {
119                                                 if (result) {
120                                                         authenticationPopup.hide();
121                                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
122                                                                         .getSelectedTableName()));
123                                                 } else {
124                                                         authenticationPopup.setTextFieldsEnabled(true);
125                                                         authenticationPopup.setError();
126                                                 }
127                                         }
128                                 };
129                                 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
130                                                 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
131                         }
132
133                 });
134         }
135
136         private void clearView() {
137                 authenticationPopup.hide();
138                 authenticationPopup.clear();
139                 listView.clear();
140         }
141
142         /*
143          * (non-Javadoc)
144          * 
145          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
146          */
147         @Override
148         public void onCancel() {
149                 clearView();
150         }
151
152         /*
153          * (non-Javadoc)
154          * 
155          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
156          */
157         @Override
158         public void onStop() {
159                 clearView();
160         }
161
162         /*
163          * (non-Javadoc)
164          * 
165          * @see org.glom.web.client.ui.ListView.Presenter#goTo(com.google.gwt.place.shared.Place)
166          */
167         public void goTo(Place place) {
168                 clientFactory.getPlaceController().goTo(place);
169         }
170
171 }