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