Support that no admin layer is available in the application.
[gpl/argeo-suite.git] / core / org.argeo.suite.ui / src / org / argeo / suite / ui / DefaultLeadPane.java
index e56a676c4259915c4dc25fc3a143d91fc73a1341..46de1770abf67a9eeebab2b61a830f743941f071 100644 (file)
@@ -1,5 +1,6 @@
 package org.argeo.suite.ui;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
@@ -13,13 +14,10 @@ import javax.jcr.RepositoryException;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.argeo.cms.LocaleUtils;
 import org.argeo.cms.Localized;
 import org.argeo.cms.auth.CurrentUser;
-import org.argeo.cms.ui.CmsTheme;
 import org.argeo.cms.ui.CmsUiProvider;
 import org.argeo.cms.ui.CmsView;
-import org.argeo.cms.ui.util.CmsIcon;
 import org.argeo.cms.ui.util.CmsUiUtils;
 import org.argeo.suite.RankedObject;
 import org.argeo.suite.SuiteUtils;
@@ -29,8 +27,9 @@ import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Label;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
+import org.osgi.framework.wiring.BundleWiring;
 
 /** Side pane listing various perspectives. */
 public class DefaultLeadPane implements CmsUiProvider {
@@ -42,7 +41,9 @@ public class DefaultLeadPane implements CmsUiProvider {
 
        private Map<String, RankedObject<SuiteLayer>> layers = Collections.synchronizedSortedMap(new TreeMap<>());
        private List<String> defaultLayers;
-       private List<String> adminLayers;
+       private List<String> adminLayers = new ArrayList<>();
+
+       private ClassLoader l10nClassLoader;
 
        @Override
        public Control createUi(Composite parent, Node node) throws RepositoryException {
@@ -58,15 +59,20 @@ public class DefaultLeadPane implements CmsUiProvider {
                appLayersC.setLayout(layout);
                appLayersC.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
 
-               Composite adminLayersC = new Composite(parent, SWT.NONE);
-               CmsUiUtils.style(adminLayersC, SuiteStyle.leadPane);
-               GridLayout adminLayout = new GridLayout();
-               adminLayout.verticalSpacing = 10;
-               adminLayout.marginBottom = 10;
-               adminLayout.marginLeft = 10;
-               adminLayout.marginRight = 10;
-               adminLayersC.setLayout(adminLayout);
-               adminLayersC.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, false, true));
+               Composite adminLayersC;
+               if (!adminLayers.isEmpty()) {
+                       adminLayersC = new Composite(parent, SWT.NONE);
+                       CmsUiUtils.style(adminLayersC, SuiteStyle.leadPane);
+                       GridLayout adminLayout = new GridLayout();
+                       adminLayout.verticalSpacing = 10;
+                       adminLayout.marginBottom = 10;
+                       adminLayout.marginLeft = 10;
+                       adminLayout.marginRight = 10;
+                       adminLayersC.setLayout(adminLayout);
+                       adminLayersC.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, false, true));
+               } else {
+                       adminLayersC = null;
+               }
 
 //             boolean isAdmin = cmsView.doAs(() -> CurrentUser.isInRole(NodeConstants.ROLE_USER_ADMIN));
                Set<String> userRoles = cmsView.doAs(() -> CurrentUser.roles());
@@ -110,47 +116,18 @@ public class DefaultLeadPane implements CmsUiProvider {
                                        buttonParent = adminLayersC;
                                else
                                        buttonParent = appLayersC;
-                               Button b = createLayerButton(buttonParent, layerId, title, icon);
+                               Button b = SuiteUiUtils.createLayerButton(buttonParent, layerId, title, icon, l10nClassLoader);
                                if (first == null)
                                        first = b;
                        }
                }
-
-//             if (isAdmin && adminLayers != null)
-//                     for (String layerId : adminLayers) {
-//                             if (layers.containsKey(layerId)) {
-//                                     RankedObject<SuiteLayer> layerObj = layers.get(layerId);
-//
-//                                     String titleStr = (String) layerObj.getProperties().get(SuiteLayer.Property.title.name());
-//                                     Localized title = null;
-//                                     if (titleStr != null)
-//                                             title = new Localized.Untranslated(titleStr);
-//
-//                                     String iconName = (String) layerObj.getProperties().get(SuiteLayer.Property.icon.name());
-//                                     SuiteIcon icon = null;
-//                                     if (iconName != null)
-//                                             icon = SuiteIcon.valueOf(iconName);
-//
-//                                     Button b = SuiteUiUtils.createLayerButton(parent, layerId, title, icon);
-//                                     if (first == null)
-//                                             first = b;
-//                             }
-//                     }
-
-//             Button dashboardB = createButton(parent, SuiteMsg.dashboard.name(), SuiteMsg.dashboard, SuiteIcon.dashboard);
-               if (!cmsView.isAnonymous()) {
-//                     createButton(parent, SuiteMsg.documents.name(), SuiteMsg.documents, SuiteIcon.documents);
-//                     createButton(parent, SuiteMsg.people.name(), SuiteMsg.people, SuiteIcon.people);
-//                     createButton(parent, SuiteMsg.locations.name(), SuiteMsg.locations, SuiteIcon.location);
-               }
                return first;
        }
 
-       protected void processLayer(String layerDef) {
-
-       }
+       public void init(BundleContext bundleContext, Map<String, Object> properties) {
+               l10nClassLoader = bundleContext != null ? bundleContext.getBundle().adapt(BundleWiring.class).getClassLoader()
+                               : getClass().getClassLoader();
 
-       public void init(Map<String, Object> properties) {
                String[] defaultLayers = (String[]) properties.get(Property.defaultLayers.toString());
                if (defaultLayers == null)
                        throw new IllegalArgumentException("Default layers must be set.");
@@ -165,6 +142,10 @@ public class DefaultLeadPane implements CmsUiProvider {
                }
        }
 
+       public void destroy(BundleContext bundleContext, Map<String, String> properties) {
+
+       }
+
        public void addLayer(SuiteLayer layer, Map<String, Object> properties) {
                if (properties.containsKey(Constants.SERVICE_PID)) {
                        String pid = (String) properties.get(Constants.SERVICE_PID);
@@ -183,25 +164,25 @@ public class DefaultLeadPane implements CmsUiProvider {
                }
        }
 
-       protected Button createLayerButton(Composite parent, String layer, Localized msg, CmsIcon icon) {
-               CmsTheme theme = CmsTheme.getCmsTheme(parent);
-               Button button = new Button(parent, SWT.PUSH);
-               CmsUiUtils.style(button, SuiteStyle.leadPane);
-               if (icon != null)
-                       button.setImage(icon.getBigIcon(theme));
-               button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, false));
-               // button.setToolTipText(msg.lead());
-               if (msg != null) {
-                       Label lbl = new Label(parent, SWT.CENTER);
-                       CmsUiUtils.style(lbl, SuiteStyle.leadPane);
-                       // CmsUiUtils.markup(lbl);
-                       ClassLoader l10nClassLoader = getClass().getClassLoader();
-                       String txt = LocaleUtils.lead(msg, l10nClassLoader);
-//                     String txt = msg.lead();
-                       lbl.setText(txt);
-                       lbl.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
-               }
-               CmsUiUtils.sendEventOnSelect(button, SuiteEvent.switchLayer.topic(), SuiteEvent.LAYER, layer);
-               return button;
-       }
+//     protected Button createLayerButton(Composite parent, String layer, Localized msg, CmsIcon icon) {
+//             CmsTheme theme = CmsTheme.getCmsTheme(parent);
+//             Button button = new Button(parent, SWT.PUSH);
+//             CmsUiUtils.style(button, SuiteStyle.leadPane);
+//             if (icon != null)
+//                     button.setImage(icon.getBigIcon(theme));
+//             button.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, false));
+//             // button.setToolTipText(msg.lead());
+//             if (msg != null) {
+//                     Label lbl = new Label(parent, SWT.CENTER);
+//                     CmsUiUtils.style(lbl, SuiteStyle.leadPane);
+//                     // CmsUiUtils.markup(lbl);
+//                     ClassLoader l10nClassLoader = getClass().getClassLoader();
+//                     String txt = LocaleUtils.lead(msg, l10nClassLoader);
+////                   String txt = msg.lead();
+//                     lbl.setText(txt);
+//                     lbl.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, false));
+//             }
+//             CmsUiUtils.sendEventOnSelect(button, SuiteEvent.switchLayer.topic(), SuiteEvent.LAYER, layer);
+//             return button;
+//     }
 }