Add the table name to the URL token for the ListPlace.
[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.GlomDocument;
30
31 import com.google.gwt.activity.shared.AbstractActivity;
32 import com.google.gwt.event.dom.client.ChangeEvent;
33 import com.google.gwt.event.dom.client.ChangeHandler;
34 import com.google.gwt.event.dom.client.HasChangeHandlers;
35 import com.google.gwt.event.shared.EventBus;
36 import com.google.gwt.event.shared.HandlerRegistration;
37 import com.google.gwt.place.shared.Place;
38 import com.google.gwt.user.client.rpc.AsyncCallback;
39 import com.google.gwt.user.client.ui.AcceptsOneWidget;
40
41 /**
42  * @author Ben Konrath <ben@bagu.org>
43  * 
44  */
45 public class TableSelectionActivity extends AbstractActivity implements TableSelectionView.Presenter {
46         private final ClientFactory clientFactory;
47         private String documentID;
48         private String tableName;
49         private HandlerRegistration changeHandlerRegistration = null;
50
51         // This activity isn't properly configured until the List or Details Place is set with the appropriate methods
52         public TableSelectionActivity(ClientFactory clientFactory) {
53                 this.clientFactory = clientFactory;
54         }
55
56         /**
57          * Invoked by the ActivityManager to start a new Activity
58          */
59         @Override
60         public void start(AcceptsOneWidget containerWidget, final EventBus eventBus) {
61                 // FIXME set the window title with the title from the glom document
62                 // Window.setTitle("Online Glom - " + documentTitle);
63
64                 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
65                 tableSelectionView.setPresenter(this);
66
67                 HasChangeHandlers tableSelector = tableSelectionView.getTableSelector();
68
69                 changeHandlerRegistration = tableSelector.addChangeHandler(new ChangeHandler() {
70                         @Override
71                         public void onChange(ChangeEvent event) {
72                                 eventBus.fireEvent(new TableChangeEvent(tableSelectionView.getSelectedTable()));
73                         }
74                 });
75
76                 // get the table names, table titles and default table index for the current document
77                 AsyncCallback<GlomDocument> callback = new AsyncCallback<GlomDocument>() {
78                         public void onFailure(Throwable caught) {
79                                 // FIXME: need to deal with failure
80                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getGlomDocument()");
81                         }
82
83                         public void onSuccess(GlomDocument result) {
84                                 tableSelectionView.setTableSelection(result.getTableNames(), result.getTableTitles());
85
86                                 if (tableName == null || tableName.isEmpty()) {
87                                         tableName = result.getTableNames().get(result.getDefaultTableIndex());
88                                 }
89
90                                 tableSelectionView.setSelectedTableName(tableName);
91                         }
92                 };
93                 OnlineGlomServiceAsync.Util.getInstance().getGlomDocument(documentID, callback);
94
95                 // we're done, set the widget
96                 containerWidget.setWidget(tableSelectionView.asWidget());
97         }
98
99         // This method will be called before the {@link TableSelectionActivity#start(AcceptsOneWidget, EventBus)} method
100         public void setPlace(HasSelectableTablePlace place) {
101                 this.documentID = place.getDocumentID();
102                 this.tableName = place.getTableName();
103
104                 TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
105
106                 // show the 'back to list' link if we're at a DetailsPlace, hide it otherwise
107                 if (place instanceof DetailsPlace) {
108                         tableSelectionView.setBackLinkVisible(true);
109                         tableSelectionView.setBackLink(documentID, tableName);
110                 } else if (place instanceof ListPlace) {
111                         tableSelectionView.setBackLinkVisible(false);
112                 }
113         }
114
115         private void clearView() {
116                 clientFactory.getTableSelectionView().clear();
117                 if (changeHandlerRegistration != null) {
118                         changeHandlerRegistration.removeHandler();
119                         changeHandlerRegistration = null;
120                 }
121         }
122
123         /*
124          * (non-Javadoc)
125          * 
126          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
127          */
128         @Override
129         public void onCancel() {
130                 clearView();
131         }
132
133         /*
134          * (non-Javadoc)
135          * 
136          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
137          */
138         @Override
139         public void onStop() {
140                 clearView();
141         }
142
143         /*
144          * (non-Javadoc)
145          * 
146          * @see org.glom.web.client.ui.TableSelectionView.Presenter#goTo(com.google.gwt.place.shared.Place)
147          */
148         @Override
149         public void goTo(Place place) {
150                 clientFactory.getPlaceController().goTo(place);
151         }
152
153 }