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