Move the *View::Presenter types, and some API into one base View.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / TableSelectionActivity.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
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.
10  *
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
14  * for more details.
15  *
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/>.
18  */
19
20 package org.glom.web.client.activity;
21
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;
31
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;
43
44 /**
45  * @author Ben Konrath <ben@bagu.org>
46  * 
47  */
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 HandlerRegistration changeHandlerRegistration = null;
54
55         // This activity isn't properly configured until the List or Details Place is set with the appropriate methods
56         public TableSelectionActivity(ClientFactory clientFactory) {
57                 this.clientFactory = clientFactory;
58         }
59
60         /**
61          * Invoked by the ActivityManager to start a new Activity
62          */
63         @Override
64         public void start(AcceptsOneWidget containerWidget, final EventBus eventBus) {
65
66                 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
67                 tableSelectionView.setPresenter(this);
68
69                 // For table changes with the tableSelector:
70                 HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
71                 changeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
72                         @Override
73                         public void onChange(ChangeEvent event) {
74                                 // Fire an table change event so that other views (e.g. the details view) know about the change and can
75                                 // update themselves.
76                                 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTableName()));
77
78                                 // Update the browser title because there's place change and the setPlace() method will not be called.
79                                 Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
80                         }
81                 });
82
83                 // get the table names, table titles and default table index for the current document
84                 AsyncCallback<DocumentInfo> callback = new AsyncCallback<DocumentInfo>() {
85                         public void onFailure(Throwable caught) {
86                                 // TODO: create a way to notify users of asynchronous callback failures
87                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getDocumentInfo()");
88                         }
89
90                         public void onSuccess(DocumentInfo result) {
91                                 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
92
93                                 if (tableName == null || tableName.isEmpty()) {
94                                         tableName = result.getTableNames().get(result.getDefaultTableIndex());
95                                 }
96
97                                 tableSelectionView.setSelectedTableName(tableName);
98                                 documentTitle = result.getTitle();
99                                 tableSelectionView.setDocumentTitle(documentTitle);
100                                 Window.setTitle(documentTitle + ": " + result.getTableTitles().get(result.getDefaultTableIndex()));
101                         }
102                 };
103                 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, callback);
104
105                 // we're done, set the widget
106                 containerWidget.setWidget(tableSelectionView.asWidget());
107         }
108
109         // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method and
110         // any time the Place changes after the start method has been called.
111         public void setPlace(HasSelectableTablePlace place) {
112                 documentID = place.getDocumentID();
113                 tableName = place.getTableName();
114
115                 TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
116
117                 // Update the selected table if it's not correct.
118                 if (!tableSelectionView.getSelectedTableName().equals(tableName)) {
119                         tableSelectionView.setSelectedTableName(tableName);
120                 }
121
122                 // Update the browser title if document title has already been setup.
123                 if (documentTitle != null && !documentTitle.isEmpty()) {
124                         Window.setTitle(documentTitle + ": " + tableSelectionView.getSelectedTableTitle());
125                 }
126
127                 // show the 'back to list' link if we're at a DetailsPlace, hide it otherwise
128                 if (place instanceof DetailsPlace) {
129                         tableSelectionView.setBackLinkVisible(true);
130                         tableSelectionView.setBackLink(documentID, tableName);
131                 } else if (place instanceof ListPlace) {
132                         tableSelectionView.setBackLinkVisible(false);
133                 }
134         }
135
136         private void clearView() {
137                 clientFactory.getTableSelectionView().clear();
138                 if (changeHandlerRegistration != null) {
139                         changeHandlerRegistration.removeHandler();
140                         changeHandlerRegistration = null;
141                 }
142         }
143
144         /*
145          * (non-Javadoc)
146          * 
147          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
148          */
149         @Override
150         public void onCancel() {
151                 clearView();
152         }
153
154         /*
155          * (non-Javadoc)
156          * 
157          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
158          */
159         @Override
160         public void onStop() {
161                 clearView();
162         }
163
164         /*
165          * (non-Javadoc)
166          * 
167          * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
168          */
169         @Override
170         public void goTo(Place place) {
171                 clientFactory.getPlaceController().goTo(place);
172         }
173
174 }