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