1 package org.argeo.suite.ui;
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.HashSet;
8 import java.util.TreeMap;
10 import javax.jcr.Node;
11 import javax.jcr.RepositoryException;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.argeo.cms.Localized;
16 import org.argeo.cms.auth.CurrentUser;
17 import org.argeo.cms.ui.CmsUiProvider;
18 import org.argeo.cms.ui.CmsView;
19 import org.argeo.suite.RankedObject;
20 import org.argeo.suite.SuiteUtils;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.osgi.framework.Constants;
27 /** Side pane listing various perspectives. */
28 public class DefaultLeadPane implements CmsUiProvider {
29 private final static Log log = LogFactory.getLog(DefaultLeadPane.class);
31 public static enum Property {
32 defaultLayers, adminLayers;
35 private Map<String, RankedObject<SuiteLayer>> layers = Collections.synchronizedSortedMap(new TreeMap<>());
36 private String[] defaultLayers;
37 private String[] adminLayers;
40 public Control createUi(Composite parent, Node node) throws RepositoryException {
41 CmsView cmsView = CmsView.getCmsView(parent);
42 GridLayout layout = new GridLayout();
43 layout.verticalSpacing = 10;
44 layout.marginTop = 10;
45 layout.marginLeft = 10;
46 layout.marginRight = 10;
47 parent.setLayout(layout);
49 // boolean isAdmin = cmsView.doAs(() -> CurrentUser.isInRole(NodeConstants.ROLE_USER_ADMIN));
50 Set<String> userRoles = cmsView.doAs(() -> CurrentUser.roles());
52 layers: for (String layerDef : defaultLayers) {
53 layerDef = layerDef.trim();
54 if ("".equals(layerDef))
55 continue layers;// skip empty lines
56 String[] semiColArr = layerDef.split(";");
57 String layerId = semiColArr[0];
58 Set<String> layerRoles = SuiteUtils.extractRoles(semiColArr);
59 if (layers.containsKey(layerId)) {
60 if (!layerRoles.isEmpty()) {
61 Set<String> intersection = new HashSet<String>(layerRoles);
62 intersection.retainAll(userRoles);
63 if (intersection.isEmpty())
64 continue layers;// skip unauthorized layer
66 RankedObject<SuiteLayer> layerObj = layers.get(layerId);
68 // TODO deal with i10n
69 String titleStr = (String) layerObj.getProperties().get(SuiteLayer.Property.title.name());
70 Localized title = null;
72 title = new Localized.Untranslated(titleStr);
74 String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
75 SuiteIcon icon = null;
77 icon = SuiteIcon.valueOf(iconName);
79 Button b = SuiteUiUtils.createLayerButton(parent, layerId, title, icon);
85 // if (isAdmin && adminLayers != null)
86 // for (String layerId : adminLayers) {
87 // if (layers.containsKey(layerId)) {
88 // RankedObject<SuiteLayer> layerObj = layers.get(layerId);
90 // String titleStr = (String) layerObj.getProperties().get(SuiteLayer.Property.title.name());
91 // Localized title = null;
92 // if (titleStr != null)
93 // title = new Localized.Untranslated(titleStr);
95 // String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
96 // SuiteIcon icon = null;
97 // if (iconName != null)
98 // icon = SuiteIcon.valueOf(iconName);
100 // Button b = SuiteUiUtils.createLayerButton(parent, layerId, title, icon);
101 // if (first == null)
106 // Button dashboardB = createButton(parent, SuiteMsg.dashboard.name(), SuiteMsg.dashboard, SuiteIcon.dashboard);
107 if (!cmsView.isAnonymous()) {
108 // createButton(parent, SuiteMsg.documents.name(), SuiteMsg.documents, SuiteIcon.documents);
109 // createButton(parent, SuiteMsg.people.name(), SuiteMsg.people, SuiteIcon.people);
110 // createButton(parent, SuiteMsg.locations.name(), SuiteMsg.locations, SuiteIcon.location);
115 public void init(Map<String, Object> properties) {
116 defaultLayers = (String[]) properties.get(Property.defaultLayers.toString());
117 if (defaultLayers == null)
118 throw new IllegalArgumentException("Default layers must be set.");
119 if (log.isDebugEnabled())
120 log.debug("Default layers: " + Arrays.asList(defaultLayers));
121 adminLayers = (String[]) properties.get(Property.adminLayers.toString());
122 if (adminLayers != null)
123 log.error("DEPRECATED - Admin layers: " + Arrays.asList(adminLayers));
126 public void addLayer(SuiteLayer layer, Map<String, Object> properties) {
127 if (properties.containsKey(Constants.SERVICE_PID)) {
128 String pid = (String) properties.get(Constants.SERVICE_PID);
129 RankedObject.putIfHigherRank(layers, pid, layer, properties);
133 public void removeLayer(SuiteLayer layer, Map<String, Object> properties) {
134 if (properties.containsKey(Constants.SERVICE_PID)) {
135 String pid = (String) properties.get(Constants.SERVICE_PID);
136 if (layers.containsKey(pid)) {
137 if (layers.get(pid).equals(new RankedObject<SuiteLayer>(layer, properties))) {