]> git.argeo.org Git - gpl/argeo-suite.git/blob - ux/DefaultLeadPane.java
Prepare next development cycle
[gpl/argeo-suite.git] / ux / DefaultLeadPane.java
1 package org.argeo.app.swt.ux;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.TreeMap;
10
11 import org.argeo.api.acr.Content;
12 import org.argeo.api.cms.CmsLog;
13 import org.argeo.api.cms.ux.CmsIcon;
14 import org.argeo.api.cms.ux.CmsView;
15 import org.argeo.app.api.RankedObject;
16 import org.argeo.app.core.SuiteUtils;
17 import org.argeo.app.ux.SuiteIcon;
18 import org.argeo.app.ux.SuiteStyle;
19 import org.argeo.app.ux.SuiteUxEvent;
20 import org.argeo.cms.CurrentUser;
21 import org.argeo.cms.LocaleUtils;
22 import org.argeo.cms.Localized;
23 import org.argeo.cms.swt.CmsSwtTheme;
24 import org.argeo.cms.swt.CmsSwtUtils;
25 import org.argeo.cms.swt.acr.SwtUiProvider;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Label;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.framework.Constants;
35 import org.osgi.framework.wiring.BundleWiring;
36
37 /** Side pane listing various perspectives. */
38 public class DefaultLeadPane implements SwtUiProvider {
39 private final static CmsLog log = CmsLog.getLog(DefaultLeadPane.class);
40
41 public static enum Property {
42 defaultLayers, adminLayers;
43 }
44
45 private Map<String, RankedObject<SwtAppLayer>> layers = Collections.synchronizedSortedMap(new TreeMap<>());
46 private List<String> defaultLayers;
47 private List<String> adminLayers = new ArrayList<>();
48
49 private ClassLoader l10nClassLoader;
50
51 @Override
52 public Control createUiPart(Composite parent, Content node) {
53 CmsView cmsView = CmsSwtUtils.getCmsView(parent);
54 parent.setLayout(CmsSwtUtils.noSpaceGridLayout());
55 Composite appLayersC = new Composite(parent, SWT.NONE);
56 CmsSwtUtils.style(appLayersC, SuiteStyle.leadPane);
57 GridLayout layout = new GridLayout();
58 layout.verticalSpacing = 10;
59 layout.marginTop = 10;
60 layout.marginLeft = 10;
61 layout.marginRight = 10;
62 appLayersC.setLayout(layout);
63 appLayersC.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
64
65 Composite adminLayersC;
66 if (!adminLayers.isEmpty()) {
67 adminLayersC = new Composite(parent, SWT.NONE);
68 CmsSwtUtils.style(adminLayersC, SuiteStyle.leadPane);
69 GridLayout adminLayout = new GridLayout();
70 adminLayout.verticalSpacing = 10;
71 adminLayout.marginBottom = 10;
72 adminLayout.marginLeft = 10;
73 adminLayout.marginRight = 10;
74 adminLayersC.setLayout(adminLayout);
75 adminLayersC.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, false, true));
76 } else {
77 adminLayersC = null;
78 }
79
80 // boolean isAdmin = cmsView.doAs(() -> CurrentUser.isInRole(NodeConstants.ROLE_USER_ADMIN));
81 // Set<String> userRoles = cmsView.doAs(() -> CurrentUser.roles());
82 Button first = null;
83 layers: for (String layerDef : defaultLayers) {
84 layerDef = layerDef.trim();
85 if ("".equals(layerDef))
86 continue layers;// skip empty lines
87 String[] semiColArr = layerDef.split(";");
88 String layerId = semiColArr[0];
89 Set<String> layerRoles = SuiteUtils.extractRoles(semiColArr);
90 if (layers.containsKey(layerId)) {
91 if (!layerRoles.isEmpty()) {
92 boolean authorized = false;
93 authorized = cmsView.doAs(() -> {
94 for (String layerRole : layerRoles) {
95 if (CurrentUser.implies(layerRole, null)) {
96 return true;
97 }
98 }
99 return false;
100 });
101 if (!authorized)
102 continue layers;// skip unauthorized layer
103 // Set<String> intersection = new HashSet<String>(layerRoles);
104 // intersection.retainAll(userRoles);
105 // if (intersection.isEmpty())
106 // continue layers;// skip unauthorized layer
107 }
108 RankedObject<SwtAppLayer> layerObj = layers.get(layerId);
109
110 Localized title = null;
111 if (!adminLayers.contains(layerId)) {
112 String titleStr = (String) layerObj.getProperties().get(SwtAppLayer.Property.title.name());
113 if (titleStr != null) {
114 if (titleStr.startsWith("%")) {
115 // LocaleUtils.local(titleStr, getClass().getClassLoader());
116 title = () -> titleStr;
117 } else {
118 title = new Localized.Untranslated(titleStr);
119 }
120 }
121 }
122
123 String iconName = (String) layerObj.getProperties().get(SwtAppLayer.Property.icon.name());
124 SuiteIcon icon = null;
125 if (iconName != null)
126 icon = SuiteIcon.valueOf(iconName);
127
128 Composite buttonParent;
129 if (adminLayers.contains(layerId))
130 buttonParent = adminLayersC;
131 else
132 buttonParent = appLayersC;
133 Button b = createLayerButton(buttonParent, layerId, title, icon, l10nClassLoader);
134 if (first == null)
135 first = b;
136 }
137 }
138 return first;
139 }
140
141 protected Button createLayerButton(Composite parent, String layer, Localized msg, CmsIcon icon,
142 ClassLoader l10nClassLoader) {
143 CmsSwtTheme theme = CmsSwtUtils.getCmsTheme(parent);
144 Button button = new Button(parent, SWT.PUSH);
145 CmsSwtUtils.style(button, SuiteStyle.leadPane);
146 if (icon != null)
147 button.setImage(theme.getBigIcon(icon));
148 button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, false));
149 // button.setToolTipText(msg.lead());
150 if (msg != null) {
151 Label lbl = new Label(parent, SWT.CENTER);
152 CmsSwtUtils.style(lbl, SuiteStyle.leadPane);
153 String txt = LocaleUtils.lead(msg, l10nClassLoader);
154 // String txt = msg.lead();
155 lbl.setText(txt);
156 lbl.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
157 }
158 CmsSwtUtils.sendEventOnSelect(button, SuiteUxEvent.switchLayer.topic(), SuiteUxEvent.LAYER, layer);
159 return button;
160 }
161
162 public void init(BundleContext bundleContext, Map<String, Object> properties) {
163 l10nClassLoader = bundleContext != null ? bundleContext.getBundle().adapt(BundleWiring.class).getClassLoader()
164 : getClass().getClassLoader();
165
166 String[] defaultLayers = (String[]) properties.get(Property.defaultLayers.toString());
167 if (defaultLayers == null)
168 throw new IllegalArgumentException("Default layers must be set.");
169 this.defaultLayers = Arrays.asList(defaultLayers);
170 if (log.isDebugEnabled())
171 log.debug("Default layers: " + Arrays.asList(defaultLayers));
172 String[] adminLayers = (String[]) properties.get(Property.adminLayers.toString());
173 if (adminLayers != null) {
174 this.adminLayers = Arrays.asList(adminLayers);
175 if (log.isDebugEnabled())
176 log.debug("Admin layers: " + Arrays.asList(adminLayers));
177 }
178 }
179
180 public void destroy(BundleContext bundleContext, Map<String, String> properties) {
181
182 }
183
184 public void addLayer(SwtAppLayer layer, Map<String, Object> properties) {
185 if (properties.containsKey(Constants.SERVICE_PID)) {
186 String pid = (String) properties.get(Constants.SERVICE_PID);
187 RankedObject.putIfHigherRank(layers, pid, layer, properties);
188 }
189 }
190
191 public void removeLayer(SwtAppLayer layer, Map<String, Object> properties) {
192 if (properties.containsKey(Constants.SERVICE_PID)) {
193 String pid = (String) properties.get(Constants.SERVICE_PID);
194 if (layers.containsKey(pid)) {
195 if (layers.get(pid).equals(new RankedObject<SwtAppLayer>(layer, properties))) {
196 layers.remove(pid);
197 }
198 }
199 }
200 }
201
202 // protected Button createLayerButton(Composite parent, String layer, Localized msg, CmsIcon icon) {
203 // CmsTheme theme = CmsTheme.getCmsTheme(parent);
204 // Button button = new Button(parent, SWT.PUSH);
205 // CmsUiUtils.style(button, SuiteStyle.leadPane);
206 // if (icon != null)
207 // button.setImage(icon.getBigIcon(theme));
208 // button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, false));
209 // // button.setToolTipText(msg.lead());
210 // if (msg != null) {
211 // Label lbl = new Label(parent, SWT.CENTER);
212 // CmsUiUtils.style(lbl, SuiteStyle.leadPane);
213 // // CmsUiUtils.markup(lbl);
214 // ClassLoader l10nClassLoader = getClass().getClassLoader();
215 // String txt = LocaleUtils.lead(msg, l10nClassLoader);
216 //// String txt = msg.lead();
217 // lbl.setText(txt);
218 // lbl.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
219 // }
220 // CmsUiUtils.sendEventOnSelect(button, SuiteEvent.switchLayer.topic(), SuiteEvent.LAYER, layer);
221 // return button;
222 // }
223 }