b777debd2a45afbd882dc62fb8b9ff7696057ed1
[gpl/argeo-suite.git] / core / org.argeo.suite.ui / src / org / argeo / suite / ui / DefaultLeadPane.java
1 package org.argeo.suite.ui;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.Map;
7 import java.util.Set;
8 import java.util.TreeMap;
9
10 import javax.jcr.Node;
11 import javax.jcr.RepositoryException;
12
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;
26
27 /** Side pane listing various perspectives. */
28 public class DefaultLeadPane implements CmsUiProvider {
29         private final static Log log = LogFactory.getLog(DefaultLeadPane.class);
30
31         public static enum Property {
32                 defaultLayers, adminLayers;
33         }
34
35         private Map<String, RankedObject<SuiteLayer>> layers = Collections.synchronizedSortedMap(new TreeMap<>());
36         private String[] defaultLayers;
37         private String[] adminLayers;
38
39         @Override
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);
48
49 //              boolean isAdmin = cmsView.doAs(() -> CurrentUser.isInRole(NodeConstants.ROLE_USER_ADMIN));
50                 Set<String> userRoles = cmsView.doAs(() -> CurrentUser.roles());
51                 Button first = null;
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
65                                 }
66                                 RankedObject<SuiteLayer> layerObj = layers.get(layerId);
67
68                                 // TODO deal with i10n
69                                 String titleStr = (String) layerObj.getProperties().get(SuiteLayer.Property.title.name());
70                                 Localized title = null;
71                                 if (titleStr != null)
72                                         title = new Localized.Untranslated(titleStr);
73
74                                 String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
75                                 SuiteIcon icon = null;
76                                 if (iconName != null)
77                                         icon = SuiteIcon.valueOf(iconName);
78
79                                 Button b = SuiteUiUtils.createLayerButton(parent, layerId, title, icon);
80                                 if (first == null)
81                                         first = b;
82                         }
83                 }
84
85 //              if (isAdmin && adminLayers != null)
86 //                      for (String layerId : adminLayers) {
87 //                              if (layers.containsKey(layerId)) {
88 //                                      RankedObject<SuiteLayer> layerObj = layers.get(layerId);
89 //
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);
94 //
95 //                                      String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
96 //                                      SuiteIcon icon = null;
97 //                                      if (iconName != null)
98 //                                              icon = SuiteIcon.valueOf(iconName);
99 //
100 //                                      Button b = SuiteUiUtils.createLayerButton(parent, layerId, title, icon);
101 //                                      if (first == null)
102 //                                              first = b;
103 //                              }
104 //                      }
105
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);
111                 }
112                 return first;
113         }
114
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));
124         }
125
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);
130                 }
131         }
132
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))) {
138                                         layers.remove(pid);
139                                 }
140                         }
141                 }
142         }
143 }