Add navigation buttons in the details view.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / DetailsActivity.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 java.util.ArrayList;
23
24 import org.glom.web.client.ClientFactory;
25 import org.glom.web.client.OnlineGlomServiceAsync;
26 import org.glom.web.client.Utils;
27 import org.glom.web.client.event.TableChangeEvent;
28 import org.glom.web.client.event.TableChangeEventHandler;
29 import org.glom.web.client.place.DetailsPlace;
30 import org.glom.web.client.ui.DetailsView;
31 import org.glom.web.client.ui.details.DetailsCell;
32 import org.glom.web.client.ui.details.Portal;
33 import org.glom.web.client.ui.details.RelatedListTable;
34 import org.glom.web.shared.DataItem;
35 import org.glom.web.shared.DetailsLayoutAndData;
36 import org.glom.web.shared.layout.LayoutGroup;
37 import org.glom.web.shared.layout.LayoutItemField;
38 import org.glom.web.shared.layout.LayoutItemPortal;
39
40 import com.google.gwt.activity.shared.AbstractActivity;
41 import com.google.gwt.core.client.GWT;
42 import com.google.gwt.event.dom.client.ClickEvent;
43 import com.google.gwt.event.dom.client.ClickHandler;
44 import com.google.gwt.event.shared.EventBus;
45 import com.google.gwt.place.shared.Place;
46 import com.google.gwt.user.client.rpc.AsyncCallback;
47 import com.google.gwt.user.client.ui.AcceptsOneWidget;
48
49 /**
50  * @author Ben Konrath <ben@bagu.org>
51  */
52 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
53         private final String documentID;
54         private final String tableName;
55         private String primaryKeyValue;
56         private final ClientFactory clientFactory;
57         private final DetailsView detailsView;
58
59         ArrayList<DetailsCell> detailsCells;
60         ArrayList<Portal> portals;
61
62         public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
63                 this.documentID = place.getDocumentID();
64                 this.tableName = place.getTableName();
65                 this.primaryKeyValue = place.getPrimaryKeyValue();
66                 this.clientFactory = clientFactory;
67                 detailsView = clientFactory.getDetailsView();
68         }
69
70         /*
71          * (non-Javadoc)
72          * 
73          * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
74          * com.google.gwt.event.shared.EventBus)
75          */
76         @Override
77         public void start(AcceptsOneWidget panel, EventBus eventBus) {
78                 // register this class as the presenter
79                 detailsView.setPresenter(this);
80
81                 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
82
83                 // set the change handler for the table selection widget
84                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
85                         @Override
86                         public void onTableChange(final TableChangeEvent event) {
87                                 goTo(new DetailsPlace(documentID, event.getNewTableName(), ""));
88                         }
89                 });
90
91                 // get the layout and data for the DetailsView
92                 AsyncCallback<DetailsLayoutAndData> callback = new AsyncCallback<DetailsLayoutAndData>() {
93                         public void onFailure(Throwable caught) {
94                                 // TODO: create a way to notify users of asynchronous callback failures
95                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsLayoutAndData()");
96                         }
97
98                         @Override
99                         public void onSuccess(DetailsLayoutAndData result) {
100
101                                 createLayout(result.getLayout());
102
103                                 setData(result.getData());
104
105                         }
106
107                 };
108                 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
109                                 callback);
110
111                 // indicate that the view is ready to be displayed
112                 panel.setWidget(detailsView.asWidget());
113         }
114
115         /*
116          * Create the layout.
117          */
118         private void createLayout(ArrayList<LayoutGroup> layout) {
119                 // add the groups
120                 for (LayoutGroup layoutGroup : layout) {
121                         detailsView.addGroup(layoutGroup);
122                 }
123
124                 // save references to the DetailsCells and the Portals
125                 detailsCells = detailsView.getCells();
126                 portals = detailsView.getPortals();
127
128                 // Setup click handlers for the navigation buttons
129                 for (final DetailsCell detailsCell : detailsCells) {
130                         final LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
131                         if (layoutItemField.getAddNavigation()) {
132                                 detailsCell.setOpenButtonClickHandler(new ClickHandler() {
133                                         @Override
134                                         public void onClick(ClickEvent event) {
135
136                                                 String newTableName = tableName;
137                                                 if (layoutItemField.getNavigationTableName() != null) {
138                                                         newTableName = layoutItemField.getNavigationTableName();
139                                                 }
140
141                                                 // Ensure the new table name is valid.
142                                                 if (newTableName != null && !newTableName.isEmpty()) {
143
144                                                         // Go to a new DetailsPlace if the table name has changed.
145                                                         String newPrimaryKeyValue = Utils.getKeyValueStringForQuery(layoutItemField.getType(),
146                                                                         detailsCell.getData());
147                                                         if (!newTableName.equals(tableName)) {
148                                                                 goTo(new DetailsPlace(documentID, newTableName, newPrimaryKeyValue));
149                                                         } else {
150                                                                 primaryKeyValue = newPrimaryKeyValue;
151                                                                 refreshData();
152                                                         }
153                                                 }
154
155                                         }
156                                 });
157                         }
158                 }
159
160         }
161
162         /*
163          * Set the data.
164          */
165         private void setData(DataItem[] data) {
166
167                 if (data == null)
168                         return;
169
170                 // TODO create proper client side logging
171                 if (data.length != detailsCells.size())
172                         GWT.log("Warning: The number of data items doesn't match the number of data detailsCells.");
173
174                 for (int i = 0; i < Math.min(detailsCells.size(), data.length); i++) {
175                         DetailsCell detailsCell = detailsCells.get(i);
176                         if (data[i] != null) {
177
178                                 // set the DatailsItem
179                                 detailsCell.setData(data[i]);
180
181                                 // see if there are any related lists that need to be setup
182                                 for (Portal portal : portals) {
183                                         LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
184                                         LayoutItemPortal layoutItemPortal = portal.getLayoutItem();
185                                         if (layoutItemField.getName().equals(layoutItemPortal.getFromField())) {
186                                                 String foreignKeyValue = Utils.getKeyValueStringForQuery(layoutItemField.getType(), data[i]);
187                                                 if (foreignKeyValue == null)
188                                                         continue;
189                                                 RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
190                                                                 foreignKeyValue);
191                                                 portal.setContents(relatedListTable);
192                                                 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(), foreignKeyValue);
193                                         }
194                                 }
195                         }
196                 }
197         }
198
199         private void refreshData() {
200
201                 // get the data for the DetailsView
202                 AsyncCallback<DataItem[]> callback = new AsyncCallback<DataItem[]>() {
203                         public void onFailure(Throwable caught) {
204                                 // TODO: create a way to notify users of asynchronous callback failures
205                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsData()");
206                         }
207
208                         @Override
209                         public void onSuccess(DataItem[] result) {
210                                 setData(result);
211                         }
212                 };
213
214                 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentID, tableName, primaryKeyValue, callback);
215
216         }
217
218         // sets the row count for the related list table
219         private void setRowCountForRelatedListTable(final RelatedListTable relatedListTable, String relationshipName,
220                         String foreignKeyValue) {
221                 AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
222                         public void onFailure(Throwable caught) {
223                                 // TODO: create a way to notify users of asynchronous callback failures
224                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getRelatedListRowCount()");
225                         }
226
227                         @Override
228                         public void onSuccess(Integer result) {
229                                 if (result.intValue() <= relatedListTable.getMinNumVisibleRows()) {
230                                         // Set the table row count to the minimum row count if the data row count is less than or equal to
231                                         // the minimum row count. This ensures that data with fewer rows than the minimum will not create
232                                         // indexes in the underlying CellTable that will override the rendering of the empty rows.
233                                         relatedListTable.setRowCount(relatedListTable.getMinNumVisibleRows());
234                                 } else {
235                                         // Set the table row count to the data row count if it's larger than the minimum number of rows
236                                         // visible.
237                                         relatedListTable.setRowCount(result.intValue());
238                                 }
239                         }
240                 };
241
242                 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
243                                 foreignKeyValue, callback);
244         }
245
246         /*
247          * (non-Javadoc)
248          * 
249          * @see com.google.gwt.activity.shared.Activity#onCancel()
250          */
251         @Override
252         public void onCancel() {
253                 detailsView.clear();
254         }
255
256         /*
257          * (non-Javadoc)
258          * 
259          * @see com.google.gwt.activity.shared.Activity#onStop()
260          */
261         @Override
262         public void onStop() {
263                 detailsView.clear();
264         }
265
266         /*
267          * (non-Javadoc)
268          * 
269          * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
270          */
271         @Override
272         public void goTo(Place place) {
273                 clientFactory.getPlaceController().goTo(place);
274         }
275
276 }