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 =
39 // FrameworkUtil.getBundle(CmsRcpApp.class).getBundleContext();
42 private CmsApp cmsApp
;
46 private LoginContext loginContext
;
48 private EventAdmin eventAdmin
;
50 private CSSEngine cssEngine
;
53 // TODO make it configurable
54 private String uiName
= "desktop";
56 public CmsRcpApp(String uiName
) {
57 uid
= UUID
.randomUUID().toString();
61 public void initRcpApp() {
62 Display display
= Display
.getCurrent();
63 shell
= new Shell(display
);
64 shell
.setText("Argeo CMS");
65 Composite parent
= shell
;
66 parent
.setLayout(CmsSwtUtils
.noSpaceGridLayout());
67 CmsSwtUtils
.registerCmsView(shell
, CmsRcpApp
.this);
70 loginContext
= new LoginContext(CmsAuth
.SINGLE_USER
.getLoginContextName());
72 } catch (LoginException e
) {
73 throw new IllegalStateException("Could not log in.", e
);
75 if (log
.isDebugEnabled())
76 log
.debug("Logged in to desktop: " + loginContext
.getSubject());
78 Subject
.doAs(loginContext
.getSubject(), (PrivilegedAction
<Void
>) () -> {
80 // TODO factorise with web app
81 parent
.setData(CmsApp
.UI_NAME_PROPERTY
, uiName
);
82 ui
= cmsApp
.initUi(parent
);
83 if (ui
instanceof Composite
)
84 ((Composite
) ui
).setLayoutData(CmsSwtUtils
.fillAll());
85 // we need ui to be set before refresh so that CmsView can store UI context data
87 cmsApp
.refreshUi(ui
, null);
90 CmsTheme theme
= CmsSwtUtils
.getCmsTheme(parent
);
92 cssEngine
= new CSSSWTEngineImpl(display
);
93 for (String path
: theme
.getSwtCssPaths()) {
94 try (InputStream in
= theme
.loadPath(path
)) {
95 cssEngine
.parseStyleSheet(in
);
96 } catch (IOException e
) {
97 throw new IllegalStateException("Cannot load stylesheet " + path
, e
);
100 cssEngine
.setErrorHandler(new CSSErrorHandler() {
101 public void error(Exception e
) {
102 log
.error("SWT styling error: ", e
);
107 shell
.layout(true, true);
119 public String
getUid() {
124 public UxContext
getUxContext() {
125 throw new UnsupportedOperationException();
129 public void navigateTo(String state
) {
130 throw new UnsupportedOperationException();
134 public void authChange(LoginContext loginContext
) {
138 public void logout() {
139 if (loginContext
!= null)
141 loginContext
.logout();
142 } catch (LoginException e
) {
143 log
.error("Cannot log out", e
);
148 public void exception(Throwable e
) {
149 log
.error("Unexpected exception in CMS RCP", e
);
153 public CmsImageManager
getImageManager() {
154 throw new UnsupportedOperationException();
158 public CmsSession
getCmsSession() {
159 CmsSession cmsSession
= cmsApp
.getCmsContext().getCmsSession(getSubject());
164 public Object
getData(String key
) {
166 return ui
.getData(key
);
168 throw new IllegalStateException("UI is not initialized");
173 public void setData(String key
, Object value
) {
175 ui
.setData(key
, value
);
177 throw new IllegalStateException("UI is not initialized");
182 public boolean isAnonymous() {
187 public void applyStyles(Object node
) {
188 if (cssEngine
!= null)
189 cssEngine
.applyStyles(node
, true);
193 public void sendEvent(String topic
, Map
<String
, Object
> properties
) {
194 if (properties
== null)
195 properties
= new HashMap
<>();
196 if (properties
.containsKey(CMS_VIEW_UID_PROPERTY
) && !properties
.get(CMS_VIEW_UID_PROPERTY
).equals(uid
))
197 throw new IllegalArgumentException("Property " + CMS_VIEW_UID_PROPERTY
+ " is set to another CMS view uid ("
198 + properties
.get(CMS_VIEW_UID_PROPERTY
) + ") then " + uid
);
199 properties
.put(CMS_VIEW_UID_PROPERTY
, uid
);
200 eventAdmin
.sendEvent(new Event(topic
, properties
));
203 public <T
> T
doAs(PrivilegedAction
<T
> action
) {
204 return Subject
.doAs(getSubject(), action
);
207 protected Subject
getSubject() {
208 return loginContext
.getSubject();
211 public Shell
getShell() {
216 * DEPENDENCY INJECTION
218 public void setCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
219 this.cmsApp
= cmsApp
;
222 public void unsetCmsApp(CmsApp cmsApp
, Map
<String
, String
> properties
) {
226 public void setEventAdmin(EventAdmin eventAdmin
) {
227 this.eventAdmin
= eventAdmin
;