Use the ?locale= query param instead of the &lang= token param.
[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.StringUtils;
23 import org.glom.web.client.ClientFactory;
24 import org.glom.web.client.OnlineGlomServiceAsync;
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.HasSelectableTablePlace;
31 import org.glom.web.client.place.ListPlace;
32 import org.glom.web.client.ui.TableSelectionView;
33 import org.glom.web.client.ui.View;
34 import org.glom.web.shared.DocumentInfo;
35 import org.glom.web.shared.Reports;
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.ChangeEvent;
40 import com.google.gwt.event.dom.client.ChangeHandler;
41 import com.google.gwt.event.dom.client.HasChangeHandlers;
42 import com.google.gwt.event.shared.EventBus;
43 import com.google.gwt.event.shared.HandlerRegistration;
44 import com.google.gwt.i18n.client.LocaleInfo;
45 import com.google.gwt.place.shared.Place;
46 import com.google.gwt.user.client.Window;
47 import com.google.gwt.user.client.rpc.AsyncCallback;
48 import com.google.gwt.user.client.ui.AcceptsOneWidget;
49
50 /**
51  *
52  */
53 public class TableSelectionActivity extends AbstractActivity implements View.Presenter {
54         private final ClientFactory clientFactory;
55         private String documentID;
56         private String documentTitle;
57         private String tableName;
58         private String quickFind;
59         private HandlerRegistration tableChangeHandlerRegistration = null;
60         private HandlerRegistration quickFindChangeHandlerRegistration = null;
61         private HandlerRegistration localeChangeHandlerRegistration = null;
62
63         // This activity isn't properly configured until the List or Details Place is set with the appropriate methods
64         public TableSelectionActivity(final ClientFactory clientFactory) {
65                 this.clientFactory = clientFactory;
66         }
67
68         /**
69          * Invoked by the ActivityManager to start a new Activity
70          */
71         @Override
72         public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) {
73
74                 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
75                 tableSelectionView.setPresenter(this);
76
77                 // For table changes with the tableSelector:
78                 final HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
79                 tableChangeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
80                         @Override
81                         public void onChange(final ChangeEvent event) {
82                                 // Fire a table change event so that other views (e.g. the details view) know about the change and can
83                                 // update themselves.
84                                 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTableName()));
85
86                                 // Update the browser title because there's place change and the setPlace() method will not be called.
87                                 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
88                         }
89                 });
90
91                 // For quick find changes with the quick find box:
92                 final HasChangeHandlers quickFindBox = tableSelectionView.getQuickFindBox();
93                 quickFindChangeHandlerRegistration = quickFindBox.addChangeHandler(new ChangeHandler() {
94                         @Override
95                         public void onChange(final ChangeEvent event) {
96                                 // Fire a quickfind change event so that other views (e.g. the details view) know about the change and
97                                 // can
98                                 // update themselves.
99                                 eventBus.fireEvent(new QuickFindChangeEvent(tableSelectionView.getQuickFindText()));
100
101                                 // Update the browser title because there's place change and the setPlace() method will not be called.
102                                 // TODO? Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
103                         }
104                 });
105
106                 // For locale changes with the localeSelector:
107                 final HasChangeHandlers localeSelector = tableSelectionView.getLocaleSelector();
108                 localeChangeHandlerRegistration = localeSelector.addChangeHandler(new ChangeHandler() {
109                         @Override
110                         public void onChange(final ChangeEvent event) {
111                                 // Show the translated version of the document title and the table names:
112                                 final String localeID = tableSelectionView.getSelectedLocale();
113                                 fillView(tableSelectionView);
114                                 
115                                 final String newURL = Window.Location.createUrlBuilder().setParameter(LocaleInfo.getLocaleQueryParam(), localeID).buildString();
116                                 Window.Location.assign(newURL);
117
118                                 // Fire a locale change event so that other views (e.g. the details view) know about the change and can
119                                 // update themselves.
120                                 eventBus.fireEvent(new LocaleChangeEvent(localeID));
121                         }
122                 });
123
124                 fillView(tableSelectionView);
125
126                 // we're done, set the widget
127                 containerWidget.setWidget(tableSelectionView.asWidget());
128         }
129
130         private void fillView(final TableSelectionView tableSelectionView) {
131                 // get the table names, table titles and default table index for the current document
132                 final AsyncCallback<DocumentInfo> callback = new AsyncCallback<DocumentInfo>() {
133                         @Override
134                         public void onFailure(final Throwable caught) {
135                                 // TODO: create a way to notify users of asynchronous callback failures
136                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getDocumentInfo()");
137                         }
138
139                         @Override
140                         public void onSuccess(final DocumentInfo result) {
141                                 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
142
143                                 if (StringUtils.isEmpty(tableName)) {
144                                         tableName = result.getTableNames().get(result.getDefaultTableIndex());
145                                 }
146
147                                 tableSelectionView.setSelectedTableName(tableName);
148
149                                 tableSelectionView.setLocaleList(result.getLocaleIDs(), result.getLocaleTitles());
150                                 
151                                 final String localeID = Utils.getCurrentLocaleID();
152                                 tableSelectionView.setSelectedLocale(localeID);
153
154                                 documentTitle = result.getTitle();
155                                 tableSelectionView.setDocumentTitle(documentTitle);
156                                 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
157                         }
158                 };
159                 final String localeID = Utils.getCurrentLocaleID();
160                 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, localeID, callback);
161
162                 // get the reports list for the current table:
163                 final AsyncCallback<Reports> callback_report = new AsyncCallback<Reports>() {
164                         @Override
165                         public void onFailure(final Throwable caught) {
166                                 // TODO: create a way to notify users of asynchronous callback failures
167                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getReportsList()");
168                         }
169
170                         @Override
171                         public void onSuccess(final Reports result) {
172                                 tableSelectionView.setReportList(result);
173                         }
174                 };
175                 OnlineGlomServiceAsync.Util.getInstance().getReportsList(documentID, tableName, localeID, callback_report);
176
177                 // Show the quickFind text that was specified by the URL token:
178                 tableSelectionView.setQuickFindText(quickFind);
179         }
180
181         // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method and
182         // any time the Place changes after the start method has been called.
183         public void setPlace(final HasSelectableTablePlace place) {
184                 documentID = place.getDocumentID();
185                 tableName = place.getTableName();
186
187                 try {
188                         final ListPlace asPlace = (ListPlace) place;
189                         quickFind = asPlace.getQuickFind();
190                 } catch (final ClassCastException ex) {
191                         quickFind = "";
192                 }
193
194                 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
195
196                 // show the 'back to list' link if we're at a DetailsPlace, hide it otherwise
197                 if (place instanceof DetailsPlace) {
198                         tableSelectionView.setBackLinkVisible(true);
199                         tableSelectionView.setBackLink(documentID, tableName, ""); // TODO: quickfind?
200                 } else if (place instanceof ListPlace) {
201                         tableSelectionView.setBackLinkVisible(false);
202                 }
203
204                 fillView(tableSelectionView);
205         }
206
207         private void clearView() {
208                 clientFactory.getTableSelectionView().clear();
209
210                 if (tableChangeHandlerRegistration != null) {
211                         tableChangeHandlerRegistration.removeHandler();
212                         tableChangeHandlerRegistration = null;
213                 }
214
215                 if (quickFindChangeHandlerRegistration != null) {
216                         quickFindChangeHandlerRegistration.removeHandler();
217                         quickFindChangeHandlerRegistration = null;
218                 }
219
220                 if (localeChangeHandlerRegistration != null) {
221                         localeChangeHandlerRegistration.removeHandler();
222                         localeChangeHandlerRegistration = null;
223                 }
224         }
225
226         /*
227          * (non-Javadoc)
228          * 
229          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
230          */
231         @Override
232         public void onCancel() {
233                 clearView();
234         }
235
236         /*
237          * (non-Javadoc)
238          * 
239          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
240          */
241         @Override
242         public void onStop() {
243                 clearView();
244         }
245
246         /*
247          * (non-Javadoc)
248          * 
249          * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
250          */
251         @Override
252         public void goTo(final Place place) {
253                 clientFactory.getPlaceController().goTo(place);
254         }
255
256 }