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