Code style changes from Eclipse
[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.FlowPanel;
39 import com.google.gwt.user.client.ui.IsWidget;
40 import com.google.gwt.user.client.ui.LayoutPanel;
41 import com.google.gwt.user.client.ui.RootLayoutPanel;
42 import com.google.gwt.user.client.ui.SimplePanel;
43 import com.google.gwt.user.client.ui.Widget;
44 import com.google.web.bindery.event.shared.EventBus;
45
46 /**
47  * Entry point classes define <code>onModuleLoad()</code>.
48  */
49 public class OnlineGlom implements EntryPoint {
50
51         /*
52          * Some of these are protected, rather than private, so that GwtTestOnlineGlom can access them.
53          */
54         private Place defaultPlace = new DocumentSelectionPlace();
55
56         private LayoutPanel rootLayoutPanel = RootLayoutPanel.get();
57         private FlowPanel layoutPanel = new FlowPanel();
58         protected SimplePanel docSelectionPanel = new SimplePanel();
59         protected SimplePanel dataPanel = new SimplePanel();
60         protected SimplePanel tableSelectionPanel = new SimplePanel();
61
62         protected ClientFactory clientFactory;
63
64         AcceptsOneWidget docSelectionDisplay = new AcceptsOneWidget() {
65                 @Override
66                 public void setWidget(final IsWidget activityWidget) {
67                         final Widget widget = Widget.asWidgetOrNull(activityWidget);
68                         docSelectionPanel.setVisible(widget != null);
69                         docSelectionPanel.setWidget(widget);
70                 }
71         };
72
73         AcceptsOneWidget dataDisplay = new AcceptsOneWidget() {
74                 @Override
75                 public void setWidget(final IsWidget activityWidget) {
76                         final Widget widget = Widget.asWidgetOrNull(activityWidget);
77                         dataPanel.setVisible(widget != null);
78                         dataPanel.setWidget(widget);
79                 }
80         };
81
82         AcceptsOneWidget tableSelectionDisplay = new AcceptsOneWidget() {
83                 @Override
84                 public void setWidget(final IsWidget activityWidget) {
85                         final Widget widget = Widget.asWidgetOrNull(activityWidget);
86                         tableSelectionPanel.setVisible(widget != null);
87                         tableSelectionPanel.setWidget(widget);
88                 }
89         };
90
91         /**
92          * This is the entry point method.
93          */
94         @Override
95         public void onModuleLoad() {
96
97                 rootLayoutPanel.add(layoutPanel);
98                 rootLayoutPanel.setWidgetVisible(layoutPanel, true);
99
100                 // TODO This value should really come from the css for the body tag but reading the value using
101                 // RootPanel.getBodyElement().getStyle().getMargin() doesn't seem to be working.
102                 layoutPanel.getElement().getStyle().setMargin(1, Unit.EM);
103
104                 // add the display regions to the main layout panel
105                 layoutPanel.add(docSelectionPanel);
106                 layoutPanel.add(tableSelectionPanel);
107                 layoutPanel.add(dataPanel);
108
109                 // set some properties for the display regions
110                 // The 'overflow: visible' adds a horizontal scrollbar when the content is larger than the browser window.
111                 // TODO: It would be better to just have the regular browser scrollbars, but for some reason they
112                 // are not shown.
113                 rootLayoutPanel.getWidgetContainerElement(layoutPanel).getStyle().setOverflow(Overflow.VISIBLE);
114
115                 // hide the display regions for the list and details places because they are not shown by default
116                 tableSelectionPanel.setVisible(false);
117                 dataPanel.setVisible(false);
118
119                 // We might, in future, use different ClientFactory implementations to create different views
120                 // for different browser types (such as mobile), so we use GWT.create() to have deferred binding.
121                 // See http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html
122                 // which describes how to do this via our OnlineGlom.gwt.xml file.
123                 clientFactory = GWT.create(ClientFactory.class);
124                 final EventBus eventBus = clientFactory.getEventBus();
125                 final PlaceController placeController = clientFactory.getPlaceController();
126
127                 // Activity manager for the data display region (list or details view).
128                 final ActivityMapper dataActivityMapper = new DataActivityMapper(clientFactory);
129                 final ActivityManager dataActivityManager = new ActivityManager(dataActivityMapper, eventBus);
130                 dataActivityManager.setDisplay(dataDisplay);
131
132                 // Activity manager for the document selection display region.
133                 final ActivityMapper docSelectionActivityMapper = new DocumentSelectionActivityMapper(clientFactory);
134                 final ActivityManager docSelectionActivityManager = new ActivityManager(docSelectionActivityMapper, eventBus);
135                 docSelectionActivityManager.setDisplay(docSelectionDisplay);
136
137                 // Activity manager for the table selection display region.
138                 final ActivityMapper tableSelectionActivityMapper = new TableSelectionActivityMapper(clientFactory);
139                 final ActivityManager tableSelectionActivityManager = new ActivityManager(tableSelectionActivityMapper,
140                                 eventBus);
141                 tableSelectionActivityManager.setDisplay(tableSelectionDisplay);
142
143                 // Start PlaceHistoryHandler with our PlaceHistoryMapper.
144                 final AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
145                 final 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 }