bfea829ec51fb4435cebf34bf515b1fde95dd414
[gpl/argeo-suite.git] / 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.Map;
6 import java.util.TreeMap;
7
8 import javax.jcr.Node;
9 import javax.jcr.RepositoryException;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.api.NodeConstants;
14 import org.argeo.cms.Localized;
15 import org.argeo.cms.auth.CurrentUser;
16 import org.argeo.cms.ui.CmsTheme;
17 import org.argeo.cms.ui.CmsUiProvider;
18 import org.argeo.cms.ui.CmsView;
19 import org.argeo.cms.ui.util.CmsIcon;
20 import org.argeo.cms.ui.util.CmsUiUtils;
21 import org.argeo.suite.RankedObject;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Label;
29 import org.osgi.framework.Constants;
30
31 /** Side pane listing various perspectives. */
32 public class DefaultLeadPane implements CmsUiProvider {
33         private final static Log log = LogFactory.getLog(DefaultLeadPane.class);
34
35         public static enum Property {
36                 defaultLayers, adminLayers;
37         }
38
39         private Map<String, RankedObject<SuiteLayer>> layers = Collections.synchronizedSortedMap(new TreeMap<>());
40         private String[] defaultLayers;
41         private String[] adminLayers;
42
43         @Override
44         public Control createUi(Composite parent, Node node) throws RepositoryException {
45                 CmsView cmsView = CmsView.getCmsView(parent);
46                 GridLayout layout = new GridLayout();
47                 layout.verticalSpacing = 10;
48                 layout.marginTop = 10;
49                 layout.marginLeft = 10;
50                 layout.marginRight = 10;
51                 parent.setLayout(layout);
52
53                 Button first = null;
54                 for (String layerId : defaultLayers) {
55                         if (layers.containsKey(layerId)) {
56                                 RankedObject<SuiteLayer> layerObj = layers.get(layerId);
57
58                                 // TODO deal with i10n
59                                 String titleStr = (String) layerObj.getProperties().get(SuiteLayer.Property.title.name());
60                                 Localized title = null;
61                                 if (titleStr != null)
62                                         title = new Localized.Untranslated(titleStr);
63
64                                 String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
65                                 SuiteIcon icon = null;
66                                 if (iconName != null)
67                                         icon = SuiteIcon.valueOf(iconName);
68
69                                 Button b = createButton(parent, layerId, title, icon);
70                                 if (first == null)
71                                         first = b;
72                         }
73                 }
74
75                 // TODO factorise
76                 boolean isAdmin = cmsView.doAs(() -> CurrentUser.isInRole(NodeConstants.ROLE_USER_ADMIN));
77                 if (isAdmin)
78                         for (String layerId : adminLayers) {
79                                 if (layers.containsKey(layerId)) {
80                                         RankedObject<SuiteLayer> layerObj = layers.get(layerId);
81
82                                         // TODO deal with i10n
83                                         String titleStr = (String) layerObj.getProperties().get(SuiteLayer.Property.title.name());
84                                         Localized title = null;
85                                         if (titleStr != null)
86                                                 title = new Localized.Untranslated(titleStr);
87
88                                         String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
89                                         SuiteIcon icon = null;
90                                         if (iconName != null)
91                                                 icon = SuiteIcon.valueOf(iconName);
92
93                                         Button b = createButton(parent, layerId, title, icon);
94                                         if (first == null)
95                                                 first = b;
96                                 }
97                         }
98
99 //              Button dashboardB = createButton(parent, SuiteMsg.dashboard.name(), SuiteMsg.dashboard, SuiteIcon.dashboard);
100                 if (!cmsView.isAnonymous()) {
101 //                      createButton(parent, SuiteMsg.documents.name(), SuiteMsg.documents, SuiteIcon.documents);
102 //                      createButton(parent, SuiteMsg.people.name(), SuiteMsg.people, SuiteIcon.people);
103 //                      createButton(parent, SuiteMsg.locations.name(), SuiteMsg.locations, SuiteIcon.location);
104                 }
105                 return first;
106         }
107
108         protected Button createButton(Composite parent, String layer, Localized msg, CmsIcon icon) {
109                 CmsTheme theme = CmsTheme.getCmsTheme(parent);
110                 Button button = new Button(parent, SWT.PUSH);
111                 CmsUiUtils.style(button, SuiteStyle.leadPane);
112                 if (icon != null)
113                         button.setImage(icon.getBigIcon(theme));
114                 button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, false));
115                 // button.setToolTipText(msg.lead());
116                 if (msg != null) {
117                         Label lbl = new Label(parent, SWT.NONE);
118                         CmsUiUtils.style(lbl, SuiteStyle.leadPane);
119                         lbl.setText(msg.lead());
120                         lbl.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
121                 }
122                 CmsUiUtils.sendEventOnSelect(button, SuiteEvent.switchLayer.topic(), SuiteEvent.LAYER, layer);
123                 return button;
124         }
125
126         public void init(Map<String, Object> properties) {
127                 defaultLayers = (String[]) properties.get(Property.defaultLayers.toString());
128                 if (defaultLayers == null)
129                         throw new IllegalArgumentException("Default layers must be set.");
130                 if (log.isDebugEnabled())
131                         log.debug("Default layers: " + Arrays.asList(defaultLayers));
132                 adminLayers = (String[]) properties.get(Property.adminLayers.toString());
133                 if (log.isDebugEnabled() && adminLayers != null)
134                         log.debug("Admin layers: " + Arrays.asList(adminLayers));
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 }