1 package org
.argeo
.app
.ui
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Arrays
;
5 import java
.util
.Collections
;
6 import java
.util
.HashSet
;
10 import java
.util
.TreeMap
;
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
;
31 /** Side pane listing various perspectives. */
32 public class DefaultLeadPane
implements CmsUiProvider
{
33 private final static CmsLog log
= CmsLog
.getLog(DefaultLeadPane
.class);
35 public static enum Property
{
36 defaultLayers
, adminLayers
;
39 private Map
<String
, RankedObject
<SuiteLayer
>> layers
= Collections
.synchronizedSortedMap(new TreeMap
<>());
40 private List
<String
> defaultLayers
;
41 private List
<String
> adminLayers
= new ArrayList
<>();
43 private ClassLoader l10nClassLoader
;
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));
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));
74 // boolean isAdmin = cmsView.doAs(() -> CurrentUser.isInRole(NodeConstants.ROLE_USER_ADMIN));
75 // Set<String> userRoles = cmsView.doAs(() -> CurrentUser.roles());
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 boolean authorized
= false;
87 authorized
= cmsView
.doAs(() -> {
88 for (String layerRole
: layerRoles
) {
89 if (CurrentUser
.implies(layerRole
, null)) {
96 continue layers
;// skip unauthorized layer
97 // Set<String> intersection = new HashSet<String>(layerRoles);
98 // intersection.retainAll(userRoles);
99 // if (intersection.isEmpty())
100 // continue layers;// skip unauthorized layer
102 RankedObject
<SuiteLayer
> layerObj
= layers
.get(layerId
);
104 Localized title
= null;
105 if (!adminLayers
.contains(layerId
)) {
106 String titleStr
= (String
) layerObj
.getProperties().get(SuiteLayer
.Property
.title
.name());
107 if (titleStr
!= null) {
108 if (titleStr
.startsWith("%")) {
109 // LocaleUtils.local(titleStr, getClass().getClassLoader());
110 title
= () -> titleStr
;
112 title
= new Localized
.Untranslated(titleStr
);
117 String iconName
= (String
) layerObj
.getProperties().get(SuiteLayer
.Property
.icon
.name());
118 SuiteIcon icon
= null;
119 if (iconName
!= null)
120 icon
= SuiteIcon
.valueOf(iconName
);
122 Composite buttonParent
;
123 if (adminLayers
.contains(layerId
))
124 buttonParent
= adminLayersC
;
126 buttonParent
= appLayersC
;
127 Button b
= SuiteUiUtils
.createLayerButton(buttonParent
, layerId
, title
, icon
, l10nClassLoader
);
135 public void init(BundleContext bundleContext
, Map
<String
, Object
> properties
) {
136 l10nClassLoader
= bundleContext
!= null ? bundleContext
.getBundle().adapt(BundleWiring
.class).getClassLoader()
137 : getClass().getClassLoader();
139 String
[] defaultLayers
= (String
[]) properties
.get(Property
.defaultLayers
.toString());
140 if (defaultLayers
== null)
141 throw new IllegalArgumentException("Default layers must be set.");
142 this.defaultLayers
= Arrays
.asList(defaultLayers
);
143 if (log
.isDebugEnabled())
144 log
.debug("Default layers: " + Arrays
.asList(defaultLayers
));
145 String
[] adminLayers
= (String
[]) properties
.get(Property
.adminLayers
.toString());
146 if (adminLayers
!= null) {
147 this.adminLayers
= Arrays
.asList(adminLayers
);
148 if (log
.isDebugEnabled())
149 log
.debug("Admin layers: " + Arrays
.asList(adminLayers
));
153 public void destroy(BundleContext bundleContext
, Map
<String
, String
> properties
) {
157 public void addLayer(SuiteLayer layer
, Map
<String
, Object
> properties
) {
158 if (properties
.containsKey(Constants
.SERVICE_PID
)) {
159 String pid
= (String
) properties
.get(Constants
.SERVICE_PID
);
160 RankedObject
.putIfHigherRank(layers
, pid
, layer
, properties
);
164 public void removeLayer(SuiteLayer layer
, Map
<String
, Object
> properties
) {
165 if (properties
.containsKey(Constants
.SERVICE_PID
)) {
166 String pid
= (String
) properties
.get(Constants
.SERVICE_PID
);
167 if (layers
.containsKey(pid
)) {
168 if (layers
.get(pid
).equals(new RankedObject
<SuiteLayer
>(layer
, properties
))) {
175 // protected Button createLayerButton(Composite parent, String layer, Localized msg, CmsIcon icon) {
176 // CmsTheme theme = CmsTheme.getCmsTheme(parent);
177 // Button button = new Button(parent, SWT.PUSH);
178 // CmsUiUtils.style(button, SuiteStyle.leadPane);
180 // button.setImage(icon.getBigIcon(theme));
181 // button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, false));
182 // // button.setToolTipText(msg.lead());
183 // if (msg != null) {
184 // Label lbl = new Label(parent, SWT.CENTER);
185 // CmsUiUtils.style(lbl, SuiteStyle.leadPane);
186 // // CmsUiUtils.markup(lbl);
187 // ClassLoader l10nClassLoader = getClass().getClassLoader();
188 // String txt = LocaleUtils.lead(msg, l10nClassLoader);
189 //// String txt = msg.lead();
191 // lbl.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
193 // CmsUiUtils.sendEventOnSelect(button, SuiteEvent.switchLayer.topic(), SuiteEvent.LAYER, layer);