Return an ArrayList of LayoutGroups for the Details layout.
[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.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;
32
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;
38
39 /**
40  * @author Ben Konrath <ben@bagu.org>
41  */
42 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
43         private final String documentTitle;
44         private final ClientFactory clientFactory;
45         private final DetailsView detailsView;
46
47         public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
48                 this.documentTitle = place.getDocumentTitle();
49                 this.clientFactory = clientFactory;
50                 detailsView = clientFactory.getDetailsView();
51         }
52
53         /*
54          * (non-Javadoc)
55          * 
56          * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
57          * com.google.gwt.event.shared.EventBus)
58          */
59         @Override
60         public void start(AcceptsOneWidget panel, EventBus eventBus) {
61                 // register this class as the presenter
62                 detailsView.setPresenter(this);
63
64                 // get the layout for the DetailsView
65                 final String selectedTable = clientFactory.getTableSelectionView().getSelectedTable();
66                 if (!selectedTable.isEmpty()) {
67                         // The table name has been set so we can use the selected table name to populate the cell table.
68                         AsyncCallback<ArrayList<LayoutGroup>> callback = new AsyncCallback<ArrayList<LayoutGroup>>() {
69                                 public void onFailure(Throwable caught) {
70                                         // FIXME: need to deal with failure
71                                         System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsLayout()");
72                                 }
73
74                                 @Override
75                                 public void onSuccess(ArrayList<LayoutGroup> result) {
76                                         addLayoutGroups(result);
77                                 }
78                         };
79                         OnlineGlomServiceAsync.Util.getInstance().getDetailsLayout(documentTitle, selectedTable, callback);
80                 } else {
81                         // The table name has not been set so we need to fill in the details layout using the default table for the
82                         // glom document.
83                         AsyncCallback<ArrayList<LayoutGroup>> callback = new AsyncCallback<ArrayList<LayoutGroup>>() {
84                                 public void onFailure(Throwable caught) {
85                                         // FIXME: need to deal with failure
86                                         System.out.println("AsyncCallback Failed: OnlineGlomService.getDefaultDetailsLayout()");
87                                 }
88
89                                 @Override
90                                 public void onSuccess(ArrayList<LayoutGroup> result) {
91                                         addLayoutGroups(result);
92                                 }
93                         };
94                         OnlineGlomServiceAsync.Util.getInstance().getDefaultDetailsLayout(documentTitle, callback);
95                 }
96
97                 // get the data from the server
98                 AsyncCallback<GlomField[]> callback = new AsyncCallback<GlomField[]>() {
99                         public void onFailure(Throwable caught) {
100                                 // FIXME: need to deal with failure
101                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsData()");
102                         }
103
104                         @Override
105                         public void onSuccess(GlomField[] result) {
106                                 // FIXME there's no guarantee that the layout will be ready for this
107                                 detailsView.setData(result);
108                         }
109                 };
110                 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentTitle, selectedTable, "0", callback);
111
112                 // indicate that the view is ready to be displayed
113                 panel.setWidget(detailsView.asWidget());
114         }
115
116         private void addLayoutGroups(ArrayList<LayoutGroup> layoutGroups) {
117                 for (LayoutGroup layoutGroup : layoutGroups) {
118                         addLayoutGroup(layoutGroup, "");
119                 }
120         }
121
122         /*
123          * This is just a temporary method for creating a basic indented layout without the flowtable/spreadtable that Glom
124          * has.
125          */
126         private void addLayoutGroup(LayoutGroup layoutGroup, String indent) {
127                 if (layoutGroup == null)
128                         return;
129
130                 // look at each child item
131                 ArrayList<LayoutItem> layoutItems = layoutGroup.getItems();
132                 for (LayoutItem layoutItem : layoutItems) {
133
134                         if (layoutItem == null)
135                                 continue;
136
137                         String title = layoutItem.getTitle();
138                         if (layoutItem instanceof LayoutItemField)
139                                 detailsView.addLayoutField(indent + title);
140                         else if (!title.isEmpty())
141                                 detailsView.addLayoutGroup(indent + title);
142
143                         // recurse into child groups
144                         if (layoutItem instanceof LayoutGroup)
145                                 addLayoutGroup((LayoutGroup) layoutItem, indent + "-- ");
146                 }
147         }
148
149         /*
150          * (non-Javadoc)
151          * 
152          * @see com.google.gwt.activity.shared.Activity#onCancel()
153          */
154         @Override
155         public void onCancel() {
156                 detailsView.clear();
157         }
158
159         /*
160          * (non-Javadoc)
161          * 
162          * @see com.google.gwt.activity.shared.Activity#onStop()
163          */
164         @Override
165         public void onStop() {
166                 detailsView.clear();
167         }
168
169         /*
170          * (non-Javadoc)
171          * 
172          * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
173          */
174         @Override
175         public void goTo(Place place) {
176                 clientFactory.getPlaceController().goTo(place);
177         }
178
179 }