Adapt branding, use latest connect snapshot
[gpl/argeo-suite.git] / org.argeo.suite.web / src / org / argeo / suite / web / DefaultMainPage.java
1 package org.argeo.suite.web;
2
3 import java.nio.file.spi.FileSystemProvider;
4 import java.util.Locale;
5 import java.util.Map;
6
7 import javax.jcr.Node;
8 import javax.jcr.RepositoryException;
9 import javax.jcr.Session;
10 import javax.jcr.nodetype.NodeType;
11
12 import org.argeo.cms.CmsMsg;
13 import org.argeo.cms.auth.CurrentUser;
14 import org.argeo.cms.ui.CmsUiProvider;
15 import org.argeo.cms.ui.CmsView;
16 import org.argeo.cms.ui.LifeCycleUiProvider;
17 import org.argeo.cms.util.CmsUtils;
18 import org.argeo.cms.widgets.auth.CmsLogin;
19 import org.argeo.connect.people.PeopleService;
20 import org.argeo.connect.people.PeopleTypes;
21 import org.argeo.connect.people.web.pages.PeopleDefaultPage;
22 import org.argeo.eclipse.ui.EclipseUiUtils;
23 import org.argeo.suite.web.fs.MyFilesBrowserPage;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionListener;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Label;
32
33 /**
34  * Default entry point for the Argeo Suite CMS. Forwards the request to the
35  * relevant CmsUiProvider
36  */
37 public class DefaultMainPage implements LifeCycleUiProvider {
38
39         private CmsUiProvider dashboardPage;
40         private CmsUiProvider peoplePage;
41         private CmsUiProvider fsBrowserPage;
42
43         /* DEPENDENCY INJECTION */
44         private PeopleService peopleService;
45         private FileSystemProvider nodeFileSystemProvider;
46         private Map<String, String> peopleIconPaths;
47
48         public DefaultMainPage() {
49         }
50
51         @Override
52         public void init(Session adminSession) throws RepositoryException {
53                 dashboardPage = new DefaultDashboard(peopleService, peopleIconPaths);
54                 peoplePage = new PeopleDefaultPage(peopleService, peopleIconPaths);
55                 fsBrowserPage = new MyFilesBrowserPage(nodeFileSystemProvider);
56         }
57
58         @Override
59         public Control createUi(Composite parent, Node context) throws RepositoryException {
60                 if (CurrentUser.isAnonymous())
61                         return createAnonymousUi(parent, context);
62
63                 if (context.isNodeType(PeopleTypes.PEOPLE_ENTITY))
64                         return peoplePage.createUi(parent, context);
65                 else if (peopleService.getBasePath(null).equals(context.getPath()))
66                         return peoplePage.createUi(parent, context);
67                 else if (context.isNodeType(NodeType.NT_FOLDER) || context.isNodeType(NodeType.NT_FILE))
68                         return fsBrowserPage.createUi(parent, context);
69                 else
70                         return dashboardPage.createUi(parent, context);
71         }
72
73         public Control createAnonymousUi(Composite parent, Node context) throws RepositoryException {
74                 parent.setLayout(new GridLayout());
75                 Composite body = new Composite(parent, SWT.NO_FOCUS);
76                 body.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
77                 body.setLayout(new GridLayout());
78
79                 Composite loginCmp = new Composite(body, SWT.NO_FOCUS);
80                 loginCmp.setLayout(EclipseUiUtils.noSpaceGridLayout());
81                 loginCmp.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
82
83                 CmsLogin login = new MyCmsLogin(CmsUtils.getCmsView());
84                 // Composite credBlockCmp =
85                 login.createCredentialsBlock(loginCmp);
86                 // Use a custom style that has no border, among other
87                 // CmsUtils.style(loginCmp, ArgeoStyles.LOGIN_INLINE_CREDBLOCK);
88
89                 Label anonymousLbl = new Label(body, SWT.WRAP);
90                 anonymousLbl.setText("You should login or register to access your private dashboard");
91
92                 return body;
93
94         }
95
96         private class MyCmsLogin extends CmsLogin {
97
98                 public MyCmsLogin(CmsView cmsView) {
99                         super(cmsView);
100                 }
101
102                 @Override
103                 protected boolean login() {
104                         boolean result = super.login();
105                         return result;
106                 }
107
108                 @Override
109                 protected void extendsCredentialsBlock(Composite credentialsBlock, Locale selectedLocale,
110                                 SelectionListener loginSelectionListener) {
111                         Button loginBtn = new Button(credentialsBlock, SWT.PUSH);
112                         loginBtn.setText(CmsMsg.login.lead(selectedLocale));
113                         loginBtn.setLayoutData(CmsUtils.fillWidth());
114                         loginBtn.addSelectionListener(loginSelectionListener);
115                         // CmsUtils.style(loginBtn, ArgeoStyles.LOGIN_SIGNIN_BTN);
116                 }
117         }
118
119         @Override
120         public void destroy() {
121         }
122
123         /* DEPENDENCY INJECTION */
124         public void setPeopleService(PeopleService peopleService) {
125                 this.peopleService = peopleService;
126         }
127
128         public void setNodeFileSystemProvider(FileSystemProvider nodeFileSystemProvider) {
129                 this.nodeFileSystemProvider = nodeFileSystemProvider;
130         }
131
132         public void setPeopleIconPaths(Map<String, String> peopleIconPaths) {
133                 this.peopleIconPaths = peopleIconPaths;
134         }
135 }