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