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.event.LocaleChangeEvent;
26 import org.glom.web.client.event.QuickFindChangeEvent;
27 import org.glom.web.client.event.TableChangeEvent;
28 import org.glom.web.client.place.DetailsPlace;
29 import org.glom.web.client.place.HasSelectableTablePlace;
30 import org.glom.web.client.place.ListPlace;
31 import org.glom.web.client.ui.TableSelectionView;
32 import org.glom.web.client.ui.View;
33 import org.glom.web.shared.DocumentInfo;
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 (StringUtils.isEmpty(tableName)) {
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 // Show the quickFind text that was specified by the URL token:
155 tableSelectionView.setQuickFindText(quickFind);
158 // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method and
159 // any time the Place changes after the start method has been called.
160 public void setPlace(final HasSelectableTablePlace place) {
161 documentID = place.getDocumentID();
162 tableName = place.getTableName();
163 localeID = place.getLocaleID();
166 final ListPlace asPlace = (ListPlace) place;
167 quickFind = asPlace.getQuickFind();
168 } catch (final ClassCastException ex) {
172 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
174 // Update the selected table if it's not correct.
175 if (!tableSelectionView.getSelectedTableName().equals(tableName)) {
176 tableSelectionView.setSelectedTableName(tableName);
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 fillView(tableSelectionView);
190 private void clearView() {
191 clientFactory.getTableSelectionView().clear();
193 if (tableChangeHandlerRegistration != null) {
194 tableChangeHandlerRegistration.removeHandler();
195 tableChangeHandlerRegistration = null;
198 if (quickFindChangeHandlerRegistration != null) {
199 quickFindChangeHandlerRegistration.removeHandler();
200 quickFindChangeHandlerRegistration = null;
203 if (localeChangeHandlerRegistration != null) {
204 localeChangeHandlerRegistration.removeHandler();
205 localeChangeHandlerRegistration = null;
212 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
215 public void onCancel() {
222 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
225 public void onStop() {
232 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
235 public void goTo(final Place place) {
236 clientFactory.getPlaceController().goTo(place);