2 * Copyright (C) 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
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.
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
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/>.
20 package org.glom.web.client.activity;
22 import java.util.ArrayList;
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;
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;
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;
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;
75 * Invoked by the ActivityManager to start a new Activity
78 public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) {
80 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
81 tableSelectionView.setPresenter(this);
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.
87 // For table changes with the tableSelector:
88 final HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
89 tableChangeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
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
94 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTableName()));
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());
101 // For quick find changes with the quick find box:
102 final HasChangeHandlers quickFindBox = tableSelectionView.getQuickFindBox();
103 quickFindChangeHandlerRegistration = quickFindBox.addChangeHandler(new ChangeHandler() {
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
108 // update themselves.
109 eventBus.fireEvent(new QuickFindChangeEvent(tableSelectionView.getQuickFindText()));
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());
116 // For locale changes with the localeSelector:
117 final HasChangeHandlers localeSelector = tableSelectionView.getLocaleSelector();
118 localeChangeHandlerRegistration = localeSelector.addChangeHandler(new ChangeHandler() {
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);
125 final String newURL = Window.Location.createUrlBuilder()
126 .setParameter(LocaleInfo.getLocaleQueryParam(), localeID).buildString();
127 Window.Location.assign(newURL);
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));
135 // For report choices with the reportSelector:
136 final HasChangeHandlers reportSelector = tableSelectionView.getReportSelector();
137 reportChangeHandlerRegistration = reportSelector.addChangeHandler(new ChangeHandler() {
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));
145 // Show the selected report:
146 goTo(new ReportPlace(documentID, tableName, reportName, quickFind));
151 fillView(tableSelectionView);
153 // we're done, set the widget
154 containerWidget.setWidget(tableSelectionView.asWidget());
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>() {
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());
167 public void onSuccess(final DocumentInfo result) {
168 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
170 if (StringUtils.isEmpty(tableName)) {
171 final ArrayList<String> tableNames = result.getTableNames();
172 if(tableNames != null) {
173 tableName = tableNames.get(result.getDefaultTableIndex());
177 tableSelectionView.setSelectedTableName(tableName);
179 tableSelectionView.setLocaleList(result.getLocaleIDs(), result.getLocaleTitles());
181 // Show what locale is currently being used:
182 String localeIDForCombo = Utils.getCurrentLocaleID();
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";
190 tableSelectionView.setSelectedLocale(localeIDForCombo);
192 documentTitle = result.getTitle();
193 tableSelectionView.setDocumentTitle(documentTitle);
194 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
198 final String localeID = Utils.getCurrentLocaleID();
199 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, localeID, callback);
201 // get the reports list for the current table:
202 final AsyncCallback<Reports> callback_report = new AsyncCallback<Reports>() {
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());
210 public void onSuccess(final Reports result) {
211 tableSelectionView.setReportList(result);
213 // Show the selected report name again:
214 // TODO: Avoid duplication in ReportActivity.
215 tableSelectionView.setSelectedReport(reportName);
218 OnlineGlomServiceAsync.Util.getInstance().getReportsList(documentID, tableName, localeID, callback_report);
220 // Show the quickFind text that was specified by the URL token:
221 tableSelectionView.setQuickFindText(quickFind);
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();
231 final HasRecordsPlace asPlace = (HasRecordsPlace) place;
232 quickFind = asPlace.getQuickFind();
233 } catch (final ClassCastException ex) {
237 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
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);
248 if (place instanceof ReportPlace) {
249 reportName = ((ReportPlace) place).getReportName();
252 fillView(tableSelectionView);
255 private void clearView() {
256 clientFactory.getTableSelectionView().clear();
258 if (tableChangeHandlerRegistration != null) {
259 tableChangeHandlerRegistration.removeHandler();
260 tableChangeHandlerRegistration = null;
263 if (quickFindChangeHandlerRegistration != null) {
264 quickFindChangeHandlerRegistration.removeHandler();
265 quickFindChangeHandlerRegistration = null;
268 if (localeChangeHandlerRegistration != null) {
269 localeChangeHandlerRegistration.removeHandler();
270 localeChangeHandlerRegistration = null;
273 if (reportChangeHandlerRegistration != null) {
274 reportChangeHandlerRegistration.removeHandler();
275 reportChangeHandlerRegistration = null;
282 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
285 public void onCancel() {
292 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
295 public void onStop() {
302 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
305 public void goTo(final Place place) {
306 clientFactory.getPlaceController().goTo(place);