1 package org
.argeo
.app
.ui
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Arrays
;
5 import java
.util
.Collections
;
9 import java
.util
.TreeMap
;
11 import org
.argeo
.api
.acr
.Content
;
12 import org
.argeo
.api
.cms
.CmsLog
;
13 import org
.argeo
.api
.cms
.ux
.CmsView
;
14 import org
.argeo
.app
.api
.RankedObject
;
15 import org
.argeo
.app
.core
.SuiteUtils
;
16 import org
.argeo
.cms
.CurrentUser
;
17 import org
.argeo
.cms
.Localized
;
18 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
19 import org
.argeo
.cms
.ui
.CmsUiProvider
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.layout
.GridData
;
22 import org
.eclipse
.swt
.layout
.GridLayout
;
23 import org
.eclipse
.swt
.widgets
.Button
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Control
;
26 import org
.osgi
.framework
.BundleContext
;
27 import org
.osgi
.framework
.Constants
;
28 import org
.osgi
.framework
.wiring
.BundleWiring
;
30 /** Side pane listing various perspectives. */
31 public class DefaultLeadPane
implements CmsUiProvider
{
32 private final static CmsLog log
= CmsLog
.getLog(DefaultLeadPane
.class);
34 public static enum Property
{
35 defaultLayers
, adminLayers
;
38 private Map
<String
, RankedObject
<SuiteLayer
>> layers
= Collections
.synchronizedSortedMap(new TreeMap
<>());
39 private List
<String
> defaultLayers
;
40 private List
<String
> adminLayers
= new ArrayList
<>();
42 private ClassLoader l10nClassLoader
;
45 public Control
createUiPart(Composite parent
, Content node
) {
46 CmsView cmsView
= CmsSwtUtils
.getCmsView(parent
);
47 parent
.setLayout(CmsSwtUtils
.noSpaceGridLayout());
48 Composite appLayersC
= new Composite(parent
, SWT
.NONE
);
49 CmsSwtUtils
.style(appLayersC
, SuiteStyle
.leadPane
);
50 GridLayout layout
= new GridLayout();
51 layout
.verticalSpacing
= 10;
52 layout
.marginTop
= 10;
53 layout
.marginLeft
= 10;
54 layout
.marginRight
= 10;
55 appLayersC
.setLayout(layout
);
56 appLayersC
.setLayoutData(new GridData(SWT
.FILL
, SWT
.TOP
, false, false));
58 Composite adminLayersC
;
59 if (!adminLayers
.isEmpty()) {
60 adminLayersC
= new Composite(parent
, SWT
.NONE
);
61 CmsSwtUtils
.style(adminLayersC
, SuiteStyle
.leadPane
);
62 GridLayout adminLayout
= new GridLayout();
63 adminLayout
.verticalSpacing
= 10;
64 adminLayout
.marginBottom
= 10;
65 adminLayout
.marginLeft
= 10;
66 adminLayout
.marginRight
= 10;
67 adminLayersC
.setLayout(adminLayout
);
68 adminLayersC
.setLayoutData(new GridData(SWT
.FILL
, SWT
.BOTTOM
, false, true));
73 // boolean isAdmin = cmsView.doAs(() -> CurrentUser.isInRole(NodeConstants.ROLE_USER_ADMIN));
74 // Set<String> userRoles = cmsView.doAs(() -> CurrentUser.roles());
76 layers
: for (String layerDef
: defaultLayers
) {
77 layerDef
= layerDef
.trim();
78 if ("".equals(layerDef
))
79 continue layers
;// skip empty lines
80 String
[] semiColArr
= layerDef
.split(";");
81 String layerId
= semiColArr
[0];
82 Set
<String
> layerRoles
= SuiteUtils
.extractRoles(semiColArr
);
83 if (layers
.containsKey(layerId
)) {
84 if (!layerRoles
.isEmpty()) {
85 boolean authorized
= false;
86 authorized
= cmsView
.doAs(() -> {
87 for (String layerRole
: layerRoles
) {
88 if (CurrentUser
.implies(layerRole
, null)) {
95 continue layers
;// skip unauthorized layer
96 // Set<String> intersection = new HashSet<String>(layerRoles);
97 // intersection.retainAll(userRoles);
98 // if (intersection.isEmpty())
99 // continue layers;// skip unauthorized layer
101 RankedObject
<SuiteLayer
> layerObj
= layers
.get(layerId
);
103 Localized title
= null;
104 if (!adminLayers
.contains(layerId
)) {
105 String titleStr
= (String
) layerObj
.getProperties().get(SuiteLayer
.Property
.title
.name());
106 if (titleStr
!= null) {
107 if (titleStr
.startsWith("%")) {
108 // LocaleUtils.local(titleStr, getClass().getClassLoader());
109 title
= () -> titleStr
;
111 title
= new Localized
.Untranslated(titleStr
);
116 String iconName
= (String
) layerObj
.getProperties().get(SuiteLayer
.Property
.icon
.name());
117 SuiteIcon icon
= null;
118 if (iconName
!= null)
119 icon
= SuiteIcon
.valueOf(iconName
);
121 Composite buttonParent
;
122 if (adminLayers
.contains(layerId
))
123 buttonParent
= adminLayersC
;
125 buttonParent
= appLayersC
;
126 Button b
= SuiteUiUtils
.createLayerButton(buttonParent
, layerId
, title
, icon
, l10nClassLoader
);
134 public void init(BundleContext bundleContext
, Map
<String
, Object
> properties
) {
135 l10nClassLoader
= bundleContext
!= null ? bundleContext
.getBundle().adapt(BundleWiring
.class).getClassLoader()
136 : getClass().getClassLoader();
138 String
[] defaultLayers
= (String
[]) properties
.get(Property
.defaultLayers
.toString());
139 if (defaultLayers
== null)
140 throw new IllegalArgumentException("Default layers must be set.");
141 this.defaultLayers
= Arrays
.asList(defaultLayers
);
142 if (log
.isDebugEnabled())
143 log
.debug("Default layers: " + Arrays
.asList(defaultLayers
));
144 String
[] adminLayers
= (String
[]) properties
.get(Property
.adminLayers
.toString());
145 if (adminLayers
!= null) {
146 this.adminLayers
= Arrays
.asList(adminLayers
);
147 if (log
.isDebugEnabled())
148 log
.debug("Admin layers: " + Arrays
.asList(adminLayers
));
152 public void destroy(BundleContext bundleContext
, Map
<String
, String
> properties
) {
156 public void addLayer(SuiteLayer layer
, Map
<String
, Object
> properties
) {
157 if (properties
.containsKey(Constants
.SERVICE_PID
)) {
158 String pid
= (String
) properties
.get(Constants
.SERVICE_PID
);
159 RankedObject
.putIfHigherRank(layers
, pid
, layer
, properties
);
163 public void removeLayer(SuiteLayer layer
, Map
<String
, Object
> properties
) {
164 if (properties
.containsKey(Constants
.SERVICE_PID
)) {
165 String pid
= (String
) properties
.get(Constants
.SERVICE_PID
);
166 if (layers
.containsKey(pid
)) {
167 if (layers
.get(pid
).equals(new RankedObject
<SuiteLayer
>(layer
, properties
))) {
174 // protected Button createLayerButton(Composite parent, String layer, Localized msg, CmsIcon icon) {
175 // CmsTheme theme = CmsTheme.getCmsTheme(parent);
176 // Button button = new Button(parent, SWT.PUSH);
177 // CmsUiUtils.style(button, SuiteStyle.leadPane);
179 // button.setImage(icon.getBigIcon(theme));
180 // button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, false));
181 // // button.setToolTipText(msg.lead());
182 // if (msg != null) {
183 // Label lbl = new Label(parent, SWT.CENTER);
184 // CmsUiUtils.style(lbl, SuiteStyle.leadPane);
185 // // CmsUiUtils.markup(lbl);
186 // ClassLoader l10nClassLoader = getClass().getClassLoader();
187 // String txt = LocaleUtils.lead(msg, l10nClassLoader);
188 //// String txt = msg.lead();
190 // lbl.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
192 // CmsUiUtils.sendEventOnSelect(button, SuiteEvent.switchLayer.topic(), SuiteEvent.LAYER, layer);