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.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;
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().setParameter(LocaleInfo.getLocaleQueryParam(), localeID).buildString();
117 Window.Location.assign(newURL);
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));
125 fillView(tableSelectionView);
127 // we're done, set the widget
128 containerWidget.setWidget(tableSelectionView.asWidget());
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>() {
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()");
141 public void onSuccess(final DocumentInfo result) {
142 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
144 if (StringUtils.isEmpty(tableName)) {
145 tableName = result.getTableNames().get(result.getDefaultTableIndex());
148 tableSelectionView.setSelectedTableName(tableName);
150 tableSelectionView.setLocaleList(result.getLocaleIDs(), result.getLocaleTitles());
152 final String localeID = Utils.getCurrentLocaleID();
153 tableSelectionView.setSelectedLocale(localeID);
155 documentTitle = result.getTitle();
156 tableSelectionView.setDocumentTitle(documentTitle);
157 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
160 final String localeID = Utils.getCurrentLocaleID();
161 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, localeID, callback);
163 // get the reports list for the current table:
164 final AsyncCallback<Reports> callback_report = new AsyncCallback<Reports>() {
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()");
172 public void onSuccess(final Reports result) {
173 tableSelectionView.setReportList(result);
176 OnlineGlomServiceAsync.Util.getInstance().getReportsList(documentID, tableName, localeID, callback_report);
178 // Show the quickFind text that was specified by the URL token:
179 tableSelectionView.setQuickFindText(quickFind);
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();
189 final HasRecordsPlace asPlace = (HasRecordsPlace) place;
190 quickFind = asPlace.getQuickFind();
191 } catch (final ClassCastException ex) {
195 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
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);
205 fillView(tableSelectionView);
208 private void clearView() {
209 clientFactory.getTableSelectionView().clear();
211 if (tableChangeHandlerRegistration != null) {
212 tableChangeHandlerRegistration.removeHandler();
213 tableChangeHandlerRegistration = null;
216 if (quickFindChangeHandlerRegistration != null) {
217 quickFindChangeHandlerRegistration.removeHandler();
218 quickFindChangeHandlerRegistration = null;
221 if (localeChangeHandlerRegistration != null) {
222 localeChangeHandlerRegistration.removeHandler();
223 localeChangeHandlerRegistration = null;
230 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
233 public void onCancel() {
240 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
243 public void onStop() {
250 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
253 public void goTo(final Place place) {
254 clientFactory.getPlaceController().goTo(place);