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
.CmsImageManager
;
17 import org
.argeo
.api
.cms
.CmsSession
;
18 import org
.argeo
.api
.cms
.CmsTheme
;
19 import org
.argeo
.api
.cms
.CmsUi
;
20 import org
.argeo
.api
.cms
.CmsView
;
21 import org
.argeo
.api
.cms
.CmsLog
;
22 import org
.argeo
.api
.cms
.UxContext
;
23 import org
.argeo
.cms
.osgi
.CmsOsgiUtils
;
24 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
25 import org
.eclipse
.e4
.ui
.css
.core
.engine
.CSSEngine
;
26 import org
.eclipse
.e4
.ui
.css
.core
.engine
.CSSErrorHandler
;
27 import org
.eclipse
.e4
.ui
.css
.swt
.engine
.CSSSWTEngineImpl
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Display
;
31 import org
.eclipse
.swt
.widgets
.Shell
;
32 import org
.osgi
.framework
.BundleContext
;
33 import org
.osgi
.framework
.FrameworkUtil
;
34 import org
.osgi
.service
.event
.Event
;
35 import org
.osgi
.service
.event
.EventAdmin
;
37 /** Runs a {@link CmsApp} as an SWT desktop application. */
38 @SuppressWarnings("restriction")
39 public class CmsRcpApp
implements CmsView
{
40 private final static CmsLog log
= CmsLog
.getLog(CmsRcpApp
.class);
42 private BundleContext bundleContext
= FrameworkUtil
.getBundle(CmsRcpApp
.class).getBundleContext();
44 private Display display
;
46 private CmsApp cmsApp
;
47 private CmsUiThread uiThread
;
51 private LoginContext loginContext
;
53 private EventAdmin eventAdmin
;
55 private CSSEngine cssEngine
;
58 // TODO make it configurable
59 private String uiName
= "desktop";
62 uid
= UUID
.randomUUID().toString();
65 public void init(Map
<String
, String
> properties
) {
68 } catch (InterruptedException e
) {
71 uiThread
= new CmsUiThread();
76 public void destroy(Map
<String
, String
> properties
) {
77 if (!shell
.isDisposed())
81 } catch (InterruptedException e
) {
88 class CmsUiThread
extends Thread
{
90 public CmsUiThread() {
96 display
= new Display();
97 shell
= new Shell(display
);
98 shell
.setText("Argeo CMS");
99 Composite parent
= shell
;
100 parent
.setLayout(new GridLayout());
101 CmsSwtUtils
.registerCmsView(shell
, CmsRcpApp
.this);
103 // Subject subject = new Subject();
104 // CmsLoginShell loginShell = new CmsLoginShell(CmsRcpApp.this);
105 // loginShell.setSubject(subject);
108 // loginContext = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER, subject, loginShell);
109 loginContext
= new LoginContext(CmsAuth
.LOGIN_CONTEXT_SINGLE_USER
);
110 loginContext
.login();
111 } catch (LoginException e
) {
112 throw new IllegalStateException("Could not log in.", e
);
113 // loginShell.createUi();
114 // loginShell.open();
116 // while (!loginShell.getShell().isDisposed()) {
117 // if (!display.readAndDispatch())
121 if (log
.isDebugEnabled())
122 log
.debug("Logged in to desktop: " + loginContext
.getSubject());
124 Subject
.doAs(loginContext
.getSubject(), (PrivilegedAction
<Void
>) () -> {
126 // TODO factorise with web app
127 parent
.setData(CmsApp
.UI_NAME_PROPERTY
, uiName
);
128 ui
= cmsApp
.initUi(parent
);
129 if (ui
instanceof Composite
)
130 ((Composite
) ui
).setLayoutData(CmsSwtUtils
.fillAll());
131 // ui.setLayoutData(CmsUiUtils.fillAll());
132 // we need ui to be set before refresh so that CmsView can store UI context data
134 cmsApp
.refreshUi(ui
, null);
137 CmsTheme theme
= CmsSwtUtils
.getCmsTheme(parent
);
139 cssEngine
= new CSSSWTEngineImpl(display
);
140 for (String path
: theme
.getSwtCssPaths()) {
141 try (InputStream in
= theme
.loadPath(path
)) {
142 cssEngine
.parseStyleSheet(in
);
143 } catch (IOException e
) {
144 throw new IllegalStateException("Cannot load stylesheet " + path
, e
);
147 cssEngine
.setErrorHandler(new CSSErrorHandler() {
148 public void error(Exception e
) {
149 log
.error("SWT styling error: ", e
);
154 shell
.layout(true, true);
157 while (!shell
.isDisposed()) {
158 if (!display
.readAndDispatch())
173 public String
getUid() {
178 public UxContext
getUxContext() {
179 throw new UnsupportedOperationException();
183 public void navigateTo(String state
) {
184 throw new UnsupportedOperationException();
188 public void authChange(LoginContext loginContext
) {
192 public void logout() {
193 if (loginContext
!= null)
195 loginContext
.logout();
196 } catch (LoginException e
) {
197 log
.error("Cannot log out", e
);
202 public void exception(Throwable e
) {
203 log
.error("Unexpected exception in CMS RCP", e
);
207 public CmsImageManager
getImageManager() {
208 throw new UnsupportedOperationException();
212 public CmsSession
getCmsSession() {
213 CmsSession cmsSession
= CmsOsgiUtils
.getCmsSession(bundleContext
, getSubject());
218 public Object
getData(String key
) {
220 return ui
.getData(key
);
222 throw new IllegalStateException("UI is not initialized");
227 public void setData(String key
, Object value
) {
229 ui
.setData(key
, value
);
231 throw new IllegalStateException("UI is not initialized");
236 public boolean isAnonymous() {
241 public void applyStyles(Object node
) {
242 if (cssEngine
!= null)
243 cssEngine
.applyStyles(node
, true);
247 public void sendEvent(String topic
, Map
<String
, Object
> properties
) {
248 if (properties
== null)
249 properties
= new HashMap
<>();
250 if (properties
.containsKey(CMS_VIEW_UID_PROPERTY
) && !properties
.get(CMS_VIEW_UID_PROPERTY
).equals(uid
))
251 throw new IllegalArgumentException("Property " + CMS_VIEW_UID_PROPERTY
+ " is set to another CMS view uid ("
252 + properties
.get(CMS_VIEW_UID_PROPERTY
) + ") then " + uid
);
253 properties
.put(CMS_VIEW_UID_PROPERTY
, uid
);
254 eventAdmin
.sendEvent(new Event(topic
, properties
));
257 public <T
> T
doAs(PrivilegedAction
<T
> action
) {
258 return Subject
.doAs(getSubject(), action
);
261 protected Subject
getSubject() {
262 return loginContext
.getSubject();
266 * DEPENDENCY INJECTION
268 public void setCmsApp(CmsApp cmsApp
) {
269 this.cmsApp
= cmsApp
;
272 public void setEventAdmin(EventAdmin eventAdmin
) {
273 this.eventAdmin
= eventAdmin
;