1 package org
.argeo
.cms
.swt
;
3 import java
.util
.HashMap
;
5 import java
.util
.Timer
;
6 import java
.util
.TimerTask
;
7 import java
.util
.concurrent
.Callable
;
8 import java
.util
.concurrent
.CompletableFuture
;
9 import java
.util
.concurrent
.CompletionException
;
10 import java
.util
.concurrent
.ExecutionException
;
12 import javax
.security
.auth
.Subject
;
13 import javax
.security
.auth
.login
.LoginContext
;
15 import org
.argeo
.api
.cms
.CmsApp
;
16 import org
.argeo
.api
.cms
.CmsEventBus
;
17 import org
.argeo
.api
.cms
.CmsLog
;
18 import org
.argeo
.api
.cms
.ux
.CmsImageManager
;
19 import org
.argeo
.api
.cms
.ux
.CmsUi
;
20 import org
.argeo
.api
.cms
.ux
.CmsView
;
21 import org
.argeo
.api
.cms
.ux
.UxContext
;
22 import org
.argeo
.cms
.CurrentUser
;
23 import org
.argeo
.cms
.util
.CurrentSubject
;
24 import org
.eclipse
.swt
.widgets
.Display
;
26 public abstract class AbstractSwtCmsView
implements CmsView
{
27 private final static CmsLog log
= CmsLog
.getLog(AbstractSwtCmsView
.class);
29 /** A timer to be used to perform background UX tasks. */
30 private final static Timer uxTimer
= new Timer(true);
34 uxTimer
.schedule(new TimerTask() {
40 }, 0, 24 * 60 * 60 * 1000);
43 protected final String uiName
;
45 protected LoginContext loginContext
;
46 protected String state
;
47 // protected Throwable exception;
48 protected UxContext uxContext
;
49 protected CmsImageManager imageManager
;
51 protected Display display
;
56 public AbstractSwtCmsView(String uiName
) {
60 public abstract CmsEventBus
getCmsEventBus();
62 public abstract CmsApp
getCmsApp();
65 public void sendEvent(String topic
, Map
<String
, Object
> properties
) {
66 if (properties
== null)
67 properties
= new HashMap
<>();
68 if (properties
.containsKey(CMS_VIEW_UID_PROPERTY
) && !properties
.get(CMS_VIEW_UID_PROPERTY
).equals(uid
))
69 throw new IllegalArgumentException("Property " + CMS_VIEW_UID_PROPERTY
+ " is set to another CMS view uid ("
70 + properties
.get(CMS_VIEW_UID_PROPERTY
) + ") then " + uid
);
71 properties
.put(CMS_VIEW_UID_PROPERTY
, uid
);
73 log
.trace(() -> uid
+ ": send event to " + topic
);
75 getCmsEventBus().sendEvent(topic
, properties
);
76 // getCmsApp().onEvent(topic, properties);
79 // public void runAs(Runnable runnable) {
80 // display.asyncExec(() -> doAs(Executors.callable(runnable)));
83 public <T
> T
doAs(Callable
<T
> action
) {
85 CompletableFuture
<T
> result
= new CompletableFuture
<>();
86 Runnable toDo
= () -> {
87 log
.trace(() -> uid
+ ": process doAs");
88 Subject subject
= CurrentSubject
.current();
90 if (subject
!= null) {
91 assert subject
== getSubject();
94 } catch (Exception e
) {
95 throw new CompletionException("Failed to execute action for " + subject
, e
);
98 res
= CurrentSubject
.callAs(getSubject(), action
);
100 result
.complete(res
);
102 if (Thread
.currentThread() == display
.getThread())
105 display
.asyncExec(toDo
);
108 // throw new IllegalStateException("Must be called from UI thread");
110 } catch (InterruptedException
| ExecutionException e
) {
111 throw new IllegalStateException("Cannot execute action ins CMS view " + uid
, e
);
116 public UxContext
getUxContext() {
121 public String
getUid() {
126 public CmsImageManager
<?
, ?
> getImageManager() {
131 public boolean isAnonymous() {
132 return CurrentUser
.isAnonymous(getSubject());
135 protected Subject
getSubject() {
136 return loginContext
.getSubject();
140 public Object
getData(String key
) {
142 return ui
.getData(key
);
144 throw new IllegalStateException("UI is not initialized");
149 public void setData(String key
, Object value
) {
151 ui
.setData(key
, value
);
153 throw new IllegalStateException("UI is not initialized");
158 public TimerTask
schedule(Runnable task
, long delay
) {
159 TimerTask timerTask
= newSwtUxTimerTask(task
);
160 uxTimer
.schedule(timerTask
, delay
);
165 public TimerTask
schedule(Runnable task
, long delay
, long period
) {
166 TimerTask timerTask
= newSwtUxTimerTask(task
);
167 uxTimer
.schedule(timerTask
, delay
, period
);
171 protected TimerTask
newSwtUxTimerTask(Runnable todo
) {
172 return new TimerTask() {
176 synchronized (display
) {
178 if (!display
.isDisposed()) {
179 display
.syncExec(() -> {
183 } catch (Exception e
) {
184 log
.error("Cannot run UX timer task", e
);