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