Object getData(String key);
void setData(String key, Object value);
-
+
CmsView getCmsView();
+
+ long getLastAccess();
+
+ void updateLastAccess();
+
}
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
+import java.util.TimerTask;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
throw new UnsupportedOperationException();
}
+ /** Schedule a one-shot UX task to be executed within the UX context/thread. */
+ TimerTask schedule(Runnable task, long delay);
+
+ /** Schedule a recurring UX task to be executed within the UX context/thread. */
+ TimerTask schedule(Runnable task, long delay, long period);
+
}
import org.argeo.api.cms.ux.CmsView;
import org.argeo.cms.util.CurrentSubject;
+/** Utilities around UX. */
public class CmsUxUtils {
public static ContentSession getContentSession(ContentRepository contentRepository, CmsView cmsView) {
return CurrentSubject.callAs(cmsView.getCmsSession().getSubject(), () -> contentRepository.get());
return content.getName().getLocalPart();
}
- /** singleton */
- private CmsUxUtils() {
-
- }
-
public static StringBuilder imgBuilder(String src, String width, String height) {
return new StringBuilder(64).append("<img width='").append(width).append("' height='").append(height)
.append("' src='").append(src).append("'");
public static String img(String src, Cms2DSize size) {
return img(src, Integer.toString(size.width()), Integer.toString(size.height()));
}
+
+ /** singleton */
+ private CmsUxUtils() {
+
+ }
+
}
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;
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;
}
}
+ @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);
+ }
+ }
+ }
+ };
+ }
}
package org.argeo.cms.swt;
+import java.util.TimerTask;
+
import org.argeo.api.cms.ux.CmsUi;
import org.argeo.api.cms.ux.CmsView;
import org.eclipse.swt.layout.GridLayout;
private static final long serialVersionUID = -107939076610406448L;
+ /** Last time the UI was accessed. */
+ private long lastAccess = System.currentTimeMillis();
+ private TimerTask timeoutTask;
+
private CmsView cmsView;
public CmsSwtUi(Composite parent, int style) {
super(parent, style);
cmsView = CmsSwtUtils.getCmsView(parent);
-
+ // TODO make timeout configurable
+ setUiTimeout(12 * 60 * 60 * 1000);// 12 hours
setLayout(new GridLayout());
}
+ @Override
public CmsView getCmsView() {
return cmsView;
}
+ @Override
+ public long getLastAccess() {
+ return lastAccess;
+ }
+
+ @Override
+ public void updateLastAccess() {
+ this.lastAccess = System.currentTimeMillis();
+ }
+
+ public void setUiTimeout(long uiTimeout) {
+ if (timeoutTask != null)
+ timeoutTask.cancel();
+ timeoutTask = cmsView.schedule(() -> {
+ if (System.currentTimeMillis() - getLastAccess() >= uiTimeout)
+ dispose();
+ }, 0, 1000);
+ }
+
}
\ No newline at end of file