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 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;
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;
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;
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;
72 * Invoked by the ActivityManager to start a new Activity
75 public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) {
77 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
78 tableSelectionView.setPresenter(this);
80 // For table changes with the tableSelector:
81 final HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
82 tableChangeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
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
87 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTableName()));
89 // Update the browser title because there's place change and the setPlace() method will not be called.
90 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
94 // For quick find changes with the quick find box:
95 final HasChangeHandlers quickFindBox = tableSelectionView.getQuickFindBox();
96 quickFindChangeHandlerRegistration = quickFindBox.addChangeHandler(new ChangeHandler() {
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
101 // update themselves.
102 eventBus.fireEvent(new QuickFindChangeEvent(tableSelectionView.getQuickFindText()));
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());
109 // For locale changes with the localeSelector:
110 final HasChangeHandlers localeSelector = tableSelectionView.getLocaleSelector();
111 localeChangeHandlerRegistration = localeSelector.addChangeHandler(new ChangeHandler() {
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);
118 final String newURL = Window.Location.createUrlBuilder()
119 .setParameter(LocaleInfo.getLocaleQueryParam(), localeID).buildString();
120 Window.Location.assign(newURL);
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));
128 // For report choices with the reportSelector:
129 final HasChangeHandlers reportSelector = tableSelectionView.getReportSelector();
130 reportChangeHandlerRegistration = reportSelector.addChangeHandler(new ChangeHandler() {
132 public void onChange(final ChangeEvent event) {
133 goTo(new ReportPlace(documentID, tableName, tableSelectionView.getSelectedReport(), quickFind));
137 fillView(tableSelectionView);
139 // we're done, set the widget
140 containerWidget.setWidget(tableSelectionView.asWidget());
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>() {
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()");
153 public void onSuccess(final DocumentInfo result) {
154 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
156 if (StringUtils.isEmpty(tableName)) {
157 tableName = result.getTableNames().get(result.getDefaultTableIndex());
160 tableSelectionView.setSelectedTableName(tableName);
162 tableSelectionView.setLocaleList(result.getLocaleIDs(), result.getLocaleTitles());
164 final String localeID = Utils.getCurrentLocaleID();
165 tableSelectionView.setSelectedLocale(localeID);
167 documentTitle = result.getTitle();
168 tableSelectionView.setDocumentTitle(documentTitle);
169 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
172 final String localeID = Utils.getCurrentLocaleID();
173 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, localeID, callback);
175 // get the reports list for the current table:
176 final AsyncCallback<Reports> callback_report = new AsyncCallback<Reports>() {
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()");
184 public void onSuccess(final Reports result) {
185 tableSelectionView.setReportList(result);
188 OnlineGlomServiceAsync.Util.getInstance().getReportsList(documentID, tableName, localeID, callback_report);
190 // Show the quickFind text that was specified by the URL token:
191 tableSelectionView.setQuickFindText(quickFind);
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();
201 final HasRecordsPlace asPlace = (HasRecordsPlace) place;
202 quickFind = asPlace.getQuickFind();
203 } catch (final ClassCastException ex) {
207 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
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);
217 fillView(tableSelectionView);
220 private void clearView() {
221 clientFactory.getTableSelectionView().clear();
223 if (tableChangeHandlerRegistration != null) {
224 tableChangeHandlerRegistration.removeHandler();
225 tableChangeHandlerRegistration = null;
228 if (quickFindChangeHandlerRegistration != null) {
229 quickFindChangeHandlerRegistration.removeHandler();
230 quickFindChangeHandlerRegistration = null;
233 if (localeChangeHandlerRegistration != null) {
234 localeChangeHandlerRegistration.removeHandler();
235 localeChangeHandlerRegistration = null;
238 if (reportChangeHandlerRegistration != null) {
239 reportChangeHandlerRegistration.removeHandler();
240 reportChangeHandlerRegistration = null;
247 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
250 public void onCancel() {
257 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
260 public void onStop() {
267 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
270 public void goTo(final Place place) {
271 clientFactory.getPlaceController().goTo(place);