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