Improve null/empty String checks.
[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.StringUtils;
23 import org.glom.web.client.ClientFactory;
24 import org.glom.web.client.OnlineGlomServiceAsync;
25 import org.glom.web.client.event.LocaleChangeEvent;
26 import org.glom.web.client.event.LocaleChangeEventHandler;
27 import org.glom.web.client.event.QuickFindChangeEvent;
28 import org.glom.web.client.event.QuickFindChangeEventHandler;
29 import org.glom.web.client.event.TableChangeEvent;
30 import org.glom.web.client.event.TableChangeEventHandler;
31 import org.glom.web.client.place.DocumentSelectionPlace;
32 import org.glom.web.client.place.ListPlace;
33 import org.glom.web.client.ui.AuthenticationPopup;
34 import org.glom.web.client.ui.ListView;
35 import org.glom.web.client.ui.View;
36 import org.glom.web.shared.layout.LayoutGroup;
37
38 import com.google.gwt.activity.shared.AbstractActivity;
39 import com.google.gwt.core.client.GWT;
40 import com.google.gwt.event.dom.client.ClickEvent;
41 import com.google.gwt.event.dom.client.ClickHandler;
42 import com.google.gwt.event.shared.EventBus;
43 import com.google.gwt.place.shared.Place;
44 import com.google.gwt.user.client.rpc.AsyncCallback;
45 import com.google.gwt.user.client.ui.AcceptsOneWidget;
46
47 public class ListActivity extends AbstractActivity implements View.Presenter {
48
49         private final String documentID;
50         private final String tableName;
51         private final String localeID;
52         private final String quickFind;
53         private final ClientFactory clientFactory;
54         private final ListView listView;
55         private final AuthenticationPopup authenticationPopup;
56
57         public ListActivity(final ListPlace place, final ClientFactory clientFactory) {
58                 this.documentID = place.getDocumentID(); // TODO: Just store the place?
59                 this.tableName = place.getTableName();
60                 this.localeID = place.getLocaleID();
61                 this.quickFind = place.getQuickFind();
62                 this.clientFactory = clientFactory;
63                 listView = clientFactory.getListView();
64                 authenticationPopup = clientFactory.getAuthenticationPopup();
65         }
66
67         @Override
68         public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
69                 if (StringUtils.isEmpty(documentID))
70                         goTo(new DocumentSelectionPlace());
71
72                 // register this class as the presenter
73                 listView.setPresenter(this);
74
75                 // TODO this should really be it's own Place/Activity
76                 // check if the authentication info has been set for the document
77                 final AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
78                         @Override
79                         public void onFailure(final Throwable caught) {
80                                 // TODO: create a way to notify users of asynchronous callback failures
81                                 GWT.log("AsyncCallback Failed: OnlineGlomService.isAuthenticated()");
82                         }
83
84                         @Override
85                         public void onSuccess(final Boolean result) {
86                                 if (!result) {
87                                         setUpAuthClickHandler(eventBus);
88                                         authenticationPopup.center();
89                                 }
90                         }
91                 };
92                 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
93
94                 // set the change handler for the table selection widget
95                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
96                         @Override
97                         public void onTableChange(final TableChangeEvent event) {
98                                 goTo(new ListPlace(documentID, event.getNewTableName(), localeID, ""));
99                         }
100                 });
101
102                 // populate the cell table with data
103                 final AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
104                         @Override
105                         public void onFailure(final Throwable caught) {
106                                 // TODO: create a way to notify users of asynchronous callback failures
107                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getListViewLayout()");
108                         }
109
110                         @Override
111                         public void onSuccess(final LayoutGroup result) {
112                                 // TODO check if result.getTableName() is the same as the tableName field. Update it if it's not the
113                                 // same.
114                                 listView.setCellTable(documentID, result, localeID, quickFind);
115                         }
116                 };
117                 OnlineGlomServiceAsync.Util.getInstance().getListViewLayout(documentID, tableName, localeID, callback);
118
119                 // TODO: Avoid the code duplication with DetailsActivity.
120                 // set the change handler for the quickfind text widget
121                 eventBus.addHandler(QuickFindChangeEvent.TYPE, new QuickFindChangeEventHandler() {
122                         @Override
123                         public void onQuickFindChange(final QuickFindChangeEvent event) {
124                                 // We switch to the List view, to show search results.
125                                 // TODO: Show the details view if there is only one result.
126                                 goTo(new ListPlace(documentID, tableName, localeID, event.getNewQuickFindText()));
127                         }
128                 });
129
130                 // Set the change handler for the table selection widget
131                 eventBus.addHandler(LocaleChangeEvent.TYPE, new LocaleChangeEventHandler() {
132                         @Override
133                         public void onLocaleChange(final LocaleChangeEvent event) {
134                                 // note the empty primary key item
135                                 goTo(new ListPlace(documentID, tableName, event.getNewLocaleID(), quickFind));
136                         }
137                 });
138
139                 // indicate that the view is ready to be displayed
140                 panel.setWidget(listView.asWidget());
141         }
142
143         private void setUpAuthClickHandler(final EventBus eventBus) {
144                 authenticationPopup.setClickOkHandler(new ClickHandler() {
145                         @Override
146                         public void onClick(final ClickEvent event) {
147                                 authenticationPopup.setTextFieldsEnabled(false);
148                                 final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
149                                         @Override
150                                         public void onFailure(final Throwable caught) {
151                                                 // TODO: create a way to notify users of asynchronous callback failures
152                                                 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication()");
153                                         }
154
155                                         @Override
156                                         public void onSuccess(final Boolean result) {
157                                                 if (result) {
158                                                         authenticationPopup.hide();
159                                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
160                                                                         .getSelectedTableName()));
161                                                 } else {
162                                                         authenticationPopup.setTextFieldsEnabled(true);
163                                                         authenticationPopup.setError();
164                                                 }
165                                         }
166                                 };
167                                 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
168                                                 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
169                         }
170
171                 });
172         }
173
174         private void clearView() {
175                 authenticationPopup.hide();
176                 authenticationPopup.clear();
177                 listView.clear();
178         }
179
180         /*
181          * (non-Javadoc)
182          * 
183          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
184          */
185         @Override
186         public void onCancel() {
187                 clearView();
188         }
189
190         /*
191          * (non-Javadoc)
192          * 
193          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
194          */
195         @Override
196         public void onStop() {
197                 clearView();
198         }
199
200         /*
201          * (non-Javadoc)
202          * 
203          * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
204          */
205         @Override
206         public void goTo(final Place place) {
207                 clientFactory.getPlaceController().goTo(place);
208         }
209
210 }