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 // Fire a locale change event so that other views (e.g. the details view) know about the change and can
109 // update themselves.
110 eventBus.fireEvent(new LocaleChangeEvent(tableSelectionView.getSelectedLocale()));
114 // get the table names, table titles and default table index for the current document
115 final AsyncCallback<DocumentInfo> callback = new AsyncCallback<DocumentInfo>() {
117 public void onFailure(final Throwable caught) {
118 // TODO: create a way to notify users of asynchronous callback failures
119 GWT.log("AsyncCallback Failed: OnlineGlomService.getDocumentInfo()");
123 public void onSuccess(final DocumentInfo result) {
124 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
126 if (tableName == null || tableName.isEmpty()) {
127 tableName = result.getTableNames().get(result.getDefaultTableIndex());
130 tableSelectionView.setSelectedTableName(tableName);
132 tableSelectionView.setLocaleList(result.getLocaleIDs(), result.getLocaleTitles());
134 documentTitle = result.getTitle();
135 tableSelectionView.setDocumentTitle(documentTitle);
136 Window.setTitle(documentTitle + ": " + result.getTableTitles().get(result.getDefaultTableIndex()));
139 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, localeID, callback);
141 // we're done, set the widget
142 containerWidget.setWidget(tableSelectionView.asWidget());
145 // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method and
146 // any time the Place changes after the start method has been called.
147 public void setPlace(final HasSelectableTablePlace place) {
148 documentID = place.getDocumentID();
149 tableName = place.getTableName();
150 localeID = place.getLocaleID();
153 final ListPlace asPlace = (ListPlace) place;
154 quickFind = asPlace.getQuickFind();
155 } catch (final ClassCastException ex) {
159 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
161 // Update the selected table if it's not correct.
162 if (!tableSelectionView.getSelectedTableName().equals(tableName)) {
163 tableSelectionView.setSelectedTableName(tableName);
166 // Update the browser title if document title has already been setup.
167 if (documentTitle != null && !documentTitle.isEmpty()) {
168 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
171 // show the 'back to list' link if we're at a DetailsPlace, hide it otherwise
172 if (place instanceof DetailsPlace) {
173 tableSelectionView.setBackLinkVisible(true);
174 tableSelectionView.setBackLink(documentID, tableName, localeID, ""); // TODO: quickfind?
175 } else if (place instanceof ListPlace) {
176 tableSelectionView.setBackLinkVisible(false);
179 // Show the quickFind text that was specified by the URL token:
180 tableSelectionView.setQuickFindText(quickFind);
182 // Show the current locale:
183 tableSelectionView.setSelectedLocale(localeID);
186 private void clearView() {
187 clientFactory.getTableSelectionView().clear();
189 if (tableChangeHandlerRegistration != null) {
190 tableChangeHandlerRegistration.removeHandler();
191 tableChangeHandlerRegistration = null;
194 if (quickFindChangeHandlerRegistration != null) {
195 quickFindChangeHandlerRegistration.removeHandler();
196 quickFindChangeHandlerRegistration = null;
199 if (localeChangeHandlerRegistration != null) {
200 localeChangeHandlerRegistration.removeHandler();
201 localeChangeHandlerRegistration = null;
208 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
211 public void onCancel() {
218 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
221 public void onStop() {
228 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
231 public void goTo(final Place place) {
232 clientFactory.getPlaceController().goTo(place);