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.TableChangeEvent;
25 import org.glom.web.client.place.DetailsPlace;
26 import org.glom.web.client.place.HasSelectableTablePlace;
27 import org.glom.web.client.place.ListPlace;
28 import org.glom.web.client.ui.TableSelectionView;
29 import org.glom.web.client.ui.View;
30 import org.glom.web.shared.DocumentInfo;
32 import com.google.gwt.activity.shared.AbstractActivity;
33 import com.google.gwt.core.client.GWT;
34 import com.google.gwt.event.dom.client.ChangeEvent;
35 import com.google.gwt.event.dom.client.ChangeHandler;
36 import com.google.gwt.event.dom.client.HasChangeHandlers;
37 import com.google.gwt.event.shared.EventBus;
38 import com.google.gwt.event.shared.HandlerRegistration;
39 import com.google.gwt.place.shared.Place;
40 import com.google.gwt.user.client.Window;
41 import com.google.gwt.user.client.rpc.AsyncCallback;
42 import com.google.gwt.user.client.ui.AcceptsOneWidget;
47 public class TableSelectionActivity extends AbstractActivity implements View.Presenter {
48 private final ClientFactory clientFactory;
49 private String documentID;
50 private String documentTitle;
51 private String tableName;
52 private HandlerRegistration changeHandlerRegistration = null;
54 // This activity isn't properly configured until the List or Details Place is set with the appropriate methods
55 public TableSelectionActivity(final ClientFactory clientFactory) {
56 this.clientFactory = clientFactory;
60 * Invoked by the ActivityManager to start a new Activity
63 public void start(final AcceptsOneWidget containerWidget, final EventBus eventBus) {
65 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
66 tableSelectionView.setPresenter(this);
68 // For table changes with the tableSelector:
69 HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
70 changeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
72 public void onChange(final ChangeEvent event) {
73 // Fire a table change event so that other views (e.g. the details view) know about the change and can
75 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTableName()));
77 // Update the browser title because there's place change and the setPlace() method will not be called.
78 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
82 // get the table names, table titles and default table index for the current document
83 final AsyncCallback<DocumentInfo> callback = new AsyncCallback<DocumentInfo>() {
85 public void onFailure(final Throwable caught) {
86 // TODO: create a way to notify users of asynchronous callback failures
87 GWT.log("AsyncCallback Failed: OnlineGlomService.getDocumentInfo()");
91 public void onSuccess(final DocumentInfo result) {
92 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
94 if (tableName == null || tableName.isEmpty()) {
95 tableName = result.getTableNames().get(result.getDefaultTableIndex());
98 tableSelectionView.setSelectedTableName(tableName);
99 documentTitle = result.getTitle();
100 tableSelectionView.setDocumentTitle(documentTitle);
101 Window.setTitle(documentTitle + ": " + result.getTableTitles().get(result.getDefaultTableIndex()));
104 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, callback);
106 // we're done, set the widget
107 containerWidget.setWidget(tableSelectionView.asWidget());
110 // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method and
111 // any time the Place changes after the start method has been called.
112 public void setPlace(final HasSelectableTablePlace place) {
113 documentID = place.getDocumentID();
114 tableName = place.getTableName();
116 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
118 // Update the selected table if it's not correct.
119 if (!tableSelectionView.getSelectedTableName().equals(tableName)) {
120 tableSelectionView.setSelectedTableName(tableName);
123 // Update the browser title if document title has already been setup.
124 if (documentTitle != null && !documentTitle.isEmpty()) {
125 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
128 // show the 'back to list' link if we're at a DetailsPlace, hide it otherwise
129 if (place instanceof DetailsPlace) {
130 tableSelectionView.setBackLinkVisible(true);
131 tableSelectionView.setBackLink(documentID, tableName);
132 } else if (place instanceof ListPlace) {
133 tableSelectionView.setBackLinkVisible(false);
137 private void clearView() {
138 clientFactory.getTableSelectionView().clear();
139 if (changeHandlerRegistration != null) {
140 changeHandlerRegistration.removeHandler();
141 changeHandlerRegistration = null;
148 * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
151 public void onCancel() {
158 * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
161 public void onStop() {
168 * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
171 public void goTo(final Place place) {
172 clientFactory.getPlaceController().goTo(place);