1 package org
.argeo
.cms
.ui
.rcp
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.security
.PrivilegedAction
;
9 import javax
.security
.auth
.Subject
;
10 import javax
.security
.auth
.login
.LoginContext
;
11 import javax
.security
.auth
.login
.LoginException
;
13 import org
.argeo
.api
.cms
.CmsApp
;
14 import org
.argeo
.api
.cms
.CmsAuth
;
15 import org
.argeo
.api
.cms
.CmsEventBus
;
16 import org
.argeo
.api
.cms
.CmsLog
;
17 import org
.argeo
.api
.cms
.CmsSession
;
18 import org
.argeo
.api
.cms
.ux
.CmsTheme
;
19 import org
.argeo
.api
.cms
.ux
.CmsView
;
20 import org
.argeo
.cms
.swt
.AbstractSwtCmsView
;
21 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
22 import org
.eclipse
.e4
.ui
.css
.core
.engine
.CSSEngine
;
23 import org
.eclipse
.e4
.ui
.css
.core
.engine
.CSSErrorHandler
;
24 import org
.eclipse
.e4
.ui
.css
.swt
.engine
.CSSSWTEngineImpl
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Display
;
27 import org
.eclipse
.swt
.widgets
.Shell
;
29 /** Runs a {@link CmsApp} as an SWT desktop application. */
30 @SuppressWarnings("restriction")
31 public class CmsRcpApp
extends AbstractSwtCmsView
implements CmsView
{
32 private final static CmsLog log
= CmsLog
.getLog(CmsRcpApp
.class);
35 private CmsApp cmsApp
;
37 private CSSEngine cssEngine
;
39 public CmsRcpApp(String uiName
) {
41 uid
= UUID
.randomUUID().toString();
44 public void initRcpApp() {
45 display
= Display
.getCurrent();
46 shell
= new Shell(display
);
47 shell
.setText("Argeo CMS");
48 Composite parent
= shell
;
49 parent
.setLayout(CmsSwtUtils
.noSpaceGridLayout());
50 CmsSwtUtils
.registerCmsView(shell
, CmsRcpApp
.this);
53 loginContext
= new LoginContext(CmsAuth
.SINGLE_USER
.getLoginContextName());
55 } catch (LoginException e
) {
56 throw new IllegalStateException("Could not log in.", e
);
58 if (log
.isDebugEnabled())
59 log
.debug("Logged in to desktop: " + loginContext
.getSubject());
61 Subject
.doAs(loginContext
.getSubject(), (PrivilegedAction
<Void
>) () -> {
63 // TODO factorise with web app
64 parent
.setData(CmsApp
.UI_NAME_PROPERTY
, uiName
);
65 ui
= cmsApp
.initUi(parent
);
66 if (ui
instanceof Composite
)
67 ((Composite
) ui
).setLayoutData(CmsSwtUtils
.fillAll());
68 // we need ui to be set before refresh so that CmsView can store UI context data
70 cmsApp
.refreshUi(ui
, null);
73 CmsTheme theme
= CmsSwtUtils
.getCmsTheme(parent
);
75 cssEngine
= new CSSSWTEngineImpl(display
);
76 for (String path
: theme
.getSwtCssPaths()) {
77 try (InputStream in
= theme
.loadPath(path
)) {
78 cssEngine
.parseStyleSheet(in
);
79 } catch (IOException e
) {
80 throw new IllegalStateException("Cannot load stylesheet " + path
, e
);
83 cssEngine
.setErrorHandler(new CSSErrorHandler() {
84 public void error(Exception e
) {
85 log
.error("SWT styling error: ", e
);
90 shell
.layout(true, true);
102 public void navigateTo(String state
) {
103 throw new UnsupportedOperationException();
107 public void authChange(LoginContext loginContext
) {
111 public void logout() {
112 if (loginContext
!= null)
114 loginContext
.logout();
115 } catch (LoginException e
) {
116 log
.error("Cannot log out", e
);
121 public void exception(Throwable e
) {
122 log
.error("Unexpected exception in CMS RCP", e
);
126 public CmsSession
getCmsSession() {
127 CmsSession cmsSession
= cmsApp
.getCmsContext().getCmsSession(getSubject());
132 public boolean isAnonymous() {
137 public void applyStyles(Object node
) {
138 if (cssEngine
!= null)
139 cssEngine
.applyStyles(node
, true);
142 public Shell
getShell() {
147 public CmsEventBus
getCmsEventBus() {
148 return cmsApp
.getCmsContext().getCmsEventBus();
152 public CmsApp
getCmsApp() {
157 * DEPENDENCY INJECTION
159 public void setCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
160 this.cmsApp
= cmsApp
;
163 public void unsetCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {