]> git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.cms.swt/src/org/argeo/cms/swt/AbstractSwtCmsView.java
2059803036f2ab476205564534f0203ed1bdde62
[lgpl/argeo-commons.git] / swt / org.argeo.cms.swt / src / org / argeo / cms / swt / AbstractSwtCmsView.java
1 package org.argeo.cms.swt;
2
3 import java.security.PrivilegedAction;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.concurrent.CompletableFuture;
7 import java.util.concurrent.ExecutionException;
8
9 import javax.security.auth.Subject;
10 import javax.security.auth.login.LoginContext;
11
12 import org.argeo.api.cms.CmsEventBus;
13 import org.argeo.api.cms.ux.CmsImageManager;
14 import org.argeo.api.cms.ux.CmsUi;
15 import org.argeo.api.cms.ux.CmsView;
16 import org.argeo.api.cms.ux.UxContext;
17 import org.argeo.cms.auth.CurrentUser;
18 import org.eclipse.swt.widgets.Display;
19
20 public abstract class AbstractSwtCmsView implements CmsView {
21 protected final String uiName;
22
23 protected LoginContext loginContext;
24 protected String state;
25 protected Throwable exception;
26 protected UxContext uxContext;
27 protected CmsImageManager imageManager;
28
29 protected Display display;
30 protected CmsUi ui;
31
32 protected String uid;
33
34 public AbstractSwtCmsView(String uiName) {
35 this.uiName = uiName;
36 }
37
38 public abstract CmsEventBus getCmsEventBus();
39
40 @Override
41 public void sendEvent(String topic, Map<String, Object> properties) {
42 if (properties == null)
43 properties = new HashMap<>();
44 if (properties.containsKey(CMS_VIEW_UID_PROPERTY) && !properties.get(CMS_VIEW_UID_PROPERTY).equals(uid))
45 throw new IllegalArgumentException("Property " + CMS_VIEW_UID_PROPERTY + " is set to another CMS view uid ("
46 + properties.get(CMS_VIEW_UID_PROPERTY) + ") then " + uid);
47 properties.put(CMS_VIEW_UID_PROPERTY, uid);
48 getCmsEventBus().sendEvent(topic, properties);
49 }
50
51 public <T> T doAs(PrivilegedAction<T> action) {
52 try {
53 CompletableFuture<T> result = new CompletableFuture<>();
54 Runnable toDo = () -> {
55 T res = Subject.doAs(getSubject(), action);
56 result.complete(res);
57 };
58 if (Thread.currentThread() == display.getThread())
59 toDo.run();
60 else
61 display.syncExec(toDo);
62 return result.get();
63 } catch (InterruptedException | ExecutionException e) {
64 throw new IllegalStateException("Cannot execute action ins CMS view " + uid, e);
65 }
66 }
67
68 @Override
69 public UxContext getUxContext() {
70 return uxContext;
71 }
72
73 @Override
74 public String getUid() {
75 return uid;
76 }
77
78 @Override
79 public CmsImageManager<?, ?> getImageManager() {
80 return imageManager;
81 }
82
83 @Override
84 public boolean isAnonymous() {
85 return CurrentUser.isAnonymous(getSubject());
86 }
87
88 protected Subject getSubject() {
89 return loginContext.getSubject();
90 }
91
92 @Override
93 public Object getData(String key) {
94 if (ui != null) {
95 return ui.getData(key);
96 } else {
97 throw new IllegalStateException("UI is not initialized");
98 }
99 }
100
101 @Override
102 public void setData(String key, Object value) {
103 if (ui != null) {
104 ui.setData(key, value);
105 } else {
106 throw new IllegalStateException("UI is not initialized");
107 }
108 }
109
110 }