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 javax
.jcr
.Node
;
13 import javax
.jcr
.RepositoryException
;
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
;
33 /** Side pane listing various perspectives. */
34 public class DefaultLeadPane
implements CmsUiProvider
{
35 private final static CmsLog log
= CmsLog
.getLog(DefaultLeadPane
.class);
37 public static enum Property
{
38 defaultLayers
, adminLayers
;
41 private Map
<String
, RankedObject
<SuiteLayer
>> layers
= Collections
.synchronizedSortedMap(new TreeMap
<>());
42 private List
<String
> defaultLayers
;
43 private List
<String
> adminLayers
= new ArrayList
<>();
45 private ClassLoader l10nClassLoader
;
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));
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));
76 // boolean isAdmin = cmsView.doAs(() -> CurrentUser.isInRole(NodeConstants.ROLE_USER_ADMIN));
77 Set
<String
> userRoles
= cmsView
.doAs(() -> CurrentUser
.roles());
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
93 RankedObject
<SuiteLayer
> layerObj
= layers
.get(layerId
);
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
;
103 title
= new Localized
.Untranslated(titleStr
);
108 String iconName
= (String
) layerObj
.getProperties().get(SuiteLayer
.Property
.icon
.name());
109 SuiteIcon icon
= null;
110 if (iconName
!= null)
111 icon
= SuiteIcon
.valueOf(iconName
);
113 Composite buttonParent
;
114 if (adminLayers
.contains(layerId
))
115 buttonParent
= adminLayersC
;
117 buttonParent
= appLayersC
;
118 Button b
= SuiteUiUtils
.createLayerButton(buttonParent
, layerId
, title
, icon
, l10nClassLoader
);
126 public void init(BundleContext bundleContext
, Map
<String
, Object
> properties
) {
127 l10nClassLoader
= bundleContext
!= null ? bundleContext
.getBundle().adapt(BundleWiring
.class).getClassLoader()
128 : getClass().getClassLoader();
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
));
144 public void destroy(BundleContext bundleContext
, Map
<String
, String
> properties
) {
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
);
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
))) {
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);
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();
182 // lbl.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
184 // CmsUiUtils.sendEventOnSelect(button, SuiteEvent.switchLayer.topic(), SuiteEvent.LAYER, layer);