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