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;
34 import com.google.gwt.activity.shared.AbstractActivity;
35 import com.google.gwt.core.client.GWT;
36 import com.google.gwt.event.dom.client.ChangeEvent;
37 import com.google.gwt.event.dom.client.ChangeHandler;
38 import com.google.gwt.event.dom.client.HasChangeHandlers;
39 import com.google.gwt.event.shared.EventBus;
40 import com.google.gwt.event.shared.HandlerRegistration;
41 import com.google.gwt.place.shared.Place;
42 import com.google.gwt.user.client.Window;
43 import com.google.gwt.user.client.rpc.AsyncCallback;
44 import com.google.gwt.user.client.ui.AcceptsOneWidget;
49 public class TableSelectionActivity extends AbstractActivity implements View.Presenter {
50 private final ClientFactory clientFactory;
51 private String documentID;
52 private String documentTitle;
53 private String tableName;
54 private String localeID;
55 private String quickFind;
56 private HandlerRegistration tableChangeHandlerRegistration = null;
57 private HandlerRegistration quickFindChangeHandlerRegistration = null;
58 private HandlerRegistration localeChangeHandlerRegistration = null;
60 // This activity isn't properly configured until the List or Details Place is set with the appropriate methods
61 public TableSelectionActivity(final ClientFactory clientFactory) {
62 this.clientFactory = clientFactory;
66 * Invoked by the ActivityManager to start a new Activity
69 public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) {
71 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
72 tableSelectionView.setPresenter(this);
74 // For table changes with the tableSelector:
75 final HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
76 tableChangeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
78 public void onChange(final ChangeEvent event) {
79 // Fire a table change event so that other views (e.g. the details view) know about the change and can
81 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTableName()));
83 // Update the browser title because there's place change and the setPlace() method will not be called.
84 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
88 // For quick find changes with the quick find box:
89 final HasChangeHandlers quickFindBox = tableSelectionView.getQuickFindBox();
90 quickFindChangeHandlerRegistration = quickFindBox.addChangeHandler(new ChangeHandler() {
92 public void onChange(final ChangeEvent event) {
93 // Fire a quickfind change event so that other views (e.g. the details view) know about the change and
96 eventBus.fireEvent(new QuickFindChangeEvent(tableSelectionView.getQuickFindText()));
98 // Update the browser title because there's place change and the setPlace() method will not be called.
99 // TODO? Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
103 // For locale changes with the localeSelector:
104 final HasChangeHandlers localeSelector = tableSelectionView.getLocaleSelector();
105 localeChangeHandlerRegistration = localeSelector.addChangeHandler(new ChangeHandler() {
107 public void onChange(final ChangeEvent event) {
108 // Show the translated version of the document title and the table names:
109 localeID = tableSelectionView.getSelectedLocale();
110 fillView(tableSelectionView);
112 // Fire a locale change event so that other views (e.g. the details view) know about the change and can
113 // update themselves.
114 eventBus.fireEvent(new LocaleChangeEvent(localeID));
118 fillView(tableSelectionView);
120 // we're done, set the widget
121 containerWidget.setWidget(tableSelectionView.asWidget());
124 private void fillView(final TableSelectionView tableSelectionView) {
125 // get the table names, table titles and default table index for the current document
126 final AsyncCallback<DocumentInfo> callback = new AsyncCallback<DocumentInfo>() {
128 public void onFailure(final Throwable caught) {
129 // TODO: create a way to notify users of asynchronous callback failures
130 GWT.log("AsyncCallback Failed: OnlineGlomService.getDocumentInfo()");
134 public void onSuccess(final DocumentInfo result) {
135 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
137 if (tableName == null || tableName.isEmpty()) {
138 tableName = result.getTableNames().get(result.getDefaultTableIndex());
141 tableSelectionView.setSelectedTableName(tableName);
143 tableSelectionView.setLocaleList(result.getLocaleIDs(), result.getLocaleTitles());
145 documentTitle = result.getTitle();
146 tableSelectionView.setDocumentTitle(documentTitle);
147 Window.setTitle(documentTitle + ": " + result.getTableTitles().get(result.getDefaultTableIndex()));
150 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, localeID, callback);
153 // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method and
154 // any time the Place changes after the start method has been called.
155 public void setPlace(final HasSelectableTablePlace place) {
156 documentID = place.getDocumentID();
157 tableName = place.getTableName();
158 localeID = place.getLocaleID();
161 final ListPlace asPlace = (ListPlace) place;
162 quickFind = asPlace.getQuickFind();
163 } catch (final ClassCastException ex) {
167 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
169 // Update the selected table if it's not correct.
170 if (!tableSelectionView.getSelectedTableName().equals(tableName)) {
171 tableSelectionView.setSelectedTableName(tableName);
174 // Update the browser title if document title has already been setup.
175 if (documentTitle != null && !documentTitle.isEmpty()) {
176 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
179 // show the 'back to list' link if we're at a DetailsPlace, hide it otherwise
180 if (place instanceof DetailsPlace) {
181 tableSelectionView.setBackLinkVisible(true);
182 tableSelectionView.setBackLink(documentID, tableName, localeID, ""); // TODO: quickfind?
183 } else if (place instanceof ListPlace) {
184 tableSelectionView.setBackLinkVisible(false);
187 // Show the quickFind text that was specified by the URL token:
188 tableSelectionView.setQuickFindText(quickFind);
190 // Show the current locale:
191 tableSelectionView.setSelectedLocale(localeID);
194 private void clearView() {
195 clientFactory.getTableSelectionView().clear();
197 if (tableChangeHandlerRegistration != null) {
198 tableChangeHandlerRegistration.removeHandler();
199 tableChangeHandlerRegistration = null;
202 if (quickFindChangeHandlerRegistration != null) {
203 quickFindChangeHandlerRegistration.removeHandler();
204 quickFindChangeHandlerRegistration = null;
207 if (localeChangeHandlerRegistration != null) {
208 localeChangeHandlerRegistration.removeHandler();
209 localeChangeHandlerRegistration = null;
216 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
219 public void onCancel() {
226 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
229 public void onStop() {
236 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
239 public void goTo(final Place place) {
240 clientFactory.getPlaceController().goTo(place);