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.QuickFindChangeEvent;
25 import org.glom.web.client.event.TableChangeEvent;
26 import org.glom.web.client.place.DetailsPlace;
27 import org.glom.web.client.place.HasSelectableTablePlace;
28 import org.glom.web.client.place.ListPlace;
29 import org.glom.web.client.ui.TableSelectionView;
30 import org.glom.web.client.ui.View;
31 import org.glom.web.shared.DocumentInfo;
33 import com.google.gwt.activity.shared.AbstractActivity;
34 import com.google.gwt.core.client.GWT;
35 import com.google.gwt.event.dom.client.ChangeEvent;
36 import com.google.gwt.event.dom.client.ChangeHandler;
37 import com.google.gwt.event.dom.client.HasChangeHandlers;
38 import com.google.gwt.event.shared.EventBus;
39 import com.google.gwt.event.shared.HandlerRegistration;
40 import com.google.gwt.place.shared.Place;
41 import com.google.gwt.user.client.Window;
42 import com.google.gwt.user.client.rpc.AsyncCallback;
43 import com.google.gwt.user.client.ui.AcceptsOneWidget;
48 public class TableSelectionActivity extends AbstractActivity implements View.Presenter {
49 private final ClientFactory clientFactory;
50 private String documentID;
51 private String documentTitle;
52 private String tableName;
53 private String localeID;
54 private String quickFind;
55 private HandlerRegistration tableChangeHandlerRegistration = null;
56 private HandlerRegistration quickFindChangeHandlerRegistration = null;
58 // This activity isn't properly configured until the List or Details Place is set with the appropriate methods
59 public TableSelectionActivity(final ClientFactory clientFactory) {
60 this.clientFactory = clientFactory;
64 * Invoked by the ActivityManager to start a new Activity
67 public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) {
69 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
70 tableSelectionView.setPresenter(this);
72 // For table changes with the tableSelector:
73 final HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
74 tableChangeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
76 public void onChange(final ChangeEvent event) {
77 // Fire a table change event so that other views (e.g. the details view) know about the change and can
79 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTableName()));
81 // Update the browser title because there's place change and the setPlace() method will not be called.
82 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
86 // For table changes with the tableSelector:
87 final HasChangeHandlers quickFindBox = tableSelectionView.getQuickFindBox();
88 quickFindChangeHandlerRegistration = quickFindBox.addChangeHandler(new ChangeHandler() {
90 public void onChange(final ChangeEvent event) {
91 // Fire a quickfind change event so that other views (e.g. the details view) know about the change and
94 eventBus.fireEvent(new QuickFindChangeEvent(tableSelectionView.getQuickFindText()));
96 // Update the browser title because there's place change and the setPlace() method will not be called.
97 // TODO? Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
101 // get the table names, table titles and default table index for the current document
102 final AsyncCallback<DocumentInfo> callback = new AsyncCallback<DocumentInfo>() {
104 public void onFailure(final Throwable caught) {
105 // TODO: create a way to notify users of asynchronous callback failures
106 GWT.log("AsyncCallback Failed: OnlineGlomService.getDocumentInfo()");
110 public void onSuccess(final DocumentInfo result) {
111 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
113 if (tableName == null || tableName.isEmpty()) {
114 tableName = result.getTableNames().get(result.getDefaultTableIndex());
117 tableSelectionView.setSelectedTableName(tableName);
118 documentTitle = result.getTitle();
119 tableSelectionView.setDocumentTitle(documentTitle);
120 Window.setTitle(documentTitle + ": " + result.getTableTitles().get(result.getDefaultTableIndex()));
123 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, localeID, callback);
125 // we're done, set the widget
126 containerWidget.setWidget(tableSelectionView.asWidget());
129 // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method and
130 // any time the Place changes after the start method has been called.
131 public void setPlace(final HasSelectableTablePlace place) {
132 documentID = place.getDocumentID();
133 tableName = place.getTableName();
134 localeID = place.getLocaleID();
137 final ListPlace asPlace = (ListPlace) place;
138 quickFind = asPlace.getQuickFind();
139 } catch (final ClassCastException ex) {
143 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
145 // Update the selected table if it's not correct.
146 if (!tableSelectionView.getSelectedTableName().equals(tableName)) {
147 tableSelectionView.setSelectedTableName(tableName);
150 // Update the browser title if document title has already been setup.
151 if (documentTitle != null && !documentTitle.isEmpty()) {
152 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
155 // show the 'back to list' link if we're at a DetailsPlace, hide it otherwise
156 if (place instanceof DetailsPlace) {
157 tableSelectionView.setBackLinkVisible(true);
158 tableSelectionView.setBackLink(documentID, tableName, localeID, ""); // TODO: quickfind?
159 } else if (place instanceof ListPlace) {
160 tableSelectionView.setBackLinkVisible(false);
163 // Show the quickFind text that was specified by the URL token:
164 tableSelectionView.setQuickFindText(quickFind);
167 private void clearView() {
168 clientFactory.getTableSelectionView().clear();
170 if (tableChangeHandlerRegistration != null) {
171 tableChangeHandlerRegistration.removeHandler();
172 tableChangeHandlerRegistration = null;
175 if (quickFindChangeHandlerRegistration != null) {
176 quickFindChangeHandlerRegistration.removeHandler();
177 quickFindChangeHandlerRegistration = null;
184 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
187 public void onCancel() {
194 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
197 public void onStop() {
204 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
207 public void goTo(final Place place) {
208 clientFactory.getPlaceController().goTo(place);