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.ui.TableSelectionView;
34 import org.glom.web.client.ui.View;
35 import org.glom.web.shared.DocumentInfo;
36 import org.glom.web.shared.Reports;
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;
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;
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;
70 * Invoked by the ActivityManager to start a new Activity
73 public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) {
75 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
76 tableSelectionView.setPresenter(this);
78 // For table changes with the tableSelector:
79 final HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
80 tableChangeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
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
85 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTableName()));
87 // Update the browser title because there's place change and the setPlace() method will not be called.
88 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
92 // For quick find changes with the quick find box:
93 final HasChangeHandlers quickFindBox = tableSelectionView.getQuickFindBox();
94 quickFindChangeHandlerRegistration = quickFindBox.addChangeHandler(new ChangeHandler() {
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
100 eventBus.fireEvent(new QuickFindChangeEvent(tableSelectionView.getQuickFindText()));
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());
107 // For locale changes with the localeSelector:
108 final HasChangeHandlers localeSelector = tableSelectionView.getLocaleSelector();
109 localeChangeHandlerRegistration = localeSelector.addChangeHandler(new ChangeHandler() {
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);
116 final String newURL = Window.Location.createUrlBuilder()
117 .setParameter(LocaleInfo.getLocaleQueryParam(), localeID).buildString();
118 Window.Location.assign(newURL);
120 // Fire a locale change event so that other views (e.g. the details view) know about the change and can
121 // update themselves.
122 eventBus.fireEvent(new LocaleChangeEvent(localeID));
126 fillView(tableSelectionView);
128 // we're done, set the widget
129 containerWidget.setWidget(tableSelectionView.asWidget());
132 private void fillView(final TableSelectionView tableSelectionView) {
133 // get the table names, table titles and default table index for the current document
134 final AsyncCallback<DocumentInfo> callback = new AsyncCallback<DocumentInfo>() {
136 public void onFailure(final Throwable caught) {
137 // TODO: create a way to notify users of asynchronous callback failures
138 GWT.log("AsyncCallback Failed: OnlineGlomService.getDocumentInfo()");
142 public void onSuccess(final DocumentInfo result) {
143 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
145 if (StringUtils.isEmpty(tableName)) {
146 tableName = result.getTableNames().get(result.getDefaultTableIndex());
149 tableSelectionView.setSelectedTableName(tableName);
151 tableSelectionView.setLocaleList(result.getLocaleIDs(), result.getLocaleTitles());
153 final String localeID = Utils.getCurrentLocaleID();
154 tableSelectionView.setSelectedLocale(localeID);
156 documentTitle = result.getTitle();
157 tableSelectionView.setDocumentTitle(documentTitle);
158 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
161 final String localeID = Utils.getCurrentLocaleID();
162 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, localeID, callback);
164 // get the reports list for the current table:
165 final AsyncCallback<Reports> callback_report = new AsyncCallback<Reports>() {
167 public void onFailure(final Throwable caught) {
168 // TODO: create a way to notify users of asynchronous callback failures
169 GWT.log("AsyncCallback Failed: OnlineGlomService.getReportsList()");
173 public void onSuccess(final Reports result) {
174 tableSelectionView.setReportList(result);
177 OnlineGlomServiceAsync.Util.getInstance().getReportsList(documentID, tableName, localeID, callback_report);
179 // Show the quickFind text that was specified by the URL token:
180 tableSelectionView.setQuickFindText(quickFind);
183 // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method and
184 // any time the Place changes after the start method has been called.
185 public void setPlace(final HasTablePlace place) {
186 documentID = place.getDocumentID();
187 tableName = place.getTableName();
190 final HasRecordsPlace asPlace = (HasRecordsPlace) place;
191 quickFind = asPlace.getQuickFind();
192 } catch (final ClassCastException ex) {
196 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
198 // show the 'back to list' link if we're at a DetailsPlace, hide it otherwise
199 if (place instanceof DetailsPlace) {
200 tableSelectionView.setBackLinkVisible(true);
201 tableSelectionView.setBackLink(documentID, tableName, ""); // TODO: quickfind?
202 } else if (place instanceof ListPlace) {
203 tableSelectionView.setBackLinkVisible(false);
206 fillView(tableSelectionView);
209 private void clearView() {
210 clientFactory.getTableSelectionView().clear();
212 if (tableChangeHandlerRegistration != null) {
213 tableChangeHandlerRegistration.removeHandler();
214 tableChangeHandlerRegistration = null;
217 if (quickFindChangeHandlerRegistration != null) {
218 quickFindChangeHandlerRegistration.removeHandler();
219 quickFindChangeHandlerRegistration = null;
222 if (localeChangeHandlerRegistration != null) {
223 localeChangeHandlerRegistration.removeHandler();
224 localeChangeHandlerRegistration = null;
231 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
234 public void onCancel() {
241 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
244 public void onStop() {
251 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
254 public void goTo(final Place place) {
255 clientFactory.getPlaceController().goTo(place);