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.event.LocaleChangeEvent;
25 import org.glom.web.client.event.QuickFindChangeEvent;
26 import org.glom.web.client.event.TableChangeEvent;
27 import org.glom.web.client.place.DetailsPlace;
28 import org.glom.web.client.place.HasSelectableTablePlace;
29 import org.glom.web.client.place.ListPlace;
30 import org.glom.web.client.ui.TableSelectionView;
31 import org.glom.web.client.ui.View;
32 import org.glom.web.shared.DocumentInfo;
33 import org.glom.web.shared.Reports;
35 import com.google.gwt.activity.shared.AbstractActivity;
36 import com.google.gwt.core.client.GWT;
37 import com.google.gwt.event.dom.client.ChangeEvent;
38 import com.google.gwt.event.dom.client.ChangeHandler;
39 import com.google.gwt.event.dom.client.HasChangeHandlers;
40 import com.google.gwt.event.shared.EventBus;
41 import com.google.gwt.event.shared.HandlerRegistration;
42 import com.google.gwt.place.shared.Place;
43 import com.google.gwt.user.client.Window;
44 import com.google.gwt.user.client.rpc.AsyncCallback;
45 import com.google.gwt.user.client.ui.AcceptsOneWidget;
50 public class TableSelectionActivity extends AbstractActivity implements View.Presenter {
51 private final ClientFactory clientFactory;
52 private String documentID;
53 private String documentTitle;
54 private String tableName;
55 private String localeID;
56 private String quickFind;
57 private HandlerRegistration tableChangeHandlerRegistration = null;
58 private HandlerRegistration quickFindChangeHandlerRegistration = null;
59 private HandlerRegistration localeChangeHandlerRegistration = null;
61 // This activity isn't properly configured until the List or Details Place is set with the appropriate methods
62 public TableSelectionActivity(final ClientFactory clientFactory) {
63 this.clientFactory = clientFactory;
67 * Invoked by the ActivityManager to start a new Activity
70 public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) {
72 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
73 tableSelectionView.setPresenter(this);
75 // For table changes with the tableSelector:
76 final HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
77 tableChangeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
79 public void onChange(final ChangeEvent event) {
80 // Fire a table change event so that other views (e.g. the details view) know about the change and can
82 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTableName()));
84 // Update the browser title because there's place change and the setPlace() method will not be called.
85 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
89 // For quick find changes with the quick find box:
90 final HasChangeHandlers quickFindBox = tableSelectionView.getQuickFindBox();
91 quickFindChangeHandlerRegistration = quickFindBox.addChangeHandler(new ChangeHandler() {
93 public void onChange(final ChangeEvent event) {
94 // Fire a quickfind change event so that other views (e.g. the details view) know about the change and
97 eventBus.fireEvent(new QuickFindChangeEvent(tableSelectionView.getQuickFindText()));
99 // Update the browser title because there's place change and the setPlace() method will not be called.
100 // TODO? Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
104 // For locale changes with the localeSelector:
105 final HasChangeHandlers localeSelector = tableSelectionView.getLocaleSelector();
106 localeChangeHandlerRegistration = localeSelector.addChangeHandler(new ChangeHandler() {
108 public void onChange(final ChangeEvent event) {
109 // Show the translated version of the document title and the table names:
110 localeID = tableSelectionView.getSelectedLocale();
111 fillView(tableSelectionView);
113 // Fire a locale change event so that other views (e.g. the details view) know about the change and can
114 // update themselves.
115 eventBus.fireEvent(new LocaleChangeEvent(localeID));
119 fillView(tableSelectionView);
121 // we're done, set the widget
122 containerWidget.setWidget(tableSelectionView.asWidget());
125 private void fillView(final TableSelectionView tableSelectionView) {
126 // get the table names, table titles and default table index for the current document
127 final AsyncCallback<DocumentInfo> callback = new AsyncCallback<DocumentInfo>() {
129 public void onFailure(final Throwable caught) {
130 // TODO: create a way to notify users of asynchronous callback failures
131 GWT.log("AsyncCallback Failed: OnlineGlomService.getDocumentInfo()");
135 public void onSuccess(final DocumentInfo result) {
136 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
138 if (tableName == null || tableName.isEmpty()) {
139 tableName = result.getTableNames().get(result.getDefaultTableIndex());
142 tableSelectionView.setSelectedTableName(tableName);
144 tableSelectionView.setLocaleList(result.getLocaleIDs(), result.getLocaleTitles());
145 tableSelectionView.setSelectedLocale(localeID);
147 documentTitle = result.getTitle();
148 tableSelectionView.setDocumentTitle(documentTitle);
149 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
152 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, localeID, callback);
154 // get the reports list for the current table:
155 final AsyncCallback<Reports> callback_report = new AsyncCallback<Reports>() {
157 public void onFailure(final Throwable caught) {
158 // TODO: create a way to notify users of asynchronous callback failures
159 GWT.log("AsyncCallback Failed: OnlineGlomService.getReportsList()");
163 public void onSuccess(final Reports result) {
164 tableSelectionView.setReportList(result);
167 OnlineGlomServiceAsync.Util.getInstance().getReportsList(documentID, tableName, localeID, callback_report);
169 // Show the quickFind text that was specified by the URL token:
170 tableSelectionView.setQuickFindText(quickFind);
173 // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method and
174 // any time the Place changes after the start method has been called.
175 public void setPlace(final HasSelectableTablePlace place) {
176 documentID = place.getDocumentID();
177 tableName = place.getTableName();
178 localeID = place.getLocaleID();
181 final ListPlace asPlace = (ListPlace) place;
182 quickFind = asPlace.getQuickFind();
183 } catch (final ClassCastException ex) {
187 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
189 // show the 'back to list' link if we're at a DetailsPlace, hide it otherwise
190 if (place instanceof DetailsPlace) {
191 tableSelectionView.setBackLinkVisible(true);
192 tableSelectionView.setBackLink(documentID, tableName, localeID, ""); // TODO: quickfind?
193 } else if (place instanceof ListPlace) {
194 tableSelectionView.setBackLinkVisible(false);
197 fillView(tableSelectionView);
200 private void clearView() {
201 clientFactory.getTableSelectionView().clear();
203 if (tableChangeHandlerRegistration != null) {
204 tableChangeHandlerRegistration.removeHandler();
205 tableChangeHandlerRegistration = null;
208 if (quickFindChangeHandlerRegistration != null) {
209 quickFindChangeHandlerRegistration.removeHandler();
210 quickFindChangeHandlerRegistration = null;
213 if (localeChangeHandlerRegistration != null) {
214 localeChangeHandlerRegistration.removeHandler();
215 localeChangeHandlerRegistration = null;
222 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
225 public void onCancel() {
232 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
235 public void onStop() {
242 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
245 public void goTo(final Place place) {
246 clientFactory.getPlaceController().goTo(place);