Introduce UX scheduler
[lgpl/argeo-commons.git] / swt / org.argeo.cms.swt / src / org / argeo / cms / swt / AbstractSwtCmsView.java
index c481a2cc07be061e38b0db28c2212e50d096775a..127be0856195fecf7b0d484a16e654030f6849c4 100644 (file)
@@ -2,6 +2,8 @@ package org.argeo.cms.swt;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Timer;
+import java.util.TimerTask;
 import java.util.concurrent.Callable;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionException;
@@ -17,18 +19,32 @@ import org.argeo.api.cms.ux.CmsImageManager;
 import org.argeo.api.cms.ux.CmsUi;
 import org.argeo.api.cms.ux.CmsView;
 import org.argeo.api.cms.ux.UxContext;
-import org.argeo.cms.auth.CurrentUser;
-import org.argeo.util.CurrentSubject;
+import org.argeo.cms.CurrentUser;
+import org.argeo.cms.util.CurrentSubject;
 import org.eclipse.swt.widgets.Display;
 
 public abstract class AbstractSwtCmsView implements CmsView {
        private final static CmsLog log = CmsLog.getLog(AbstractSwtCmsView.class);
 
+       /** A timer to be used to perform background UX tasks. */
+       private final static Timer uxTimer = new Timer(true);
+
+       static {
+               // purge every day
+               uxTimer.schedule(new TimerTask() {
+
+                       @Override
+                       public void run() {
+                               uxTimer.purge();
+                       }
+               }, 0, 24 * 60 * 60 * 1000);
+       }
+
        protected final String uiName;
 
        protected LoginContext loginContext;
        protected String state;
-       protected Throwable exception;
+//     protected Throwable exception;
        protected UxContext uxContext;
        protected CmsImageManager imageManager;
 
@@ -54,10 +70,10 @@ public abstract class AbstractSwtCmsView implements CmsView {
                                        + properties.get(CMS_VIEW_UID_PROPERTY) + ") then " + uid);
                properties.put(CMS_VIEW_UID_PROPERTY, uid);
 
-               log.debug(() -> uid + ": send event to " + topic);
+               log.trace(() -> uid + ": send event to " + topic);
 
                getCmsEventBus().sendEvent(topic, properties);
-               //getCmsApp().onEvent(topic, properties);
+               // getCmsApp().onEvent(topic, properties);
        }
 
 //     public void runAs(Runnable runnable) {
@@ -68,7 +84,7 @@ public abstract class AbstractSwtCmsView implements CmsView {
                try {
                        CompletableFuture<T> result = new CompletableFuture<>();
                        Runnable toDo = () -> {
-                               log.debug(() -> uid + ": process doAs");
+                               log.trace(() -> uid + ": process doAs");
                                Subject subject = CurrentSubject.current();
                                T res;
                                if (subject != null) {
@@ -138,4 +154,37 @@ public abstract class AbstractSwtCmsView implements CmsView {
                }
        }
 
+       @Override
+       public TimerTask schedule(Runnable task, long delay) {
+               TimerTask timerTask = newSwtUxTimerTask(task);
+               uxTimer.schedule(timerTask, delay);
+               return timerTask;
+       }
+
+       @Override
+       public TimerTask schedule(Runnable task, long delay, long period) {
+               TimerTask timerTask = newSwtUxTimerTask(task);
+               uxTimer.schedule(timerTask, delay, period);
+               return timerTask;
+       }
+
+       protected TimerTask newSwtUxTimerTask(Runnable todo) {
+               return new TimerTask() {
+
+                       @Override
+                       public void run() {
+                               synchronized (display) {
+                                       try {
+                                               if (!display.isDisposed()) {
+                                                       display.syncExec(() -> {
+                                                               todo.run();
+                                                       });
+                                               }
+                                       } catch (Exception e) {
+                                               log.error("Cannot run UX timer task", e);
+                                       }
+                               }
+                       }
+               };
+       }
 }