DetailsActivity: Check for authentication here too.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / TableSelectionActivity.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.StringUtils;
25 import org.glom.web.client.Utils;
26 import org.glom.web.client.event.LocaleChangeEvent;
27 import org.glom.web.client.event.QuickFindChangeEvent;
28 import org.glom.web.client.event.TableChangeEvent;
29 import org.glom.web.client.place.DetailsPlace;
30 import org.glom.web.client.place.HasRecordsPlace;
31 import org.glom.web.client.place.HasTablePlace;
32 import org.glom.web.client.place.ListPlace;
33 import org.glom.web.client.place.ReportPlace;
34 import org.glom.web.client.ui.TableSelectionView;
35 import org.glom.web.client.ui.View;
36 import org.glom.web.shared.DocumentInfo;
37 import org.glom.web.shared.Reports;
38
39 import com.google.gwt.activity.shared.AbstractActivity;
40 import com.google.gwt.core.client.GWT;
41 import com.google.gwt.event.dom.client.ChangeEvent;
42 import com.google.gwt.event.dom.client.ChangeHandler;
43 import com.google.gwt.event.dom.client.HasChangeHandlers;
44 import com.google.gwt.event.shared.EventBus;
45 import com.google.gwt.event.shared.HandlerRegistration;
46 import com.google.gwt.i18n.client.LocaleInfo;
47 import com.google.gwt.place.shared.Place;
48 import com.google.gwt.user.client.Window;
49 import com.google.gwt.user.client.rpc.AsyncCallback;
50 import com.google.gwt.user.client.ui.AcceptsOneWidget;
51
52 /**
53  *
54  */
55 public class TableSelectionActivity extends AbstractActivity implements View.Presenter {
56         private final ClientFactory clientFactory;
57         private String documentID;
58         private String documentTitle;
59         private String tableName;
60         private String quickFind;
61         private String reportName;
62         private HandlerRegistration tableChangeHandlerRegistration = null;
63         private HandlerRegistration quickFindChangeHandlerRegistration = null;
64         private HandlerRegistration localeChangeHandlerRegistration = null;
65         private HandlerRegistration reportChangeHandlerRegistration = null;
66
67         // This activity isn't properly configured until the List or Details Place is set with the appropriate methods
68         public TableSelectionActivity(final ClientFactory clientFactory) {
69                 this.clientFactory = clientFactory;
70         }
71
72         /**
73          * Invoked by the ActivityManager to start a new Activity
74          */
75         @Override
76         public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) {
77
78                 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
79                 tableSelectionView.setPresenter(this);
80                 
81                 // TODO: Check for authentication here?
82                 // Or just let it fail to retrieve the list of tables,
83                 // and let the other activity on the page ask for authentication.
84
85                 // For table changes with the tableSelector:
86                 final HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
87                 tableChangeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
88                         @Override
89                         public void onChange(final ChangeEvent event) {
90                                 // Fire a table change event so that other views (e.g. the details view) know about the change and can
91                                 // update themselves.
92                                 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTableName()));
93
94                                 // Update the browser title because there's place change and the setPlace() method will not be called.
95                                 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
96                         }
97                 });
98
99                 // For quick find changes with the quick find box:
100                 final HasChangeHandlers quickFindBox = tableSelectionView.getQuickFindBox();
101                 quickFindChangeHandlerRegistration = quickFindBox.addChangeHandler(new ChangeHandler() {
102                         @Override
103                         public void onChange(final ChangeEvent event) {
104                                 // Fire a quickfind change event so that other views (e.g. the details view) know about the change and
105                                 // can
106                                 // update themselves.
107                                 eventBus.fireEvent(new QuickFindChangeEvent(tableSelectionView.getQuickFindText()));
108
109                                 // Update the browser title because there's place change and the setPlace() method will not be called.
110                                 // TODO? Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
111                         }
112                 });
113
114                 // For locale changes with the localeSelector:
115                 final HasChangeHandlers localeSelector = tableSelectionView.getLocaleSelector();
116                 localeChangeHandlerRegistration = localeSelector.addChangeHandler(new ChangeHandler() {
117                         @Override
118                         public void onChange(final ChangeEvent event) {
119                                 // Show the translated version of the document title and the table names:
120                                 final String localeID = tableSelectionView.getSelectedLocale();
121                                 fillView(tableSelectionView);
122
123                                 final String newURL = Window.Location.createUrlBuilder()
124                                                 .setParameter(LocaleInfo.getLocaleQueryParam(), localeID).buildString();
125                                 Window.Location.assign(newURL);
126
127                                 // Fire a locale change event so that other views (e.g. the details view) know about the change and can
128                                 // update themselves.
129                                 eventBus.fireEvent(new LocaleChangeEvent(localeID));
130                         }
131                 });
132
133                 // For report choices with the reportSelector:
134                 final HasChangeHandlers reportSelector = tableSelectionView.getReportSelector();
135                 reportChangeHandlerRegistration = reportSelector.addChangeHandler(new ChangeHandler() {
136                         @Override
137                         public void onChange(final ChangeEvent event) {
138                                 final String reportName = tableSelectionView.getSelectedReport();
139                                 if (StringUtils.isEmpty(reportName)) {
140                                         // Interpret selecting no report as requesting the list view.
141                                         goTo(new ListPlace(documentID, tableName, quickFind));
142                                 } else {
143                                         // Show the selected report:
144                                         goTo(new ReportPlace(documentID, tableName, reportName, quickFind));
145                                 }
146                         }
147                 });
148
149                 fillView(tableSelectionView);
150
151                 // we're done, set the widget
152                 containerWidget.setWidget(tableSelectionView.asWidget());
153         }
154
155         private void fillView(final TableSelectionView tableSelectionView) {
156                 // get the table names, table titles and default table index for the current document
157                 final AsyncCallback<DocumentInfo> callback = new AsyncCallback<DocumentInfo>() {
158                         @Override
159                         public void onFailure(final Throwable caught) {
160                                 // TODO: create a way to notify users of asynchronous callback failures
161                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getDocumentInfo(): " + caught.getMessage());
162                         }
163
164                         @Override
165                         public void onSuccess(final DocumentInfo result) {
166                                 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
167
168                                 if (StringUtils.isEmpty(tableName)) {
169                                         tableName = result.getTableNames().get(result.getDefaultTableIndex());
170                                 }
171
172                                 tableSelectionView.setSelectedTableName(tableName);
173
174                                 tableSelectionView.setLocaleList(result.getLocaleIDs(), result.getLocaleTitles());
175
176                                 // Show what locale is currently being used:
177                                 String localeIDForCombo = Utils.getCurrentLocaleID();
178
179                                 // Indicate that we use English if no other locale has been specified by either
180                                 // the URL or the configuration.
181                                 // Alternatively we could also show the locale in the URL, even if it is en.
182                                 if (StringUtils.isEmpty(localeIDForCombo)) {
183                                         localeIDForCombo = "en";
184                                 }
185                                 tableSelectionView.setSelectedLocale(localeIDForCombo);
186
187                                 documentTitle = result.getTitle();
188                                 tableSelectionView.setDocumentTitle(documentTitle);
189                                 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
190                         }
191                 };
192
193                 final String localeID = Utils.getCurrentLocaleID();
194                 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, localeID, callback);
195
196                 // get the reports list for the current table:
197                 final AsyncCallback<Reports> callback_report = new AsyncCallback<Reports>() {
198                         @Override
199                         public void onFailure(final Throwable caught) {
200                                 // TODO: create a way to notify users of asynchronous callback failures
201                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getReportsList(): " + caught.getMessage());
202                         }
203
204                         @Override
205                         public void onSuccess(final Reports result) {
206                                 tableSelectionView.setReportList(result);
207
208                                 // Show the selected report name again:
209                                 // TODO: Avoid duplication in ReportActivity.
210                                 tableSelectionView.setSelectedReport(reportName);
211                         }
212                 };
213                 OnlineGlomServiceAsync.Util.getInstance().getReportsList(documentID, tableName, localeID, callback_report);
214
215                 // Show the quickFind text that was specified by the URL token:
216                 tableSelectionView.setQuickFindText(quickFind);
217         }
218
219         // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method and
220         // any time the Place changes after the start method has been called.
221         public void setPlace(final HasTablePlace place) {
222                 documentID = place.getDocumentID();
223                 tableName = place.getTableName();
224
225                 try {
226                         final HasRecordsPlace asPlace = (HasRecordsPlace) place;
227                         quickFind = asPlace.getQuickFind();
228                 } catch (final ClassCastException ex) {
229                         quickFind = "";
230                 }
231
232                 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
233
234                 // Show the 'back to list' link if we're at a DetailsPlace or a ReportPlace.
235                 if (place instanceof DetailsPlace || place instanceof ReportPlace) {
236                         tableSelectionView.setBackLinkVisible(true);
237                         tableSelectionView.setBackLink(documentID, tableName, ""); // TODO: quickfind?
238                 } else if (place instanceof ListPlace) {
239                         tableSelectionView.setBackLinkVisible(false);
240                 }
241
242                 reportName = "";
243                 if (place instanceof ReportPlace) {
244                         reportName = ((ReportPlace) place).getReportName();
245                 }
246
247                 fillView(tableSelectionView);
248         }
249
250         private void clearView() {
251                 clientFactory.getTableSelectionView().clear();
252
253                 if (tableChangeHandlerRegistration != null) {
254                         tableChangeHandlerRegistration.removeHandler();
255                         tableChangeHandlerRegistration = null;
256                 }
257
258                 if (quickFindChangeHandlerRegistration != null) {
259                         quickFindChangeHandlerRegistration.removeHandler();
260                         quickFindChangeHandlerRegistration = null;
261                 }
262
263                 if (localeChangeHandlerRegistration != null) {
264                         localeChangeHandlerRegistration.removeHandler();
265                         localeChangeHandlerRegistration = null;
266                 }
267
268                 if (reportChangeHandlerRegistration != null) {
269                         reportChangeHandlerRegistration.removeHandler();
270                         reportChangeHandlerRegistration = null;
271                 }
272         }
273
274         /*
275          * (non-Javadoc)
276          * 
277          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
278          */
279         @Override
280         public void onCancel() {
281                 clearView();
282         }
283
284         /*
285          * (non-Javadoc)
286          * 
287          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
288          */
289         @Override
290         public void onStop() {
291                 clearView();
292         }
293
294         /*
295          * (non-Javadoc)
296          * 
297          * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
298          */
299         @Override
300         public void goTo(final Place place) {
301                 clientFactory.getPlaceController().goTo(place);
302         }
303
304 }