Enable the "Details" buttons.
[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.ListPlace;
27 import org.glom.web.client.ui.TableSelectionView;
28 import org.glom.web.shared.GlomDocument;
29
30 import com.google.gwt.activity.shared.AbstractActivity;
31 import com.google.gwt.event.dom.client.ChangeEvent;
32 import com.google.gwt.event.dom.client.ChangeHandler;
33 import com.google.gwt.event.dom.client.HasChangeHandlers;
34 import com.google.gwt.event.shared.EventBus;
35 import com.google.gwt.event.shared.HandlerRegistration;
36 import com.google.gwt.user.client.Window;
37 import com.google.gwt.user.client.rpc.AsyncCallback;
38 import com.google.gwt.user.client.ui.AcceptsOneWidget;
39
40 /**
41  * @author Ben Konrath <ben@bagu.org>
42  * 
43  */
44 public class TableSelectionActivity extends AbstractActivity {
45         private final ClientFactory clientFactory;
46         private String documentTitle;
47         private HandlerRegistration changeHandlerRegistration = null;
48
49         public TableSelectionActivity(ListPlace place, ClientFactory clientFactory) {
50                 this.documentTitle = place.getDocumentTitle();
51                 this.clientFactory = clientFactory;
52         }
53
54         public TableSelectionActivity(DetailsPlace place, ClientFactory clientFactory) {
55                 this.documentTitle = place.getDocumentTitle();
56                 this.clientFactory = clientFactory;
57         }
58
59         /**
60          * Invoked by the ActivityManager to start a new Activity
61          */
62         @Override
63         public void start(AcceptsOneWidget containerWidget, final EventBus eventBus) {
64                 Window.setTitle("Online Glom - " + documentTitle);
65
66                 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
67
68                 HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
69
70                 changeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
71                         @Override
72                         public void onChange(ChangeEvent event) {
73                                 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTable()));
74                         }
75                 });
76
77                 // get the table names, table titles and default table index for the current document
78                 AsyncCallback<GlomDocument> callback = new AsyncCallback<GlomDocument>() {
79                         public void onFailure(Throwable caught) {
80                                 // FIXME: need to deal with failure
81                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getGlomDocument()");
82                         }
83
84                         public void onSuccess(GlomDocument result) {
85                                 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
86                                 tableSelectionView.setTableSelectedIndex(result.getDefaultTableIndex());
87                         }
88                 };
89                 OnlineGlomServiceAsync.Util.getInstance().getGlomDocument(documentTitle, callback);
90
91                 // we're done, set the widget
92                 containerWidget.setWidget(tableSelectionView.asWidget());
93         }
94
95         private void clearView() {
96                 clientFactory.getTableSelectionView().clear();
97                 if (changeHandlerRegistration != null) {
98                         changeHandlerRegistration.removeHandler();
99                         changeHandlerRegistration = null;
100                 }
101         }
102
103         /*
104          * (non-Javadoc)
105          * 
106          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
107          */
108         @Override
109         public void onCancel() {
110                 clearView();
111         }
112
113         /*
114          * (non-Javadoc)
115          * 
116          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
117          */
118         @Override
119         public void onStop() {
120                 clearView();
121         }
122 }