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