2 * Copyright (C) 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
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.
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
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/>.
20 package org.glom.web.client.activity;
22 import java.util.ArrayList;
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;
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;
50 * @author Ben Konrath <ben@bagu.org>
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;
59 ArrayList<DetailsCell> detailsCells;
60 ArrayList<Portal> portals;
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();
73 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
74 * com.google.gwt.event.shared.EventBus)
77 public void start(AcceptsOneWidget panel, EventBus eventBus) {
78 // register this class as the presenter
79 detailsView.setPresenter(this);
81 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
83 // set the change handler for the table selection widget
84 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
86 public void onTableChange(final TableChangeEvent event) {
87 goTo(new DetailsPlace(documentID, event.getNewTableName(), ""));
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()");
99 public void onSuccess(DetailsLayoutAndData result) {
101 createLayout(result.getLayout());
103 setData(result.getData());
108 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
111 // indicate that the view is ready to be displayed
112 panel.setWidget(detailsView.asWidget());
118 private void createLayout(ArrayList<LayoutGroup> layout) {
120 for (LayoutGroup layoutGroup : layout) {
121 detailsView.addGroup(layoutGroup);
124 // save references to the DetailsCells and the Portals
125 detailsCells = detailsView.getCells();
126 portals = detailsView.getPortals();
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() {
134 public void onClick(ClickEvent event) {
136 String newTableName = tableName;
137 if (layoutItemField.getNavigationTableName() != null) {
138 newTableName = layoutItemField.getNavigationTableName();
141 // Ensure the new table name is valid.
142 if (newTableName != null && !newTableName.isEmpty()) {
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));
150 primaryKeyValue = newPrimaryKeyValue;
165 private void setData(DataItem[] data) {
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.");
174 for (int i = 0; i < Math.min(detailsCells.size(), data.length); i++) {
175 DetailsCell detailsCell = detailsCells.get(i);
176 if (data[i] != null) {
178 // set the DatailsItem
179 detailsCell.setData(data[i]);
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)
189 RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
191 portal.setContents(relatedListTable);
192 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(), foreignKeyValue);
199 private void refreshData() {
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()");
209 public void onSuccess(DataItem[] result) {
214 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentID, tableName, primaryKeyValue, callback);
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()");
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());
235 // Set the table row count to the data row count if it's larger than the minimum number of rows
237 relatedListTable.setRowCount(result.intValue());
242 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
243 foreignKeyValue, callback);
249 * @see com.google.gwt.activity.shared.Activity#onCancel()
252 public void onCancel() {
259 * @see com.google.gwt.activity.shared.Activity#onStop()
262 public void onStop() {
269 * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
272 public void goTo(Place place) {
273 clientFactory.getPlaceController().goTo(place);