Work on styling.
[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.CmsUiProvider;
17 import org.argeo.cms.ui.CmsView;
18 import org.argeo.suite.RankedObject;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.osgi.framework.Constants;
24
25 /** Side pane listing various perspectives. */
26 public class DefaultLeadPane implements CmsUiProvider {
27         private final static Log log = LogFactory.getLog(DefaultLeadPane.class);
28
29         public static enum Property {
30                 defaultLayers, adminLayers;
31         }
32
33         private Map<String, RankedObject<SuiteLayer>> layers = Collections.synchronizedSortedMap(new TreeMap<>());
34         private String[] defaultLayers;
35         private String[] adminLayers;
36
37         @Override
38         public Control createUi(Composite parent, Node node) throws RepositoryException {
39                 CmsView cmsView = CmsView.getCmsView(parent);
40                 GridLayout layout = new GridLayout();
41                 layout.verticalSpacing = 10;
42                 layout.marginTop = 10;
43                 layout.marginLeft = 10;
44                 layout.marginRight = 10;
45                 parent.setLayout(layout);
46
47                 Button first = null;
48                 for (String layerId : defaultLayers) {
49                         if (layers.containsKey(layerId)) {
50                                 RankedObject<SuiteLayer> layerObj = layers.get(layerId);
51
52                                 // TODO deal with i10n
53                                 String titleStr = (String) layerObj.getProperties().get(SuiteLayer.Property.title.name());
54                                 Localized title = null;
55                                 if (titleStr != null)
56                                         title = new Localized.Untranslated(titleStr);
57
58                                 String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
59                                 SuiteIcon icon = null;
60                                 if (iconName != null)
61                                         icon = SuiteIcon.valueOf(iconName);
62
63                                 Button b = SuiteUiUtils.createLayerButton(parent, layerId, title, icon);
64                                 if (first == null)
65                                         first = b;
66                         }
67                 }
68
69                 // TODO factorise
70                 boolean isAdmin = cmsView.doAs(() -> CurrentUser.isInRole(NodeConstants.ROLE_USER_ADMIN));
71                 if (isAdmin && adminLayers != null)
72                         for (String layerId : adminLayers) {
73                                 if (layers.containsKey(layerId)) {
74                                         RankedObject<SuiteLayer> layerObj = layers.get(layerId);
75
76                                         // TODO deal with i10n
77                                         String titleStr = (String) layerObj.getProperties().get(SuiteLayer.Property.title.name());
78                                         Localized title = null;
79                                         if (titleStr != null)
80                                                 title = new Localized.Untranslated(titleStr);
81
82                                         String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
83                                         SuiteIcon icon = null;
84                                         if (iconName != null)
85                                                 icon = SuiteIcon.valueOf(iconName);
86
87                                         Button b = SuiteUiUtils.createLayerButton(parent, layerId, title, icon);
88                                         if (first == null)
89                                                 first = b;
90                                 }
91                         }
92
93 //              Button dashboardB = createButton(parent, SuiteMsg.dashboard.name(), SuiteMsg.dashboard, SuiteIcon.dashboard);
94                 if (!cmsView.isAnonymous()) {
95 //                      createButton(parent, SuiteMsg.documents.name(), SuiteMsg.documents, SuiteIcon.documents);
96 //                      createButton(parent, SuiteMsg.people.name(), SuiteMsg.people, SuiteIcon.people);
97 //                      createButton(parent, SuiteMsg.locations.name(), SuiteMsg.locations, SuiteIcon.location);
98                 }
99                 return first;
100         }
101
102         public void init(Map<String, Object> properties) {
103                 defaultLayers = (String[]) properties.get(Property.defaultLayers.toString());
104                 if (defaultLayers == null)
105                         throw new IllegalArgumentException("Default layers must be set.");
106                 if (log.isDebugEnabled())
107                         log.debug("Default layers: " + Arrays.asList(defaultLayers));
108                 adminLayers = (String[]) properties.get(Property.adminLayers.toString());
109                 if (log.isDebugEnabled() && adminLayers != null)
110                         log.debug("Admin layers: " + Arrays.asList(adminLayers));
111         }
112
113         public void addLayer(SuiteLayer layer, Map<String, Object> properties) {
114                 if (properties.containsKey(Constants.SERVICE_PID)) {
115                         String pid = (String) properties.get(Constants.SERVICE_PID);
116                         RankedObject.putIfHigherRank(layers, pid, layer, properties);
117                 }
118         }
119
120         public void removeLayer(SuiteLayer layer, Map<String, Object> properties) {
121                 if (properties.containsKey(Constants.SERVICE_PID)) {
122                         String pid = (String) properties.get(Constants.SERVICE_PID);
123                         if (layers.containsKey(pid)) {
124                                 if (layers.get(pid).equals(new RankedObject<SuiteLayer>(layer, properties))) {
125                                         layers.remove(pid);
126                                 }
127                         }
128                 }
129         }
130 }