Add comments with my understanding of the ClientFactory
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / OnlineGlom.java
1 /*
2  * Copyright (C) 2010, 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;
21
22 import org.glom.web.client.mvp.AppPlaceHistoryMapper;
23 import org.glom.web.client.mvp.DataActivityMapper;
24 import org.glom.web.client.mvp.DocumentSelectionActivityMapper;
25 import org.glom.web.client.mvp.TableSelectionActivityMapper;
26 import org.glom.web.client.place.DocumentSelectionPlace;
27
28 import com.google.gwt.activity.shared.ActivityManager;
29 import com.google.gwt.activity.shared.ActivityMapper;
30 import com.google.gwt.core.client.EntryPoint;
31 import com.google.gwt.core.client.GWT;
32 import com.google.gwt.dom.client.Style.Overflow;
33 import com.google.gwt.dom.client.Style.Unit;
34 import com.google.gwt.place.shared.Place;
35 import com.google.gwt.place.shared.PlaceController;
36 import com.google.gwt.place.shared.PlaceHistoryHandler;
37 import com.google.gwt.user.client.ui.AcceptsOneWidget;
38 import com.google.gwt.user.client.ui.IsWidget;
39 import com.google.gwt.user.client.ui.LayoutPanel;
40 import com.google.gwt.user.client.ui.RootLayoutPanel;
41 import com.google.gwt.user.client.ui.SimplePanel;
42 import com.google.gwt.user.client.ui.Widget;
43 import com.google.web.bindery.event.shared.EventBus;
44
45 /**
46  * Entry point classes define <code>onModuleLoad()</code>.
47  */
48 public class OnlineGlom implements EntryPoint {
49         private Place defaultPlace = new DocumentSelectionPlace();
50         private LayoutPanel layoutPanel = RootLayoutPanel.get();
51         private SimplePanel docSelectionPanel = new SimplePanel();
52         private SimplePanel dataPanel = new SimplePanel();
53         private SimplePanel tableSelectionPanel = new SimplePanel();
54
55         AcceptsOneWidget docSelectionDisplay = new AcceptsOneWidget() {
56                 @Override
57                 public void setWidget(IsWidget activityWidget) {
58                         Widget widget = Widget.asWidgetOrNull(activityWidget);
59                         layoutPanel.setWidgetVisible(docSelectionPanel, widget != null);
60                         docSelectionPanel.setWidget(widget);
61                 }
62         };
63
64         AcceptsOneWidget dataDisplay = new AcceptsOneWidget() {
65                 @Override
66                 public void setWidget(IsWidget activityWidget) {
67                         Widget widget = Widget.asWidgetOrNull(activityWidget);
68                         layoutPanel.setWidgetVisible(dataPanel, widget != null);
69                         dataPanel.setWidget(widget);
70                 }
71         };
72
73         AcceptsOneWidget tableSelectionDisplay = new AcceptsOneWidget() {
74                 @Override
75                 public void setWidget(IsWidget activityWidget) {
76                         Widget widget = Widget.asWidgetOrNull(activityWidget);
77                         layoutPanel.setWidgetVisible(tableSelectionPanel, widget != null);
78                         tableSelectionPanel.setWidget(widget);
79                 }
80         };
81
82         /**
83          * This is the entry point method.
84          */
85         @Override
86         public void onModuleLoad() {
87
88                 // TODO This value should really come from the css for the body tag but reading the value using
89                 // RootPanel.getBodyElement().getStyle().getMargin() doesn't seem to be working.
90                 layoutPanel.getElement().getStyle().setMargin(1, Unit.EM);
91
92                 // add the display regions to the main layout panel
93                 layoutPanel.add(docSelectionPanel);
94                 layoutPanel.add(tableSelectionPanel);
95                 layoutPanel.add(dataPanel);
96
97                 // set some properties for the display regions
98                 // The 'overflow: visible' adds a horizontal scrollbar when the list view table is larger than the browser
99                 // window.
100                 layoutPanel.getWidgetContainerElement(dataPanel).getStyle().setOverflow(Overflow.VISIBLE);
101
102                 // set the layout for the list and details places
103                 // TODO Figure out a way to make the layout without absolute positioning. Right now changes to the vertical
104                 // height of the table selector (i.e. CSS changes that affect the vertical height) require the
105                 // tableSelectionSize to be updated.
106                 double tableSelectionSize = 4.7;
107                 layoutPanel.setWidgetTopHeight(tableSelectionPanel, 0, Unit.PCT, tableSelectionSize, Unit.EM);
108                 layoutPanel.setWidgetTopHeight(dataPanel, tableSelectionSize, Unit.EM, 100, Unit.PCT);
109
110                 // hide the display regions for the list and details places because they are not shown by default
111                 layoutPanel.setWidgetVisible(tableSelectionPanel, false);
112                 layoutPanel.setWidgetVisible(dataPanel, false);
113
114                 // We might, in future, use different ClientFactory implementations to create different views
115                 // for different browser types (such as mobile), so we use GWT.create() to have deferred binding.
116                 // See http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html
117                 // which describes how to do this via our OnlineGlom.gwt.xml file.
118                 ClientFactory clientFactory = GWT.create(ClientFactory.class);
119                 EventBus eventBus = clientFactory.getEventBus();
120                 PlaceController placeController = clientFactory.getPlaceController();
121
122                 // Activity manager for the data display region (list or details view).
123                 ActivityMapper dataActivityMapper = new DataActivityMapper(clientFactory);
124                 ActivityManager dataActivityManager = new ActivityManager(dataActivityMapper, eventBus);
125                 dataActivityManager.setDisplay(dataDisplay);
126
127                 // Activity manager for the document selection display region.
128                 ActivityMapper docSelectionActivityMapper = new DocumentSelectionActivityMapper(clientFactory);
129                 ActivityManager docSelectionActivityManager = new ActivityManager(docSelectionActivityMapper, eventBus);
130                 docSelectionActivityManager.setDisplay(docSelectionDisplay);
131
132                 // Activity manager for the table selection display region.
133                 ActivityMapper tableSelectionActivityMapper = new TableSelectionActivityMapper(clientFactory);
134                 ActivityManager tableSelectionActivityManager = new ActivityManager(tableSelectionActivityMapper, eventBus);
135                 tableSelectionActivityManager.setDisplay(tableSelectionDisplay);
136
137                 // Start PlaceHistoryHandler with our PlaceHistoryMapper.
138                 AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
139                 PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
140                 historyHandler.register(placeController, eventBus, defaultPlace);
141
142                 // Goes to the place represented on the URL or the default place.
143                 historyHandler.handleCurrentHistory();
144         }
145 }