1 package org
.argeo
.cms
.swt
;
3 import java
.util
.HashMap
;
5 import java
.util
.concurrent
.Callable
;
6 import java
.util
.concurrent
.CompletableFuture
;
7 import java
.util
.concurrent
.CompletionException
;
8 import java
.util
.concurrent
.ExecutionException
;
10 import javax
.security
.auth
.Subject
;
11 import javax
.security
.auth
.login
.LoginContext
;
13 import org
.argeo
.api
.cms
.CmsApp
;
14 import org
.argeo
.api
.cms
.CmsEventBus
;
15 import org
.argeo
.api
.cms
.CmsLog
;
16 import org
.argeo
.api
.cms
.ux
.CmsImageManager
;
17 import org
.argeo
.api
.cms
.ux
.CmsUi
;
18 import org
.argeo
.api
.cms
.ux
.CmsView
;
19 import org
.argeo
.api
.cms
.ux
.UxContext
;
20 import org
.argeo
.cms
.auth
.CurrentUser
;
21 import org
.argeo
.util
.CurrentSubject
;
22 import org
.eclipse
.swt
.widgets
.Display
;
24 public abstract class AbstractSwtCmsView
implements CmsView
{
25 private final static CmsLog log
= CmsLog
.getLog(AbstractSwtCmsView
.class);
27 protected final String uiName
;
29 protected LoginContext loginContext
;
30 protected String state
;
31 // protected Throwable exception;
32 protected UxContext uxContext
;
33 protected CmsImageManager imageManager
;
35 protected Display display
;
40 public AbstractSwtCmsView(String uiName
) {
44 public abstract CmsEventBus
getCmsEventBus();
46 public abstract CmsApp
getCmsApp();
49 public void sendEvent(String topic
, Map
<String
, Object
> properties
) {
50 if (properties
== null)
51 properties
= new HashMap
<>();
52 if (properties
.containsKey(CMS_VIEW_UID_PROPERTY
) && !properties
.get(CMS_VIEW_UID_PROPERTY
).equals(uid
))
53 throw new IllegalArgumentException("Property " + CMS_VIEW_UID_PROPERTY
+ " is set to another CMS view uid ("
54 + properties
.get(CMS_VIEW_UID_PROPERTY
) + ") then " + uid
);
55 properties
.put(CMS_VIEW_UID_PROPERTY
, uid
);
57 log
.trace(() -> uid
+ ": send event to " + topic
);
59 getCmsEventBus().sendEvent(topic
, properties
);
60 // getCmsApp().onEvent(topic, properties);
63 // public void runAs(Runnable runnable) {
64 // display.asyncExec(() -> doAs(Executors.callable(runnable)));
67 public <T
> T
doAs(Callable
<T
> action
) {
69 CompletableFuture
<T
> result
= new CompletableFuture
<>();
70 Runnable toDo
= () -> {
71 log
.trace(() -> uid
+ ": process doAs");
72 Subject subject
= CurrentSubject
.current();
74 if (subject
!= null) {
75 assert subject
== getSubject();
78 } catch (Exception e
) {
79 throw new CompletionException("Failed to execute action for " + subject
, e
);
82 res
= CurrentSubject
.callAs(getSubject(), action
);
86 if (Thread
.currentThread() == display
.getThread())
89 display
.asyncExec(toDo
);
92 // throw new IllegalStateException("Must be called from UI thread");
94 } catch (InterruptedException
| ExecutionException e
) {
95 throw new IllegalStateException("Cannot execute action ins CMS view " + uid
, e
);
100 public UxContext
getUxContext() {
105 public String
getUid() {
110 public CmsImageManager
<?
, ?
> getImageManager() {
115 public boolean isAnonymous() {
116 return CurrentUser
.isAnonymous(getSubject());
119 protected Subject
getSubject() {
120 return loginContext
.getSubject();
124 public Object
getData(String key
) {
126 return ui
.getData(key
);
128 throw new IllegalStateException("UI is not initialized");
133 public void setData(String key
, Object value
) {
135 ui
.setData(key
, value
);
137 throw new IllegalStateException("UI is not initialized");