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());
144 tableSelectionView.setSelectedLocale(localeID);
146 documentTitle = result.getTitle();
147 tableSelectionView.setDocumentTitle(documentTitle);
148 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
151 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, localeID, callback);
153 // Show the quickFind text that was specified by the URL token:
154 tableSelectionView.setQuickFindText(quickFind);
157 // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method and
158 // any time the Place changes after the start method has been called.
159 public void setPlace(final HasSelectableTablePlace place) {
160 documentID = place.getDocumentID();
161 tableName = place.getTableName();
162 localeID = place.getLocaleID();
165 final ListPlace asPlace = (ListPlace) place;
166 quickFind = asPlace.getQuickFind();
167 } catch (final ClassCastException ex) {
171 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
173 // Update the selected table if it's not correct.
174 if (!tableSelectionView.getSelectedTableName().equals(tableName)) {
175 tableSelectionView.setSelectedTableName(tableName);
178 // show the 'back to list' link if we're at a DetailsPlace, hide it otherwise
179 if (place instanceof DetailsPlace) {
180 tableSelectionView.setBackLinkVisible(true);
181 tableSelectionView.setBackLink(documentID, tableName, localeID, ""); // TODO: quickfind?
182 } else if (place instanceof ListPlace) {
183 tableSelectionView.setBackLinkVisible(false);
186 fillView(tableSelectionView);
189 private void clearView() {
190 clientFactory.getTableSelectionView().clear();
192 if (tableChangeHandlerRegistration != null) {
193 tableChangeHandlerRegistration.removeHandler();
194 tableChangeHandlerRegistration = null;
197 if (quickFindChangeHandlerRegistration != null) {
198 quickFindChangeHandlerRegistration.removeHandler();
199 quickFindChangeHandlerRegistration = null;
202 if (localeChangeHandlerRegistration != null) {
203 localeChangeHandlerRegistration.removeHandler();
204 localeChangeHandlerRegistration = null;
211 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
214 public void onCancel() {
221 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
224 public void onStop() {
231 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
234 public void goTo(final Place place) {
235 clientFactory.getPlaceController().goTo(place);