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