1 package org
.argeo
.cms
.ui
.rcp
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.security
.PrivilegedAction
;
6 import java
.util
.HashMap
;
10 import javax
.security
.auth
.Subject
;
11 import javax
.security
.auth
.login
.LoginContext
;
12 import javax
.security
.auth
.login
.LoginException
;
14 import org
.argeo
.api
.cms
.CmsApp
;
15 import org
.argeo
.api
.cms
.CmsAuth
;
16 import org
.argeo
.api
.cms
.CmsLog
;
17 import org
.argeo
.api
.cms
.CmsSession
;
18 import org
.argeo
.api
.cms
.ux
.CmsImageManager
;
19 import org
.argeo
.api
.cms
.ux
.CmsTheme
;
20 import org
.argeo
.api
.cms
.ux
.CmsUi
;
21 import org
.argeo
.api
.cms
.ux
.CmsView
;
22 import org
.argeo
.api
.cms
.ux
.UxContext
;
23 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
24 import org
.eclipse
.e4
.ui
.css
.core
.engine
.CSSEngine
;
25 import org
.eclipse
.e4
.ui
.css
.core
.engine
.CSSErrorHandler
;
26 import org
.eclipse
.e4
.ui
.css
.swt
.engine
.CSSSWTEngineImpl
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
28 import org
.eclipse
.swt
.widgets
.Display
;
29 import org
.eclipse
.swt
.widgets
.Shell
;
30 import org
.osgi
.service
.event
.Event
;
31 import org
.osgi
.service
.event
.EventAdmin
;
33 /** Runs a {@link CmsApp} as an SWT desktop application. */
34 @SuppressWarnings("restriction")
35 public class CmsRcpApp
implements CmsView
{
36 private final static CmsLog log
= CmsLog
.getLog(CmsRcpApp
.class);
38 //private BundleContext bundleContext = FrameworkUtil.getBundle(CmsRcpApp.class).getBundleContext();
41 private CmsApp cmsApp
;
45 private LoginContext loginContext
;
47 private EventAdmin eventAdmin
;
49 private CSSEngine cssEngine
;
52 // TODO make it configurable
53 private String uiName
= "desktop";
55 public CmsRcpApp(String uiName
) {
56 uid
= UUID
.randomUUID().toString();
60 public void initRcpApp() {
61 Display display
= Display
.getCurrent();
62 shell
= new Shell(display
);
63 shell
.setText("Argeo CMS");
64 Composite parent
= shell
;
65 parent
.setLayout(CmsSwtUtils
.noSpaceGridLayout());
66 CmsSwtUtils
.registerCmsView(shell
, CmsRcpApp
.this);
69 loginContext
= new LoginContext(CmsAuth
.SINGLE_USER
.getLoginContextName());
71 } catch (LoginException e
) {
72 throw new IllegalStateException("Could not log in.", e
);
74 if (log
.isDebugEnabled())
75 log
.debug("Logged in to desktop: " + loginContext
.getSubject());
77 Subject
.doAs(loginContext
.getSubject(), (PrivilegedAction
<Void
>) () -> {
79 // TODO factorise with web app
80 parent
.setData(CmsApp
.UI_NAME_PROPERTY
, uiName
);
81 ui
= cmsApp
.initUi(parent
);
82 if (ui
instanceof Composite
)
83 ((Composite
) ui
).setLayoutData(CmsSwtUtils
.fillAll());
84 // we need ui to be set before refresh so that CmsView can store UI context data
86 cmsApp
.refreshUi(ui
, null);
89 CmsTheme theme
= CmsSwtUtils
.getCmsTheme(parent
);
91 cssEngine
= new CSSSWTEngineImpl(display
);
92 for (String path
: theme
.getSwtCssPaths()) {
93 try (InputStream in
= theme
.loadPath(path
)) {
94 cssEngine
.parseStyleSheet(in
);
95 } catch (IOException e
) {
96 throw new IllegalStateException("Cannot load stylesheet " + path
, e
);
99 cssEngine
.setErrorHandler(new CSSErrorHandler() {
100 public void error(Exception e
) {
101 log
.error("SWT styling error: ", e
);
106 shell
.layout(true, true);
118 public String
getUid() {
123 public UxContext
getUxContext() {
124 throw new UnsupportedOperationException();
128 public void navigateTo(String state
) {
129 throw new UnsupportedOperationException();
133 public void authChange(LoginContext loginContext
) {
137 public void logout() {
138 if (loginContext
!= null)
140 loginContext
.logout();
141 } catch (LoginException e
) {
142 log
.error("Cannot log out", e
);
147 public void exception(Throwable e
) {
148 log
.error("Unexpected exception in CMS RCP", e
);
152 public CmsImageManager
getImageManager() {
153 throw new UnsupportedOperationException();
157 public CmsSession
getCmsSession() {
158 CmsSession cmsSession
= cmsApp
.getCmsContext().getCmsSession(getSubject());
163 public Object
getData(String key
) {
165 return ui
.getData(key
);
167 throw new IllegalStateException("UI is not initialized");
172 public void setData(String key
, Object value
) {
174 ui
.setData(key
, value
);
176 throw new IllegalStateException("UI is not initialized");
181 public boolean isAnonymous() {
186 public void applyStyles(Object node
) {
187 if (cssEngine
!= null)
188 cssEngine
.applyStyles(node
, true);
192 public void sendEvent(String topic
, Map
<String
, Object
> properties
) {
193 if (properties
== null)
194 properties
= new HashMap
<>();
195 if (properties
.containsKey(CMS_VIEW_UID_PROPERTY
) && !properties
.get(CMS_VIEW_UID_PROPERTY
).equals(uid
))
196 throw new IllegalArgumentException("Property " + CMS_VIEW_UID_PROPERTY
+ " is set to another CMS view uid ("
197 + properties
.get(CMS_VIEW_UID_PROPERTY
) + ") then " + uid
);
198 properties
.put(CMS_VIEW_UID_PROPERTY
, uid
);
199 eventAdmin
.sendEvent(new Event(topic
, properties
));
202 public <T
> T
doAs(PrivilegedAction
<T
> action
) {
203 return Subject
.doAs(getSubject(), action
);
206 protected Subject
getSubject() {
207 return loginContext
.getSubject();
211 * DEPENDENCY INJECTION
213 public void setCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
214 this.cmsApp
= cmsApp
;
217 public void unsetCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
221 public void setEventAdmin(EventAdmin eventAdmin
) {
222 this.eventAdmin
= eventAdmin
;