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