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