1 package org
.argeo
.suite
.ui
;
3 import java
.util
.Arrays
;
4 import java
.util
.Collections
;
5 import java
.util
.HashSet
;
9 import java
.util
.TreeMap
;
11 import javax
.jcr
.Node
;
12 import javax
.jcr
.RepositoryException
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.cms
.LocaleUtils
;
17 import org
.argeo
.cms
.Localized
;
18 import org
.argeo
.cms
.auth
.CurrentUser
;
19 import org
.argeo
.cms
.ui
.CmsTheme
;
20 import org
.argeo
.cms
.ui
.CmsUiProvider
;
21 import org
.argeo
.cms
.ui
.CmsView
;
22 import org
.argeo
.cms
.ui
.util
.CmsIcon
;
23 import org
.argeo
.cms
.ui
.util
.CmsUiUtils
;
24 import org
.argeo
.suite
.RankedObject
;
25 import org
.argeo
.suite
.SuiteUtils
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Button
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
31 import org
.eclipse
.swt
.widgets
.Control
;
32 import org
.eclipse
.swt
.widgets
.Label
;
33 import org
.osgi
.framework
.Constants
;
35 /** Side pane listing various perspectives. */
36 public class DefaultLeadPane
implements CmsUiProvider
{
37 private final static Log log
= LogFactory
.getLog(DefaultLeadPane
.class);
39 public static enum Property
{
40 defaultLayers
, adminLayers
;
43 private Map
<String
, RankedObject
<SuiteLayer
>> layers
= Collections
.synchronizedSortedMap(new TreeMap
<>());
44 private List
<String
> defaultLayers
;
45 private List
<String
> adminLayers
;
48 public Control
createUi(Composite parent
, Node node
) throws RepositoryException
{
49 CmsView cmsView
= CmsView
.getCmsView(parent
);
50 parent
.setLayout(CmsUiUtils
.noSpaceGridLayout());
51 Composite appLayersC
= new Composite(parent
, SWT
.NONE
);
52 CmsUiUtils
.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
= new Composite(parent
, SWT
.NONE
);
62 CmsUiUtils
.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));
71 // boolean isAdmin = cmsView.doAs(() -> CurrentUser.isInRole(NodeConstants.ROLE_USER_ADMIN));
72 Set
<String
> userRoles
= cmsView
.doAs(() -> CurrentUser
.roles());
74 layers
: for (String layerDef
: defaultLayers
) {
75 layerDef
= layerDef
.trim();
76 if ("".equals(layerDef
))
77 continue layers
;// skip empty lines
78 String
[] semiColArr
= layerDef
.split(";");
79 String layerId
= semiColArr
[0];
80 Set
<String
> layerRoles
= SuiteUtils
.extractRoles(semiColArr
);
81 if (layers
.containsKey(layerId
)) {
82 if (!layerRoles
.isEmpty()) {
83 Set
<String
> intersection
= new HashSet
<String
>(layerRoles
);
84 intersection
.retainAll(userRoles
);
85 if (intersection
.isEmpty())
86 continue layers
;// skip unauthorized layer
88 RankedObject
<SuiteLayer
> layerObj
= layers
.get(layerId
);
90 Localized title
= null;
91 if (!adminLayers
.contains(layerId
)) {
92 String titleStr
= (String
) layerObj
.getProperties().get(SuiteLayer
.Property
.title
.name());
93 if (titleStr
!= null) {
94 if (titleStr
.startsWith("%")) {
95 // LocaleUtils.local(titleStr, getClass().getClassLoader());
96 title
= () -> titleStr
;
98 title
= new Localized
.Untranslated(titleStr
);
103 String iconName
= (String
) layerObj
.getProperties().get(SuiteLayer
.Property
.icon
.name());
104 SuiteIcon icon
= null;
105 if (iconName
!= null)
106 icon
= SuiteIcon
.valueOf(iconName
);
108 Composite buttonParent
;
109 if (adminLayers
.contains(layerId
))
110 buttonParent
= adminLayersC
;
112 buttonParent
= appLayersC
;
113 Button b
= createLayerButton(buttonParent
, layerId
, title
, icon
);
121 public void init(Map
<String
, Object
> properties
) {
122 String
[] defaultLayers
= (String
[]) properties
.get(Property
.defaultLayers
.toString());
123 if (defaultLayers
== null)
124 throw new IllegalArgumentException("Default layers must be set.");
125 this.defaultLayers
= Arrays
.asList(defaultLayers
);
126 if (log
.isDebugEnabled())
127 log
.debug("Default layers: " + Arrays
.asList(defaultLayers
));
128 String
[] adminLayers
= (String
[]) properties
.get(Property
.adminLayers
.toString());
129 if (adminLayers
!= null) {
130 this.adminLayers
= Arrays
.asList(adminLayers
);
131 if (log
.isDebugEnabled())
132 log
.debug("Admin layers: " + Arrays
.asList(adminLayers
));
136 public void addLayer(SuiteLayer layer
, Map
<String
, Object
> properties
) {
137 if (properties
.containsKey(Constants
.SERVICE_PID
)) {
138 String pid
= (String
) properties
.get(Constants
.SERVICE_PID
);
139 RankedObject
.putIfHigherRank(layers
, pid
, layer
, properties
);
143 public void removeLayer(SuiteLayer layer
, Map
<String
, Object
> properties
) {
144 if (properties
.containsKey(Constants
.SERVICE_PID
)) {
145 String pid
= (String
) properties
.get(Constants
.SERVICE_PID
);
146 if (layers
.containsKey(pid
)) {
147 if (layers
.get(pid
).equals(new RankedObject
<SuiteLayer
>(layer
, properties
))) {
154 protected Button
createLayerButton(Composite parent
, String layer
, Localized msg
, CmsIcon icon
) {
155 CmsTheme theme
= CmsTheme
.getCmsTheme(parent
);
156 Button button
= new Button(parent
, SWT
.PUSH
);
157 CmsUiUtils
.style(button
, SuiteStyle
.leadPane
);
159 button
.setImage(icon
.getBigIcon(theme
));
160 button
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.BOTTOM
, true, false));
161 // button.setToolTipText(msg.lead());
163 Label lbl
= new Label(parent
, SWT
.CENTER
);
164 CmsUiUtils
.style(lbl
, SuiteStyle
.leadPane
);
165 // CmsUiUtils.markup(lbl);
166 ClassLoader l10nClassLoader
= getClass().getClassLoader();
167 String txt
= LocaleUtils
.lead(msg
, l10nClassLoader
);
168 // String txt = msg.lead();
170 lbl
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.TOP
, true, false));
172 CmsUiUtils
.sendEventOnSelect(button
, SuiteEvent
.switchLayer
.topic(), SuiteEvent
.LAYER
, layer
);