Use GWT.log for client-side debugging statements.
[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.shared.DocumentInfo;
30
31 import com.google.gwt.activity.shared.AbstractActivity;
32 import com.google.gwt.core.client.GWT;
33 import com.google.gwt.event.dom.client.ChangeEvent;
34 import com.google.gwt.event.dom.client.ChangeHandler;
35 import com.google.gwt.event.dom.client.HasChangeHandlers;
36 import com.google.gwt.event.shared.EventBus;
37 import com.google.gwt.event.shared.HandlerRegistration;
38 import com.google.gwt.place.shared.Place;
39 import com.google.gwt.user.client.Window;
40 import com.google.gwt.user.client.rpc.AsyncCallback;
41 import com.google.gwt.user.client.ui.AcceptsOneWidget;
42 import com.google.gwt.user.client.ui.ListBox;
43
44 /**
45  * @author Ben Konrath <ben@bagu.org>
46  * 
47  */
48 public class TableSelectionActivity extends AbstractActivity implements TableSelectionView.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                 HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
70
71                 changeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
72                         @Override
73                         public void onChange(ChangeEvent event) {
74                                 ListBox listBox = (ListBox) event.getSource();
75                                 int selectedIndex = listBox.getSelectedIndex();
76                                 eventBus.fireEvent(new TableChangeEvent(selectedIndex < 0 ? "" : listBox.getValue(selectedIndex)));
77                                 Window.setTitle(documentTitle + (selectedIndex < 0 ? "" : ": " + listBox.getItemText(selectedIndex)));
78                         }
79                 });
80
81                 // get the table names, table titles and default table index for the current document
82                 AsyncCallback<DocumentInfo> callback = new AsyncCallback<DocumentInfo>() {
83                         public void onFailure(Throwable caught) {
84                                 // TODO: create a way to notify users of asynchronous callback failures
85                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getDocumentInfo()");
86                         }
87
88                         public void onSuccess(DocumentInfo result) {
89                                 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
90
91                                 if (tableName == null || tableName.isEmpty()) {
92                                         tableName = result.getTableNames().get(result.getDefaultTableIndex());
93                                 }
94
95                                 tableSelectionView.setSelectedTableName(tableName);
96                                 documentTitle = result.getTitle();
97                                 tableSelectionView.setDocumentTitle(documentTitle);
98                                 Window.setTitle(documentTitle + ": " + result.getTableTitles().get(result.getDefaultTableIndex()));
99                         }
100                 };
101                 OnlineGlomServiceAsync.Util.getInstance().getDocumentInfo(documentID, callback);
102
103                 // we're done, set the widget
104                 containerWidget.setWidget(tableSelectionView.asWidget());
105         }
106
107         // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method
108         public void setPlace(HasSelectableTablePlace place) {
109                 this.documentID = place.getDocumentID();
110                 this.tableName = place.getTableName();
111
112                 TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
113
114                 // show the 'back to list' link if we're at a DetailsPlace, hide it otherwise
115                 if (place instanceof DetailsPlace) {
116                         tableSelectionView.setBackLinkVisible(true);
117                         tableSelectionView.setBackLink(documentID, tableName);
118                 } else if (place instanceof ListPlace) {
119                         tableSelectionView.setBackLinkVisible(false);
120                 }
121         }
122
123         private void clearView() {
124                 clientFactory.getTableSelectionView().clear();
125                 if (changeHandlerRegistration != null) {
126                         changeHandlerRegistration.removeHandler();
127                         changeHandlerRegistration = null;
128                 }
129         }
130
131         /*
132          * (non-Javadoc)
133          * 
134          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
135          */
136         @Override
137         public void onCancel() {
138                 clearView();
139         }
140
141         /*
142          * (non-Javadoc)
143          * 
144          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
145          */
146         @Override
147         public void onStop() {
148                 clearView();
149         }
150
151         /*
152          * (non-Javadoc)
153          * 
154          * @see org.glom.web.client.ui.TableSelectionView.Presenter#goTo(com.google.gwt.place.shared.Place)
155          */
156         @Override
157         public void goTo(Place place) {
158                 clientFactory.getPlaceController().goTo(place);
159         }
160
161 }