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