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 HandlerRegistration tableChangeHandlerRegistration = null;
55 private HandlerRegistration quickFindChangeHandlerRegistration = null;
57 // This activity isn't properly configured until the List or Details Place is set with the appropriate methods
58 public TableSelectionActivity(final ClientFactory clientFactory) {
59 this.clientFactory = clientFactory;
63 * Invoked by the ActivityManager to start a new Activity
66 public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) {
68 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
69 tableSelectionView.setPresenter(this);
71 // For table changes with the tableSelector:
72 final HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
73 tableChangeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
75 public void onChange(final ChangeEvent event) {
76 // Fire a table change event so that other views (e.g. the details view) know about the change and can
78 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTableName()));
80 // Update the browser title because there's place change and the setPlace() method will not be called.
81 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
85 // For table changes with the tableSelector:
86 final HasChangeHandlers quickFindBox = tableSelectionView.getQuickFindBox();
87 quickFindChangeHandlerRegistration = quickFindBox.addChangeHandler(new ChangeHandler() {
89 public void onChange(final ChangeEvent event) {
90 // Fire a quickfind change event so that other views (e.g. the details view) know about the change and
93 eventBus.fireEvent(new QuickFindChangeEvent(tableSelectionView.getQuickFindText()));
95 // Update the browser title because there's place change and the setPlace() method will not be called.
96 // TODO? Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
100 // get the table names, table titles and default table index for the current document
101 final AsyncCallback<DocumentInfo> callback = new AsyncCallback<DocumentInfo>() {
103 public void onFailure(final Throwable caught) {
104 // TODO: create a way to notify users of asynchronous callback failures
105 GWT.log("AsyncCallback Failed: OnlineGlomService.getDocumentInfo()");
109 public void onSuccess(final DocumentInfo result) {
110 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
112 if (tableName == null || tableName.isEmpty()) {
113 tableName = result.getTableNames().get(result.getDefaultTableIndex());
116 tableSelectionView.setSelectedTableName(tableName);
117 documentTitle = result.getTitle();
118 tableSelectionView.setDocumentTitle(documentTitle);
119 Window.setTitle(documentTitle + ": " + result.getTableTitles().get(result.getDefaultTableIndex()));
122 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, localeID, callback);
124 // we're done, set the widget
125 containerWidget.setWidget(tableSelectionView.asWidget());
128 // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method and
129 // any time the Place changes after the start method has been called.
130 public void setPlace(final HasSelectableTablePlace place) {
131 documentID = place.getDocumentID();
132 tableName = place.getTableName();
133 localeID = place.getLocaleID();
135 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
137 // Update the selected table if it's not correct.
138 if (!tableSelectionView.getSelectedTableName().equals(tableName)) {
139 tableSelectionView.setSelectedTableName(tableName);
142 // Update the browser title if document title has already been setup.
143 if (documentTitle != null && !documentTitle.isEmpty()) {
144 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
147 // show the 'back to list' link if we're at a DetailsPlace, hide it otherwise
148 if (place instanceof DetailsPlace) {
149 tableSelectionView.setBackLinkVisible(true);
150 tableSelectionView.setBackLink(documentID, tableName, localeID, ""); // TODO: quickfind?
151 } else if (place instanceof ListPlace) {
152 tableSelectionView.setBackLinkVisible(false);
156 private void clearView() {
157 clientFactory.getTableSelectionView().clear();
159 if (tableChangeHandlerRegistration != null) {
160 tableChangeHandlerRegistration.removeHandler();
161 tableChangeHandlerRegistration = null;
164 if (quickFindChangeHandlerRegistration != null) {
165 quickFindChangeHandlerRegistration.removeHandler();
166 quickFindChangeHandlerRegistration = null;
173 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
176 public void onCancel() {
183 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
186 public void onStop() {
193 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
196 public void goTo(final Place place) {
197 clientFactory.getPlaceController().goTo(place);