Enable the table selector in the DetailsView.
[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.event.TableChangeEvent;
27 import org.glom.web.client.event.TableChangeEventHandler;
28 import org.glom.web.client.place.DetailsPlace;
29 import org.glom.web.client.ui.DetailsView;
30 import org.glom.web.shared.GlomField;
31 import org.glom.web.shared.layout.LayoutGroup;
32 import org.glom.web.shared.layout.LayoutItem;
33 import org.glom.web.shared.layout.LayoutItemField;
34
35 import com.google.gwt.activity.shared.AbstractActivity;
36 import com.google.gwt.event.shared.EventBus;
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 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
45         private final String documentID;
46         private final String tableName;
47         private final String primaryKey;
48         private final ClientFactory clientFactory;
49         private final DetailsView detailsView;
50
51         public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
52                 this.documentID = place.getDocumentID();
53                 this.tableName = place.getTableName();
54                 this.primaryKey = place.getPrimaryKeyValue();
55                 this.clientFactory = clientFactory;
56                 detailsView = clientFactory.getDetailsView();
57         }
58
59         /*
60          * (non-Javadoc)
61          * 
62          * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
63          * com.google.gwt.event.shared.EventBus)
64          */
65         @Override
66         public void start(AcceptsOneWidget panel, EventBus eventBus) {
67                 // register this class as the presenter
68                 detailsView.setPresenter(this);
69
70                 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
71
72                 // set the change handler for the table selection widget
73                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
74                         @Override
75                         public void onTableChange(final TableChangeEvent event) {
76                                 goTo(new DetailsPlace(documentID, event.getTableName(), ""));
77                         }
78                 });
79
80                 // get the layout for the DetailsView
81                 AsyncCallback<ArrayList<LayoutGroup>> layoutCallback = new AsyncCallback<ArrayList<LayoutGroup>>() {
82                         public void onFailure(Throwable caught) {
83                                 // FIXME: need to deal with failure
84                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsLayout()");
85                         }
86
87                         @Override
88                         public void onSuccess(ArrayList<LayoutGroup> result) {
89                                 addLayoutGroups(result);
90                         }
91                 };
92                 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayout(documentID, tableName, layoutCallback);
93
94                 // get the data from the server
95                 AsyncCallback<GlomField[]> callback = new AsyncCallback<GlomField[]>() {
96                         public void onFailure(Throwable caught) {
97                                 // FIXME: need to deal with failure
98                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsData()");
99                         }
100
101                         @Override
102                         public void onSuccess(GlomField[] result) {
103                                 // FIXME there's no guarantee that the layout will be ready for this
104                                 detailsView.setData(result);
105                         }
106                 };
107                 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentID, tableName, primaryKey, callback);
108
109                 // indicate that the view is ready to be displayed
110                 panel.setWidget(detailsView.asWidget());
111         }
112
113         private void addLayoutGroups(ArrayList<LayoutGroup> layoutGroups) {
114                 for (LayoutGroup layoutGroup : layoutGroups) {
115                         addLayoutGroup(layoutGroup, "");
116                 }
117         }
118
119         /*
120          * This is just a temporary method for creating a basic indented layout without the flowtable/spreadtable that Glom
121          * has.
122          */
123         private void addLayoutGroup(LayoutGroup layoutGroup, String indent) {
124                 if (layoutGroup == null)
125                         return;
126
127                 // look at each child item
128                 ArrayList<LayoutItem> layoutItems = layoutGroup.getItems();
129                 for (LayoutItem layoutItem : layoutItems) {
130
131                         if (layoutItem == null)
132                                 continue;
133
134                         String title = layoutItem.getTitle();
135                         if (layoutItem instanceof LayoutItemField)
136                                 detailsView.addLayoutField(indent + title);
137                         else if (!title.isEmpty())
138                                 detailsView.addLayoutGroup(indent + title);
139
140                         // recurse into child groups
141                         if (layoutItem instanceof LayoutGroup)
142                                 addLayoutGroup((LayoutGroup) layoutItem, indent + "-- ");
143                 }
144         }
145
146         /*
147          * (non-Javadoc)
148          * 
149          * @see com.google.gwt.activity.shared.Activity#onCancel()
150          */
151         @Override
152         public void onCancel() {
153                 detailsView.clear();
154         }
155
156         /*
157          * (non-Javadoc)
158          * 
159          * @see com.google.gwt.activity.shared.Activity#onStop()
160          */
161         @Override
162         public void onStop() {
163                 detailsView.clear();
164         }
165
166         /*
167          * (non-Javadoc)
168          * 
169          * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
170          */
171         @Override
172         public void goTo(Place place) {
173                 clientFactory.getPlaceController().goTo(place);
174         }
175
176 }