]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/CmsView.java
Merge remote-tracking branch 'origin/master' into v2.x
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / CmsView.java
1 package org.argeo.cms.ui;
2
3 import java.security.PrivilegedAction;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import javax.security.auth.login.LoginContext;
8
9 import org.argeo.cms.auth.CmsSession;
10 import org.eclipse.swt.widgets.Control;
11 import org.eclipse.swt.widgets.Shell;
12
13 /** Provides interaction with the CMS system. */
14 public interface CmsView {
15 final static String CMS_VIEW_UID_PROPERTY = "argeo.cms.view.uid";
16 // String KEY = "org.argeo.cms.ui.view";
17
18 String getUid();
19
20 UxContext getUxContext();
21
22 // NAVIGATION
23 void navigateTo(String state);
24
25 // SECURITY
26 void authChange(LoginContext loginContext);
27
28 void logout();
29
30 // void registerCallbackHandler(CallbackHandler callbackHandler);
31
32 // SERVICES
33 void exception(Throwable e);
34
35 CmsImageManager getImageManager();
36
37 boolean isAnonymous();
38
39 /**
40 * Send an event to this topic. Does nothing by default., but if implemented it
41 * MUST set the {@link #CMS_VIEW_UID_PROPERTY} in the properties.
42 */
43 default void sendEvent(String topic, Map<String, Object> properties) {
44
45 }
46
47 /**
48 * Convenience methods for when {@link #sendEvent(String, Map)} only requires
49 * one single parameter.
50 */
51 default void sendEvent(String topic, String param, Object value) {
52 Map<String, Object> properties = new HashMap<>();
53 properties.put(param, value);
54 sendEvent(topic, properties);
55 }
56
57 default void applyStyles(Object widget) {
58
59 }
60
61 default <T> T doAs(PrivilegedAction<T> action) {
62 throw new UnsupportedOperationException();
63 }
64
65 default Void runAs(Runnable runnable) {
66 return doAs(new PrivilegedAction<Void>() {
67
68 @Override
69 public Void run() {
70 if (runnable != null)
71 runnable.run();
72 return null;
73 }
74 });
75 }
76
77 default void stateChanged(String state, String title) {
78 }
79
80 default CmsSession getCmsSession() {
81 throw new UnsupportedOperationException();
82 }
83
84 default Object getData(String key) {
85 throw new UnsupportedOperationException();
86 }
87
88 @SuppressWarnings("unchecked")
89 default <T> T getUiContext(Class<T> clss) {
90 return (T) getData(clss.getName());
91 }
92
93 default <T> void setUiContext(Class<T> clss, T instance) {
94 setData(clss.getName(), instance);
95 }
96
97 default void setData(String key, Object value) {
98 throw new UnsupportedOperationException();
99 }
100
101 static CmsView getCmsView(Control parent) {
102 // find parent shell
103 Shell topShell = parent.getShell();
104 while (topShell.getParent() != null)
105 topShell = (Shell) topShell.getParent();
106 return (CmsView) topShell.getData(CmsView.class.getName());
107 }
108
109 static void registerCmsView(Shell shell, CmsView view) {
110 // find parent shell
111 Shell topShell = shell;
112 while (topShell.getParent() != null)
113 topShell = (Shell) topShell.getParent();
114 // check if already set
115 if (topShell.getData(CmsView.class.getName()) != null) {
116 CmsView registeredView = (CmsView) topShell.getData(CmsView.class.getName());
117 throw new IllegalArgumentException("Cms view " + registeredView + " already registered in this shell");
118 }
119 shell.setData(CmsView.class.getName(), view);
120 }
121
122 }