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