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