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