]> git.argeo.org Git - gpl/argeo-suite.git/blob - core/org.argeo.suite.ui/src/org/argeo/suite/ui/DefaultLeadPane.java
[maven-release-plugin] prepare release argeo-suite-2.1.19
[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 Localized title = null;
91 if (!adminLayers.contains(layerId)) {
92 String titleStr = (String) layerObj.getProperties().get(SuiteLayer.Property.title.name());
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
103 String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
104 SuiteIcon icon = null;
105 if (iconName != null)
106 icon = SuiteIcon.valueOf(iconName);
107
108 Composite buttonParent;
109 if (adminLayers.contains(layerId))
110 buttonParent = adminLayersC;
111 else
112 buttonParent = appLayersC;
113 Button b = createLayerButton(buttonParent, layerId, title, icon);
114 if (first == null)
115 first = b;
116 }
117 }
118 return first;
119 }
120
121 public void init(Map<String, Object> properties) {
122 String[] defaultLayers = (String[]) properties.get(Property.defaultLayers.toString());
123 if (defaultLayers == null)
124 throw new IllegalArgumentException("Default layers must be set.");
125 this.defaultLayers = Arrays.asList(defaultLayers);
126 if (log.isDebugEnabled())
127 log.debug("Default layers: " + Arrays.asList(defaultLayers));
128 String[] adminLayers = (String[]) properties.get(Property.adminLayers.toString());
129 if (adminLayers != null) {
130 this.adminLayers = Arrays.asList(adminLayers);
131 if (log.isDebugEnabled())
132 log.debug("Admin layers: " + Arrays.asList(adminLayers));
133 }
134 }
135
136 public void addLayer(SuiteLayer layer, Map<String, Object> properties) {
137 if (properties.containsKey(Constants.SERVICE_PID)) {
138 String pid = (String) properties.get(Constants.SERVICE_PID);
139 RankedObject.putIfHigherRank(layers, pid, layer, properties);
140 }
141 }
142
143 public void removeLayer(SuiteLayer layer, Map<String, Object> properties) {
144 if (properties.containsKey(Constants.SERVICE_PID)) {
145 String pid = (String) properties.get(Constants.SERVICE_PID);
146 if (layers.containsKey(pid)) {
147 if (layers.get(pid).equals(new RankedObject<SuiteLayer>(layer, properties))) {
148 layers.remove(pid);
149 }
150 }
151 }
152 }
153
154 protected Button createLayerButton(Composite parent, String layer, Localized msg, CmsIcon icon) {
155 CmsTheme theme = CmsTheme.getCmsTheme(parent);
156 Button button = new Button(parent, SWT.PUSH);
157 CmsUiUtils.style(button, SuiteStyle.leadPane);
158 if (icon != null)
159 button.setImage(icon.getBigIcon(theme));
160 button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, false));
161 // button.setToolTipText(msg.lead());
162 if (msg != null) {
163 Label lbl = new Label(parent, SWT.CENTER);
164 CmsUiUtils.style(lbl, SuiteStyle.leadPane);
165 // CmsUiUtils.markup(lbl);
166 ClassLoader l10nClassLoader = getClass().getClassLoader();
167 String txt = LocaleUtils.lead(msg, l10nClassLoader);
168 // String txt = msg.lead();
169 lbl.setText(txt);
170 lbl.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
171 }
172 CmsUiUtils.sendEventOnSelect(button, SuiteEvent.switchLayer.topic(), SuiteEvent.LAYER, layer);
173 return button;
174 }
175 }