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