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