1dcc4d5278397a3c196a589861331c59b18c5cd4
[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.ui.fs.MyFilesBrowserPage;
19 import org.argeo.cms.util.CmsUtils;
20 import org.argeo.cms.widgets.auth.CmsLogin;
21 import org.argeo.connect.people.PeopleService;
22 import org.argeo.connect.people.PeopleTypes;
23 import org.argeo.connect.people.web.pages.PeopleDefaultPage;
24 import org.argeo.eclipse.ui.EclipseUiUtils;
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                 CmsUtils.style(anonymousLbl, TextStyles.TEXT_QUOTE);
93
94                 return body;
95
96         }
97
98         private class MyCmsLogin extends CmsLogin {
99
100                 public MyCmsLogin(CmsView cmsView) {
101                         super(cmsView);
102                 }
103
104                 @Override
105                 protected boolean login() {
106                         boolean result = super.login();
107                         return result;
108                 }
109
110                 @Override
111                 protected void extendsCredentialsBlock(Composite credentialsBlock, Locale selectedLocale,
112                                 SelectionListener loginSelectionListener) {
113                         Button loginBtn = new Button(credentialsBlock, SWT.PUSH);
114                         loginBtn.setText(CmsMsg.login.lead(selectedLocale));
115                         loginBtn.setLayoutData(CmsUtils.fillWidth());
116                         loginBtn.addSelectionListener(loginSelectionListener);
117                         // CmsUtils.style(loginBtn, ArgeoStyles.LOGIN_SIGNIN_BTN);
118                 }
119         }
120
121         @Override
122         public void destroy() {
123         }
124
125         /* DEPENDENCY INJECTION */
126         public void setPeopleService(PeopleService peopleService) {
127                 this.peopleService = peopleService;
128         }
129
130         public void setNodeFileSystemProvider(FileSystemProvider nodeFileSystemProvider) {
131                 this.nodeFileSystemProvider = nodeFileSystemProvider;
132         }
133
134         public void setPeopleIconPaths(Map<String, String> peopleIconPaths) {
135                 this.peopleIconPaths = peopleIconPaths;
136         }
137 }