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