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.place.DetailsPlace;
27 import org.glom.web.client.ui.DetailsView;
28 import org.glom.web.shared.GlomField;
29 import org.glom.web.shared.layout.LayoutGroup;
30 import org.glom.web.shared.layout.LayoutItem;
31 import org.glom.web.shared.layout.LayoutItemField;
33 import com.google.gwt.activity.shared.AbstractActivity;
34 import com.google.gwt.event.shared.EventBus;
35 import com.google.gwt.place.shared.Place;
36 import com.google.gwt.user.client.rpc.AsyncCallback;
37 import com.google.gwt.user.client.ui.AcceptsOneWidget;
40 * @author Ben Konrath <ben@bagu.org>
42 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
43 private final String documentTitle;
44 private final String primaryKey;
45 private final ClientFactory clientFactory;
46 private final DetailsView detailsView;
48 public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
49 this.documentTitle = place.getDocumentTitle();
50 this.primaryKey = place.getPrimaryKey();
51 this.clientFactory = clientFactory;
52 detailsView = clientFactory.getDetailsView();
58 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
59 * com.google.gwt.event.shared.EventBus)
62 public void start(AcceptsOneWidget panel, EventBus eventBus) {
63 // register this class as the presenter
64 detailsView.setPresenter(this);
66 // get the layout for the DetailsView
67 final String selectedTable = clientFactory.getTableSelectionView().getSelectedTable();
68 if (!selectedTable.isEmpty()) {
69 // The table name has been set so we can use the selected table name to populate the cell table.
70 AsyncCallback<ArrayList<LayoutGroup>> callback = new AsyncCallback<ArrayList<LayoutGroup>>() {
71 public void onFailure(Throwable caught) {
72 // FIXME: need to deal with failure
73 System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsLayout()");
77 public void onSuccess(ArrayList<LayoutGroup> result) {
78 addLayoutGroups(result);
81 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayout(documentTitle, selectedTable, callback);
83 // The table name has not been set so we need to fill in the details layout using the default table for the
85 AsyncCallback<ArrayList<LayoutGroup>> callback = new AsyncCallback<ArrayList<LayoutGroup>>() {
86 public void onFailure(Throwable caught) {
87 // FIXME: need to deal with failure
88 System.out.println("AsyncCallback Failed: OnlineGlomService.getDefaultDetailsLayout()");
92 public void onSuccess(ArrayList<LayoutGroup> result) {
93 addLayoutGroups(result);
96 OnlineGlomServiceAsync.Util.getInstance().getDefaultDetailsLayout(documentTitle, callback);
99 // get the data from the server
100 AsyncCallback<GlomField[]> callback = new AsyncCallback<GlomField[]>() {
101 public void onFailure(Throwable caught) {
102 // FIXME: need to deal with failure
103 System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsData()");
107 public void onSuccess(GlomField[] result) {
108 // FIXME there's no guarantee that the layout will be ready for this
109 detailsView.setData(result);
112 // FIXME Need a getDefaultDetailsData so that we can grab data from the default list when a table is not
113 // specified in the URL (which it's not now). This affects starting OnlineGlom from a bookmarked or shared
114 // URL to the DetailsView. We'll also want to add a table URL variable and perform validation of the URL
116 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentTitle, selectedTable, primaryKey, callback);
118 // indicate that the view is ready to be displayed
119 panel.setWidget(detailsView.asWidget());
122 private void addLayoutGroups(ArrayList<LayoutGroup> layoutGroups) {
123 for (LayoutGroup layoutGroup : layoutGroups) {
124 addLayoutGroup(layoutGroup, "");
129 * This is just a temporary method for creating a basic indented layout without the flowtable/spreadtable that Glom
132 private void addLayoutGroup(LayoutGroup layoutGroup, String indent) {
133 if (layoutGroup == null)
136 // look at each child item
137 ArrayList<LayoutItem> layoutItems = layoutGroup.getItems();
138 for (LayoutItem layoutItem : layoutItems) {
140 if (layoutItem == null)
143 String title = layoutItem.getTitle();
144 if (layoutItem instanceof LayoutItemField)
145 detailsView.addLayoutField(indent + title);
146 else if (!title.isEmpty())
147 detailsView.addLayoutGroup(indent + title);
149 // recurse into child groups
150 if (layoutItem instanceof LayoutGroup)
151 addLayoutGroup((LayoutGroup) layoutItem, indent + "-- ");
158 * @see com.google.gwt.activity.shared.Activity#onCancel()
161 public void onCancel() {
168 * @see com.google.gwt.activity.shared.Activity#onStop()
171 public void onStop() {
178 * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
181 public void goTo(Place place) {
182 clientFactory.getPlaceController().goTo(place);