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